diff --git a/src/editor.py b/src/editor.py index 327748e..167dcb3 100644 --- a/src/editor.py +++ b/src/editor.py @@ -11,10 +11,16 @@ from src.graph.graph import Graph class Editor: + APP_NAME: str = "lycacraft-paths" + APP_AUTHOR: str = "Lycacraft" WIDTH: int = 800 HEIGHT: int = 600 MAP_SIZE: int = 1024 - MAPS_DIR: str = os.path.join(platformdirs.user_cache_dir(appname="lycacraft-paths", appauthor="Lycacraft"), "maps") + CACHE_DIR: str = platformdirs.user_cache_dir(appname=APP_NAME, appauthor=APP_AUTHOR) + MAPS_DIR: str = os.path.join(CACHE_DIR, "maps") + AUTOSAVE_PATH: str = os.path.join(CACHE_DIR, "AUTOSAVE.txt") + AUTOSAVE_INTERVAL: int = 5 * 60 * 1000 + AUTOSAVE_EVENT: int = pygame.event.custom_type() ZOOMS: tuple[float] = (0.25, 0.5, 1, 2, 4) CROSSHAIR_SIZE: int = 10 @@ -51,11 +57,16 @@ class Editor: 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.dirty: bool = False + pygame.time.set_timer(self.AUTOSAVE_EVENT, self.AUTOSAVE_INTERVAL) def mainloop(self) -> None: self.state = State.LOADING while self.state != State.STOPPING: - pygame.display.set_caption(f"Lycacraft Map Editor - {self.clock.get_fps():.2f}fps") + caption = f"Lycacraft Map Editor - {self.clock.get_fps():.2f}fps" + if self.dirty: + caption += " (unsaved)" + pygame.display.set_caption(caption) self.process_events() if self.state == State.LOADING: self.render_loading() @@ -104,9 +115,9 @@ class Editor: elif event.key == pygame.K_BACKSPACE: self.deleted_selected_objects() elif event.key == pygame.K_s and event.mod & (pygame.KMOD_CTRL | pygame.KMOD_META): - self.graph.save(input("Save as: ")) + self.save() elif event.key == pygame.K_l and event.mod & (pygame.KMOD_CTRL | pygame.KMOD_META): - self.graph = Graph.load(input("Load from: ")) + 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 @@ -144,6 +155,8 @@ class Editor: self.confirm_move_poses() elif self.selection_rectangle != None: self.release_selection_rect(keys[pygame.K_LSHIFT] or keys[pygame.K_RSHIFT]) + elif event.type == self.AUTOSAVE_EVENT: + self.graph.save(self.AUTOSAVE_PATH) if keys[pygame.K_LEFT]: self.center[0] -= 4 / self.zoom @@ -417,18 +430,21 @@ class Editor: self.selected_edges = [] def rename_nodes(self) -> None: + self.dirty = True for node in self.selected_nodes: self.graph.nodes[node].rename_node(self.typing_text) self.typing_text = "" self.is_renaming_node = False def create_node(self, pos, typing_text = "") -> None: + self.dirty = True self.graph.add_node(pos[0], pos[1], typing_text) if len(self.selected_nodes) == 1: self.previously_created_nodes.append(self.selected_nodes[0]) self.select_node(self.graph.number_of_nodes() - 1) def create_edge(self, node_1: int, node_2: int) -> None: + self.dirty = True self.graph.add_edge(node_1, node_2) def get_hovering_nodes(self) -> tuple[list[int], list[float]]: @@ -505,6 +521,7 @@ class Editor: return ((px - node_pos[0]) ** 2 + (pz - node_pos[1]) ** 2) ** 0.5 def deleted_selected_objects(self): + self.dirty = True edges_to_delete = [self.graph.edges[i] for i in self.selected_edges] nodes_to_delete = [self.graph.nodes[i] for i in self.selected_nodes] for edge in edges_to_delete: @@ -582,6 +599,13 @@ class Editor: self.original_move_pos = None self.move_old_poses = None + def save(self) -> None: + self.graph.save(input("Save as: ")) + self.dirty = False + + def load(self) -> None: + self.graph = Graph.load(input("Load from: ")) + self.dirty = False class State(Enum):