2024-06-28 12:20:37 +00:00
|
|
|
from math import floor
|
|
|
|
from typing import Optional
|
|
|
|
|
|
|
|
import pygame
|
|
|
|
|
2024-06-28 16:41:47 +00:00
|
|
|
from src.image_handler import ImageHandler
|
|
|
|
|
2024-06-28 12:20:37 +00:00
|
|
|
|
|
|
|
class Editor:
|
2024-06-28 16:41:47 +00:00
|
|
|
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
|
2024-06-28 12:20:37 +00:00
|
|
|
|
|
|
|
def __init__(self):
|
2024-06-28 16:41:47 +00:00
|
|
|
pygame.init()
|
2024-06-28 12:20:37 +00:00
|
|
|
self.width: int = self.WIDTH
|
|
|
|
self.height: int = self.HEIGHT
|
|
|
|
self.win: pygame.Surface = pygame.display.set_mode([self.width, self.height], pygame.RESIZABLE)
|
2024-06-28 16:41:47 +00:00
|
|
|
pygame.display.set_caption("Lycacraft Map Editor")
|
2024-06-28 12:20:37 +00:00
|
|
|
self.center: list[int] = [0, 0]
|
|
|
|
self.zoom: float = 1
|
|
|
|
self.running: bool = False
|
2024-06-28 16:41:47 +00:00
|
|
|
self.image_handler: ImageHandler = ImageHandler(self.MAPS_DIR, self.MAP_SIZE)
|
2024-06-28 12:20:37 +00:00
|
|
|
self.clock: pygame.time.Clock = pygame.time.Clock()
|
|
|
|
self.drag_pos: Optional[tuple[int, int]] = None
|
2024-06-28 16:41:47 +00:00
|
|
|
self.font: pygame.font.Font = pygame.font.SysFont("Ubuntu", 20)
|
2024-06-28 12:20:37 +00:00
|
|
|
|
|
|
|
def mainloop(self) -> None:
|
|
|
|
self.running = True
|
|
|
|
while self.running:
|
2024-06-28 16:41:47 +00:00
|
|
|
pygame.display.set_caption(f"Lycacraft Map Editor - {self.clock.get_fps():.2f}fps")
|
2024-06-28 12:20:37 +00:00
|
|
|
self.process_events()
|
|
|
|
self.render()
|
|
|
|
self.clock.tick(30)
|
|
|
|
|
|
|
|
def process_events(self) -> None:
|
|
|
|
events = pygame.event.get()
|
|
|
|
|
|
|
|
for event in events:
|
|
|
|
if event.type == pygame.QUIT:
|
|
|
|
self.running = False
|
|
|
|
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
|
|
|
|
elif event.key == pygame.K_PAGEUP:
|
|
|
|
self.zoom_in()
|
|
|
|
elif event.key == pygame.K_PAGEDOWN:
|
|
|
|
self.zoom_out()
|
|
|
|
elif event.type == pygame.MOUSEBUTTONDOWN:
|
|
|
|
if event.button == 2:
|
|
|
|
self.drag_pos = event.pos
|
|
|
|
elif event.type == pygame.MOUSEBUTTONUP:
|
|
|
|
if event.button == 2:
|
|
|
|
self.drag_pos = None
|
|
|
|
|
|
|
|
keys = pygame.key.get_pressed()
|
|
|
|
if keys[pygame.K_LEFT]:
|
|
|
|
self.center[0] -= 4 / self.zoom
|
|
|
|
if keys[pygame.K_RIGHT]:
|
|
|
|
self.center[0] += 4 / self.zoom
|
|
|
|
if keys[pygame.K_UP]:
|
|
|
|
self.center[1] -= 4 / self.zoom
|
|
|
|
if keys[pygame.K_DOWN]:
|
|
|
|
self.center[1] += 4 / self.zoom
|
|
|
|
|
|
|
|
mbtns = pygame.mouse.get_pressed()
|
|
|
|
mpos = pygame.mouse.get_pos()
|
|
|
|
if mbtns[1]:
|
|
|
|
dx = mpos[0] - self.drag_pos[0]
|
|
|
|
dy = mpos[1] - self.drag_pos[1]
|
|
|
|
self.center[0] -= dx / self.zoom
|
|
|
|
self.center[1] -= dy / self.zoom
|
|
|
|
self.drag_pos = mpos
|
|
|
|
|
|
|
|
def render(self) -> None:
|
|
|
|
self.win.fill((0, 0, 0))
|
2024-06-28 16:41:47 +00:00
|
|
|
off_x = (self.center[0] * self.zoom) % self.MAP_SIZE
|
|
|
|
off_y = (self.center[1] * self.zoom) % self.MAP_SIZE
|
2024-06-28 12:20:37 +00:00
|
|
|
|
|
|
|
w2 = self.width / 2
|
|
|
|
h2 = self.height / 2
|
|
|
|
|
2024-06-28 16:41:47 +00:00
|
|
|
# In game top-left / bottom-right corners
|
2024-06-28 12:20:37 +00:00
|
|
|
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)
|
|
|
|
|
2024-06-28 16:41:47 +00:00
|
|
|
# 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)
|
2024-06-28 12:20:37 +00:00
|
|
|
|
|
|
|
h_maps = mx1 - mx0 + 1
|
|
|
|
v_maps = my1 - my0 + 1
|
2024-06-28 16:41:47 +00:00
|
|
|
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
|
2024-06-28 12:20:37 +00:00
|
|
|
|
|
|
|
for y in range(v_maps):
|
|
|
|
for x in range(h_maps):
|
2024-06-28 16:41:47 +00:00
|
|
|
map_image = self.image_handler.get_for_pos(mx0 + x, my0 + y, self.zoom)
|
2024-06-28 12:20:37 +00:00
|
|
|
if map_image is None:
|
|
|
|
continue
|
2024-06-28 16:41:47 +00:00
|
|
|
self.win.blit(map_image, [
|
|
|
|
ox + x * self.MAP_SIZE,
|
|
|
|
oy + y * self.MAP_SIZE
|
|
|
|
])
|
|
|
|
|
|
|
|
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])
|
2024-06-28 12:20:37 +00:00
|
|
|
|
|
|
|
pygame.display.flip()
|
|
|
|
|
|
|
|
def set_zoom(self, zoom: float) -> None:
|
|
|
|
self.zoom = max(self.MIN_ZOOM, min(self.MAX_ZOOM, zoom))
|
|
|
|
|
|
|
|
def zoom_in(self) -> None:
|
|
|
|
self.set_zoom(self.zoom * 2)
|
|
|
|
|
|
|
|
def zoom_out(self) -> None:
|
|
|
|
self.set_zoom(self.zoom / 2)
|