added node selection and changed hovering to single node at a time

This commit is contained in:
Toby Lane 2024-06-30 22:13:06 +02:00
parent 6351381789
commit 28a0ad26ce

View File

@ -45,6 +45,7 @@ class Editor:
self.typing_text: str = ""
self.node_candidate_pos: tuple[int, int] = None
self.node_radius: int = 10
self.selected_node: int = -1
def mainloop(self) -> None:
self.state = State.LOADING
@ -84,7 +85,10 @@ class Editor:
self.typing_text += event.unicode
else:
if event.key == pygame.K_ESCAPE:
self.state = State.STOPPING
if self.selected_node != -1:
self.selected_node = -1
else:
self.state = State.STOPPING
elif event.key == pygame.K_PAGEUP:
self.zoom_in()
elif event.key == pygame.K_PAGEDOWN:
@ -96,9 +100,11 @@ class Editor:
if keys[pygame.K_LCTRL]:
self.left_drag_pos = event.pos
else:
self.node_candidate_pos = self.screen_to_world(event.pos[0], event.pos[1])
self.is_creating_node = True
self.typing = True
self.selected_node = self.get_hover_node()
elif event.button == 3:
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:
@ -246,17 +252,21 @@ class Editor:
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)
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:
if self.selected_node != -1:
node = self.graph.nodes[self.selected_node]
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()])
pygame.draw.circle(self.win, (0, 255, 255), (node_pos[0], node_pos[1]), self.node_radius)
self.render_mouse_hover_node()
def render_mouse_hover_node(self):
node_index = self.get_hover_node()
if node_index != -1:
node = self.graph.nodes[self.get_hover_node()]
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, (int) (self.node_radius / 5))
def set_zoom(self, zoom_i: int) -> None:
self.zoom_i = max(0, min(len(self.ZOOMS) - 1, zoom_i))
@ -291,6 +301,23 @@ class Editor:
self.typing = False
self.is_creating_node = False
def get_hovering_nodes(self) -> list[int]:
hovering = []
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))
return hovering
def get_hover_node(self) -> int:
hover_nodes = self.get_hovering_nodes()
return -1 if len(hover_nodes) == 0 else hover_nodes[0]