improved performances + added zoom slider
This commit is contained in:
parent
c7d84246b4
commit
9346637462
123
src/editor.py
123
src/editor.py
@ -1,68 +1,37 @@
|
||||
from math import floor
|
||||
import os
|
||||
from typing import Optional
|
||||
|
||||
import pygame
|
||||
|
||||
from src.image_handler import ImageHandler
|
||||
|
||||
|
||||
class Editor:
|
||||
WIDTH = 800
|
||||
HEIGHT = 600
|
||||
MAP_SIZE = 1024
|
||||
MAPS_DIR = "/tmp/lycacraft_maps"
|
||||
MIN_ZOOM = 0.25
|
||||
MAX_ZOOM = 4
|
||||
WIDTH: int = 800
|
||||
HEIGHT: int = 600
|
||||
MAP_SIZE: int = 1024
|
||||
MAPS_DIR: str = "/tmp/lycacraft_maps"
|
||||
MIN_ZOOM: float = 0.25
|
||||
MAX_ZOOM: float = 4
|
||||
|
||||
def __init__(self):
|
||||
pygame.init()
|
||||
self.width: int = self.WIDTH
|
||||
self.height: int = self.HEIGHT
|
||||
self.win: pygame.Surface = pygame.display.set_mode([self.width, self.height], pygame.RESIZABLE)
|
||||
pygame.display.set_caption("Lycacraft Map Editor")
|
||||
self.center: list[int] = [0, 0]
|
||||
self.zoom: float = 1
|
||||
self.map_size: int = self.MAP_SIZE
|
||||
self.running: bool = False
|
||||
self.cache: dict[float, dict[tuple[int, int], pygame.Surface]] = {}
|
||||
self.load_maps()
|
||||
self.image_handler: ImageHandler = ImageHandler(self.MAPS_DIR, self.MAP_SIZE)
|
||||
self.clock: pygame.time.Clock = pygame.time.Clock()
|
||||
self.visible_maps_count: int = 0
|
||||
self.drag_pos: Optional[tuple[int, int]] = None
|
||||
|
||||
def load_maps(self) -> None:
|
||||
cache = {}
|
||||
for path in os.listdir(self.MAPS_DIR):
|
||||
fullpath = os.path.join(self.MAPS_DIR, path)
|
||||
name, x, y = path.split(".")[0].split("_")
|
||||
cache[(int(x), int(y))] = pygame.image.load(fullpath)
|
||||
self.cache = {
|
||||
self.zoom: cache
|
||||
}
|
||||
|
||||
def resize_maps(self) -> None:
|
||||
if self.zoom in self.cache:
|
||||
return
|
||||
|
||||
cache = {}
|
||||
for pos, img in self.cache[1].items():
|
||||
cache[pos] = pygame.transform.scale_by(img, self.zoom)
|
||||
|
||||
self.cache[self.zoom] = cache
|
||||
|
||||
def get_map(self, x: int, y: int) -> Optional[pygame.Surface]:
|
||||
cache = self.cache.get(self.zoom, {})
|
||||
pos = (x, y)
|
||||
if pos not in cache:
|
||||
if pos not in self.cache[1]:
|
||||
return None
|
||||
|
||||
img = self.cache[1][pos]
|
||||
cache[pos] = pygame.transform.scale_by(img, self.zoom)
|
||||
|
||||
return cache[pos]
|
||||
self.font: pygame.font.Font = pygame.font.SysFont("Ubuntu", 20)
|
||||
|
||||
def mainloop(self) -> None:
|
||||
self.running = True
|
||||
while self.running:
|
||||
pygame.display.set_caption(f"Lycacraft Map Editor - {self.clock.get_fps():.2f}fps - {self.visible_maps_count} maps rendered")
|
||||
pygame.display.set_caption(f"Lycacraft Map Editor - {self.clock.get_fps():.2f}fps")
|
||||
self.process_events()
|
||||
self.render()
|
||||
self.clock.tick(30)
|
||||
@ -111,63 +80,61 @@ class Editor:
|
||||
|
||||
def render(self) -> None:
|
||||
self.win.fill((0, 0, 0))
|
||||
off_x = self.center[0] % self.MAP_SIZE
|
||||
off_y = self.center[1] % self.MAP_SIZE
|
||||
off_x = (self.center[0] * self.zoom) % self.MAP_SIZE
|
||||
off_y = (self.center[1] * self.zoom) % self.MAP_SIZE
|
||||
|
||||
w2 = self.width / 2
|
||||
h2 = self.height / 2
|
||||
|
||||
# In game top-left / bottom-right corners
|
||||
x0 = floor(self.center[0] - w2 / self.zoom)
|
||||
y0 = floor(self.center[1] - h2 / self.zoom)
|
||||
x1 = floor(self.center[0] + w2 / self.zoom)
|
||||
y1 = floor(self.center[1] + h2 / self.zoom)
|
||||
|
||||
mx0 = floor(x0 / self.MAP_SIZE)
|
||||
my0 = floor(y0 / self.MAP_SIZE)
|
||||
mx1 = floor(x1 / self.MAP_SIZE)
|
||||
my1 = floor(y1 / self.MAP_SIZE)
|
||||
# Top-left / bottom-right maps
|
||||
mx0 = floor(x0 * self.zoom / self.MAP_SIZE)
|
||||
my0 = floor(y0 * self.zoom / self.MAP_SIZE)
|
||||
mx1 = floor(x1 * self.zoom / self.MAP_SIZE)
|
||||
my1 = floor(y1 * self.zoom / self.MAP_SIZE)
|
||||
|
||||
h_maps = mx1 - mx0 + 1
|
||||
v_maps = my1 - my0 + 1
|
||||
cx = floor(self.center[0] / self.MAP_SIZE)
|
||||
cy = floor(self.center[1] / self.MAP_SIZE)
|
||||
self.visible_maps_count = h_maps * v_maps
|
||||
ox = w2 + (mx0 - cx) * self.map_size - off_x * self.zoom
|
||||
oy = h2 + (my0 - cy) * self.map_size - off_y * self.zoom
|
||||
# cache = self.cache[self.zoom]
|
||||
cx = floor(self.center[0] * self.zoom / self.MAP_SIZE)
|
||||
cy = floor(self.center[1] * self.zoom / self.MAP_SIZE)
|
||||
ox = w2 + (mx0 - cx) * self.MAP_SIZE - off_x
|
||||
oy = h2 + (my0 - cy) * self.MAP_SIZE - off_y
|
||||
|
||||
for y in range(v_maps):
|
||||
for x in range(h_maps):
|
||||
# map_pos = (mx0 + x, my0 + y)
|
||||
map_image = self.get_map(mx0 + x, my0 + y)
|
||||
# if map_pos not in cache:
|
||||
map_image = self.image_handler.get_for_pos(mx0 + x, my0 + y, self.zoom)
|
||||
if map_image is None:
|
||||
continue
|
||||
self.win.blit(map_image, [
|
||||
ox + x * self.MAP_SIZE,
|
||||
oy + y * self.MAP_SIZE
|
||||
])
|
||||
|
||||
# map_image = cache[map_pos]
|
||||
|
||||
sx = ox + x * self.map_size
|
||||
sy = oy + y * self.map_size
|
||||
ex = sx + self.map_size
|
||||
ey = sy + self.map_size
|
||||
sx2, ex2 = min(self.width, max(0, sx)), min(self.width, max(0, ex))
|
||||
sy2, ey2 = min(self.height, max(0, sy)), min(self.height, max(0, ey))
|
||||
"""self.win.blit(map_image, [
|
||||
ox + x * self.map_size,
|
||||
oy + y * self.map_size,
|
||||
])"""
|
||||
self.win.blit(map_image, [sx2, sy2], [sx2 - sx, sy2 - sy, ex2 - sx2, ey2 - sy2])
|
||||
|
||||
for y in range(v_maps):
|
||||
for x in range(h_maps):
|
||||
pygame.draw.rect(self.win, (255, 0, 0), [ox + x * self.map_size, oy + y * self.map_size, self.map_size, self.map_size], 1)
|
||||
zoom_height = self.height * 0.2
|
||||
zoom_h_margin = self.width * 0.02
|
||||
zoom_v_margin = self.height * 0.05
|
||||
zoom_x = self.width - zoom_h_margin
|
||||
zoom_y = self.height - zoom_v_margin - zoom_height
|
||||
zoom_space = zoom_height / 4
|
||||
zoom_r = zoom_space / 4
|
||||
pygame.draw.line(self.win, (255, 255, 255), [zoom_x, zoom_y], [zoom_x, zoom_y + zoom_height])
|
||||
for i in range(5):
|
||||
y = zoom_y + zoom_height - i * zoom_space
|
||||
zoom = 2 ** (i - 2)
|
||||
col = (255, 0, 0) if zoom == self.zoom else (255, 255, 255)
|
||||
pygame.draw.circle(self.win, col, [zoom_x, y], zoom_r)
|
||||
txt = self.font.render(str(zoom), True, (255, 255, 255))
|
||||
self.win.blit(txt, [zoom_x - txt.get_width() - zoom_r - 5, y - txt.get_height() / 2])
|
||||
|
||||
pygame.display.flip()
|
||||
|
||||
def set_zoom(self, zoom: float) -> None:
|
||||
self.zoom = max(self.MIN_ZOOM, min(self.MAX_ZOOM, zoom))
|
||||
self.map_size = self.MAP_SIZE * self.zoom
|
||||
# self.resize_maps()
|
||||
|
||||
def zoom_in(self) -> None:
|
||||
self.set_zoom(self.zoom * 2)
|
||||
|
60
src/image_handler.py
Normal file
60
src/image_handler.py
Normal file
@ -0,0 +1,60 @@
|
||||
import os
|
||||
from math import floor
|
||||
from typing import Optional
|
||||
|
||||
import pygame
|
||||
|
||||
|
||||
class ImageHandler:
|
||||
def __init__(self, maps_dir: str, base_size: int):
|
||||
self.maps_dir: str = maps_dir
|
||||
self.base_size: int = base_size
|
||||
self.cache: dict = {}
|
||||
self.load_base_images()
|
||||
|
||||
def load_base_images(self) -> None:
|
||||
cache = {}
|
||||
|
||||
for path in os.listdir(self.maps_dir):
|
||||
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.cache = {
|
||||
1: cache
|
||||
}
|
||||
|
||||
def get_for_pos(self, x: int, y: int, zoom: float = 1) -> Optional[pygame.Surface]:
|
||||
cache = self.cache.get(zoom, {})
|
||||
pos = (x, y)
|
||||
if pos not in cache:
|
||||
if zoom == 1:
|
||||
return None
|
||||
|
||||
# Multiple maps per zone
|
||||
elif zoom < 1:
|
||||
img = pygame.Surface((self.base_size, self.base_size), pygame.SRCALPHA)
|
||||
n_maps = int(1 / zoom)
|
||||
sub_size = self.base_size * zoom
|
||||
for sub_y in range(n_maps):
|
||||
for sub_x in range(n_maps):
|
||||
sub_img = self.get_for_pos(x * n_maps + sub_x, y * n_maps + sub_y, 1)
|
||||
if sub_img is not None:
|
||||
sub_img = pygame.transform.scale_by(sub_img, zoom)
|
||||
img.blit(sub_img, [sub_x * sub_size, sub_y * sub_size])
|
||||
|
||||
# Zone is part of a map
|
||||
else:
|
||||
img = self.get_for_pos(floor(x / zoom), floor(y / zoom), 1)
|
||||
if img is not None:
|
||||
sub_x = x % zoom
|
||||
sub_y = y % zoom
|
||||
sub_size = self.base_size / zoom
|
||||
img = img.subsurface([sub_x * sub_size, sub_y * sub_size, sub_size, sub_size])
|
||||
img = pygame.transform.scale_by(img, zoom)
|
||||
|
||||
print("Generating new image")
|
||||
cache[pos] = img
|
||||
|
||||
self.cache[zoom] = cache
|
||||
return cache[pos]
|
Loading…
Reference in New Issue
Block a user