From b1e858ea5231f6df6a0031a3702183e18b0908df Mon Sep 17 00:00:00 2001 From: klingbolt Date: Sun, 9 Jun 2024 13:18:09 -0400 Subject: [PATCH] Addressing code review. Splits from using node to having explicit variables and pathways --- Gizmo/PointEdit.gd | 2 +- Icon.gd | 6 +-- Route.gd | 6 +-- Route2D.gd | 12 +++--- Spatial.gd | 93 +++++++++++++++++++++++++++++----------------- Spatial.tscn | 4 +- 6 files changed, 74 insertions(+), 49 deletions(-) diff --git a/Gizmo/PointEdit.gd b/Gizmo/PointEdit.gd index aeda1ceb..1b9bcb3f 100644 --- a/Gizmo/PointEdit.gd +++ b/Gizmo/PointEdit.gd @@ -2,7 +2,7 @@ extends Spatial var camera: Camera signal selected(selected_object) -signal deselected(selected_object) +signal deselected() signal updated(point_position) var last_translation var selected: bool = false diff --git a/Icon.gd b/Icon.gd index 5f488793..d291db53 100644 --- a/Icon.gd +++ b/Icon.gd @@ -23,8 +23,8 @@ func set_icon_image(texture_path: String): self.texture = texture self.material_override.set_shader_param("texture_albedo", texture) -func update_poistion(position: Vector3): +func set_position(position: Vector3): self.translation = position -func remove(index: int = 0): - queue_free() +func remove_icon(): + self.queue_free() diff --git a/Route.gd b/Route.gd index 92af260e..d6cb8231 100644 --- a/Route.gd +++ b/Route.gd @@ -78,7 +78,7 @@ func get_point_count(): func get_point_position(index: int): return self.point_list[index] -func update_poistion(position: Vector3, index: int): +func set_point_position(position: Vector3, index: int): self.point_list[index] = position refresh_mesh() @@ -89,11 +89,11 @@ func add_point(position: Vector3, index: int = -1): self.point_list.insert(index, position) refresh_mesh() -func remove(index: int): +func remove_point(index: int): self.point_list.remove(index) refresh_mesh() -func new_point_after(midpoint: Vector3, index): +func new_point_after(midpoint: Vector3, index: int): var start: Vector3 = self.get_point_position(index) if self.get_point_count() > index+1: var end: Vector3 = self.get_point_position(index+1) diff --git a/Route2D.gd b/Route2D.gd index b93e758e..cd891542 100644 --- a/Route2D.gd +++ b/Route2D.gd @@ -1,9 +1,6 @@ extends Line2D -func add_new_point(position: Vector3, index: int = -1): - self.add_point(Vector2(position.x, position.z), index) - -func update_poistion(position: Vector3, index: int): +func on_update_position(position: Vector3, index: int): self.set_point_position(index, Vector2(position.x, position.z)) func new_point_after(midpoint: Vector3, index: int): @@ -15,5 +12,10 @@ func new_point_after(midpoint: Vector3, index: int): midpoint2d = ((start-end)/2) + end add_point(midpoint2d, index+1) -func remove(index: int): +func remove_point(index: int): self.remove_point(index) + +func reverse(): + var points = self.points + points.invert() + self.points = points diff --git a/Spatial.gd b/Spatial.gd index a444a7d4..8da9fc71 100644 --- a/Spatial.gd +++ b/Spatial.gd @@ -692,47 +692,56 @@ func gen_adjustment_nodes(): continue var new_gizmo = gizmo_scene.instance() new_gizmo.translation = gizmo_position - new_gizmo.connect("selected", self, "on_gizmo_selected", [route, "path", path2d, i]) - new_gizmo.connect("deselected", self, "on_gizmo_deselected", [route, "path", path2d, i]) - new_gizmo.connect("updated", route, "update_point_poistion", [i]) - new_gizmo.connect("updated", path2d, "update_point_poistion", [i]) + new_gizmo.connect("selected", self, "on_path_gizmo_selected", [route, path2d, i]) + new_gizmo.connect("deselected", self, "on_gizmo_deselected") + new_gizmo.connect("updated", route, "set_point_position", [i]) + new_gizmo.connect("updated", path2d, "on_update_position", [i]) $Gizmos.add_child(new_gizmo) for icon in category3d.icons: var new_gizmo = gizmo_scene.instance() new_gizmo.translation = icon.translation - new_gizmo.connect("selected", self, "on_gizmo_selected", [icon, "icon"]) - new_gizmo.connect("deselected", self, "on_gizmo_deselected", [icon, "icon"]) - new_gizmo.connect("updated", icon, "update_point_poistion") + new_gizmo.connect("selected", self, "on_icon_gizmo_selected", [icon]) + new_gizmo.connect("deselected", self, "on_gizmo_deselected") + new_gizmo.connect("updated", icon, "set_position") $Gizmos.add_child(new_gizmo) var currently_selected_gizmo = null -var currently_selected_node3d = null -var currently_selected_node2d = null +var currently_selected_icon = null +var currently_selected_path3d = null +var currently_selected_path2d = null var currently_selected_index = 0 -func on_gizmo_selected(object, node3d, point_type: String, node2d = null, index: int = 0): +func on_icon_gizmo_selected(object: Spatial, icon: Sprite3D): self.currently_selected_gizmo = object - self.currently_selected_node3d = node3d - self.currently_selected_node2d = node2d + self.currently_selected_icon = icon + $Control/Dialogs/NodeEditorDialog/ScrollContainer/VBoxContainer/DeleteNode.disabled = false + $Control/Dialogs/NodeEditorDialog/ScrollContainer/VBoxContainer/SnapSelectedToPlayer.disabled = false + $Control/Dialogs/NodeEditorDialog/ScrollContainer/VBoxContainer/XZSnapToPlayer.disabled = false + $Control/Dialogs/NodeEditorDialog/ScrollContainer/VBoxContainer/YSnapToPlayer.disabled = false + + +func on_path_gizmo_selected(object: Spatial, path3d: Spatial, path2d: Line2D, index: int): + self.currently_selected_gizmo = object + self.currently_selected_path3d = path3d + self.currently_selected_path2d = path2d self.currently_selected_index = index $Control/Dialogs/NodeEditorDialog/ScrollContainer/VBoxContainer/DeleteNode.disabled = false - # Only enable these buttons if the object selected is a point on the path not an icon - if point_type == "path": - $Control/Dialogs/NodeEditorDialog/ScrollContainer/VBoxContainer/NewNodeAfter.disabled = false - $Control/Dialogs/NodeEditorDialog/ScrollContainer/VBoxContainer/ReversePathDirection.disabled = false - $Control/Dialogs/NodeEditorDialog/ScrollContainer/VBoxContainer/SetActivePath.disabled = false + $Control/Dialogs/NodeEditorDialog/ScrollContainer/VBoxContainer/NewPathPointAfter.disabled = false + $Control/Dialogs/NodeEditorDialog/ScrollContainer/VBoxContainer/ReversePathDirection.disabled = false + $Control/Dialogs/NodeEditorDialog/ScrollContainer/VBoxContainer/SetActivePath.disabled = false $Control/Dialogs/NodeEditorDialog/ScrollContainer/VBoxContainer/SnapSelectedToPlayer.disabled = false $Control/Dialogs/NodeEditorDialog/ScrollContainer/VBoxContainer/XZSnapToPlayer.disabled = false $Control/Dialogs/NodeEditorDialog/ScrollContainer/VBoxContainer/YSnapToPlayer.disabled = false -func on_gizmo_deselected(object): +func on_gizmo_deselected(): self.currently_selected_gizmo = null - self.currently_selected_node3d = null - self.currently_selected_node2d = null + self.currently_selected_icon = null + self.currently_selected_path3d = null + self.currently_selected_path2d = null self.currently_selected_index = 0 $Control/Dialogs/NodeEditorDialog/ScrollContainer/VBoxContainer/DeleteNode.disabled = true - $Control/Dialogs/NodeEditorDialog/ScrollContainer/VBoxContainer/NewNodeAfter.disabled = true + $Control/Dialogs/NodeEditorDialog/ScrollContainer/VBoxContainer/NewPathPointAfter.disabled = true $Control/Dialogs/NodeEditorDialog/ScrollContainer/VBoxContainer/SnapSelectedToPlayer.disabled = true $Control/Dialogs/NodeEditorDialog/ScrollContainer/VBoxContainer/XZSnapToPlayer.disabled = true $Control/Dialogs/NodeEditorDialog/ScrollContainer/VBoxContainer/YSnapToPlayer.disabled = true @@ -826,58 +835,72 @@ func _on_NewPathPoint_pressed(): var z_accurate_player_position = player_position z_accurate_player_position.z = -z_accurate_player_position.z self.currently_active_path.add_point(z_accurate_player_position) - self.currently_active_path2d.add_point(z_accurate_player_position) + self.currently_active_path2d.add_point(Vector2(z_accurate_player_position.x, z_accurate_player_position.z)) func _on_NodeEditorDialog_hide(): - self.currently_selected_gizmo = null + on_gizmo_deselected() clear_adjustment_nodes() _on_Dialog_hide() func _on_DeleteNode_pressed(): - self.currently_selected_node3d.remove(self.currently_selected_index) - if self.currently_selected_node2d != null: - self.currently_selected_node2d.remove(self.currently_selected_index) - on_gizmo_deselected(self.currently_selected_gizmo) + if self.currently_selected_icon != null: + self.currently_selected_icon.remove_icon() + if self.currently_selected_path3d != null: + self.currently_selected_path3d.remove_point(self.currently_selected_index) + if self.currently_selected_path2d != null: + self.currently_selected_path2d.remove_point(self.currently_selected_index) + on_gizmo_deselected() clear_adjustment_nodes() gen_adjustment_nodes() -func _on_NewNodeAfter_pressed(): +func _on_NewPathPointAfter_pressed(): print("insert path node") var midpoint = self.player_position midpoint.z = -midpoint.z - self.currently_selected_node3d.new_point_after(midpoint, self.currently_selected_index) - if self.currently_selected_node2d != null: - self.currently_selected_node2d.new_point_after(midpoint, self.currently_selected_index) + if self.currently_selected_path3d != null: + self.currently_selected_path3d.new_point_after(midpoint, self.currently_selected_index) + if self.currently_selected_path2d != null: + self.currently_selected_path2d.new_point_after(midpoint, self.currently_selected_index) - on_gizmo_deselected(self.currently_selected_gizmo) + on_gizmo_deselected() clear_adjustment_nodes() gen_adjustment_nodes() func _on_XZSnapToPlayer_pressed(): + if self.currently_selected_gizmo == null: + print("Warning: No Point Selected") self.currently_selected_gizmo.translation.x = self.player_position.x self.currently_selected_gizmo.translation.z = -self.player_position.z func _on_YSnapToPlayer_pressed(): + if self.currently_selected_gizmo == null: + print("Warning: No Point Selected") self.currently_selected_gizmo.translation.y = self.player_position.y func _on_SnapSelectedToPlayer_pressed(): + if self.currently_selected_gizmo == null: + print("Warning: No Point Selected") self.currently_selected_gizmo.translation.x = self.player_position.x self.currently_selected_gizmo.translation.z = -self.player_position.z self.currently_selected_gizmo.translation.y = self.player_position.y func _on_SetActivePath_pressed(): - self.currently_active_path = self.currently_selected_node3d - self.currently_active_path2d = self.currently_selected_node2d + self.currently_active_path = self.currently_selected_path3d + self.currently_active_path2d = self.currently_selected_path2d func set_active_path(path): self.currently_active_path = path func _on_ReversePathDirection_pressed(): - self.currently_selected_node3d.reverse() + self.currently_selected_path3d.reverse() + self.currently_selected_path2d.reverse() + on_gizmo_deselected() + clear_adjustment_nodes() + gen_adjustment_nodes() func _on_ExitButton_pressed(): diff --git a/Spatial.tscn b/Spatial.tscn index 05650e14..32505f06 100644 --- a/Spatial.tscn +++ b/Spatial.tscn @@ -550,7 +550,7 @@ size_flags_horizontal = 3 disabled = true text = "Delete Selected" -[node name="NewNodeAfter" type="Button" parent="Control/Dialogs/NodeEditorDialog/ScrollContainer/VBoxContainer"] +[node name="NewPathPointAfter" type="Button" parent="Control/Dialogs/NodeEditorDialog/ScrollContainer/VBoxContainer"] margin_top = 44.0 margin_right = 323.0 margin_bottom = 84.0 @@ -917,7 +917,7 @@ material/0 = SubResource( 4 ) [connection signal="hide" from="Control/Dialogs/SaveDialog" to="." method="_on_Dialog_hide"] [connection signal="hide" from="Control/Dialogs/NodeEditorDialog" to="." method="_on_NodeEditorDialog_hide"] [connection signal="pressed" from="Control/Dialogs/NodeEditorDialog/ScrollContainer/VBoxContainer/DeleteNode" to="." method="_on_DeleteNode_pressed"] -[connection signal="pressed" from="Control/Dialogs/NodeEditorDialog/ScrollContainer/VBoxContainer/NewNodeAfter" to="." method="_on_NewNodeAfter_pressed"] +[connection signal="pressed" from="Control/Dialogs/NodeEditorDialog/ScrollContainer/VBoxContainer/NewPathPointAfter" to="." method="_on_NewPathPointAfter_pressed"] [connection signal="pressed" from="Control/Dialogs/NodeEditorDialog/ScrollContainer/VBoxContainer/SnapSelectedToPlayer" to="." method="_on_SnapSelectedToPlayer_pressed"] [connection signal="pressed" from="Control/Dialogs/NodeEditorDialog/ScrollContainer/VBoxContainer/XZSnapToPlayer" to="." method="_on_XZSnapToPlayer_pressed"] [connection signal="pressed" from="Control/Dialogs/NodeEditorDialog/ScrollContainer/VBoxContainer/YSnapToPlayer" to="." method="_on_YSnapToPlayer_pressed"]