-
-
Notifications
You must be signed in to change notification settings - Fork 116
Expand file tree
/
Copy pathElement.gd
More file actions
334 lines (268 loc) · 11.9 KB
/
Copy pathElement.gd
File metadata and controls
334 lines (268 loc) · 11.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
## Represents an element, either standalone (<element/>) or container (<element>...</element>).
@abstract class_name Element extends XNode
signal attribute_changed(name: String)
signal ancestor_attribute_changed(name: String)
signal descendant_attribute_changed(name: String)
var _child_elements: Array[XNode]
var _attributes: Dictionary[String, Attribute]
func _init() -> void:
attribute_changed.connect(_on_attribute_changed)
ancestor_attribute_changed.connect(_on_ancestor_attribute_changed)
descendant_attribute_changed.connect(_on_descendant_attribute_changed)
func _on_attribute_changed(attribute_name: String) -> void:
for child in get_children():
if child.is_element():
child.ancestor_attribute_changed.emit(attribute_name)
if parent != null:
parent.descendant_attribute_changed.emit(attribute_name)
if root != null:
root.any_attribute_changed.emit(xid)
func _on_ancestor_attribute_changed(attribute_name: String) -> void:
for child in get_children():
if child.is_element():
child.ancestor_attribute_changed.emit(attribute_name)
func _on_descendant_attribute_changed(attribute_name: String) -> void:
if parent != null:
parent.descendant_attribute_changed.emit(attribute_name)
func _on_attribute_value_changed(attribute: Attribute) -> void:
var has_attrib := has_attribute(attribute.name)
if has_attrib and _attributes[attribute.name].get_value().is_empty():
_attributes.erase(attribute.name)
elif not has_attrib:
_attributes[attribute.name] = attribute
attribute_changed.emit(attribute.name)
func get_children() -> Array[XNode]:
return _child_elements.duplicate()
func get_child(idx: int) -> XNode:
return _child_elements[idx]
func has_children() -> bool:
return not _child_elements.is_empty()
func get_child_count() -> int:
return _child_elements.size()
func get_all_element_descendants() -> Array[Element]:
var elements: Array[Element] = []
for child in get_children():
if child.is_element():
elements.append(child)
elements += child.get_all_element_descendants()
return elements
func get_all_valid_element_descendants() -> Array[Element]:
var elements: Array[Element] = []
for child in get_children():
if child.is_element() and DB.is_child_element_valid(self.name, child.name):
elements.append(child)
elements += child.get_all_valid_element_descendants()
return elements
# Gets the basic XML nodes too.
func get_all_xnode_descendants() -> Array[XNode]:
var xnodes: Array[XNode] = []
for child in get_children():
xnodes.append(child)
if child.is_element():
xnodes += child.get_all_xnode_descendants()
return xnodes
func replace_child(idx: int, new_xnode: XNode) -> void:
var old_xnode := get_child(idx)
_child_elements[idx] = new_xnode
if new_xnode.is_element():
for grandchild_element in new_xnode.get_children():
grandchild_element.parent = new_xnode
if new_xnode is ElementSVG:
grandchild_element.svg = new_xnode
new_xnode.xid = old_xnode.xid
new_xnode.parent = old_xnode.parent
new_xnode.svg = old_xnode.svg
new_xnode.root = old_xnode.root
func insert_child(idx: int, new_xnode: XNode) -> void:
if idx < 0:
idx += get_child_count() + 1
new_xnode.parent = self
new_xnode.root = root
new_xnode.svg = self if self is ElementSVG else svg
if new_xnode is Element:
for xnode_descendant in new_xnode.get_all_xnode_descendants():
xnode_descendant.svg = xnode_descendant if xnode_descendant is ElementSVG else xnode_descendant.parent.svg
xnode_descendant.root = root
var new_xid := xid.duplicate()
new_xid.append(idx)
new_xnode.xid = new_xid
new_xnode.propagate_xid_correction()
for i in range(idx, get_child_count()):
var child := get_child(i)
child.xid[-1] += 1
child.propagate_xid_correction()
_child_elements.insert(idx, new_xnode)
func remove_child(idx: int) -> void:
for i in range(idx + 1, get_child_count()):
var child := get_child(i)
child.xid[-1] -= 1
child.propagate_xid_correction()
_child_elements.remove_at(idx)
func pop_child(idx: int) -> XNode:
for i in range(idx + 1, get_child_count()):
var child := get_child(i)
child.xid[-1] -= 1
child.propagate_xid_correction()
return _child_elements.pop_at(idx)
func propagate_xid_correction() -> void:
for i in get_child_count():
var new_xid := xid.duplicate()
new_xid.append(i)
var child := get_child(i)
child.xid = new_xid
child.propagate_xid_correction()
func has_attribute(attribute_name: String) -> bool:
return _attributes.has(attribute_name)
# If the attribute exists, gets that attribute. If it doesn't, generates it.
func get_attribute(attribute_name: String) -> Attribute:
if has_attribute(attribute_name):
return _attributes[attribute_name]
return new_attribute(attribute_name)
func get_attribute_value(attribute_name: String) -> String:
if has_attribute(attribute_name):
return _attributes[attribute_name].get_value()
return ""
func get_implied_attribute_value(attribute_name: String) -> String:
if has_attribute(attribute_name):
return _attributes[attribute_name].get_value()
return get_default(attribute_name)
func get_attribute_true_color(attribute_name: String) -> String:
if DB.get_attribute_type(attribute_name) != DB.AttributeType.COLOR:
push_error("Attribute not the correct type.")
var attrib_value := get_implied_attribute_value(attribute_name)
if attrib_value == "currentColor":
return get_default("color")
return attrib_value
func get_attribute_num(attribute_name: String) -> float:
if DB.get_attribute_type(attribute_name) != DB.AttributeType.NUMERIC:
push_error("Attribute not the correct type.")
var num: float = _attributes[attribute_name].get_num() if has_attribute(attribute_name) else AttributeNumeric.text_to_num(get_default(attribute_name))
# Possibly adjust for percentage.
if is_attribute_percentage(attribute_name):
var percentage_handling := get_percentage_handling(attribute_name)
if percentage_handling == DB.PercentageHandling.FRACTION:
return num
elif root == self:
# TODO Implement this properly.
match percentage_handling:
DB.PercentageHandling.HORIZONTAL: return 1024 * num
DB.PercentageHandling.VERTICAL: return 1024 * num
DB.PercentageHandling.NORMALIZED: return 1024 * num
else:
match percentage_handling:
DB.PercentageHandling.HORIZONTAL: return svg.viewbox.size.x * num
DB.PercentageHandling.VERTICAL: return svg.viewbox.size.y * num
DB.PercentageHandling.NORMALIZED: return svg.viewbox_normalized_diagonal * num
return num
func is_attribute_percentage(attribute_name: String) -> bool:
if DB.get_attribute_type(attribute_name) != DB.AttributeType.NUMERIC:
push_error("Attribute not the correct type.")
return _attributes[attribute_name].is_percentage() if has_attribute(attribute_name) else AttributeNumeric.text_check_percentage(get_default(attribute_name))
func get_attribute_list(attribute_name: String) -> PackedFloat64Array:
if DB.get_attribute_type(attribute_name) != DB.AttributeType.LIST:
push_error("Attribute not the correct type.")
return _attributes[attribute_name].get_list() if has_attribute(attribute_name) else AttributeList.text_to_list(get_default(attribute_name))
func get_attribute_final_precise_transform(attribute_name: String) -> PackedFloat64Array:
if DB.get_attribute_type(attribute_name) != DB.AttributeType.TRANSFORM_LIST:
push_error("Attribute not the correct type.")
var attrib: AttributeTransformList = _attributes[attribute_name] if has_attribute(attribute_name) else new_default_attribute(attribute_name)
return attrib.get_final_precise_transform()
func set_attribute(attrib_name: String, value: Variant) -> void:
var value_type := typeof(value)
var has_attrib := has_attribute(attrib_name)
if not has_attrib and value_type == TYPE_STRING and value.is_empty():
return
var attrib := _attributes[attrib_name] if has_attrib else new_attribute(attrib_name)
if value_type == TYPE_STRING:
attrib.set_value(value)
else:
match DB.get_attribute_type(attrib_name):
DB.AttributeType.NUMERIC:
if value_type in [TYPE_FLOAT, TYPE_INT]: attrib.set_num(value)
else: push_error("Invalid value set to attribute.")
DB.AttributeType.LIST:
if value_type == TYPE_PACKED_FLOAT64_ARRAY: attrib.set_list(value)
else: push_error("Invalid value set to attribute.")
DB.AttributeType.PATHDATA:
if value_type == TYPE_ARRAY: attrib.set_commands(value)
else: push_error("Invalid value set to attribute.")
DB.AttributeType.TRANSFORM_LIST:
if value_type == TYPE_ARRAY: attrib.set_transform_list(value)
else: push_error("Invalid value set to attribute.")
_:
push_error("Invalid value set to attribute.")
func get_default(attribute_name: String) -> String:
if attribute_name in DB.PROPAGATED_ATTRIBUTES:
if is_parent_g():
return parent.get_implied_attribute_value(attribute_name)
elif svg != null:
return svg.get_implied_attribute_value(attribute_name)
return _get_own_default(attribute_name)
func get_all_attributes() -> Array:
return _attributes.values()
func duplicate(include_children := true) -> Element:
var type: GDScript = get_script()
var new_element: Element
if type == ElementUnrecognized:
new_element = ElementUnrecognized.new(self.name)
else:
new_element = type.new()
for attribute_name in _attributes:
new_element.set_attribute(attribute_name, _attributes[attribute_name].get_value())
if include_children:
for i in get_child_count():
new_element.insert_child(i, get_child(i).duplicate())
return new_element
# Applies children and attributes to another element. Useful for conversion.
func apply_to(element: Element, dropped_attributes: PackedStringArray) -> void:
element._child_elements = _child_elements
for attribute_name in _attributes:
if not attribute_name in dropped_attributes:
element.set_attribute(attribute_name, _attributes[attribute_name].get_value())
# Converts a percentage numeric attribute to absolute.
# TODO this is no longer used, but might become useful again in the future.
func make_attribute_absolute(attrib_name: String) -> void:
if is_attribute_percentage(attrib_name):
var new_attrib := new_attribute(attrib_name)
new_attrib.set_num(get_attribute_num(attrib_name))
_attributes[attrib_name] = new_attrib
# To be overridden in extending classes.
func _get_own_default(_attribute_name: String) -> String:
return ""
func get_percentage_handling(attribute_name: String) -> DB.PercentageHandling:
return DB.get_attribute_default_percentage_handling(attribute_name)
func can_replace(_new_element: String) -> bool:
return false
func get_replacement(_new_element: String) -> Element:
return null
func get_config_warnings() -> PackedStringArray:
var warnings := PackedStringArray()
var own_name: String = self.name
if parent != null and not DB.is_child_element_valid(parent.name, own_name):
warnings.append(Translator.translate("{element} must be inside {allowed} to have any effect.").format(
{"element": own_name, "allowed": "[%s]" % ", ".join(DB.get_valid_parents(own_name))}))
return warnings
func is_attribute_necessary(attribute_name: String) -> bool:
return get_attribute_value(attribute_name) == get_default(attribute_name)
func user_setup(_what = null) -> void:
return
# Helpers
func is_parent_g() -> bool:
return parent != null and parent is ElementG
func get_precise_transform() -> PackedFloat64Array:
var result := PackedFloat64Array([1.0, 0.0, 0.0, 1.0, 0.0, 0.0])
if is_parent_g():
result = Utils64Bit.transforms_mult(result, parent.get_precise_transform())
if has_attribute("transform"):
result = Utils64Bit.transforms_mult(result, get_attribute_final_precise_transform("transform"))
return result
func get_transform() -> Transform2D:
return Utils64Bit.get_transform(get_precise_transform())
func new_attribute(name: String, value := "") -> Attribute:
var attrib := _create_attribute(name, value)
attrib.value_changed.connect(_on_attribute_value_changed.bind(attrib))
return attrib
func new_default_attribute(name: String) -> Attribute:
return _create_attribute(name, get_default(name))
func _create_attribute(name: String, value := "") -> Attribute:
return DB.attribute(name, value)