added names to nodes: made node creator functional (you can type node name), added node name display when hovering

This commit is contained in:
Toby Lane 2024-06-30 21:26:15 +02:00
parent d1bcdcd530
commit 6351381789
2 changed files with 55 additions and 12 deletions

View File

@ -38,9 +38,13 @@ class Editor:
lambda z: self.font.render(str(z), True, (255, 255, 255)),
self.ZOOMS
))
self.is_creating_node = False
self.is_creating_node: bool = False
self.typing: bool = False
self.state: State = State.STOPPING
self.graph = Graph()
self.typing_text: str = ""
self.node_candidate_pos: tuple[int, int] = None
self.node_radius: int = 10
def mainloop(self) -> None:
self.state = State.LOADING
@ -66,15 +70,25 @@ class Editor:
self.width = event.x
self.height = event.y
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
if self.is_creating_node:
if self.typing:
if event.key == pygame.K_ESCAPE:
self.typing = False
self.is_creating_node = False
self.node_candidate_pos = None
self.typing_text = ""
elif event.key == pygame.K_RETURN:
self.create_node()
elif event.key == pygame.K_BACKSPACE:
self.typing_text = self.typing_text[:-1]
else:
self.typing_text += event.unicode
else:
if event.key == pygame.K_ESCAPE:
self.state = State.STOPPING
elif event.key == pygame.K_PAGEUP:
self.zoom_in()
elif event.key == pygame.K_PAGEDOWN:
self.zoom_out()
elif event.key == pygame.K_PAGEUP:
self.zoom_in()
elif event.key == pygame.K_PAGEDOWN:
self.zoom_out()
elif event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 2:
self.mid_drag_pos = event.pos
@ -82,9 +96,9 @@ class Editor:
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.node_candidate_pos = self.screen_to_world(event.pos[0], event.pos[1])
self.is_creating_node = True
self.typing = True
elif event.button == 4:
self.zoom_in()
elif event.button == 5:
@ -216,12 +230,33 @@ class Editor:
height = self.height / 2
x0 = (self.width - width) / 2
y0 = (self.height - height) / 2
line_height = height / 6
nc_txt = self.loading_font.render("NODE CREATOR", True, (255, 255, 255))
name_txt = self.loading_font.render("Name:", True, (255, 255, 255))
txt = self.loading_font.render(self.typing_text, True, (255, 255, 255))
pygame.draw.rect(self.win, (0, 0, 0), [x0, y0, width, height])
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_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)
pygame.draw.circle(self.win, (255, 0, 0), (blitpos[0], blitpos[1]), self.node_radius)
self.render_mouse_hover_node_text()
def render_mouse_hover_node_text(self):
mouse_pos = pygame.mouse.get_pos()
for node in self.graph.nodes:
node_pos = self.world_to_screen(node.x, node.z)
hovering = ((mouse_pos[0] - node_pos[0]) ** 2 + (mouse_pos[1] - node_pos[1]) ** 2) < self.node_radius ** 2
if (hovering):
txt = self.loading_font.render(node.name, True, (0, 0, 0))
self.win.blit(txt, [node_pos[0] - txt.get_width(), node_pos[1] - txt.get_height()])
def set_zoom(self, zoom_i: int) -> None:
self.zoom_i = max(0, min(len(self.ZOOMS) - 1, zoom_i))
@ -248,6 +283,14 @@ class Editor:
screen_y = (world_z - self.center[1]) * self.zoom + h2
return int(screen_x), int(screen_y)
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 = ""
self.node_candidate_pos = None
self.typing = False
self.is_creating_node = False

View File

@ -9,8 +9,8 @@ class Graph:
self.edges: list[Edge] = []
self.nodes: list[Node] = []
def add_node(self, x: int, y: int) -> None:
self.nodes.append(Node(x, y, "NO NAME"))
def add_node(self, x: int, y: int, name: str) -> None:
self.nodes.append(Node(x, y, name))
def add_edge(self, start_index: int, end_index: int, length: float) -> None:
self.edges.append(Edge(start_index, end_index, length))