ponguito/src/plugins/click.py

49 lines
1.1 KiB
Python
Raw Normal View History

2023-12-28 18:34:38 +00:00
"""
Un plugin permettant de savoir si l'on a cliqué sur une entité.
"""
from typing import Callable
from engine import GlobalPlugin
from engine.ecs import Entity, World
from plugins.hover import Hovered
from plugins.inputs import Pressed
2024-01-03 18:58:49 +00:00
from plugins.render import Origin, Position, Scale
2023-12-28 18:34:38 +00:00
class Clicked:
"""
Component ajouté a toutes les entitées qui viennent d'être cliqué.
"""
class Clickable:
"""
Composant qui permet d'executer une fonction lorsqu'une entité est cliquee.
"""
def __init__(self, callback: Callable[[World, Entity], object]):
self.callback = callback
def __update_clicked(world: World):
"""
Met à jour les composants `Clicked`.
"""
mouse_click = "button_1" in world[Pressed]
2024-01-03 18:58:49 +00:00
sprite_entities = world.query(Position, Scale, Origin)
2023-12-28 18:34:38 +00:00
for entity in sprite_entities:
if Hovered in entity and mouse_click:
entity[Clicked] = Clicked()
if Clickable in entity:
entity[Clickable].callback(world, entity)
else:
del entity[Clicked]
PLUGIN = GlobalPlugin(
[],
[__update_clicked],
[],
[],
)