diff --git a/src/editor.py b/src/editor.py index 1d3554c..16f5e1c 100644 --- a/src/editor.py +++ b/src/editor.py @@ -38,11 +38,10 @@ class Editor: lambda z: self.font.render(str(z), True, (255, 255, 255)), self.ZOOMS )) - self.is_creating_node: bool = False + self.is_renaming_node: bool = False self.state: State = State.STOPPING self.graph = Graph() 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.edge_detect_radius: int = 3 * self.line_size @@ -77,13 +76,12 @@ class Editor: self.width = event.x self.height = event.y elif event.type == pygame.KEYDOWN: - if self.is_creating_node: + if self.is_renaming_node: if event.key == pygame.K_ESCAPE: - self.is_creating_node = False - self.node_candidate_pos = None + self.is_renaming_node = False self.typing_text = "" elif event.key == pygame.K_RETURN: - self.create_node() + self.rename_nodes() elif event.key == pygame.K_BACKSPACE: self.typing_text = self.typing_text[:-1] else: @@ -101,6 +99,10 @@ class Editor: self.zoom_out() elif event.key == pygame.K_BACKSPACE: self.deleted_selected_objects() + elif event.key == pygame.K_RETURN: + 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.MOUSEBUTTONDOWN: if event.button == 2: self.mid_drag_pos = event.pos @@ -112,8 +114,7 @@ class Editor: else: self.select_object(keys[pygame.K_LSHIFT] or keys[pygame.K_RSHIFT]) elif event.button == 3: - self.node_candidate_pos = self.screen_to_world(event.pos[0], event.pos[1]) - self.is_creating_node = True + self.create_node(self.screen_to_world(event.pos[0], event.pos[1])) elif event.button == 4: self.zoom_in() elif event.button == 5: @@ -200,8 +201,8 @@ class Editor: pygame.draw.rect(self.win, (80, 80, 80), [0, 0, mouse_txt.get_width() + 10, mouse_txt.get_height() + 10]) self.win.blit(mouse_txt, [5, 5]) - if self.is_creating_node: - self.render_node_creator() + if self.is_renaming_node: + self.render_node_renamer() pygame.display.flip() @@ -246,7 +247,7 @@ class Editor: pygame.display.flip() - def render_node_creator(self) -> None: + def render_node_renamer(self) -> None: width = self.width / 2 height = self.height / 2 x0 = (self.width - width) / 2 @@ -254,8 +255,8 @@ class Editor: line_height = height / 6 - nc_txt = self.loading_font.render("NODE CREATOR", True, (255, 255, 255)) - name_txt = self.loading_font.render("Name:", True, (255, 255, 255)) + nc_txt = self.loading_font.render("RENAME NODE", True, (255, 255, 255)) + name_txt = self.loading_font.render("New name:", True, (255, 255, 255)) txt = self.loading_font.render(self.typing_text, True, (255, 255, 255)) pygame.draw.rect(self.win, (0, 0, 0), [x0, y0, width, height]) @@ -397,17 +398,20 @@ class Editor: def clear_selection(self) -> None: self.selected_nodes = [] self.selected_edges = [] - - def create_node(self) -> None: - self.graph.add_node(self.node_candidate_pos[0], self.node_candidate_pos[1], self.typing_text) + + def rename_nodes(self) -> None: + for node in self.selected_nodes: + self.graph.nodes[node].rename_node(self.typing_text) self.typing_text = "" - self.node_candidate_pos = None - self.is_creating_node = False + self.is_renaming_node = False + + def create_node(self, pos, typing_text = "") -> None: + self.graph.add_node(pos[0], pos[1], typing_text) if len(self.selected_nodes) == 1: self.previously_created_nodes.append(self.selected_nodes[0]) self.select_node(self.graph.number_of_nodes() - 1) - def create_edge(self, node_1: int, node_2: int): + def create_edge(self, node_1: int, node_2: int) -> None: 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) diff --git a/src/graph/graph.py b/src/graph/graph.py index 763629a..091b19c 100644 --- a/src/graph/graph.py +++ b/src/graph/graph.py @@ -9,8 +9,8 @@ class Graph: self.edges: list[Edge] = [] self.nodes: list[Node] = [] - def add_node(self, x: int, z: int, name: str) -> None: - self.nodes.append(Node(x, z, name, len(self.nodes))) + def add_node(self, x: int, z: int, name: str = "") -> None: + self.nodes.append(Node(x, z, len(self.nodes), name)) def add_edge(self, start_index: int, end_index: int, length: float) -> None: self.edges.append(Edge(start_index, end_index, length, len(self.edges))) diff --git a/src/graph/node.py b/src/graph/node.py index 4da36c9..87ed3f5 100644 --- a/src/graph/node.py +++ b/src/graph/node.py @@ -1,6 +1,9 @@ class Node: - def __init__(self, x: int, z: int, name: str, index: int): + def __init__(self, x: int, z: int, index: int, name: str = ""): self.x: int = x self.z: int = z self.index: int = index - self.name: str = name \ No newline at end of file + self.name: str = name + + def rename_node(self, name: str): + self.name = name \ No newline at end of file