]> Untitled Git - wolf-seeking-sheep.git/blob - addons/dialogic/Editor/Events/Fields/field_flex_value.gd
Squashed commit of the following:
[wolf-seeking-sheep.git] / addons / dialogic / Editor / Events / Fields / field_flex_value.gd
1 @tool
2 extends HBoxContainer
3
4 ## Event block field part for a value that can change type.
5
6 signal value_changed
7
8 var value_field: Node
9 var value_type: int = -1
10
11 var current_value: Variant
12
13 func _ready() -> void:
14         %ValueType.options = [{
15                         'label': 'String',
16                         'icon': ["String", "EditorIcons"],
17                         'value': TYPE_STRING
18                 },{
19                         'label': 'Number (int)',
20                         'icon': ["int", "EditorIcons"],
21                         'value': TYPE_INT
22                 },{
23                         'label': 'Number (float)',
24                         'icon': ["float", "EditorIcons"],
25                         'value': TYPE_FLOAT
26                 },{
27                         'label': 'Boolean',
28                         'icon': ["bool", "EditorIcons"],
29                         'value': TYPE_BOOL
30                 },{
31                         'label': 'Expression',
32                         'icon': ["Variant", "EditorIcons"],
33                         'value': TYPE_MAX
34                 }
35                 ]
36         %ValueType.symbol_only = true
37         %ValueType.value_changed.connect(_on_type_changed.bind())
38         %ValueType.tooltip_text = "Change type"
39
40
41 func set_value(value:Variant):
42         change_field_type(deduce_type(value))
43         %ValueType.set_value(deduce_type(value))
44         current_value = value
45         match value_type:
46                 TYPE_BOOL:
47                         value_field.button_pressed = value
48                 TYPE_STRING:
49                         value_field.text = value
50                 TYPE_FLOAT, TYPE_INT:
51                         value_field.set_value(value)
52                 TYPE_MAX, _:
53                         value_field.text = value.trim_prefix('@')
54
55
56 func deduce_type(value:Variant) -> int:
57         if value is String and value.begins_with('@'):
58                 return TYPE_MAX
59         else:
60                 return typeof(value)
61
62
63 func _on_type_changed(prop:String, type:Variant) -> void:
64         if type == value_type:
65                 return
66
67         match type:
68                 TYPE_BOOL:
69                         if typeof(current_value) == TYPE_STRING:
70                                 current_value = DialogicUtil.str_to_bool(current_value)
71                         elif value_type == TYPE_FLOAT or value_type == TYPE_INT:
72                                 current_value = bool(current_value)
73                         else:
74                                 current_value = true if current_value else false
75                         set_value(current_value)
76                 TYPE_STRING:
77                         current_value = str(current_value).trim_prefix('@')
78                         set_value(current_value)
79                 TYPE_FLOAT:
80                         current_value = float(current_value)
81                         set_value(current_value)
82                 TYPE_INT:
83                         current_value = int(current_value)
84                         set_value(current_value)
85                 TYPE_MAX,_:
86                         current_value = var_to_str(current_value)
87                         set_value('@'+current_value)
88
89
90         emit_signal.call_deferred('value_changed')
91
92
93 func get_value() -> Variant:
94         return current_value
95
96
97 func _on_delete_pressed() -> void:
98         queue_free()
99         value_changed.emit()
100
101
102 func change_field_type(type:int) -> void:
103         if type == value_type:
104                 return
105
106         value_type = type
107
108         if value_field:
109                 value_field.queue_free()
110         match type:
111                 TYPE_BOOL:
112                         value_field = CheckBox.new()
113                         value_field.toggled.connect(_on_bool_toggled)
114                 TYPE_STRING:
115                         value_field = LineEdit.new()
116                         value_field.theme_type_variation = "DialogicEventEdit"
117                         value_field.text_changed.connect(_on_str_text_changed)
118                         value_field.expand_to_text_length = true
119                 TYPE_FLOAT, TYPE_INT:
120                         value_field = load("res://addons/dialogic/Editor/Events/Fields/field_number.tscn").instantiate()
121                         if type == TYPE_FLOAT:
122                                 value_field.use_float_mode()
123                         else:
124                                 value_field.use_int_mode()
125                         value_field.value_changed.connect(_on_number_value_changed.bind(type == TYPE_INT))
126                 TYPE_MAX, _:
127                         value_field = LineEdit.new()
128                         value_field.expand_to_text_length = true
129                         value_field.text_changed.connect(_on_expression_changed)
130         add_child(value_field)
131         move_child(value_field, 1)
132
133
134 func _on_bool_toggled(value:bool) -> void:
135         current_value = value
136         value_changed.emit()
137
138
139 func _on_str_text_changed(value:String) -> void:
140         current_value = value
141         value_changed.emit()
142
143
144 func _on_expression_changed(value:String) -> void:
145         current_value = '@'+value
146         value_changed.emit()
147
148
149 func _on_number_value_changed(prop:String, value:float, int := false) -> void:
150         if int:
151                 current_value = int(value)
152         else:
153                 current_value = value
154         value_changed.emit()