]> Untitled Git - wolf-seeking-sheep.git/blob - addons/dialogic/Editor/Events/Fields/field_dictionary.gd
Initial Godot project with Dialogic 2.0-Alpha-17
[wolf-seeking-sheep.git] / addons / dialogic / Editor / Events / Fields / field_dictionary.gd
1 @tool
2 extends DialogicVisualEditorField
3
4 ## Event block field for editing dictionaries.
5
6 const DictionaryValue = "res://addons/dialogic/Editor/Events/Fields/dictionary_part.tscn"
7
8 func _ready() -> void:
9         %Add.icon = get_theme_icon("Add", "EditorIcons")
10
11
12 func _set_value(value:Variant) -> void:
13         for child in get_children():
14                 if child != %Add:
15                         child.queue_free()
16
17         var dict: Dictionary
18
19         # attempt to take dictionary values, create a fresh one if not possible
20         if typeof(value) == TYPE_DICTIONARY:
21                 dict = value
22         elif typeof(value) == TYPE_STRING:
23                 if value.begins_with('{'):
24                         var result: Variant = JSON.parse_string(value)
25                         if result != null:
26                                 dict = result as Dictionary
27
28         var keys := dict.keys()
29         var values := dict.values()
30
31         for index in dict.size():
32                 var x: Node = load(DictionaryValue).instantiate()
33                 add_child(x)
34                 x.set_value(values[index])
35                 x.set_key(keys[index])
36                 x.value_changed.connect(recalculate_values)
37                 move_child(%Add, -1)
38
39
40 func _on_value_changed(value:Variant) -> void:
41         value_changed.emit(property_name, value)
42
43
44 func recalculate_values() -> void:
45         var dict := {}
46         for child in get_children():
47                 if child != %Add and !child.is_queued_for_deletion():
48                         dict[child.get_key()] = child.get_value()
49         _on_value_changed(dict)
50
51
52 func _on_AddButton_pressed() -> void:
53         var x: Control = load(DictionaryValue).instantiate()
54         add_child(x)
55         x.set_key("")
56         x.set_value("")
57         x.value_changed.connect(recalculate_values)
58         x.focus_key()
59         recalculate_values()
60         move_child(%Add, -1)