made loading async + added progress bar
This commit is contained in:
parent
bbb248da16
commit
a8d3aa64a1
@ -1,4 +1,5 @@
|
|||||||
import os
|
import os
|
||||||
|
from enum import Enum, auto
|
||||||
from math import floor
|
from math import floor
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
|
|
||||||
@ -30,17 +31,24 @@ class Editor:
|
|||||||
self.clock: pygame.time.Clock = pygame.time.Clock()
|
self.clock: pygame.time.Clock = pygame.time.Clock()
|
||||||
self.drag_pos: Optional[tuple[int, int]] = None
|
self.drag_pos: Optional[tuple[int, int]] = None
|
||||||
self.font: pygame.font.Font = pygame.font.SysFont("Ubuntu", 20)
|
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(
|
self.zooms_texts: list[pygame.Surface] = list(map(
|
||||||
lambda z: self.font.render(str(z), True, (255, 255, 255)),
|
lambda z: self.font.render(str(z), True, (255, 255, 255)),
|
||||||
self.ZOOMS
|
self.ZOOMS
|
||||||
))
|
))
|
||||||
|
self.state: State = State.STOPPING
|
||||||
|
|
||||||
def mainloop(self) -> None:
|
def mainloop(self) -> None:
|
||||||
self.running = True
|
self.state = State.LOADING
|
||||||
while self.running:
|
while self.state != State.STOPPING:
|
||||||
pygame.display.set_caption(f"Lycacraft Map Editor - {self.clock.get_fps():.2f}fps")
|
pygame.display.set_caption(f"Lycacraft Map Editor - {self.clock.get_fps():.2f}fps")
|
||||||
self.process_events()
|
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)
|
self.clock.tick(30)
|
||||||
|
|
||||||
def process_events(self) -> None:
|
def process_events(self) -> None:
|
||||||
@ -48,13 +56,13 @@ class Editor:
|
|||||||
|
|
||||||
for event in events:
|
for event in events:
|
||||||
if event.type == pygame.QUIT:
|
if event.type == pygame.QUIT:
|
||||||
self.running = False
|
self.state = State.STOPPING
|
||||||
elif event.type == pygame.WINDOWRESIZED:
|
elif event.type == pygame.WINDOWRESIZED:
|
||||||
self.width = event.x
|
self.width = event.x
|
||||||
self.height = event.y
|
self.height = event.y
|
||||||
elif event.type == pygame.KEYDOWN:
|
elif event.type == pygame.KEYDOWN:
|
||||||
if event.key == pygame.K_ESCAPE:
|
if event.key == pygame.K_ESCAPE:
|
||||||
self.running = False
|
self.state = State.STOPPING
|
||||||
elif event.key == pygame.K_PAGEUP:
|
elif event.key == pygame.K_PAGEUP:
|
||||||
self.zoom_in()
|
self.zoom_in()
|
||||||
elif event.key == pygame.K_PAGEDOWN:
|
elif event.key == pygame.K_PAGEDOWN:
|
||||||
@ -156,6 +164,24 @@ class Editor:
|
|||||||
pygame.draw.circle(self.win, col, [zoom_x, y], zoom_r)
|
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])
|
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:
|
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]
|
||||||
@ -173,3 +199,9 @@ class Editor:
|
|||||||
world_z = floor((y - h2) / self.zoom + self.center[1])
|
world_z = floor((y - h2) / self.zoom + self.center[1])
|
||||||
|
|
||||||
return int(world_x), int(world_z)
|
return int(world_x), int(world_z)
|
||||||
|
|
||||||
|
|
||||||
|
class State(Enum):
|
||||||
|
STOPPING = auto()
|
||||||
|
LOADING = auto()
|
||||||
|
RUNNING = auto()
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
import os
|
import os
|
||||||
|
import threading
|
||||||
from math import floor
|
from math import floor
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
|
|
||||||
@ -10,19 +11,29 @@ class ImageHandler:
|
|||||||
self.maps_dir: str = maps_dir
|
self.maps_dir: str = maps_dir
|
||||||
self.base_size: int = base_size
|
self.base_size: int = base_size
|
||||||
self.cache: dict = {}
|
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:
|
def load_base_images(self) -> None:
|
||||||
cache = {}
|
cache = {}
|
||||||
|
paths = os.listdir(self.maps_dir)
|
||||||
for path in 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)
|
fullpath = os.path.join(self.maps_dir, path)
|
||||||
name, x, y = path.split(".")[0].split("_")
|
name, x, y = path.split(".")[0].split("_")
|
||||||
cache[(int(x), int(y))] = pygame.image.load(fullpath).convert_alpha()
|
cache[(int(x), int(y))] = pygame.image.load(fullpath).convert_alpha()
|
||||||
|
self.count += 1
|
||||||
|
|
||||||
self.cache = {
|
self.cache = {
|
||||||
1: cache
|
1: cache
|
||||||
}
|
}
|
||||||
|
self.loading = False
|
||||||
|
|
||||||
def get_for_pos(self, x: int, y: int, zoom: float = 1) -> Optional[pygame.Surface]:
|
def get_for_pos(self, x: int, y: int, zoom: float = 1) -> Optional[pygame.Surface]:
|
||||||
cache = self.cache.get(zoom, {})
|
cache = self.cache.get(zoom, {})
|
||||||
|
Loading…
Reference in New Issue
Block a user