]> Untitled Git - wolf-seeking-sheep.git/blob - addons/dialogic/Modules/Condition/event_condition.gd
Updated export config options
[wolf-seeking-sheep.git] / addons / dialogic / Modules / Condition / event_condition.gd
1 @tool
2 class_name DialogicConditionEvent
3 extends DialogicEvent
4
5 ## Event that allows branching a timeline based on a condition.
6
7 enum ConditionTypes {IF, ELIF, ELSE}
8
9 ### Settings
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.
13 var condition := ""
14
15
16 ################################################################################
17 ##                                              EXECUTE
18 ################################################################################
19
20 func _execute() -> void:
21         if condition_type == ConditionTypes.ELSE:
22                 finish()
23                 return
24
25         if condition.is_empty(): condition = "true"
26
27         var result: bool = dialogic.Expressions.execute_condition(condition)
28         if not result:
29                 var idx: int = dialogic.current_event_idx
30                 var ignore := 1
31                 while true:
32                         idx += 1
33                         if not dialogic.current_timeline.get_event(idx) or ignore == 0:
34                                 break
35                         elif dialogic.current_timeline.get_event(idx).can_contain_events:
36                                 ignore += 1
37                         elif dialogic.current_timeline.get_event(idx) is DialogicEndBranchEvent:
38                                 ignore -= 1
39
40                 dialogic.current_event_idx = idx-1
41         finish()
42
43
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
48
49
50 ################################################################################
51 ##                                              INITIALIZE
52 ################################################################################
53
54 func _init() -> void:
55         event_name = "Condition"
56         set_default_color('Color3')
57         event_category = "Flow"
58         event_sorting_index = 1
59         can_contain_events = true
60
61
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()
65
66 ################################################################################
67 ##                                              SAVING/LOADING
68 ################################################################################
69
70 func to_text() -> String:
71         var result_string := ""
72
73         match condition_type:
74                 ConditionTypes.IF:
75                         result_string = 'if '+condition+':'
76                 ConditionTypes.ELIF:
77                         result_string = 'elif '+condition+':'
78                 ConditionTypes.ELSE:
79                         result_string = 'else:'
80
81         return result_string
82
83
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'):
92                 condition = ""
93                 condition_type = ConditionTypes.ELSE
94
95
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')):
98                 return true
99         return false
100
101
102 ################################################################################
103 ##                                              EDITOR REPRESENTATION
104 ################################################################################
105
106 func build_event_editor() -> void:
107         add_header_edit('condition_type', ValueType.FIXED_OPTIONS, {
108                 'options': [
109                         {
110                                 'label': 'IF',
111                                 'value': ConditionTypes.IF,
112                         },
113                         {
114                                 'label': 'ELIF',
115                                 'value': ConditionTypes.ELIF,
116                         },
117                         {
118                                 'label': 'ELSE',
119                                 'value': ConditionTypes.ELSE,
120                         }
121                 ], 'disabled':true})
122         add_header_edit('condition', ValueType.CONDITION, {}, 'condition_type != %s'%ConditionTypes.ELSE)
123
124
125 ####################### CODE COMPLETION ########################################
126 ################################################################################
127
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)
131
132
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)
137
138
139 #################### SYNTAX HIGHLIGHTING #######################################
140 ################################################################################
141
142
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)
148         return dict