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

View File

@ -3,4 +3,4 @@ class Edge:
self.length: float = length self.length: float = length
self.start: int = start self.start: int = start
self.end: int = end self.end: int = end
self.index: int = index self.index: int = index

View File

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

View File

@ -6,4 +6,4 @@ class Node:
self.name: str = name self.name: str = name
def rename_node(self, name: str): def rename_node(self, name: str):
self.name = name self.name = name

View File

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