added mouse coordinates + crosshair

This commit is contained in:
Louis Heredero 2024-06-28 21:49:09 +02:00
parent 9346637462
commit ca74986b82
Signed by: HEL
GPG Key ID: 8D83DE470F8544E7
2 changed files with 43 additions and 14 deletions

View File

@ -11,8 +11,8 @@ class Editor:
HEIGHT: int = 600
MAP_SIZE: int = 1024
MAPS_DIR: str = "/tmp/lycacraft_maps"
MIN_ZOOM: float = 0.25
MAX_ZOOM: float = 4
ZOOMS: tuple[float] = (0.25, 0.5, 1, 2, 4)
CROSSHAIR_SIZE: int = 10
def __init__(self):
pygame.init()
@ -21,12 +21,17 @@ class Editor:
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.zoom_i: int = 2
self.zoom: float = self.ZOOMS[self.zoom_i]
self.running: bool = False
self.image_handler: ImageHandler = ImageHandler(self.MAPS_DIR, self.MAP_SIZE)
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.zooms_texts: list[pygame.Surface] = list(map(
lambda z: self.font.render(str(z), True, (255, 255, 255)),
self.ZOOMS
))
def mainloop(self) -> None:
self.running = True
@ -115,6 +120,19 @@ class Editor:
oy + y * self.MAP_SIZE
])
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])
pygame.display.flip()
def render_zoom_slider(self) -> None:
zoom_height = self.height * 0.2
zoom_h_margin = self.width * 0.02
zoom_v_margin = self.height * 0.05
@ -122,22 +140,34 @@ class Editor:
zoom_y = self.height - zoom_v_margin - zoom_height
zoom_space = zoom_height / 4
zoom_r = zoom_space / 4
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
])
pygame.draw.line(self.win, (255, 255, 255), [zoom_x, zoom_y], [zoom_x, zoom_y + zoom_height])
for i in range(5):
for i, txt in enumerate(self.zooms_texts):
y = zoom_y + zoom_height - i * zoom_space
zoom = 2 ** (i - 2)
col = (255, 0, 0) if zoom == self.zoom else (255, 255, 255)
col = (255, 0, 0) if i == self.zoom_i 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))
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]
def zoom_in(self) -> None:
self.set_zoom(self.zoom * 2)
self.set_zoom(self.zoom_i + 1)
def zoom_out(self) -> None:
self.set_zoom(self.zoom / 2)
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)

View File

@ -53,7 +53,6 @@ class ImageHandler:
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