Added node creation by left-click, node drawing and first part of node creator panel drawing

This commit is contained in:
Toby Lane 2024-06-30 20:27:24 +02:00
parent 9870a643dd
commit d1bcdcd530
3 changed files with 45 additions and 6 deletions

View File

@ -7,6 +7,7 @@ import platformdirs
import pygame import pygame
from src.image_handler import ImageHandler from src.image_handler import ImageHandler
from src.graph.graph import Graph
class Editor: class Editor:
@ -37,7 +38,9 @@ class Editor:
lambda z: self.font.render(str(z), True, (255, 255, 255)), lambda z: self.font.render(str(z), True, (255, 255, 255)),
self.ZOOMS self.ZOOMS
)) ))
self.is_creating_node = False
self.state: State = State.STOPPING self.state: State = State.STOPPING
self.graph = Graph()
def mainloop(self) -> None: def mainloop(self) -> None:
self.state = State.LOADING self.state = State.LOADING
@ -64,7 +67,10 @@ class Editor:
self.height = event.y self.height = event.y
elif event.type == pygame.KEYDOWN: elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE: 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: elif event.key == pygame.K_PAGEUP:
self.zoom_in() self.zoom_in()
elif event.key == pygame.K_PAGEDOWN: elif event.key == pygame.K_PAGEDOWN:
@ -72,8 +78,13 @@ class Editor:
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
elif event.button == 1 and keys[pygame.K_LCTRL]: elif event.button == 1:
self.left_drag_pos = event.pos 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: elif event.button == 4:
self.zoom_in() self.zoom_in()
elif event.button == 5: elif event.button == 5:
@ -143,6 +154,8 @@ class Editor:
oy + y * 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 - 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]) pygame.draw.line(self.win, (150, 150, 150), [w2, h2 - self.CROSSHAIR_SIZE], [w2, h2 + self.CROSSHAIR_SIZE])
self.render_zoom_slider() self.render_zoom_slider()
@ -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]) 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]) self.win.blit(mouse_txt, [5, 5])
if self.is_creating_node:
self.render_node_creator()
pygame.display.flip() pygame.display.flip()
def render_zoom_slider(self) -> None: def render_zoom_slider(self) -> None:
@ -195,6 +211,18 @@ class Editor:
pygame.display.flip() 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: 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))
self.zoom = self.ZOOMS[self.zoom_i] self.zoom = self.ZOOMS[self.zoom_i]
@ -213,8 +241,18 @@ class Editor:
return int(world_x), int(world_z) 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): class State(Enum):
STOPPING = auto() STOPPING = auto()
LOADING = auto() LOADING = auto()
RUNNING = auto() RUNNING = auto()
CREATING_NODE = auto()

View File

@ -10,7 +10,7 @@ class Graph:
self.nodes: list[Node] = [] self.nodes: list[Node] = []
def add_node(self, x: int, y: int) -> None: 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: 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))

View File

@ -1,4 +1,5 @@
class Node: class Node:
def __init__(self, x: int, y: int): def __init__(self, x: int, z: int, name: str):
self.x: int = x self.x: int = x
self.y: int = y self.z: int = z
self.name: str = name