From 2add469e4be1d9c7d0c2512e9995087ee797a4b2 Mon Sep 17 00:00:00 2001 From: Mew Pur Pur <85438892+MewPurPur@users.noreply.github.com> Date: Sat, 19 Oct 2024 12:08:58 +0300 Subject: [PATCH] Add xml node type checking utils (#930) --- src/autoload/Indications.gd | 6 ++-- src/data_classes/Element.gd | 48 ++++++++++++++--------------- src/data_classes/SVGParser.gd | 2 +- src/data_classes/XNode.gd | 9 ++++++ src/ui_parts/element_container.gd | 2 +- src/ui_parts/handles_manager.gd | 2 +- src/ui_widgets/basic_xnode_frame.gd | 2 +- src/ui_widgets/element_frame.gd | 2 +- src/utils/XNodeChildrenBuilder.gd | 2 +- 9 files changed, 42 insertions(+), 33 deletions(-) diff --git a/src/autoload/Indications.gd b/src/autoload/Indications.gd index 5be80dc9..cdc7ee58 100644 --- a/src/autoload/Indications.gd +++ b/src/autoload/Indications.gd @@ -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, @@ -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), diff --git a/src/data_classes/Element.gd b/src/data_classes/Element.gd index 27af12eb..8cb8c803 100644 --- a/src/data_classes/Element.gd +++ b/src/data_classes/Element.gd @@ -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) @@ -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: @@ -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 @@ -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()): diff --git a/src/data_classes/SVGParser.gd b/src/data_classes/SVGParser.gd index 6ecc65c7..b53fc8b8 100644 --- a/src/data_classes/SVGParser.gd +++ b/src/data_classes/SVGParser.gd @@ -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 += "" % xnode.get_text() BasicXNode.NodeType.CDATA: text += "" % xnode.get_text() diff --git a/src/data_classes/XNode.gd b/src/data_classes/XNode.gd index 25c96f07..aa705413 100644 --- a/src/data_classes/XNode.gd +++ b/src/data_classes/XNode.gd @@ -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 diff --git a/src/ui_parts/element_container.gd b/src/ui_parts/element_container.gd index 722ea596..d651f3e5 100644 --- a/src/ui_parts/element_container.gd +++ b/src/ui_parts/element_container.gd @@ -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 diff --git a/src/ui_parts/handles_manager.gd b/src/ui_parts/handles_manager.gd index b22a2e3b..fc8c474a 100644 --- a/src/ui_parts/handles_manager.gd +++ b/src/ui_parts/handles_manager.gd @@ -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, diff --git a/src/ui_widgets/basic_xnode_frame.gd b/src/ui_widgets/basic_xnode_frame.gd index 83eae634..41b6928c 100644 --- a/src/ui_widgets/basic_xnode_frame.gd +++ b/src/ui_widgets/basic_xnode_frame.gd @@ -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 diff --git a/src/ui_widgets/element_frame.gd b/src/ui_widgets/element_frame.gd index e71d2bf7..6175edfc 100644 --- a/src/ui_widgets/element_frame.gd +++ b/src/ui_widgets/element_frame.gd @@ -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 diff --git a/src/utils/XNodeChildrenBuilder.gd b/src/utils/XNodeChildrenBuilder.gd index 9ff4fa4e..9529884b 100644 --- a/src/utils/XNodeChildrenBuilder.gd +++ b/src/utils/XNodeChildrenBuilder.gd @@ -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)