From 92c907211dc75bbdac3889d19eec859a4b735adf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Mikrut?= Date: Tue, 26 Jan 2021 09:05:59 +0100 Subject: [PATCH] Add Reparenting Deleting --- Autoload/Autoload.gd | 2 +- MainScenes/Node2D.gd | 2 +- Reparenting/Reparenting.gd | 206 --------------- Reparenting/Reparenting.tscn | 6 - ReparentingDeleting/ReparentingDeleting.gd | 252 +++++++++++++++++++ ReparentingDeleting/ReparentingDeleting.tscn | 6 + 6 files changed, 260 insertions(+), 214 deletions(-) delete mode 100644 Reparenting/Reparenting.gd delete mode 100644 Reparenting/Reparenting.tscn create mode 100644 ReparentingDeleting/ReparentingDeleting.gd create mode 100644 ReparentingDeleting/ReparentingDeleting.tscn diff --git a/Autoload/Autoload.gd b/Autoload/Autoload.gd index bc26c38..a56af8d 100644 --- a/Autoload/Autoload.gd +++ b/Autoload/Autoload.gd @@ -22,8 +22,8 @@ const alone_steps : Array = [ "res://Physics/3D/Physics3D.tscn", "res://Rendering/Lights2D/Lights2D.tscn", "res://Rendering/Lights3D/Lights3D.tscn", + "res://ReparentingDeleting/ReparentingDeleting.tscn", "res://Text/Text.tscn", - "res://Reparenting/Reparenting.tscn", ] # All scenes run in one step diff --git a/MainScenes/Node2D.gd b/MainScenes/Node2D.gd index a6236a0..c11858f 100644 --- a/MainScenes/Node2D.gd +++ b/MainScenes/Node2D.gd @@ -15,7 +15,7 @@ func _ready(): add_child(CPUParticles2D.new()) add_child(Camera2D.new()) add_child(CanvasModulate.new()) -# add_child(CollisionPolygon2D.new()) # Errors + add_child(CollisionPolygon2D.new()) add_child(CollisionShape2D.new()) add_child(DampedSpringJoint2D.new()) add_child(GrooveJoint2D.new()) diff --git a/Reparenting/Reparenting.gd b/Reparenting/Reparenting.gd deleted file mode 100644 index 8d346a6..0000000 --- a/Reparenting/Reparenting.gd +++ /dev/null @@ -1,206 +0,0 @@ -extends Node - -var number_of_nodes : int = -1 - -func _ready() -> void: - seed(45) - - for _i in range(3): - add_child(Control.new()) - add_child(Popup.new()) - add_child(AcceptDialog.new()) - add_child(ConfirmationDialog.new()) - add_child(FileDialog.new()) - add_child(PopupMenu.new()) - add_child(PopupPanel.new()) - add_child(Button.new()) - add_child(CheckBox.new()) - add_child(CheckButton.new()) - add_child(ColorPickerButton.new()) - add_child(MenuButton.new()) - add_child(OptionButton.new()) - add_child(LinkButton.new()) - add_child(TextureButton.new()) - add_child(Container.new()) - add_child(ColorPicker.new()) - add_child(VBoxContainer.new()) - add_child(HBoxContainer.new()) - add_child(CenterContainer.new()) - add_child(ScrollContainer.new()) - add_child(GraphNode.new()) - add_child(GridContainer.new()) - add_child(HSplitContainer.new()) - add_child(VSplitContainer.new()) - add_child(MarginContainer.new()) - add_child(PanelContainer.new()) - add_child(TabContainer.new()) - add_child(ColorRect.new()) - add_child(HScrollBar.new()) - add_child(VScrollBar.new()) - add_child(HSlider.new()) - add_child(VSlider.new()) - add_child(ProgressBar.new()) - add_child(SpinBox.new()) - add_child(TextureProgressBar.new()) - add_child(GraphEdit.new()) - add_child(HSeparator.new()) - add_child(VSeparator.new()) - add_child(ItemList.new()) - add_child(Label.new()) - add_child(LineEdit.new()) - add_child(NinePatchRect.new()) - add_child(Panel.new()) - add_child(ReferenceRect.new()) - add_child(RichTextLabel.new()) - add_child(Tabs.new()) - add_child(TextEdit.new()) - add_child(TextureRect.new()) - add_child(Tree.new()) - add_child(VideoPlayer.new()) - - add_child(Node2D.new()) - add_child(AnimatedSprite2D.new()) - add_child(Area2D.new()) - add_child(KinematicBody2D.new()) - add_child(RigidBody2D.new()) - add_child(StaticBody2D.new()) - add_child(AudioStreamPlayer2D.new()) - add_child(BackBufferCopy.new()) - add_child(Bone2D.new()) - add_child(CPUParticles2D.new()) - add_child(Camera2D.new()) - add_child(CanvasModulate.new()) -# add_child(CollisionPolygon2D.new()) # Errors - add_child(CollisionShape2D.new()) - add_child(DampedSpringJoint2D.new()) - add_child(GrooveJoint2D.new()) - add_child(PinJoint2D.new()) - add_child(Light2D.new()) - add_child(LightOccluder2D.new()) - add_child(Line2D.new()) - add_child(MeshInstance2D.new()) - add_child(MultiMeshInstance2D.new()) - add_child(Navigation2D.new()) - add_child(ParallaxLayer.new()) - add_child(GPUParticles2D.new()) - add_child(Path2D.new()) - add_child(PathFollow2D.new()) - add_child(Polygon2D.new()) - add_child(Position2D.new()) - add_child(RayCast2D.new()) - add_child(RemoteTransform2D.new()) - add_child(Skeleton2D.new()) - add_child(Sprite2D.new()) - add_child(TileMap.new()) - add_child(TouchScreenButton.new()) - add_child(VisibilityNotifier2D.new()) - add_child(VisibilityEnabler2D.new()) - add_child(YSort.new()) - - add_child(Node3D.new()) - add_child(XRAnchor3D.new()) - add_child(Camera3D.new()) - add_child(XRCamera3D.new()) - add_child(ClippedCamera3D.new()) - add_child(XRController3D.new()) - add_child(XROrigin3D.new()) - add_child(AnimatedSprite3D.new()) - add_child(Sprite3D.new()) - add_child(CPUParticles3D.new()) - add_child(CSGBox3D.new()) - add_child(CSGCylinder3D.new()) - add_child(CSGMesh3D.new()) - add_child(CSGPolygon3D.new()) - add_child(CSGSphere3D.new()) - add_child(CSGTorus3D.new()) - add_child(CSGCombiner3D.new()) - add_child(ImmediateGeometry3D.new()) - add_child(MeshInstance3D.new()) - add_child(SoftBody3D.new()) - add_child(MultiMeshInstance3D.new()) - add_child(GPUParticles3D.new()) - add_child(BakedLightmap.new()) - add_child(DirectionalLight3D.new()) - add_child(OmniLight3D.new()) - add_child(SpotLight3D.new()) - add_child(GIProbe.new()) - add_child(ReflectionProbe.new()) - add_child(Area3D.new()) - add_child(KinematicBody3D.new()) - add_child(PhysicalBone3D.new()) - add_child(RigidBody3D.new()) - add_child(VehicleBody3D.new()) - add_child(StaticBody3D.new()) - add_child(AudioStreamPlayer3D.new()) - add_child(BoneAttachment3D.new()) - add_child(CollisionPolygon3D.new()) - add_child(CollisionShape3D.new()) - add_child(ConeTwistJoint3D.new()) - add_child(Generic6DOFJoint3D.new()) - add_child(HingeJoint3D.new()) - add_child(PinJoint3D.new()) - add_child(SliderJoint3D.new()) - add_child(GridMap.new()) - add_child(Listener3D.new()) - add_child(Navigation3D.new()) - add_child(Path3D.new()) - add_child(PathFollow3D.new()) - add_child(Position3D.new()) - add_child(ProximityGroup3D.new()) - add_child(RayCast3D.new()) - add_child(RemoteTransform3D.new()) - add_child(Skeleton3D.new()) - add_child(SpringArm3D.new()) - add_child(VehicleWheel3D.new()) - add_child(VisibilityNotifier3D.new()) - add_child(VisibilityEnabler3D.new()) - - - add_child(GPUParticlesAttractorBox.new()) - add_child(GPUParticlesAttractorSphere.new()) - add_child(GPUParticlesAttractorVectorField.new()) - - add_child(GPUParticlesCollisionBox.new()) -# add_child(GPUParticlesCollisionHeightField.new()) - add_child(GPUParticlesCollisionSphere.new()) - add_child(GPUParticlesCollisionSDF.new()) - - add_child(AnimationPlayer.new()) - add_child(AnimationTree.new()) - add_child(AudioStreamPlayer.new()) - add_child(CanvasLayer.new()) - add_child(ParallaxBackground.new()) - add_child(HTTPRequest.new()) - add_child(ResourcePreloader.new()) - add_child(Skeleton2D.new()) - add_child(Timer.new()) - add_child(Tween.new()) - add_child(Viewport.new()) - add_child(WorldEnvironment.new()) - - - - for i in range(get_child_count()): - get_child(i).set_name("Node " + str(i)) - - number_of_nodes = get_child_count() - -func _process(delta: float) -> void: - assert(Performance.get_monitor(Performance.OBJECT_ORPHAN_NODE_COUNT) == 0) - - var choosen_node : Node - var parent_of_node : Node - for i in range(20): - var number = "Node " + str(randi() % number_of_nodes) - choosen_node = find_node(number,true,false) - parent_of_node = choosen_node.get_parent() - - var random_node = find_node("Node " + str(randi() % number_of_nodes),true,false) - if choosen_node.find_node(random_node.get_name(),true,false) != null: # Cannot set as node parent one of its child - continue - if choosen_node == random_node: # Do not reparent node to self - continue - parent_of_node.remove_child(choosen_node) - random_node.add_child(choosen_node) - if !choosen_node.is_inside_tree() || choosen_node.get_parent() == null: # Failed to add node, due e.g. requsrive problem or similar - add_child(choosen_node) diff --git a/Reparenting/Reparenting.tscn b/Reparenting/Reparenting.tscn deleted file mode 100644 index ac33eed..0000000 --- a/Reparenting/Reparenting.tscn +++ /dev/null @@ -1,6 +0,0 @@ -[gd_scene load_steps=2 format=2] - -[ext_resource path="res://Reparenting/Reparenting.gd" type="Script" id=1] - -[node name="Reparenting" type="Node"] -script = ExtResource( 1 ) diff --git a/ReparentingDeleting/ReparentingDeleting.gd b/ReparentingDeleting/ReparentingDeleting.gd new file mode 100644 index 0000000..881467e --- /dev/null +++ b/ReparentingDeleting/ReparentingDeleting.gd @@ -0,0 +1,252 @@ +extends Node + +var number_of_nodes : int = -1 +var node_to_handle_others : Node = Node.new() # Special node to handle all available nodes in tree + +func _ready() -> void: + seed(405) + + + node_to_handle_others.set_name("handle_node") + + for i in range(3): + var parent_node : Node + if i == 0: + parent_node = node_to_handle_others + else: + parent_node = self + + parent_node.add_child(Control.new()) + parent_node.add_child(Popup.new()) + parent_node.add_child(AcceptDialog.new()) + parent_node.add_child(ConfirmationDialog.new()) + parent_node.add_child(FileDialog.new()) + parent_node.add_child(PopupMenu.new()) + parent_node.add_child(PopupPanel.new()) + parent_node.add_child(Button.new()) + parent_node.add_child(CheckBox.new()) + parent_node.add_child(CheckButton.new()) + parent_node.add_child(ColorPickerButton.new()) + parent_node.add_child(MenuButton.new()) + parent_node.add_child(OptionButton.new()) + parent_node.add_child(LinkButton.new()) + parent_node.add_child(TextureButton.new()) + parent_node.add_child(Container.new()) + parent_node.add_child(ColorPicker.new()) + parent_node.add_child(VBoxContainer.new()) + parent_node.add_child(HBoxContainer.new()) + parent_node.add_child(CenterContainer.new()) + parent_node.add_child(ScrollContainer.new()) + parent_node.add_child(GraphNode.new()) + parent_node.add_child(GridContainer.new()) + parent_node.add_child(HSplitContainer.new()) + parent_node.add_child(VSplitContainer.new()) + parent_node.add_child(MarginContainer.new()) + parent_node.add_child(PanelContainer.new()) + parent_node.add_child(TabContainer.new()) + parent_node.add_child(ColorRect.new()) + parent_node.add_child(HScrollBar.new()) + parent_node.add_child(VScrollBar.new()) + parent_node.add_child(HSlider.new()) + parent_node.add_child(VSlider.new()) + parent_node.add_child(ProgressBar.new()) + parent_node.add_child(SpinBox.new()) + parent_node.add_child(TextureProgressBar.new()) + parent_node.add_child(GraphEdit.new()) + parent_node.add_child(HSeparator.new()) + parent_node.add_child(VSeparator.new()) + parent_node.add_child(ItemList.new()) + parent_node.add_child(Label.new()) + parent_node.add_child(LineEdit.new()) + parent_node.add_child(NinePatchRect.new()) + parent_node.add_child(Panel.new()) + parent_node.add_child(ReferenceRect.new()) + parent_node.add_child(RichTextLabel.new()) + parent_node.add_child(Tabs.new()) + parent_node.add_child(TextEdit.new()) + parent_node.add_child(TextureRect.new()) + parent_node.add_child(Tree.new()) + parent_node.add_child(VideoPlayer.new()) + + parent_node.add_child(Node2D.new()) +# parent_node.add_child(AnimatedSprite2D.new()) + parent_node.add_child(Area2D.new()) + parent_node.add_child(KinematicBody2D.new()) + parent_node.add_child(RigidBody2D.new()) + parent_node.add_child(StaticBody2D.new()) + parent_node.add_child(AudioStreamPlayer2D.new()) + parent_node.add_child(BackBufferCopy.new()) + parent_node.add_child(Bone2D.new()) + parent_node.add_child(CPUParticles2D.new()) + parent_node.add_child(Camera2D.new()) + parent_node.add_child(CanvasModulate.new()) + parent_node.add_child(CollisionPolygon2D.new()) + parent_node.add_child(CollisionShape2D.new()) + parent_node.add_child(DampedSpringJoint2D.new()) + parent_node.add_child(GrooveJoint2D.new()) + parent_node.add_child(PinJoint2D.new()) + parent_node.add_child(Light2D.new()) + parent_node.add_child(LightOccluder2D.new()) + parent_node.add_child(Line2D.new()) + parent_node.add_child(MeshInstance2D.new()) + parent_node.add_child(MultiMeshInstance2D.new()) + parent_node.add_child(Navigation2D.new()) + parent_node.add_child(ParallaxLayer.new()) + parent_node.add_child(GPUParticles2D.new()) + parent_node.add_child(Path2D.new()) + parent_node.add_child(PathFollow2D.new()) + parent_node.add_child(Polygon2D.new()) + parent_node.add_child(Position2D.new()) + parent_node.add_child(RayCast2D.new()) + parent_node.add_child(RemoteTransform2D.new()) + parent_node.add_child(Skeleton2D.new()) + parent_node.add_child(Sprite2D.new()) + parent_node.add_child(TileMap.new()) + parent_node.add_child(TouchScreenButton.new()) + parent_node.add_child(VisibilityNotifier2D.new()) + parent_node.add_child(VisibilityEnabler2D.new()) + parent_node.add_child(YSort.new()) + + parent_node.add_child(AnimationPlayer.new()) + parent_node.add_child(AnimationTree.new()) + parent_node.add_child(AudioStreamPlayer.new()) + parent_node.add_child(CanvasLayer.new()) + parent_node.add_child(ParallaxBackground.new()) + parent_node.add_child(HTTPRequest.new()) + parent_node.add_child(ResourcePreloader.new()) + parent_node.add_child(Skeleton2D.new()) + parent_node.add_child(Timer.new()) + parent_node.add_child(Tween.new()) + parent_node.add_child(Viewport.new()) + parent_node.add_child(WorldEnvironment.new()) + + parent_node.add_child(Node3D.new()) + parent_node.add_child(XRAnchor3D.new()) + parent_node.add_child(Camera3D.new()) + parent_node.add_child(XRCamera3D.new()) + parent_node.add_child(ClippedCamera3D.new()) + parent_node.add_child(XRController3D.new()) + parent_node.add_child(XROrigin3D.new()) + parent_node.add_child(AnimatedSprite3D.new()) + parent_node.add_child(Sprite3D.new()) + parent_node.add_child(CPUParticles3D.new()) + parent_node.add_child(CSGBox3D.new()) + parent_node.add_child(CSGCylinder3D.new()) + parent_node.add_child(CSGMesh3D.new()) + parent_node.add_child(CSGPolygon3D.new()) + parent_node.add_child(CSGSphere3D.new()) + parent_node.add_child(CSGTorus3D.new()) + parent_node.add_child(CSGCombiner3D.new()) + parent_node.add_child(ImmediateGeometry3D.new()) + parent_node.add_child(MeshInstance3D.new()) + parent_node.add_child(SoftBody3D.new()) + parent_node.add_child(MultiMeshInstance3D.new()) + parent_node.add_child(GPUParticles3D.new()) + parent_node.add_child(BakedLightmap.new()) + parent_node.add_child(DirectionalLight3D.new()) + parent_node.add_child(OmniLight3D.new()) + parent_node.add_child(SpotLight3D.new()) + parent_node.add_child(GIProbe.new()) +# parent_node.add_child(ReflectionProbe.new()) + parent_node.add_child(Area3D.new()) + parent_node.add_child(KinematicBody3D.new()) + parent_node.add_child(PhysicalBone3D.new()) + parent_node.add_child(RigidBody3D.new()) + parent_node.add_child(VehicleBody3D.new()) + parent_node.add_child(StaticBody3D.new()) + parent_node.add_child(AudioStreamPlayer3D.new()) + parent_node.add_child(BoneAttachment3D.new()) + parent_node.add_child(CollisionPolygon3D.new()) + parent_node.add_child(CollisionShape3D.new()) + parent_node.add_child(ConeTwistJoint3D.new()) + parent_node.add_child(Generic6DOFJoint3D.new()) + parent_node.add_child(HingeJoint3D.new()) + parent_node.add_child(PinJoint3D.new()) + parent_node.add_child(SliderJoint3D.new()) + parent_node.add_child(GridMap.new()) + parent_node.add_child(Listener3D.new()) + parent_node.add_child(Navigation3D.new()) + parent_node.add_child(Path3D.new()) + parent_node.add_child(PathFollow3D.new()) + parent_node.add_child(Position3D.new()) + parent_node.add_child(ProximityGroup3D.new()) + parent_node.add_child(RayCast3D.new()) + parent_node.add_child(RemoteTransform3D.new()) + parent_node.add_child(Skeleton3D.new()) + parent_node.add_child(SpringArm3D.new()) + parent_node.add_child(VehicleWheel3D.new()) + parent_node.add_child(VisibilityNotifier3D.new()) + parent_node.add_child(VisibilityEnabler3D.new()) + + + parent_node.add_child(GPUParticlesAttractorBox.new()) + parent_node.add_child(GPUParticlesAttractorSphere.new()) + parent_node.add_child(GPUParticlesAttractorVectorField.new()) + + parent_node.add_child(GPUParticlesCollisionBox.new()) +# parent_node.add_child(GPUParticlesCollisionHeightField.new()) + parent_node.add_child(GPUParticlesCollisionSphere.new()) + parent_node.add_child(GPUParticlesCollisionSDF.new()) + + + for i in range(get_child_count()): + get_child(i).set_name("Special Node " + str(i)) + + number_of_nodes = get_child_count() + + add_child(node_to_handle_others) # Adding at the end to not broke calculations + +## It is quite easy algorithm to reparent and delete items +# - Add multiple nodes to scene +# - Set name to each +# - In process +# - Get random node +# - Remove its parent +# - Play with a russian roulette +# - If node will be deleted, be sure to get list of its all children and then +# replace all with new nodes(change also name) and old remove with queue_free() +# - Get another random node +# - If nodes are the same, add node to root one(cannot set self as self parent) and repeat steps +# - If second node is child of first, add first node to root one(prevents from memory leaks due invalid reparenting) +# - At the end add first random node as child of second + +func _process(delta: float) -> void: + assert(Performance.get_monitor(Performance.OBJECT_ORPHAN_NODE_COUNT) == 0) + + var choosen_node : Node + var parent_of_node : Node + for i in range(5): + var number : String = "Special Node " + str(randi() % number_of_nodes) + choosen_node = find_node(number,true,false) + parent_of_node = choosen_node.get_parent() + + var random_node = find_node("Special Node " + str(randi() % number_of_nodes),true,false) + parent_of_node.remove_child(choosen_node) + + if randi() % 6 == 0: # 16% chance to remove node with children + var names_to_remove : Array = find_all_special_children_names(choosen_node) + for name_to_remove in names_to_remove: + var node : Node = node_to_handle_others.get_child(randi() % node_to_handle_others.get_child_count()).duplicate(0) + node.set_name(name_to_remove) + add_child(node) + choosen_node.queue_free() + continue + + + if choosen_node.find_node(random_node.get_name(),true,false) != null: # Cannot set as node parent one of its child + add_child(choosen_node) + continue + if choosen_node == random_node: # Do not reparent node to self + add_child(choosen_node) + continue + random_node.add_child(choosen_node) + + +func find_all_special_children_names(node : Node) -> Array: + var array : Array = [] + array.append(node.get_name()) + for child in node.get_children(): + if String(child.get_name()).begins_with("Special Node"): + array.append_array(find_all_special_children_names(child)) + + return array diff --git a/ReparentingDeleting/ReparentingDeleting.tscn b/ReparentingDeleting/ReparentingDeleting.tscn new file mode 100644 index 0000000..90a8bbb --- /dev/null +++ b/ReparentingDeleting/ReparentingDeleting.tscn @@ -0,0 +1,6 @@ +[gd_scene load_steps=2 format=2] + +[ext_resource path="res://ReparentingDeleting/ReparentingDeleting.gd" type="Script" id=1] + +[node name="ReparentingDeleting" type="Node"] +script = ExtResource( 1 )