From 5d99a0d2c5f9616eebdc3e314c2e4c995fc049da Mon Sep 17 00:00:00 2001 From: Toby Lane Date: Tue, 2 Jul 2024 00:10:56 +0200 Subject: [PATCH] added node movability --- src/editor.py | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/src/editor.py b/src/editor.py index 16f5e1c..c1a5e63 100644 --- a/src/editor.py +++ b/src/editor.py @@ -49,6 +49,8 @@ class Editor: self.selected_edges: list[int] = [] self.previously_created_nodes: list[int] = [] self.selection_rectangle: list[tuple[int, int], tuple[int, int]] = None + self.original_move_pos: tuple[int, int] = None + self.move_old_poses: dict[int, tuple[int, int]] = None def mainloop(self) -> None: self.state = State.LOADING @@ -62,6 +64,8 @@ class Editor: elif self.state == State.RUNNING: if self.selection_rectangle != None: self.expand_selection_rect() + if self.original_move_pos != None: + self.move_poses() self.render() self.clock.tick(30) @@ -103,6 +107,10 @@ class Editor: if len(self.selected_nodes) > 0: self.typing_text = "" if len(self.selected_nodes) > 1 else self.graph.nodes[self.selected_nodes[0]].name self.is_renaming_node = True + elif event.type == pygame.KEYUP: + if event.key == pygame.K_m: + if self.original_move_pos != None: + self.reset_move_poses() elif event.type == pygame.MOUSEBUTTONDOWN: if event.button == 2: self.mid_drag_pos = event.pos @@ -111,6 +119,9 @@ class Editor: self.left_drag_pos = event.pos elif keys[pygame.K_LALT]: self.create_selection_rect(keys[pygame.K_LSHIFT] or keys[pygame.K_RSHIFT]) + elif keys[pygame.K_m]: + self.original_move_pos = event.pos + self.start_moving() else: self.select_object(keys[pygame.K_LSHIFT] or keys[pygame.K_RSHIFT]) elif event.button == 3: @@ -125,6 +136,8 @@ class Editor: elif event.button == 1: if keys[pygame.K_LCTRL]: self.left_drag_pos = None + elif keys[pygame.K_m] and self.original_move_pos != None: + self.confirm_move_poses() elif self.selection_rectangle != None: self.release_selection_rect(keys[pygame.K_LSHIFT] or keys[pygame.K_RSHIFT]) @@ -537,9 +550,35 @@ class Editor: print(top, "<=", pos[1], "<=", bottom) self.selection_rectangle = None + def start_moving(self): + self.move_old_poses = {} + for node_index in self.selected_nodes: + node = self.graph.nodes[node_index] + self.move_old_poses[node_index] = (node.x, node.z) + def move_poses(self): + mouse_pos = pygame.mouse.get_pos() + start_pos = self.original_move_pos + delta_x = mouse_pos[0] - start_pos[0] + delta_z = mouse_pos[1] - start_pos[1] + for node_index in self.move_old_poses.keys(): + node = self.graph.nodes[node_index] + old_pos = self.move_old_poses[node_index] + node.x = old_pos[0] + delta_x + node.z = old_pos[1] + delta_z + def reset_move_poses(self): + self.original_move_pos = None + for node_index in self.move_old_poses.keys(): + node = self.graph.nodes[node_index] + old_pos = self.move_old_poses[node_index] + node.x = old_pos[0] + node.z = old_pos[1] + self.move_old_poses = None + def confirm_move_poses(self): + self.original_move_pos = None + self.move_old_poses = None