33 lines
1.1 KiB
Python
33 lines
1.1 KiB
Python
import pygame
|
|
from pygame.event import Event
|
|
|
|
from sprite.PositionScale import PositionScale
|
|
from ui_elements import CoordinateTransform
|
|
|
|
|
|
class ClickEvent:
|
|
CLICK_LEFT = 1
|
|
CLICK_MIDDLE = 2
|
|
CLICK_RIGHT = 3
|
|
|
|
def __init__(self, world_position: tuple[float, float], screen_position: tuple[float, float], event: Event):
|
|
self.world_position = world_position
|
|
self.screen_position = screen_position
|
|
self.event = event
|
|
|
|
def is_click_down(self, button: int) -> bool:
|
|
return self.event.type == pygame.MOUSEBUTTONDOWN and self.event.button == button
|
|
|
|
def is_click_up(self, button: int) -> bool:
|
|
return self.event.type == pygame.MOUSEBUTTONUP and self.event.button == button
|
|
|
|
@staticmethod
|
|
def create_events(event: list[Event], screen_transform: PositionScale) -> list['ClickEvent']:
|
|
return [
|
|
ClickEvent(CoordinateTransform.transform_screen_to_world(pygame.mouse.get_pos(), screen_transform),
|
|
pygame.mouse.get_pos(),
|
|
e)
|
|
for e in event
|
|
if e.type == pygame.MOUSEBUTTONDOWN or e.type == pygame.MOUSEBUTTONUP
|
|
]
|