added node selection and changed hovering to single node at a time
This commit is contained in:
parent
6351381789
commit
28a0ad26ce
@ -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.selected_node: int = -1
|
||||||
|
|
||||||
def mainloop(self) -> None:
|
def mainloop(self) -> None:
|
||||||
self.state = State.LOADING
|
self.state = State.LOADING
|
||||||
@ -84,7 +85,10 @@ class Editor:
|
|||||||
self.typing_text += event.unicode
|
self.typing_text += event.unicode
|
||||||
else:
|
else:
|
||||||
if event.key == pygame.K_ESCAPE:
|
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:
|
elif event.key == pygame.K_PAGEUP:
|
||||||
self.zoom_in()
|
self.zoom_in()
|
||||||
elif event.key == pygame.K_PAGEDOWN:
|
elif event.key == pygame.K_PAGEDOWN:
|
||||||
@ -96,9 +100,11 @@ 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.node_candidate_pos = self.screen_to_world(event.pos[0], event.pos[1])
|
self.selected_node = self.get_hover_node()
|
||||||
self.is_creating_node = True
|
elif event.button == 3:
|
||||||
self.typing = True
|
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:
|
elif event.button == 4:
|
||||||
self.zoom_in()
|
self.zoom_in()
|
||||||
elif event.button == 5:
|
elif event.button == 5:
|
||||||
@ -246,17 +252,21 @@ class Editor:
|
|||||||
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)
|
||||||
pygame.draw.circle(self.win, (255, 0, 0), (blitpos[0], blitpos[1]), self.node_radius)
|
pygame.draw.circle(self.win, (255, 0, 0), (blitpos[0], blitpos[1]), self.node_radius)
|
||||||
self.render_mouse_hover_node_text()
|
if self.selected_node != -1:
|
||||||
|
node = self.graph.nodes[self.selected_node]
|
||||||
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)
|
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
|
pygame.draw.circle(self.win, (0, 255, 255), (node_pos[0], node_pos[1]), self.node_radius)
|
||||||
if (hovering):
|
self.render_mouse_hover_node()
|
||||||
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 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:
|
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))
|
||||||
@ -290,6 +300,23 @@ class Editor:
|
|||||||
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
|
||||||
|
|
||||||
|
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]
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user