Added edge creation and rendering

This commit is contained in:
Toby Lane 2024-06-30 23:02:15 +02:00
parent 25f1b20a7a
commit f53dab338a
2 changed files with 28 additions and 4 deletions

View File

@ -45,6 +45,7 @@ class Editor:
self.typing_text: str = "" self.typing_text: str = ""
self.node_candidate_pos: tuple[int, int] = None self.node_candidate_pos: tuple[int, int] = None
self.node_radius: int = 10 self.node_radius: int = 10
self.line_size: int = (int) (self.node_radius / 5)
self.selected_node: int = -1 self.selected_node: int = -1
def mainloop(self) -> None: def mainloop(self) -> None:
@ -86,7 +87,7 @@ class Editor:
else: else:
if event.key == pygame.K_ESCAPE: if event.key == pygame.K_ESCAPE:
if self.selected_node != -1: if self.selected_node != -1:
self.selected_node = -1 self.select_node(-1)
else: else:
self.state = State.STOPPING self.state = State.STOPPING
elif event.key == pygame.K_PAGEUP: elif event.key == pygame.K_PAGEUP:
@ -100,7 +101,7 @@ class Editor:
if keys[pygame.K_LCTRL]: if keys[pygame.K_LCTRL]:
self.left_drag_pos = event.pos self.left_drag_pos = event.pos
else: else:
self.selected_node = self.get_hover_node() self.select_node(self.get_hover_node())
elif event.button == 3: elif event.button == 3:
self.node_candidate_pos = self.screen_to_world(event.pos[0], event.pos[1]) self.node_candidate_pos = self.screen_to_world(event.pos[0], event.pos[1])
self.is_creating_node = True self.is_creating_node = True
@ -174,6 +175,8 @@ class Editor:
oy + y * self.MAP_SIZE oy + y * self.MAP_SIZE
]) ])
self.render_edges()
self.render_nodes() self.render_nodes()
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 - self.CROSSHAIR_SIZE, h2], [w2 + self.CROSSHAIR_SIZE, h2])
@ -249,6 +252,11 @@ class Editor:
self.win.blit(name_txt, [self.width / 2 - name_txt.get_width() / 2, y0 + 3 * 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]) 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: def render_nodes(self) -> None:
for node in self.graph.nodes: for node in self.graph.nodes:
blitpos = self.world_to_screen(node.x, node.z) blitpos = self.world_to_screen(node.x, node.z)
@ -259,7 +267,6 @@ class Editor:
pygame.draw.circle(self.win, (0, 255, 255), (node_pos[0], node_pos[1]), self.node_radius) pygame.draw.circle(self.win, (0, 255, 255), (node_pos[0], node_pos[1]), self.node_radius)
self.render_mouse_hover_node() self.render_mouse_hover_node()
def render_mouse_hover_node(self): def render_mouse_hover_node(self):
node_index = self.get_hover_node() node_index = self.get_hover_node()
if node_index != -1: if node_index != -1:
@ -267,7 +274,7 @@ class Editor:
txt = self.loading_font.render(node.name, True, (0, 0, 0)) txt = self.loading_font.render(node.name, True, (0, 0, 0))
node_pos = self.world_to_screen(node.x, node.z) 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()]) 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: def set_zoom(self, zoom_i: int) -> None:
self.zoom_i = max(0, min(len(self.ZOOMS) - 1, zoom_i)) 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) 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: def create_node(self) -> None:
self.graph.add_node(self.node_candidate_pos[0], self.node_candidate_pos[1], self.typing_text) self.graph.add_node(self.node_candidate_pos[0], self.node_candidate_pos[1], self.typing_text)
self.typing_text = "" self.typing_text = ""
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
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]: def get_hovering_nodes(self) -> list[int]:
hovering = [] hovering = []

View File

@ -15,6 +15,12 @@ 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)) 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]: 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) return filter(lambda e: e.start == node_i or e.end == node_i, self.edges)