made loading async + added progress bar

This commit is contained in:
2024-06-29 00:53:29 +02:00
parent bbb248da16
commit a8d3aa64a1
2 changed files with 51 additions and 8 deletions

View File

@@ -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()