2 class_name DialogicSettingEvent
5 ## Event that allows changing a specific setting.
10 enum Modes {SET, RESET, RESET_ALL}
11 enum SettingValueType {
18 ## The name of the setting to save to.
20 var _value_type := 0 :
25 if not _suppress_default_value:
27 SettingValueType.STRING, SettingValueType.VARIABLE, SettingValueType.EXPRESSION:
29 SettingValueType.NUMBER:
31 ui_update_needed.emit()
33 var value: Variant = ""
37 ## Used to suppress _value_type from overwriting value with a default value when the type changes
38 ## This is only used when initializing the event_variable.
39 var _suppress_default_value: bool = false
41 ################################################################################
43 ################################################################################
45 func _execute() -> void:
46 if mode == Modes.RESET or mode == Modes.RESET_ALL:
47 if !name.is_empty() and mode != Modes.RESET_ALL:
48 dialogic.Settings.reset_setting(name)
50 dialogic.Settings.reset_all()
53 SettingValueType.STRING:
54 dialogic.Settings.set(name, value)
55 SettingValueType.NUMBER:
56 dialogic.Settings.set(name, float(value))
57 SettingValueType.VARIABLE:
58 if dialogic.has_subsystem('VAR'):
59 dialogic.Settings.set(name, dialogic.VAR.get_variable('{'+value+'}'))
60 SettingValueType.EXPRESSION:
61 if dialogic.has_subsystem('VAR'):
62 dialogic.Settings.set(name, dialogic.VAR.get_variable(value))
66 ################################################################################
68 ################################################################################
71 event_name = "Setting"
72 set_default_color('Color9')
73 event_category = "Helpers"
74 event_sorting_index = 2
77 func _get_icon() -> Resource:
78 return load(self.get_script().get_path().get_base_dir().path_join('icon.svg'))
81 ################################################################################
83 ################################################################################
85 func to_text() -> String:
86 var string := "setting "
90 if !name.is_empty() and mode != Modes.RESET_ALL:
91 string += '"' + name + '"'
97 SettingValueType.STRING: # String
98 string += '"'+value.replace('"', '\\"')+'"'
99 SettingValueType.NUMBER,SettingValueType.EXPRESSION: # Float or Expression
101 SettingValueType.VARIABLE: # Variable
102 string += '{'+value+'}'
107 func from_text(string:String) -> void:
108 var reg := RegEx.new()
109 reg.compile('setting (?<reset>reset)? *("(?<name>[^=+\\-*\\/]*)")?( *= *(?<value>.*))?')
110 var result := reg.search(string)
114 if result.get_string('reset'):
117 name = result.get_string('name').strip_edges()
119 if name.is_empty() and mode == Modes.RESET:
120 mode = Modes.RESET_ALL
122 if result.get_string('value'):
123 _suppress_default_value = true
124 value = result.get_string('value').strip_edges()
125 if value.begins_with('"') and value.ends_with('"') and value.count('"')-value.count('\\"') == 2:
126 value = result.get_string('value').strip_edges().replace('"', '')
127 _value_type = SettingValueType.STRING
128 elif value.begins_with('{') and value.ends_with('}') and value.count('{') == 1:
129 value = result.get_string('value').strip_edges().trim_suffix('}').trim_prefix('{')
130 _value_type = SettingValueType.VARIABLE
132 value = result.get_string('value').strip_edges()
133 if value.is_valid_float():
134 _value_type = SettingValueType.NUMBER
136 _value_type = SettingValueType.EXPRESSION
137 _suppress_default_value = false
140 func is_valid_event(string:String) -> bool:
141 return string.begins_with('setting')
144 ################################################################################
145 ## EDITOR REPRESENTATION
146 ################################################################################
148 func build_event_editor() -> void:
149 add_header_edit('mode', ValueType.FIXED_OPTIONS, {
153 'icon': load("res://addons/dialogic/Editor/Images/Dropdown/default.svg")
156 'value': Modes.RESET,
157 'icon': load("res://addons/dialogic/Editor/Images/Dropdown/update.svg")
159 'label': 'Reset All',
160 'value': Modes.RESET_ALL,
161 'icon': load("res://addons/dialogic/Editor/Images/Dropdown/update.svg")
165 add_header_edit('name', ValueType.DYNAMIC_OPTIONS, {'placeholder':'Type setting', 'suggestions_func':get_settings_suggestions}, 'mode != Modes.RESET_ALL')
166 add_header_edit('_value_type', ValueType.FIXED_OPTIONS, {'left_text':'to',
170 'icon': ["String", "EditorIcons"],
171 'value': SettingValueType.STRING
174 'icon': ["float", "EditorIcons"],
175 'value': SettingValueType.NUMBER
178 'icon': ["ClassList", "EditorIcons"],
179 'value': SettingValueType.VARIABLE
181 'label': 'Expression',
182 'icon': ["Variant", "EditorIcons"],
183 'value': SettingValueType.EXPRESSION
186 '!name.is_empty() and mode == Modes.SET')
187 add_header_edit('value', ValueType.SINGLELINE_TEXT, {}, '!name.is_empty() and (_value_type == SettingValueType.STRING or _value_type == SettingValueType.EXPRESSION) and mode == Modes.SET')
188 add_header_edit('value', ValueType.NUMBER, {}, '!name.is_empty() and _value_type == SettingValueType.NUMBER and mode == Modes.SET')
189 add_header_edit('value', ValueType.DYNAMIC_OPTIONS,
190 {'suggestions_func' : get_value_suggestions, 'placeholder':'Select Variable'},
191 '!name.is_empty() and _value_type == SettingValueType.VARIABLE and mode == Modes.SET')
194 func get_settings_suggestions(filter:String) -> Dictionary:
195 var suggestions := {filter:{'value':filter, 'editor_icon':["GDScriptInternal", "EditorIcons"]}}
197 for prop in ProjectSettings.get_property_list():
198 if prop.name.begins_with('dialogic/settings/'):
199 suggestions[prop.name.trim_prefix('dialogic/settings/')] = {'value':prop.name.trim_prefix('dialogic/settings/'), 'editor_icon':["GDScript", "EditorIcons"]}
203 func get_value_suggestions(_filter:String) -> Dictionary:
204 var suggestions := {}
206 var vars: Dictionary = ProjectSettings.get_setting('dialogic/variables', {})
207 for var_path in DialogicUtil.list_variables(vars):
208 suggestions[var_path] = {'value':var_path, 'editor_icon':["ClassList", "EditorIcons"]}
213 ####################### CODE COMPLETION ########################################
214 ################################################################################
216 func _get_code_completion(CodeCompletionHelper:Node, TextNode:TextEdit, line:String, _word:String, symbol:String) -> void:
217 if symbol == " " and !"reset" in line and !'=' in line and !'"' in line:
218 TextNode.add_code_completion_option(CodeEdit.KIND_MEMBER, "reset", "reset ", event_color.lerp(TextNode.syntax_highlighter.normal_color, 0.5), TextNode.get_theme_icon("RotateLeft", "EditorIcons"))
219 TextNode.add_code_completion_option(CodeEdit.KIND_MEMBER, "reset all", "reset \n", event_color.lerp(TextNode.syntax_highlighter.normal_color, 0.5), TextNode.get_theme_icon("ToolRotate", "EditorIcons"))
221 if (symbol == " " or symbol == '"') and !"=" in line and CodeCompletionHelper.get_line_untill_caret(line).count('"') != 2:
222 for i in get_settings_suggestions(''):
226 TextNode.add_code_completion_option(CodeEdit.KIND_MEMBER, i, i, event_color.lerp(TextNode.syntax_highlighter.normal_color, 0.5), TextNode.get_theme_icon("GDScript", "EditorIcons"), '"')
228 TextNode.add_code_completion_option(CodeEdit.KIND_MEMBER, i, '"'+i, event_color.lerp(TextNode.syntax_highlighter.normal_color, 0.5), TextNode.get_theme_icon("GDScript", "EditorIcons"), '"')
231 func _get_start_code_completion(_CodeCompletionHelper:Node, TextNode:TextEdit) -> void:
232 TextNode.add_code_completion_option(CodeEdit.KIND_PLAIN_TEXT, 'setting', 'setting ', event_color)
234 #################### SYNTAX HIGHLIGHTING #######################################
235 ################################################################################
237 func _get_syntax_highlighting(Highlighter:SyntaxHighlighter, dict:Dictionary, line:String) -> Dictionary:
238 dict[line.find('setting')] = {"color":event_color}
239 dict[line.find('setting')+7] = {"color":Highlighter.normal_color}
240 dict = Highlighter.color_word(dict, event_color, line, 'reset')
241 dict = Highlighter.color_region(dict, Highlighter.string_color, line, '"', '"')
242 dict = Highlighter.color_region(dict, Highlighter.variable_color, line, '{', '}')