From 60416e447c3938ad3b83be342afef4c87f7ca6b6 Mon Sep 17 00:00:00 2001 From: LordBaryhobal Date: Tue, 2 Jul 2024 17:09:37 +0200 Subject: [PATCH] added dirty to move + improved syntax --- src/editor.py | 46 ++++++++++++++++++++++++------------------- src/graph/edge.py | 2 +- src/graph/graph.py | 2 +- src/graph/node.py | 2 +- utils/map_splitter.py | 1 + 5 files changed, 30 insertions(+), 23 deletions(-) diff --git a/src/editor.py b/src/editor.py index 8c20d27..82222f7 100644 --- a/src/editor.py +++ b/src/editor.py @@ -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 @@ -646,4 +652,4 @@ class Editor: class State(Enum): STOPPING = auto() LOADING = auto() - RUNNING = auto() \ No newline at end of file + RUNNING = auto() diff --git a/src/graph/edge.py b/src/graph/edge.py index 98254e4..935f1bf 100644 --- a/src/graph/edge.py +++ b/src/graph/edge.py @@ -3,4 +3,4 @@ class Edge: self.length: float = length self.start: int = start self.end: int = end - self.index: int = index \ No newline at end of file + self.index: int = index diff --git a/src/graph/graph.py b/src/graph/graph.py index a28cba2..0f33158 100644 --- a/src/graph/graph.py +++ b/src/graph/graph.py @@ -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) diff --git a/src/graph/node.py b/src/graph/node.py index 5bea4e3..11383fe 100644 --- a/src/graph/node.py +++ b/src/graph/node.py @@ -6,4 +6,4 @@ class Node: self.name: str = name def rename_node(self, name: str): - self.name = name \ No newline at end of file + self.name = name diff --git a/utils/map_splitter.py b/utils/map_splitter.py index bc818a8..6a3837d 100644 --- a/utils/map_splitter.py +++ b/utils/map_splitter.py @@ -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))