diff --git a/src/editor.py b/src/editor.py index 9806e0b..40f58bf 100644 --- a/src/editor.py +++ b/src/editor.py @@ -7,6 +7,7 @@ import platformdirs import pygame from src.image_handler import ImageHandler +from src.graph.graph import Graph class Editor: @@ -37,7 +38,9 @@ class Editor: lambda z: self.font.render(str(z), True, (255, 255, 255)), self.ZOOMS )) + self.is_creating_node = False self.state: State = State.STOPPING + self.graph = Graph() def mainloop(self) -> None: self.state = State.LOADING @@ -64,7 +67,10 @@ class Editor: self.height = event.y elif event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: - self.state = State.STOPPING + if self.is_creating_node: + self.is_creating_node = False + else: + self.state = State.STOPPING elif event.key == pygame.K_PAGEUP: self.zoom_in() elif event.key == pygame.K_PAGEDOWN: @@ -72,8 +78,13 @@ class Editor: elif event.type == pygame.MOUSEBUTTONDOWN: if event.button == 2: self.mid_drag_pos = event.pos - elif event.button == 1 and keys[pygame.K_LCTRL]: - self.left_drag_pos = event.pos + elif event.button == 1: + if keys[pygame.K_LCTRL]: + self.left_drag_pos = event.pos + else: + worldpos = self.screen_to_world(event.pos[0], event.pos[1]) + self.graph.add_node(worldpos[0], worldpos[1]) + self.is_creating_node = True elif event.button == 4: self.zoom_in() elif event.button == 5: @@ -142,6 +153,8 @@ class Editor: ox + x * self.MAP_SIZE, oy + y * self.MAP_SIZE ]) + + 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, h2 - self.CROSSHAIR_SIZE], [w2, h2 + self.CROSSHAIR_SIZE]) @@ -153,6 +166,9 @@ 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() + pygame.display.flip() def render_zoom_slider(self) -> None: @@ -195,6 +211,18 @@ class Editor: pygame.display.flip() + def render_node_creator(self) -> None: + width = self.width / 2 + height = self.height / 2 + x0 = (self.width - width) / 2 + y0 = (self.height - height) / 2 + pygame.draw.rect(self.win, (0, 0, 0), [x0, y0, width, height]) + + 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]), 10) + def set_zoom(self, zoom_i: int) -> None: self.zoom_i = max(0, min(len(self.ZOOMS) - 1, zoom_i)) self.zoom = self.ZOOMS[self.zoom_i] @@ -212,9 +240,19 @@ class Editor: world_z = floor((y - h2) / self.zoom + self.center[1]) return int(world_x), int(world_z) + + def world_to_screen(self, world_x: int, world_z: int) -> tuple[int, int]: + w2 = self.width / 2 + h2 = self.height / 2 + screen_x = (world_x - self.center[0]) * self.zoom + w2 + screen_y = (world_z - self.center[1]) * self.zoom + h2 + + return int(screen_x), int(screen_y) + class State(Enum): STOPPING = auto() LOADING = auto() RUNNING = auto() + CREATING_NODE = auto() \ No newline at end of file diff --git a/src/graph/graph.py b/src/graph/graph.py index 57921fc..aa0da7c 100644 --- a/src/graph/graph.py +++ b/src/graph/graph.py @@ -10,7 +10,7 @@ class Graph: self.nodes: list[Node] = [] def add_node(self, x: int, y: int) -> None: - self.nodes.append(Node(x, y)) + self.nodes.append(Node(x, y, "NO NAME")) def add_edge(self, start_index: int, end_index: int, length: float) -> None: self.edges.append(Edge(start_index, end_index, length)) diff --git a/src/graph/node.py b/src/graph/node.py index b37a03d..0dfbf44 100644 --- a/src/graph/node.py +++ b/src/graph/node.py @@ -1,4 +1,5 @@ class Node: - def __init__(self, x: int, y: int): + def __init__(self, x: int, z: int, name: str): self.x: int = x - self.y: int = y \ No newline at end of file + self.z: int = z + self.name: str = name \ No newline at end of file