diff --git a/src/editor.py b/src/editor.py index 7ea11c0..0549d90 100644 --- a/src/editor.py +++ b/src/editor.py @@ -49,6 +49,7 @@ class Editor: self.edge_detect_radius: int = 3 * self.line_size self.selected_nodes: list[int] = [] self.selected_edges: list[int] = [] + self.previously_created_nodes: list[int] = [] def mainloop(self) -> None: self.state = State.LOADING @@ -90,12 +91,15 @@ class Editor: if event.key == pygame.K_ESCAPE: if self.selected_nodes != [] or self.selected_edges != []: self.clear_selection() + self.previously_created_nodes = [] else: self.state = State.STOPPING elif event.key == pygame.K_PAGEUP: self.zoom_in() elif event.key == pygame.K_PAGEDOWN: self.zoom_out() + elif event.key == pygame.K_BACKSPACE: + self.deleted_selected_objects() elif event.type == pygame.MOUSEBUTTONDOWN: if event.button == 2: self.mid_drag_pos = event.pos @@ -321,6 +325,8 @@ class Editor: def select_object(self, shifting: bool = False) -> None: hover_index, is_node = self.get_hover_object() + + self.previously_created_nodes = [] if is_node: self.select_node(hover_index, shifting) @@ -339,8 +345,8 @@ class Editor: return if node in self.selected_nodes: - self.selected_nodes.remove(node) self.clear_selection() + self.selected_nodes.append(node) return if node != -1: @@ -360,6 +366,7 @@ class Editor: if edge in self.selected_edges: self.clear_selection() + self.selected_edges.append(edge) return self.selected_edges = [] if edge == -1 else [edge] self.selected_nodes = [] @@ -378,6 +385,8 @@ class Editor: self.node_candidate_pos = None self.typing = False self.is_creating_node = False + 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): @@ -409,7 +418,6 @@ class Editor: for edge in self.graph.edges: dist = self.get_edge_distance(edge.index, mouse_pos[0], mouse_pos[1]) - print(dist) if dist < self.edge_detect_radius: hovering.append(edge.index) dists.append(dist) @@ -448,8 +456,6 @@ class Editor: proj_len = scal_prod / edge_vec_len - print(proj_len, edge_vec_len) - if proj_len < 0: return self.get_node_distance(start_n.index, px, pz) if proj_len > edge_vec_len: @@ -460,6 +466,19 @@ class Editor: node = self.graph.nodes[node_i] node_pos = self.world_to_screen(node.x, node.z) return ((px - node_pos[0]) ** 2 + (pz - node_pos[1]) ** 2) ** 0.5 + + def deleted_selected_objects(self): + edges_to_delete = [self.graph.edges[i] for i in self.selected_edges] + nodes_to_delete = [self.graph.nodes[i] for i in self.selected_nodes] + for edge in edges_to_delete: + self.graph.delete_edge(edge) + for node in nodes_to_delete: + self.graph.delete_node(node) + self.clear_selection() + n = len(self.previously_created_nodes) + if n != 0: + self.selected_nodes.append(self.previously_created_nodes[n - 1]) + self.previously_created_nodes.pop() diff --git a/src/graph/graph.py b/src/graph/graph.py index ff909f0..bd8c926 100644 --- a/src/graph/graph.py +++ b/src/graph/graph.py @@ -15,6 +15,27 @@ class Graph: 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))) + def delete_edge(self, edge: Edge) -> None: + self.edges.remove(edge) + for ed in self.edges: + ed.index = self.edges.index(ed) + + def delete_node(self, node: Node) -> None: + edges_to_delete=[] + for edge in self.edges: + if node.index in (edge.start, edge.end): + edges_to_delete.append(edge) + continue + if edge.start > node.index: + edge.start -= 1 + if edge.end > node.index: + edge.end -= 1 + for edge in edges_to_delete: + self.delete_edge(edge) + self.nodes.remove(node) + for no in self.nodes: + no.index = self.nodes.index(no) + def number_of_nodes(self) -> int: return len(self.nodes)