From f53dab338afb8bd8f1f4a22c8f9de9b1d17364b5 Mon Sep 17 00:00:00 2001 From: Toby Lane Date: Sun, 30 Jun 2024 23:02:15 +0200 Subject: [PATCH] Added edge creation and rendering --- src/editor.py | 26 ++++++++++++++++++++++---- src/graph/graph.py | 6 ++++++ 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/src/editor.py b/src/editor.py index fed8a3c..234d9da 100644 --- a/src/editor.py +++ b/src/editor.py @@ -45,6 +45,7 @@ class Editor: self.typing_text: str = "" self.node_candidate_pos: tuple[int, int] = None self.node_radius: int = 10 + self.line_size: int = (int) (self.node_radius / 5) self.selected_node: int = -1 def mainloop(self) -> None: @@ -86,7 +87,7 @@ class Editor: else: if event.key == pygame.K_ESCAPE: if self.selected_node != -1: - self.selected_node = -1 + self.select_node(-1) else: self.state = State.STOPPING elif event.key == pygame.K_PAGEUP: @@ -100,7 +101,7 @@ class Editor: if keys[pygame.K_LCTRL]: self.left_drag_pos = event.pos else: - self.selected_node = self.get_hover_node() + self.select_node(self.get_hover_node()) elif event.button == 3: self.node_candidate_pos = self.screen_to_world(event.pos[0], event.pos[1]) self.is_creating_node = True @@ -173,6 +174,8 @@ class Editor: ox + x * self.MAP_SIZE, oy + y * self.MAP_SIZE ]) + + self.render_edges() self.render_nodes() @@ -248,6 +251,11 @@ class Editor: self.win.blit(nc_txt, [self.width / 2 - nc_txt.get_width() / 2, y0 + line_height]) self.win.blit(name_txt, [self.width / 2 - name_txt.get_width() / 2, y0 + 3 * line_height]) self.win.blit(txt, [self.width / 2 - txt.get_width() / 2, y0 + 4 * line_height]) + + def render_edges(self) -> None: + for edge in self.graph.edges: + node_1, node_2 = self.graph.get_edge_nodes(edge) + pygame.draw.line(self.win, (255, 0, 0), self.world_to_screen(node_1.x, node_1.z), self.world_to_screen(node_2.x, node_2.z), self.line_size) def render_nodes(self) -> None: for node in self.graph.nodes: @@ -258,7 +266,6 @@ class Editor: node_pos = self.world_to_screen(node.x, node.z) pygame.draw.circle(self.win, (0, 255, 255), (node_pos[0], node_pos[1]), self.node_radius) self.render_mouse_hover_node() - def render_mouse_hover_node(self): node_index = self.get_hover_node() @@ -267,7 +274,7 @@ class Editor: txt = self.loading_font.render(node.name, True, (0, 0, 0)) node_pos = self.world_to_screen(node.x, node.z) self.win.blit(txt, [node_pos[0] - txt.get_width(), node_pos[1] - txt.get_height()]) - pygame.draw.circle(self.win, (0, 0, 0), (node_pos[0], node_pos[1]), self.node_radius, (int) (self.node_radius / 5)) + pygame.draw.circle(self.win, (0, 0, 0), (node_pos[0], node_pos[1]), self.node_radius, self.line_size) def set_zoom(self, zoom_i: int) -> None: self.zoom_i = max(0, min(len(self.ZOOMS) - 1, zoom_i)) @@ -295,12 +302,23 @@ class Editor: return int(screen_x), int(screen_y) + def select_node(self, node: int): + if self.selected_node != -1 and node != -1: + self.create_edge(self.selected_node, node) + self.selected_node = node + def create_node(self) -> None: self.graph.add_node(self.node_candidate_pos[0], self.node_candidate_pos[1], self.typing_text) self.typing_text = "" self.node_candidate_pos = None self.typing = False self.is_creating_node = False + self.select_node(self.graph.number_of_nodes() - 1) + + def create_edge(self, node_1: int, node_2: int): + n1 = self.graph.nodes[node_1] + n2 = self.graph.nodes[node_2] + self.graph.add_edge(node_1, node_2, ((n1.x - n2.x) ** 2 + (n1.z - n2.z) ** 2) ** 0.5) def get_hovering_nodes(self) -> list[int]: hovering = [] diff --git a/src/graph/graph.py b/src/graph/graph.py index e868b31..0844f80 100644 --- a/src/graph/graph.py +++ b/src/graph/graph.py @@ -15,6 +15,12 @@ class Graph: def add_edge(self, start_index: int, end_index: int, length: float) -> None: self.edges.append(Edge(start_index, end_index, length)) + def number_of_nodes(self) -> int: + return len(self.nodes) + + def get_edge_nodes(self, edge) -> tuple[Node, Node]: + return self.nodes[edge.start], self.nodes[edge.end] + def edges_adjacent_to(self, node_i: int) -> Iterator[Edge]: return filter(lambda e: e.start == node_i or e.end == node_i, self.edges)