added dirty to move + improved syntax

This commit is contained in:
Louis Heredero 2024-07-02 17:09:37 +02:00
parent 0077a768e8
commit 60416e447c
Signed by: HEL
GPG Key ID: 8D83DE470F8544E7
5 changed files with 30 additions and 23 deletions

View File

@ -57,9 +57,9 @@ class Editor:
self.selected_nodes: list[int] = []
self.selected_edges: list[int] = []
self.previously_created_nodes: list[int] = []
self.selection_rectangle: list[tuple[int, int], tuple[int, int]] = None
self.original_move_pos: tuple[int, int] = None
self.move_old_poses: dict[int, tuple[int, int]] = None
self.selection_rectangle: Optional[list[tuple[int, int], tuple[int, int]]] = None
self.original_move_pos: Optional[tuple[int, int]] = None
self.move_old_poses: Optional[dict[int, tuple[int, int]]] = None
self.dirty: bool = False
pygame.time.set_timer(self.AUTOSAVE_EVENT, self.config.AUTOSAVE_INTERVAL)
@ -82,9 +82,9 @@ class Editor:
if not self.image_handler.loading:
self.state = State.RUNNING
elif self.state == State.RUNNING:
if self.selection_rectangle != None:
if self.selection_rectangle is not None:
self.expand_selection_rect()
if self.original_move_pos != None:
if self.original_move_pos is not None:
self.move_poses()
self.render()
self.clock.tick(30)
@ -134,11 +134,13 @@ class Editor:
self.load()
elif event.key == pygame.K_RETURN:
if len(self.selected_nodes) > 0:
self.typing_text = "" if len(self.selected_nodes) > 1 else self.graph.nodes[self.selected_nodes[0]].name
self.typing_text = ""
if len(self.selected_nodes) == 1:
self.typing_text = self.graph.nodes[self.selected_nodes[0]].name
self.is_renaming_node = True
elif event.type == pygame.KEYUP:
if event.key == pygame.K_m:
if self.original_move_pos != None:
if self.original_move_pos is not None:
self.reset_move_poses()
elif event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 2:
@ -165,9 +167,9 @@ class Editor:
elif event.button == 1:
if keys[pygame.K_LCTRL]:
self.left_drag_pos = None
elif keys[pygame.K_m] and self.original_move_pos != None:
elif keys[pygame.K_m] and self.original_move_pos is not None:
self.confirm_move_poses()
elif self.selection_rectangle != None:
elif self.selection_rectangle is not None:
self.release_selection_rect(keys[pygame.K_LSHIFT] or keys[pygame.K_RSHIFT])
elif event.type == self.AUTOSAVE_EVENT:
if self.dirty:
@ -315,10 +317,10 @@ class Editor:
hover_index, is_node = self.get_hover_object()
if is_node:
self.render_nodes()
if self.selection_rectangle == None:
if self.selection_rectangle is None:
self.render_hover_node(hover_index)
else:
if self.selection_rectangle == None:
if self.selection_rectangle is None:
self.render_hover_edge(hover_index)
self.render_nodes()
@ -326,7 +328,9 @@ class Editor:
for edge in self.graph.edges:
node_1, node_2 = self.graph.get_edge_nodes(edge)
color = (0, 255, 255) if edge.index in self.selected_edges else (255, 0, 0)
pygame.draw.line(self.win, color, self.world_to_screen(node_1.x, node_1.z), self.world_to_screen(node_2.x, node_2.z), self.line_size)
start = self.world_to_screen(node_1.x, node_1.z)
end = self.world_to_screen(node_2.x, node_2.z)
pygame.draw.line(self.win, color, start, end, self.line_size)
def render_nodes(self) -> None:
for node in self.graph.nodes:
@ -348,20 +352,21 @@ class Editor:
def render_hover_edge(self, edge_index):
if edge_index != -1:
node_1, node_2 = self.graph.get_edge_nodes(self.graph.edges[edge_index])
pygame.draw.line(self.win, (0, 0, 0), self.world_to_screen(node_1.x, node_1.z), self.world_to_screen(node_2.x, node_2.z), self.edge_detect_radius)
start = self.world_to_screen(node_1.x, node_1.z)
end = self.world_to_screen(node_2.x, node_2.z)
pygame.draw.line(self.win, (0, 0, 0), start, end, self.edge_detect_radius)
color = (0, 255, 255) if edge_index in self.selected_edges else (255, 0, 0)
pygame.draw.line(self.win, color, self.world_to_screen(node_1.x, node_1.z), self.world_to_screen(node_2.x, node_2.z), self.line_size)
pygame.draw.line(self.win, color, start, end, self.line_size)
def render_selection_rect(self):
rect = self.selection_rectangle
if rect != None:
if rect is not None:
left = min(rect[0][0], rect[1][0])
top = min(rect[0][1], rect[1][1])
width = abs(rect[0][0] - rect[1][0])
height = abs(rect[0][1] - rect[1][1])
pygame.draw.rect(self.win, (32, 32, 32), pygame.Rect(left, top, width, height), self.line_size)
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]
@ -451,7 +456,7 @@ class Editor:
self.typing_text = ""
self.is_renaming_node = False
def create_node(self, pos, typing_text = "") -> None:
def create_node(self, pos: tuple[int, int], typing_text: str = "") -> None:
self.dirty = True
self.graph.add_node(pos[0], pos[1], typing_text)
if len(self.selected_nodes) == 1:
@ -549,7 +554,7 @@ class Editor:
self.selected_nodes.append(self.previously_created_nodes[n - 1])
self.previously_created_nodes.pop()
def create_selection_rect(self, shifting = False):
def create_selection_rect(self, shifting: bool = False):
if not shifting:
self.clear_selection()
self.previously_created_nodes = []
@ -559,7 +564,7 @@ class Editor:
def expand_selection_rect(self):
self.selection_rectangle[1] = pygame.mouse.get_pos()
def release_selection_rect(self, shifting = False):
def release_selection_rect(self, shifting: bool = False):
if not shifting:
self.clear_selection()
self.previously_created_nodes = []
@ -611,6 +616,7 @@ class Editor:
self.move_old_poses = None
def confirm_move_poses(self):
self.dirty = True
self.original_move_pos = None
self.move_old_poses = None

View File

@ -27,7 +27,7 @@ class Graph:
ed.index = self.edges.index(ed)
def delete_node(self, node: Node) -> None:
edges_to_delete=[]
edges_to_delete = []
for edge in self.edges:
if node.index in (edge.start, edge.end):
edges_to_delete.append(edge)

View File

@ -8,6 +8,7 @@ Image.MAX_IMAGE_PIXELS = 200000000
MAP_SIZE = 1024
DEFAULT_PATH = os.path.join(platformdirs.user_cache_dir(appname="lycacraft-paths", appauthor="Lycacraft"), "maps")
def clamp(mn, value, mx):
return max(mn, min(mx, value))