ponguito/src/plugins/click.py
Timéo Cézard d8e64348cd Good
2024-01-03 19:58:49 +01:00

49 lines
1.1 KiB
Python

"""
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
from plugins.render import Origin, Position, Scale
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]
sprite_entities = world.query(Position, Scale, Origin)
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],
[],
[],
)