added deletion
This commit is contained in:
parent
df8198ef94
commit
77f11a7440
@ -49,6 +49,7 @@ class Editor:
|
|||||||
self.edge_detect_radius: int = 3 * self.line_size
|
self.edge_detect_radius: int = 3 * self.line_size
|
||||||
self.selected_nodes: list[int] = []
|
self.selected_nodes: list[int] = []
|
||||||
self.selected_edges: list[int] = []
|
self.selected_edges: list[int] = []
|
||||||
|
self.previously_created_nodes: list[int] = []
|
||||||
|
|
||||||
def mainloop(self) -> None:
|
def mainloop(self) -> None:
|
||||||
self.state = State.LOADING
|
self.state = State.LOADING
|
||||||
@ -90,12 +91,15 @@ class Editor:
|
|||||||
if event.key == pygame.K_ESCAPE:
|
if event.key == pygame.K_ESCAPE:
|
||||||
if self.selected_nodes != [] or self.selected_edges != []:
|
if self.selected_nodes != [] or self.selected_edges != []:
|
||||||
self.clear_selection()
|
self.clear_selection()
|
||||||
|
self.previously_created_nodes = []
|
||||||
else:
|
else:
|
||||||
self.state = State.STOPPING
|
self.state = State.STOPPING
|
||||||
elif event.key == pygame.K_PAGEUP:
|
elif event.key == pygame.K_PAGEUP:
|
||||||
self.zoom_in()
|
self.zoom_in()
|
||||||
elif event.key == pygame.K_PAGEDOWN:
|
elif event.key == pygame.K_PAGEDOWN:
|
||||||
self.zoom_out()
|
self.zoom_out()
|
||||||
|
elif event.key == pygame.K_BACKSPACE:
|
||||||
|
self.deleted_selected_objects()
|
||||||
elif event.type == pygame.MOUSEBUTTONDOWN:
|
elif event.type == pygame.MOUSEBUTTONDOWN:
|
||||||
if event.button == 2:
|
if event.button == 2:
|
||||||
self.mid_drag_pos = event.pos
|
self.mid_drag_pos = event.pos
|
||||||
@ -321,6 +325,8 @@ class Editor:
|
|||||||
|
|
||||||
def select_object(self, shifting: bool = False) -> None:
|
def select_object(self, shifting: bool = False) -> None:
|
||||||
hover_index, is_node = self.get_hover_object()
|
hover_index, is_node = self.get_hover_object()
|
||||||
|
|
||||||
|
self.previously_created_nodes = []
|
||||||
|
|
||||||
if is_node:
|
if is_node:
|
||||||
self.select_node(hover_index, shifting)
|
self.select_node(hover_index, shifting)
|
||||||
@ -339,8 +345,8 @@ class Editor:
|
|||||||
return
|
return
|
||||||
|
|
||||||
if node in self.selected_nodes:
|
if node in self.selected_nodes:
|
||||||
self.selected_nodes.remove(node)
|
|
||||||
self.clear_selection()
|
self.clear_selection()
|
||||||
|
self.selected_nodes.append(node)
|
||||||
return
|
return
|
||||||
|
|
||||||
if node != -1:
|
if node != -1:
|
||||||
@ -360,6 +366,7 @@ class Editor:
|
|||||||
|
|
||||||
if edge in self.selected_edges:
|
if edge in self.selected_edges:
|
||||||
self.clear_selection()
|
self.clear_selection()
|
||||||
|
self.selected_edges.append(edge)
|
||||||
return
|
return
|
||||||
self.selected_edges = [] if edge == -1 else [edge]
|
self.selected_edges = [] if edge == -1 else [edge]
|
||||||
self.selected_nodes = []
|
self.selected_nodes = []
|
||||||
@ -378,6 +385,8 @@ class Editor:
|
|||||||
self.node_candidate_pos = None
|
self.node_candidate_pos = None
|
||||||
self.typing = False
|
self.typing = False
|
||||||
self.is_creating_node = 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)
|
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):
|
||||||
@ -409,7 +418,6 @@ class Editor:
|
|||||||
|
|
||||||
for edge in self.graph.edges:
|
for edge in self.graph.edges:
|
||||||
dist = self.get_edge_distance(edge.index, mouse_pos[0], mouse_pos[1])
|
dist = self.get_edge_distance(edge.index, mouse_pos[0], mouse_pos[1])
|
||||||
print(dist)
|
|
||||||
if dist < self.edge_detect_radius:
|
if dist < self.edge_detect_radius:
|
||||||
hovering.append(edge.index)
|
hovering.append(edge.index)
|
||||||
dists.append(dist)
|
dists.append(dist)
|
||||||
@ -448,8 +456,6 @@ class Editor:
|
|||||||
|
|
||||||
proj_len = scal_prod / edge_vec_len
|
proj_len = scal_prod / edge_vec_len
|
||||||
|
|
||||||
print(proj_len, edge_vec_len)
|
|
||||||
|
|
||||||
if proj_len < 0:
|
if proj_len < 0:
|
||||||
return self.get_node_distance(start_n.index, px, pz)
|
return self.get_node_distance(start_n.index, px, pz)
|
||||||
if proj_len > edge_vec_len:
|
if proj_len > edge_vec_len:
|
||||||
@ -460,6 +466,19 @@ class Editor:
|
|||||||
node = self.graph.nodes[node_i]
|
node = self.graph.nodes[node_i]
|
||||||
node_pos = self.world_to_screen(node.x, node.z)
|
node_pos = self.world_to_screen(node.x, node.z)
|
||||||
return ((px - node_pos[0]) ** 2 + (pz - node_pos[1]) ** 2) ** 0.5
|
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()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -15,6 +15,27 @@ class Graph:
|
|||||||
def add_edge(self, start_index: int, end_index: int, length: float) -> None:
|
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)))
|
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:
|
def number_of_nodes(self) -> int:
|
||||||
return len(self.nodes)
|
return len(self.nodes)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user