From a8d3aa64a14800772dfe9f70dc3d9a0933c1e4a1 Mon Sep 17 00:00:00 2001 From: LordBaryhobal Date: Sat, 29 Jun 2024 00:53:29 +0200 Subject: [PATCH] made loading async + added progress bar --- src/editor.py | 42 +++++++++++++++++++++++++++++++++++++----- src/image_handler.py | 17 ++++++++++++++--- 2 files changed, 51 insertions(+), 8 deletions(-) diff --git a/src/editor.py b/src/editor.py index b2fea52..94fa753 100644 --- a/src/editor.py +++ b/src/editor.py @@ -1,4 +1,5 @@ import os +from enum import Enum, auto from math import floor from typing import Optional @@ -30,17 +31,24 @@ class Editor: self.clock: pygame.time.Clock = pygame.time.Clock() self.drag_pos: Optional[tuple[int, int]] = None self.font: pygame.font.Font = pygame.font.SysFont("Ubuntu", 20) + self.loading_font: pygame.font.Font = pygame.font.SysFont("Ubuntu", 30) self.zooms_texts: list[pygame.Surface] = list(map( lambda z: self.font.render(str(z), True, (255, 255, 255)), self.ZOOMS )) + self.state: State = State.STOPPING def mainloop(self) -> None: - self.running = True - while self.running: + self.state = State.LOADING + while self.state != State.STOPPING: pygame.display.set_caption(f"Lycacraft Map Editor - {self.clock.get_fps():.2f}fps") self.process_events() - self.render() + if self.state == State.LOADING: + self.render_loading() + if not self.image_handler.loading: + self.state = State.RUNNING + elif self.state == State.RUNNING: + self.render() self.clock.tick(30) def process_events(self) -> None: @@ -48,13 +56,13 @@ class Editor: for event in events: if event.type == pygame.QUIT: - self.running = False + self.state = State.STOPPING elif event.type == pygame.WINDOWRESIZED: self.width = event.x self.height = event.y elif event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: - self.running = False + self.state = State.STOPPING elif event.key == pygame.K_PAGEUP: self.zoom_in() elif event.key == pygame.K_PAGEDOWN: @@ -156,6 +164,24 @@ class Editor: pygame.draw.circle(self.win, col, [zoom_x, y], zoom_r) self.win.blit(txt, [zoom_x - txt.get_width() - zoom_r - 5, y - txt.get_height() / 2]) + def render_loading(self) -> None: + self.win.fill((0, 0, 0)) + count = self.image_handler.count + total = self.image_handler.total + txt = self.loading_font.render(f"Loading maps - {count}/{total}", True, (255, 255, 255)) + + width = self.width * 0.6 + height = self.height * 0.05 + w2 = self.width / 2 + h2 = self.height / 2 + x0 = w2 - width / 2 + y0 = h2 - height / 2 + pygame.draw.rect(self.win, (160, 160, 160), [x0, y0, width, height]) + pygame.draw.rect(self.win, (90, 250, 90), [x0, y0, width * count / total, height]) + self.win.blit(txt, [w2 - txt.get_width() / 2, y0 - txt.get_height() - 5]) + + pygame.display.flip() + 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] @@ -173,3 +199,9 @@ class Editor: world_z = floor((y - h2) / self.zoom + self.center[1]) return int(world_x), int(world_z) + + +class State(Enum): + STOPPING = auto() + LOADING = auto() + RUNNING = auto() diff --git a/src/image_handler.py b/src/image_handler.py index 5aa98bc..85fa68b 100644 --- a/src/image_handler.py +++ b/src/image_handler.py @@ -1,4 +1,5 @@ import os +import threading from math import floor from typing import Optional @@ -10,19 +11,29 @@ class ImageHandler: self.maps_dir: str = maps_dir self.base_size: int = base_size self.cache: dict = {} - self.load_base_images() + self.count: int = 0 + self.total: int = 0 + self.loading: bool = False + + t = threading.Thread(target=self.load_base_images) + t.start() def load_base_images(self) -> None: cache = {} - - for path in os.listdir(self.maps_dir): + paths = os.listdir(self.maps_dir) + self.loading = True + self.count = 0 + self.total = len(paths) + for path in paths: fullpath = os.path.join(self.maps_dir, path) name, x, y = path.split(".")[0].split("_") cache[(int(x), int(y))] = pygame.image.load(fullpath).convert_alpha() + self.count += 1 self.cache = { 1: cache } + self.loading = False def get_for_pos(self, x: int, y: int, zoom: float = 1) -> Optional[pygame.Surface]: cache = self.cache.get(zoom, {})