diff --git a/src/editor.py b/src/editor.py index fc75b99..7ea11c0 100644 --- a/src/editor.py +++ b/src/editor.py @@ -46,8 +46,9 @@ class Editor: 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 - self.selected_edge: int = -1 + self.edge_detect_radius: int = 3 * self.line_size + self.selected_nodes: list[int] = [] + self.selected_edges: list[int] = [] def mainloop(self) -> None: self.state = State.LOADING @@ -87,8 +88,8 @@ class Editor: self.typing_text += event.unicode else: if event.key == pygame.K_ESCAPE: - if self.selected_node != -1: - self.select_node(-1) + if self.selected_nodes != [] or self.selected_edges != []: + self.clear_selection() else: self.state = State.STOPPING elif event.key == pygame.K_PAGEUP: @@ -102,7 +103,7 @@ class Editor: if keys[pygame.K_LCTRL]: self.left_drag_pos = event.pos else: - self.select_node(self.get_hover_node()) + 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 @@ -176,9 +177,7 @@ class Editor: oy + y * self.MAP_SIZE ]) - self.render_edges() - - self.render_nodes() + self.render_graph() pygame.draw.line(self.win, (150, 150, 150), [w2 - self.CROSSHAIR_SIZE, h2], [w2 + self.CROSSHAIR_SIZE, h2]) pygame.draw.line(self.win, (150, 150, 150), [w2, h2 - self.CROSSHAIR_SIZE], [w2, h2 + self.CROSSHAIR_SIZE]) @@ -252,33 +251,47 @@ 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_graph(self) -> None: + self.render_edges() + + hover_index, is_node = self.get_hover_object() + if is_node: + self.render_nodes() + self.render_hover_node(hover_index) + else: + self.render_hover_edge(hover_index) + self.render_nodes() 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) - if self.selected_edge != -1: - node_1, node_2 = self.graph.get_edge_nodes(self.graph.edges[self.selected_edge]) - pygame.draw.line(self.win, (0, 255, 255), self.world_to_screen(node_1.x, node_1.z), self.world_to_screen(node_2.x, node_2.z), self.line_size) + color = (0, 255, 255) if edge.index in self.selected_edges else (255, 0, 0) + pygame.draw.line(self.win, color, 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: blitpos = self.world_to_screen(node.x, node.z) pygame.draw.circle(self.win, (255, 0, 0), (blitpos[0], blitpos[1]), self.node_radius) - if self.selected_node != -1: - node = self.graph.nodes[self.selected_node] - 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() + for node_index in self.selected_nodes: + node = self.graph.nodes[node_index] + blitpos = self.world_to_screen(node.x, node.z) + pygame.draw.circle(self.win, (0, 255, 255), (blitpos[0], blitpos[1]), self.node_radius) - def render_mouse_hover_node(self): - node_index = self.get_hover_node() + def render_hover_node(self, node_index): if node_index != -1: - node = self.graph.nodes[self.get_hover_node()] + node = self.graph.nodes[node_index] 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, self.line_size) + + def render_hover_edge(self, edge_index): + if edge_index != -1: + node_1, node_2 = self.graph.get_edge_nodes(self.graph.edges[edge_index]) + pygame.draw.line(self.win, (0, 0, 0), self.world_to_screen(node_1.x, node_1.z), self.world_to_screen(node_2.x, node_2.z), self.edge_detect_radius) + color = (0, 255, 255) if edge_index in self.selected_edges else (255, 0, 0) + pygame.draw.line(self.win, color, self.world_to_screen(node_1.x, node_1.z), self.world_to_screen(node_2.x, node_2.z), self.line_size) def set_zoom(self, zoom_i: int) -> None: self.zoom_i = max(0, min(len(self.ZOOMS) - 1, zoom_i)) @@ -306,18 +319,59 @@ class Editor: return int(screen_x), int(screen_y) - def select_node(self, node: int): - self.selected_edge = -1 - if self.selected_node != -1 and node != -1: - self.link_nodes(self.selected_node, node) - self.selected_node = node - - def link_nodes(self, node_1: int, node_2: int): - if self.graph.edge_exists(node_1, node_2): - self.selected_edge = self.graph.get_edge(node_1, node_2) + def select_object(self, shifting: bool = False) -> None: + hover_index, is_node = self.get_hover_object() + + if is_node: + self.select_node(hover_index, shifting) + elif hover_index != -1: + self.select_edge(hover_index, shifting) else: + self.clear_selection() + + def select_node(self, node: int, shifting: bool = False) -> None: + if shifting: + if node in self.selected_nodes: + self.selected_nodes.remove(node) + return + if node != -1: + self.selected_nodes.append(node) + return + + if node in self.selected_nodes: + self.selected_nodes.remove(node) + self.clear_selection() + return + + if node != -1: + for sel_node in self.selected_nodes: + self.link_nodes(sel_node, node) + self.selected_nodes = [] if node == -1 else [node] + self.selected_edges = [] + + def select_edge(self, edge: int, shifting: bool = False) -> None: + if shifting: + if edge in self.selected_edges: + self.selected_edges.remove(edge) + return + if edge != -1: + self.selected_edges.append(edge) + return + + if edge in self.selected_edges: + self.clear_selection() + return + self.selected_edges = [] if edge == -1 else [edge] + self.selected_nodes = [] + + def link_nodes(self, node_1: int, node_2: int) -> None: + if not self.graph.edge_exists(node_1, node_2): self.create_edge(node_1, node_2) + 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) self.typing_text = "" @@ -331,20 +385,82 @@ class Editor: 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]: + def get_hovering_nodes(self) -> tuple[list[int], list[float]]: hovering = [] + dists = [] mouse_pos = pygame.mouse.get_pos() for node in self.graph.nodes: - node_pos = self.world_to_screen(node.x, node.z) - if ((mouse_pos[0] - node_pos[0]) ** 2 + (mouse_pos[1] - node_pos[1]) ** 2) < self.node_radius ** 2 : - hovering.append(self.graph.nodes.index(node)) + dist = self.get_node_distance(node.index, mouse_pos[0], mouse_pos[1]) + if dist < self.node_radius: + hovering.append(node.index) + dists.append(dist) - return hovering + return hovering, dists def get_hover_node(self) -> int: - hover_nodes = self.get_hovering_nodes() - return -1 if len(hover_nodes) == 0 else hover_nodes[0] + hover_nodes, distances = self.get_hovering_nodes() + return -1 if len(hover_nodes) == 0 else hover_nodes[distances.index(min(distances))] + + def get_hovering_edges(self) -> tuple[list[int], list[float]]: + hovering = [] + dists = [] + mouse_pos = pygame.mouse.get_pos() + + 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) + + return hovering, dists + + def get_hover_edge(self) -> int: + hover_edges, distances = self.get_hovering_edges() + return -1 if len(hover_edges) == 0 else hover_edges[distances.index(min(distances))] + + def get_hover_object(self) -> tuple[int, bool]: + node = self.get_hover_node() + if node != -1: + return node, True + + edge = self.get_hover_edge() + if edge != -1: + return edge, False + + return -1, False + + def get_edge_distance(self, edge_i: int, px: int, pz: int) -> float: + start_n, end_n = self.graph.get_edge_nodes(self.graph.edges[edge_i]) + start_p = self.world_to_screen(start_n.x, start_n.z) + end_p = self.world_to_screen(end_n.x, end_n.z) + + edge_vec = (end_p[0] - start_p[0], end_p[1] - start_p[1]) + start_vec = (px - start_p[0], pz - start_p[1]) + + edge_vec_len = (edge_vec[0] ** 2 + edge_vec[1] ** 2) ** 0.5 + + if edge_vec_len == 0: + return self.get_node_distance(start_n.index, px, pz) + + scal_prod = start_vec[0] * edge_vec[0] + start_vec[1] * edge_vec[1] + + 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: + return self.get_node_distance(end_n.index, px, pz) + return abs((edge_vec[0] * start_vec[1] - edge_vec[1] * start_vec[0]) / edge_vec_len) + + def get_node_distance(self, node_i: int, px: int, pz: int) -> float: + 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 + diff --git a/src/graph/edge.py b/src/graph/edge.py index 839100d..98254e4 100644 --- a/src/graph/edge.py +++ b/src/graph/edge.py @@ -1,5 +1,6 @@ class Edge: - def __init__(self, start: int, end: int, length: float): + def __init__(self, start: int, end: int, length: float, index: int): self.length: float = length self.start: int = start - self.end: int = end \ No newline at end of file + self.end: int = end + self.index: int = index \ No newline at end of file diff --git a/src/graph/graph.py b/src/graph/graph.py index cfeb64f..ff909f0 100644 --- a/src/graph/graph.py +++ b/src/graph/graph.py @@ -10,10 +10,10 @@ class Graph: self.nodes: list[Node] = [] def add_node(self, x: int, y: int, name: str) -> None: - self.nodes.append(Node(x, y, name)) + self.nodes.append(Node(x, y, name, len(self.nodes))) def add_edge(self, start_index: int, end_index: int, length: float) -> None: - self.edges.append(Edge(start_index, end_index, length)) + self.edges.append(Edge(start_index, end_index, length, len(self.edges))) def number_of_nodes(self) -> int: return len(self.nodes) diff --git a/src/graph/node.py b/src/graph/node.py index 0dfbf44..4da36c9 100644 --- a/src/graph/node.py +++ b/src/graph/node.py @@ -1,5 +1,6 @@ class Node: - def __init__(self, x: int, z: int, name: str): + def __init__(self, x: int, z: int, name: str, index: int): self.x: int = x self.z: int = z + self.index: int = index self.name: str = name \ No newline at end of file