2024-06-28 22:53:05 +00:00
|
|
|
import os
|
2024-06-28 22:53:29 +00:00
|
|
|
from enum import Enum, auto
|
2024-06-28 12:20:37 +00:00
|
|
|
from math import floor
|
|
|
|
from typing import Optional
|
|
|
|
|
2024-06-28 22:53:05 +00:00
|
|
|
import platformdirs
|
2024-06-28 12:20:37 +00:00
|
|
|
import pygame
|
|
|
|
|
2024-06-28 16:41:47 +00:00
|
|
|
from src.image_handler import ImageHandler
|
2024-06-30 18:27:24 +00:00
|
|
|
from src.graph.graph import Graph
|
2024-06-28 16:41:47 +00:00
|
|
|
|
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
|
2024-06-28 22:53:05 +00:00
|
|
|
MAPS_DIR: str = os.path.join(platformdirs.user_cache_dir(appname="lycacraft-paths", appauthor="Lycacraft"), "maps")
|
2024-06-28 19:49:09 +00:00
|
|
|
ZOOMS: tuple[float] = (0.25, 0.5, 1, 2, 4)
|
|
|
|
CROSSHAIR_SIZE: int = 10
|
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]
|
2024-06-28 19:49:09 +00:00
|
|
|
self.zoom_i: int = 2
|
|
|
|
self.zoom: float = self.ZOOMS[self.zoom_i]
|
2024-06-28 12:20:37 +00:00
|
|
|
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()
|
2024-06-30 16:56:26 +00:00
|
|
|
self.left_drag_pos: Optional[tuple[int, int]] = None
|
|
|
|
self.mid_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 22:53:29 +00:00
|
|
|
self.loading_font: pygame.font.Font = pygame.font.SysFont("Ubuntu", 30)
|
2024-06-28 19:49:09 +00:00
|
|
|
self.zooms_texts: list[pygame.Surface] = list(map(
|
|
|
|
lambda z: self.font.render(str(z), True, (255, 255, 255)),
|
|
|
|
self.ZOOMS
|
|
|
|
))
|
2024-06-30 19:26:15 +00:00
|
|
|
self.is_creating_node: bool = False
|
|
|
|
self.typing: bool = False
|
2024-06-28 22:53:29 +00:00
|
|
|
self.state: State = State.STOPPING
|
2024-06-30 18:27:24 +00:00
|
|
|
self.graph = Graph()
|
2024-06-30 19:26:15 +00:00
|
|
|
self.typing_text: str = ""
|
|
|
|
self.node_candidate_pos: tuple[int, int] = None
|
|
|
|
self.node_radius: int = 10
|
2024-06-28 12:20:37 +00:00
|
|
|
|
|
|
|
def mainloop(self) -> None:
|
2024-06-28 22:53:29 +00:00
|
|
|
self.state = State.LOADING
|
|
|
|
while self.state != State.STOPPING:
|
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()
|
2024-06-28 22:53:29 +00:00
|
|
|
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()
|
2024-06-28 12:20:37 +00:00
|
|
|
self.clock.tick(30)
|
|
|
|
|
|
|
|
def process_events(self) -> None:
|
|
|
|
events = pygame.event.get()
|
|
|
|
|
2024-06-30 16:56:26 +00:00
|
|
|
keys = pygame.key.get_pressed()
|
2024-06-28 12:20:37 +00:00
|
|
|
for event in events:
|
|
|
|
if event.type == pygame.QUIT:
|
2024-06-28 22:53:29 +00:00
|
|
|
self.state = State.STOPPING
|
2024-06-28 12:20:37 +00:00
|
|
|
elif event.type == pygame.WINDOWRESIZED:
|
|
|
|
self.width = event.x
|
|
|
|
self.height = event.y
|
|
|
|
elif event.type == pygame.KEYDOWN:
|
2024-06-30 19:26:15 +00:00
|
|
|
if self.typing:
|
|
|
|
if event.key == pygame.K_ESCAPE:
|
|
|
|
self.typing = False
|
2024-06-30 18:27:24 +00:00
|
|
|
self.is_creating_node = False
|
2024-06-30 19:26:15 +00:00
|
|
|
self.node_candidate_pos = None
|
|
|
|
self.typing_text = ""
|
|
|
|
elif event.key == pygame.K_RETURN:
|
|
|
|
self.create_node()
|
|
|
|
elif event.key == pygame.K_BACKSPACE:
|
|
|
|
self.typing_text = self.typing_text[:-1]
|
2024-06-30 18:27:24 +00:00
|
|
|
else:
|
2024-06-30 19:26:15 +00:00
|
|
|
self.typing_text += event.unicode
|
|
|
|
else:
|
|
|
|
if event.key == pygame.K_ESCAPE:
|
2024-06-30 18:27:24 +00:00
|
|
|
self.state = State.STOPPING
|
2024-06-30 19:26:15 +00:00
|
|
|
elif event.key == pygame.K_PAGEUP:
|
|
|
|
self.zoom_in()
|
|
|
|
elif event.key == pygame.K_PAGEDOWN:
|
|
|
|
self.zoom_out()
|
2024-06-28 12:20:37 +00:00
|
|
|
elif event.type == pygame.MOUSEBUTTONDOWN:
|
|
|
|
if event.button == 2:
|
2024-06-30 16:56:26 +00:00
|
|
|
self.mid_drag_pos = event.pos
|
2024-06-30 18:27:24 +00:00
|
|
|
elif event.button == 1:
|
|
|
|
if keys[pygame.K_LCTRL]:
|
|
|
|
self.left_drag_pos = event.pos
|
|
|
|
else:
|
2024-06-30 19:26:15 +00:00
|
|
|
self.node_candidate_pos = self.screen_to_world(event.pos[0], event.pos[1])
|
2024-06-30 18:27:24 +00:00
|
|
|
self.is_creating_node = True
|
2024-06-30 19:26:15 +00:00
|
|
|
self.typing = True
|
2024-06-30 16:56:26 +00:00
|
|
|
elif event.button == 4:
|
|
|
|
self.zoom_in()
|
|
|
|
elif event.button == 5:
|
|
|
|
self.zoom_out()
|
2024-06-28 12:20:37 +00:00
|
|
|
elif event.type == pygame.MOUSEBUTTONUP:
|
|
|
|
if event.button == 2:
|
2024-06-30 16:56:26 +00:00
|
|
|
self.mid_drag_pos = None
|
|
|
|
elif event.button == 1:
|
|
|
|
self.left_drag_pos = None
|
2024-06-28 12:20:37 +00:00
|
|
|
|
|
|
|
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()
|
2024-06-30 16:56:26 +00:00
|
|
|
if mbtns[1] or (mbtns[0] and keys[pygame.K_LCTRL]):
|
|
|
|
drag_pos = self.mid_drag_pos if mbtns[1] else self.left_drag_pos
|
|
|
|
dx = mpos[0] - drag_pos[0]
|
|
|
|
dy = mpos[1] - drag_pos[1]
|
2024-06-28 12:20:37 +00:00
|
|
|
self.center[0] -= dx / self.zoom
|
|
|
|
self.center[1] -= dy / self.zoom
|
2024-06-30 16:56:26 +00:00
|
|
|
if mbtns[0] and keys[pygame.K_LCTRL]:
|
|
|
|
self.left_drag_pos = mpos
|
|
|
|
if mbtns[1]:
|
|
|
|
self.mid_drag_pos = mpos
|
2024-06-28 12:20:37 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
])
|
2024-06-30 18:27:24 +00:00
|
|
|
|
|
|
|
self.render_nodes()
|
2024-06-28 16:41:47 +00:00
|
|
|
|
2024-06-28 19:49:09 +00:00
|
|
|
pygame.draw.line(self.win, (150, 150, 150), [w2 - self.CROSSHAIR_SIZE, h2], [w2 + self.CROSSHAIR_SIZE, h2])
|
|
|
|
pygame.draw.line(self.win, (150, 150, 150), [w2, h2 - self.CROSSHAIR_SIZE], [w2, h2 + self.CROSSHAIR_SIZE])
|
|
|
|
self.render_zoom_slider()
|
|
|
|
|
|
|
|
mpos = pygame.mouse.get_pos()
|
|
|
|
world_x, world_z = self.screen_to_world(*mpos)
|
|
|
|
mouse_txt = self.font.render(f"x: {world_x} / z: {world_z}", True, (255, 255, 255))
|
|
|
|
pygame.draw.rect(self.win, (80, 80, 80), [0, 0, mouse_txt.get_width() + 10, mouse_txt.get_height() + 10])
|
|
|
|
self.win.blit(mouse_txt, [5, 5])
|
|
|
|
|
2024-06-30 18:27:24 +00:00
|
|
|
if self.is_creating_node:
|
|
|
|
self.render_node_creator()
|
|
|
|
|
2024-06-28 19:49:09 +00:00
|
|
|
pygame.display.flip()
|
|
|
|
|
|
|
|
def render_zoom_slider(self) -> None:
|
2024-06-28 16:41:47 +00:00
|
|
|
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
|
2024-06-28 19:49:09 +00:00
|
|
|
zoom_width = max(s.get_width() for s in self.zooms_texts) + 2 * zoom_r + 5
|
|
|
|
pygame.draw.rect(self.win, (80, 80, 80), [
|
|
|
|
zoom_x + zoom_r - zoom_width - 5,
|
|
|
|
zoom_y - zoom_r - 5,
|
|
|
|
zoom_width + 10,
|
|
|
|
zoom_height + 2 * zoom_r + 10
|
|
|
|
])
|
2024-06-28 16:41:47 +00:00
|
|
|
pygame.draw.line(self.win, (255, 255, 255), [zoom_x, zoom_y], [zoom_x, zoom_y + zoom_height])
|
2024-06-28 19:49:09 +00:00
|
|
|
for i, txt in enumerate(self.zooms_texts):
|
2024-06-28 16:41:47 +00:00
|
|
|
y = zoom_y + zoom_height - i * zoom_space
|
2024-06-28 19:49:09 +00:00
|
|
|
col = (255, 0, 0) if i == self.zoom_i else (255, 255, 255)
|
2024-06-28 16:41:47 +00:00
|
|
|
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])
|
2024-06-28 12:20:37 +00:00
|
|
|
|
2024-06-28 22:53:29 +00:00
|
|
|
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()
|
|
|
|
|
2024-06-30 18:27:24 +00:00
|
|
|
def render_node_creator(self) -> None:
|
|
|
|
width = self.width / 2
|
|
|
|
height = self.height / 2
|
|
|
|
x0 = (self.width - width) / 2
|
|
|
|
y0 = (self.height - height) / 2
|
2024-06-30 19:26:15 +00:00
|
|
|
|
|
|
|
line_height = height / 6
|
|
|
|
|
|
|
|
nc_txt = self.loading_font.render("NODE CREATOR", True, (255, 255, 255))
|
|
|
|
name_txt = self.loading_font.render("Name:", True, (255, 255, 255))
|
|
|
|
txt = self.loading_font.render(self.typing_text, True, (255, 255, 255))
|
|
|
|
|
2024-06-30 18:27:24 +00:00
|
|
|
pygame.draw.rect(self.win, (0, 0, 0), [x0, y0, width, height])
|
2024-06-30 19:26:15 +00:00
|
|
|
self.win.blit(nc_txt, [self.width / 2 - nc_txt.get_width() / 2, y0 + line_height])
|
|
|
|
self.win.blit(name_txt, [self.width / 2 - name_txt.get_width() / 2, y0 + 3 * line_height])
|
|
|
|
self.win.blit(txt, [self.width / 2 - txt.get_width() / 2, y0 + 4 * line_height])
|
2024-06-30 18:27:24 +00:00
|
|
|
|
|
|
|
def render_nodes(self) -> None:
|
|
|
|
for node in self.graph.nodes:
|
|
|
|
blitpos = self.world_to_screen(node.x, node.z)
|
2024-06-30 19:26:15 +00:00
|
|
|
pygame.draw.circle(self.win, (255, 0, 0), (blitpos[0], blitpos[1]), self.node_radius)
|
|
|
|
self.render_mouse_hover_node_text()
|
|
|
|
|
|
|
|
def render_mouse_hover_node_text(self):
|
|
|
|
mouse_pos = pygame.mouse.get_pos()
|
|
|
|
|
|
|
|
for node in self.graph.nodes:
|
|
|
|
node_pos = self.world_to_screen(node.x, node.z)
|
|
|
|
hovering = ((mouse_pos[0] - node_pos[0]) ** 2 + (mouse_pos[1] - node_pos[1]) ** 2) < self.node_radius ** 2
|
|
|
|
if (hovering):
|
|
|
|
txt = self.loading_font.render(node.name, True, (0, 0, 0))
|
|
|
|
self.win.blit(txt, [node_pos[0] - txt.get_width(), node_pos[1] - txt.get_height()])
|
2024-06-30 18:27:24 +00:00
|
|
|
|
2024-06-28 19:49:09 +00:00
|
|
|
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]
|
2024-06-28 12:20:37 +00:00
|
|
|
|
|
|
|
def zoom_in(self) -> None:
|
2024-06-28 19:49:09 +00:00
|
|
|
self.set_zoom(self.zoom_i + 1)
|
2024-06-28 12:20:37 +00:00
|
|
|
|
|
|
|
def zoom_out(self) -> None:
|
2024-06-28 19:49:09 +00:00
|
|
|
self.set_zoom(self.zoom_i - 1)
|
|
|
|
|
|
|
|
def screen_to_world(self, x: int, y: int) -> tuple[int, int]:
|
|
|
|
w2 = self.width / 2
|
|
|
|
h2 = self.height / 2
|
|
|
|
world_x = floor((x - w2) / self.zoom + self.center[0])
|
|
|
|
world_z = floor((y - h2) / self.zoom + self.center[1])
|
|
|
|
|
|
|
|
return int(world_x), int(world_z)
|
2024-06-30 18:27:24 +00:00
|
|
|
|
|
|
|
def world_to_screen(self, world_x: int, world_z: int) -> tuple[int, int]:
|
|
|
|
w2 = self.width / 2
|
|
|
|
h2 = self.height / 2
|
|
|
|
screen_x = (world_x - self.center[0]) * self.zoom + w2
|
|
|
|
screen_y = (world_z - self.center[1]) * self.zoom + h2
|
|
|
|
|
|
|
|
return int(screen_x), int(screen_y)
|
2024-06-30 19:26:15 +00:00
|
|
|
|
|
|
|
def create_node(self) -> None:
|
|
|
|
self.graph.add_node(self.node_candidate_pos[0], self.node_candidate_pos[1], self.typing_text)
|
|
|
|
self.typing_text = ""
|
|
|
|
self.node_candidate_pos = None
|
|
|
|
self.typing = False
|
|
|
|
self.is_creating_node = False
|
|
|
|
|
2024-06-30 18:27:24 +00:00
|
|
|
|
2024-06-28 22:53:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
class State(Enum):
|
|
|
|
STOPPING = auto()
|
|
|
|
LOADING = auto()
|
|
|
|
RUNNING = auto()
|
2024-06-30 18:27:24 +00:00
|
|
|
CREATING_NODE = auto()
|