2023-03-26 09:51:11 +02:00
|
|
|
import abc
|
|
|
|
from typing import Optional
|
|
|
|
|
|
|
|
import pygame
|
|
|
|
from pygame import Surface
|
|
|
|
|
2023-03-26 11:13:34 +02:00
|
|
|
from sprite.BoundingBox import BoundingBox
|
2023-03-26 09:51:11 +02:00
|
|
|
from sprite.PositionScale import PositionScale
|
|
|
|
|
|
|
|
|
|
|
|
class UiElement:
|
|
|
|
def __init__(self):
|
|
|
|
self.position_scale = PositionScale()
|
|
|
|
self.visible = True
|
2023-03-26 11:13:34 +02:00
|
|
|
self.click_listeners = []
|
|
|
|
|
|
|
|
def add_click_listener(self, listener):
|
|
|
|
self.click_listeners.append(listener)
|
|
|
|
|
|
|
|
def remove_click_listener(self, listener):
|
|
|
|
self.click_listeners.remove(listener)
|
|
|
|
|
|
|
|
def click(self, click_event):
|
|
|
|
for listener in self.click_listeners:
|
|
|
|
listener(self, click_event)
|
2023-03-26 09:51:11 +02:00
|
|
|
|
|
|
|
@abc.abstractmethod
|
|
|
|
def tick(self, dt: float):
|
2023-03-26 11:13:34 +02:00
|
|
|
pass
|
2023-03-26 09:51:11 +02:00
|
|
|
|
|
|
|
@abc.abstractmethod
|
|
|
|
def render_sprite_image(self) -> Optional[Surface]:
|
2023-03-26 11:13:34 +02:00
|
|
|
pass
|
|
|
|
|
|
|
|
@abc.abstractmethod
|
|
|
|
def get_bounding_box(self) -> BoundingBox:
|
|
|
|
pass
|
2023-03-26 09:51:11 +02:00
|
|
|
|
|
|
|
def draw(self, screen: pygame.Surface, screen_transform: PositionScale):
|
|
|
|
if not self.visible:
|
|
|
|
return
|
|
|
|
|
|
|
|
image = self.render_sprite_image()
|
|
|
|
|
|
|
|
if image is not None:
|
2023-03-26 11:13:34 +02:00
|
|
|
target_position = UiElement.transform_world_to_screen(self.position_scale.position, screen_transform)
|
2023-03-26 09:51:11 +02:00
|
|
|
|
2023-03-26 11:13:34 +02:00
|
|
|
screen_scale = screen_transform.scale
|
2023-03-26 09:51:11 +02:00
|
|
|
object_scale = self.position_scale.scale
|
|
|
|
|
|
|
|
target_size = (int(screen_scale[0] * object_scale[0] * image.get_width()),
|
|
|
|
int(screen_scale[1] * object_scale[1] * image.get_height()))
|
|
|
|
|
2023-03-26 11:13:34 +02:00
|
|
|
target_image = UiElement.get_scaled_image(image, target_size)
|
2023-03-26 09:51:11 +02:00
|
|
|
screen.blit(target_image, target_position)
|
|
|
|
|
2023-03-26 11:13:34 +02:00
|
|
|
@staticmethod
|
|
|
|
def get_scaled_image(image, resize):
|
2023-03-26 09:51:11 +02:00
|
|
|
return pygame.transform.scale(image, resize)
|
2023-03-26 11:13:34 +02:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def transform_screen_to_world(convert_position: tuple, screen_transform: PositionScale):
|
|
|
|
screen_scale = screen_transform.scale
|
|
|
|
screen_position = screen_transform.position
|
|
|
|
|
|
|
|
object_position = (convert_position[0] / screen_scale[0] - screen_position[0],
|
|
|
|
convert_position[1] / screen_scale[1] - screen_position[1])
|
|
|
|
|
|
|
|
return object_position
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def transform_world_to_screen(convert_position: tuple, screen_transform: PositionScale):
|
|
|
|
screen_scale = screen_transform.scale
|
|
|
|
screen_position = screen_transform.position
|
|
|
|
|
|
|
|
object_position = (int((convert_position[0] + screen_position[0]) * screen_scale[0]),
|
|
|
|
(int(convert_position[1] + screen_position[1]) * screen_scale[1]))
|
|
|
|
|
|
|
|
return object_position
|