2 class_name DialogicConditionEvent
5 ## Event that allows branching a timeline based on a condition.
7 enum ConditionTypes {IF, ELIF, ELSE}
10 ## condition type (see [ConditionTypes]). Defaults to if.
11 var condition_type := ConditionTypes.IF
12 ## The condition as a string. Will be executed as an Expression.
16 ################################################################################
18 ################################################################################
20 func _execute() -> void:
21 if condition_type == ConditionTypes.ELSE:
25 if condition.is_empty(): condition = "true"
27 var result: bool = dialogic.Expressions.execute_condition(condition)
29 var idx: int = dialogic.current_event_idx
33 if not dialogic.current_timeline.get_event(idx) or ignore == 0:
35 elif dialogic.current_timeline.get_event(idx).can_contain_events:
37 elif dialogic.current_timeline.get_event(idx) is DialogicEndBranchEvent:
40 dialogic.current_event_idx = idx-1
44 ## only called if the previous event was an end-branch event
45 ## return true if this event should be executed if the previous event was an end-branch event
46 func should_execute_this_branch() -> bool:
47 return condition_type == ConditionTypes.IF
50 ################################################################################
52 ################################################################################
55 event_name = "Condition"
56 set_default_color('Color3')
57 event_category = "Flow"
58 event_sorting_index = 1
59 can_contain_events = true
62 # return a control node that should show on the END BRANCH node
63 func get_end_branch_control() -> Control:
64 return load(get_script().resource_path.get_base_dir().path_join('ui_condition_end.tscn')).instantiate()
66 ################################################################################
68 ################################################################################
70 func to_text() -> String:
71 var result_string := ""
75 result_string = 'if '+condition+':'
77 result_string = 'elif '+condition+':'
79 result_string = 'else:'
84 func from_text(string:String) -> void:
85 if string.strip_edges().begins_with('if'):
86 condition = string.strip_edges().trim_prefix('if ').trim_suffix(':').strip_edges()
87 condition_type = ConditionTypes.IF
88 elif string.strip_edges().begins_with('elif'):
89 condition = string.strip_edges().trim_prefix('elif ').trim_suffix(':').strip_edges()
90 condition_type = ConditionTypes.ELIF
91 elif string.strip_edges().begins_with('else'):
93 condition_type = ConditionTypes.ELSE
96 func is_valid_event(string:String) -> bool:
97 if string.strip_edges() in ['if', 'elif', 'else'] or (string.strip_edges().begins_with('if ') or string.strip_edges().begins_with('elif ') or string.strip_edges().begins_with('else')):
102 ################################################################################
103 ## EDITOR REPRESENTATION
104 ################################################################################
106 func build_event_editor() -> void:
107 add_header_edit('condition_type', ValueType.FIXED_OPTIONS, {
111 'value': ConditionTypes.IF,
115 'value': ConditionTypes.ELIF,
119 'value': ConditionTypes.ELSE,
122 add_header_edit('condition', ValueType.CONDITION, {}, 'condition_type != %s'%ConditionTypes.ELSE)
125 ####################### CODE COMPLETION ########################################
126 ################################################################################
128 func _get_code_completion(CodeCompletionHelper:Node, TextNode:TextEdit, line:String, _word:String, symbol:String) -> void:
129 if (line.begins_with('if') or line.begins_with('elif')) and symbol == '{':
130 CodeCompletionHelper.suggest_variables(TextNode)
133 func _get_start_code_completion(_CodeCompletionHelper:Node, TextNode:TextEdit) -> void:
134 TextNode.add_code_completion_option(CodeEdit.KIND_PLAIN_TEXT, 'if', 'if ', TextNode.syntax_highlighter.code_flow_color)
135 TextNode.add_code_completion_option(CodeEdit.KIND_PLAIN_TEXT, 'elif', 'elif ', TextNode.syntax_highlighter.code_flow_color)
136 TextNode.add_code_completion_option(CodeEdit.KIND_PLAIN_TEXT, 'else', 'else:\n ', TextNode.syntax_highlighter.code_flow_color)
139 #################### SYNTAX HIGHLIGHTING #######################################
140 ################################################################################
143 func _get_syntax_highlighting(Highlighter:SyntaxHighlighter, dict:Dictionary, line:String) -> Dictionary:
144 var word := line.get_slice(' ', 0)
145 dict[line.find(word)] = {"color":Highlighter.code_flow_color}
146 dict[line.find(word)+len(word)] = {"color":Highlighter.normal_color}
147 dict = Highlighter.color_condition(dict, line)