Skip to content

Commit

Permalink
Add xml node type checking utils (#930)
Browse files Browse the repository at this point in the history
  • Loading branch information
MewPurPur authored Oct 19, 2024
1 parent 0d93079 commit 2add469
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 33 deletions.
6 changes: 3 additions & 3 deletions src/autoload/Indications.gd
Original file line number Diff line number Diff line change
Expand Up @@ -522,8 +522,8 @@ func get_selection_context(popup_method: Callable, context: Context) -> ContextP
"duplicate"))

var xnode := SVG.root_element.get_xnode(selected_xids[0])
if (selected_xids.size() == 1 and xnode is BasicXNode) or\
(xnode is Element and not xnode.possible_conversions.is_empty()):
if (selected_xids.size() == 1 and not xnode.is_element()) or\
(xnode.is_element() and not xnode.possible_conversions.is_empty()):
btn_arr.append(ContextPopup.create_button(
TranslationServer.translate("Convert To"),
popup_convert_to_context.bind(popup_method), false,
Expand Down Expand Up @@ -581,7 +581,7 @@ func popup_convert_to_context(popup_method: Callable) -> void:
if not selected_xids.is_empty():
var btn_arr: Array[Button] = []
var xnode := SVG.root_element.get_xnode(selected_xids[0])
if xnode is BasicXNode:
if not xnode.is_element():
for xnode_type in xnode.get_possible_conversions():
var btn := ContextPopup.create_button(BasicXNode.get_type_string(xnode_type),
convert_selected_xnode_to.bind(xnode_type),
Expand Down
48 changes: 24 additions & 24 deletions src/data_classes/Element.gd
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func _init() -> void:

func _on_attribute_changed(attribute_name: String) -> void:
for child in get_children():
if child is Element:
if child.is_element():
child.ancestor_attribute_changed.emit(attribute_name)
if parent != null:
parent.descendant_attribute_changed.emit(attribute_name)
Expand All @@ -24,7 +24,7 @@ func _on_attribute_changed(attribute_name: String) -> void:

func _on_ancestor_attribute_changed(attribute_name: String) -> void:
for child in get_children():
if child is Element:
if child.is_element():
child.ancestor_attribute_changed.emit(attribute_name)

func _on_descendant_attribute_changed(attribute_name: String) -> void:
Expand Down Expand Up @@ -55,7 +55,7 @@ func get_child_count() -> int:
func get_all_element_descendants() -> Array[Element]:
var elements: Array[Element] = []
for child in get_children():
if child is Element:
if child.is_element():
elements.append(child)
elements += child.get_all_element_descendants()
return elements
Expand All @@ -64,39 +64,39 @@ func get_all_xnode_descendants() -> Array[XNode]:
var xnodes: Array[XNode] = []
for child in get_children():
xnodes.append(child)
if child is Element:
if child.is_element():
xnodes += child.get_all_xnode_descendants()
return xnodes


func replace_child(idx: int, new_element: XNode) -> void:
var old_element := get_child(idx)
_child_elements[idx] = new_element
if new_element is Element:
for grandchild_element in new_element.get_children():
grandchild_element.parent = new_element
if new_element is ElementSVG:
grandchild_element.svg = new_element
new_element.xid = old_element.xid
new_element.parent = old_element.parent
new_element.svg = old_element.svg
new_element.root = old_element.root

func insert_child(idx: int, new_element: XNode) -> void:
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_element.parent = self
new_element.root = root
new_element.svg = self if self is ElementSVG else svg
new_xnode.parent = self
new_xnode.root = root
new_xnode.svg = self if self is ElementSVG else svg
var new_xid := xid.duplicate()
new_xid.append(idx)
new_element.xid = new_xid
new_element.propagate_xid_correction()
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_element)
_child_elements.insert(idx, new_xnode)

func remove_child(idx: int) -> void:
for i in range(idx + 1, get_child_count()):
Expand Down
2 changes: 1 addition & 1 deletion src/data_classes/SVGParser.gd
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ make_attributes_absolute := false) -> String:
else:
text = '\t'.repeat(xnode.xid.size())

if xnode is BasicXNode:
if not xnode.is_element():
match xnode.get_type():
BasicXNode.NodeType.COMMENT: text += "<!--%s-->" % xnode.get_text()
BasicXNode.NodeType.CDATA: text += "<![CDATA[%s]]>" % xnode.get_text()
Expand Down
9 changes: 9 additions & 0 deletions src/data_classes/XNode.gd
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,12 @@ var root: ElementRoot:
# Overridden only in Element. In other XNodes, it does nothing.
func propagate_xid_correction() -> void:
return

func is_element() -> bool:
return self is Element

func is_text_xnode() -> bool:
return self is BasicXNode and self._type == BasicXNode.NodeType.TEXT

func is_non_text_xnode() -> bool:
return self is BasicXNode and self._type != BasicXNode.NodeType.TEXT
2 changes: 1 addition & 1 deletion src/ui_parts/element_container.gd
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func update_proposed_xid() -> void:
var xnode_rect := get_xnode_editor_rect(xnode.xid)
var xnode_start := xnode_rect.position.y
var xnode_end := xnode_rect.end.y
var buffer := minf(xnode_rect.size.y / 3, 26) if xnode is Element else\
var buffer := minf(xnode_rect.size.y / 3, 26) if xnode.is_element() else\
xnode_rect.size.y / 2 + 1
if y_pos < xnode_end and xnode_end < next_y:
next_y = xnode_end
Expand Down
2 changes: 1 addition & 1 deletion src/ui_parts/handles_manager.gd
Original file line number Diff line number Diff line change
Expand Up @@ -631,7 +631,7 @@ func _draw() -> void:

for xid in Indications.selected_xids:
var xnode := SVG.root_element.get_xnode(xid)
if xnode is Element and DB.is_attribute_recognized(xnode.name, "transform"):
if xnode.is_element() and DB.is_attribute_recognized(xnode.name, "transform"):
var bounding_box: Rect2 = xnode.get_bounding_box()
if bounding_box.has_area():
RenderingServer.canvas_item_add_set_transform(selections_surface,
Expand Down
2 changes: 1 addition & 1 deletion src/ui_widgets/basic_xnode_frame.gd
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ func _draw() -> void:
var are_all_children_valid := true
for xid in Indications.selected_xids:
var selected_xnode := xnode.root.get_xnode(xid)
if selected_xnode is BasicXNode:
if not selected_xnode.is_element():
continue
if !DB.is_child_element_valid(drop_element.name, selected_xnode.name):
are_all_children_valid = false
Expand Down
2 changes: 1 addition & 1 deletion src/ui_widgets/element_frame.gd
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ func _draw() -> void:
var are_all_children_valid := true
for xid in Indications.selected_xids:
var xnode := root_element.get_xnode(xid)
if xnode is Element and !DB.is_child_element_valid(drop_tag.name, xnode.name):
if xnode.is_element() and !DB.is_child_element_valid(drop_tag.name, xnode.name):
are_all_children_valid = false
break

Expand Down
2 changes: 1 addition & 1 deletion src/utils/XNodeChildrenBuilder.gd
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const BasicXNodeFrame = preload("res://src/ui_widgets/basic_xnode_frame.tscn")
static func create(element: Element) -> Array[Control]:
var arr: Array[Control] = []
for xnode in element.get_children():
if xnode is Element:
if xnode.is_element():
var element_editor := ElementFrame.instantiate()
element_editor.element = xnode
arr.append(element_editor)
Expand Down

0 comments on commit 2add469

Please sign in to comment.