diff --git a/src/engine/plugins/hover.py b/src/engine/plugins/hover.py index fbc0977..a57ad22 100644 --- a/src/engine/plugins/hover.py +++ b/src/engine/plugins/hover.py @@ -3,6 +3,7 @@ Definit un plugin qui verifie si la souris est sur un element (qui a une texture """ from engine import * +from typing import Callable from engine.math import Vec2 from engine.plugins.pygame import Mouse from engine.plugins.render import Position, Offset, Texture, TextureManager @@ -18,10 +19,9 @@ class HoverPlugin(Plugin): """ Met a jour les composants Hover des entités. """ - entities = world.query(Texture, Position) textures = world[TextureManager] mouse_pos = world[Mouse].position - for entity in entities: + for entity in world.query(Hoverable, Texture, Position): # Récupération de la position et de la taille de l'entité entity_pos: Vec2 = entity[Position] if Offset in entity: @@ -35,9 +35,20 @@ class HoverPlugin(Plugin): and mouse_pos.y <= entity_pos.y + entity_size.y ): entity.set(Hover()) + + # Si l'entité n'était pas survolée on execute son callback + if Hover not in entity: + entity[Hoverable].enter_callback(world, entity) + + # Si l'entité est survolée on execute son callback + entity[Hoverable].update_callback(world, entity) else: entity.remove(Hover) + # Si l'entité est survolée on execute son callback + if Hover in entity: + entity[Hoverable].exit_callback(world, entity) + def apply(self, game: Game) -> None: """ Applique le plugin a un jeu. @@ -48,6 +59,22 @@ class HoverPlugin(Plugin): game.add_pre_update_tasks(self._update) +class Hoverable: + """ + Un composant qui marque une entitée comme pouvant etre survolée. + """ + + def __init__( + self, + enter_callback: Callable[[World, Entity], None] = lambda _w, _e: None, + update_callback: Callable[[World, Entity], None] = lambda _w, _e: None, + exit_callback: Callable[[World, Entity], None] = lambda _w, _e: None, + ) -> None: + self.update_callback = update_callback + self.enter_callback = enter_callback + self.exit_callback = exit_callback + + class Hover: """ Un composant qui marque une entitée comme etant survolée. diff --git a/src/engine/plugins/pygame.py b/src/engine/plugins/pygame.py index 4a6ffd7..b71626c 100644 --- a/src/engine/plugins/pygame.py +++ b/src/engine/plugins/pygame.py @@ -131,8 +131,8 @@ class Display: Ressource qui represente la fenetre du jeu. """ - WIDTH = 1080.0 - HEIGHT = 810.0 + WIDTH = 1440.0 + HEIGHT = 1080.0 RATIO = WIDTH / HEIGHT INVERT_RATIO = HEIGHT / WIDTH diff --git a/src/main.py b/src/main.py index 9230e07..991b221 100644 --- a/src/main.py +++ b/src/main.py @@ -5,9 +5,9 @@ Ceci est un exemple de comment l'on peut utiliser le moteur du jeu. from engine import * from engine.math import Vec2 -from engine.plugins.animation import AnimatedSprite, AnimationPlugin -from engine.plugins.clickable import Clickable, ClickablePlugin -from engine.plugins.hover import HoverPlugin +from engine.plugins.animation import AnimationPlugin +from engine.plugins.clickable import ClickablePlugin +from engine.plugins.hover import HoverPlugin, Hoverable from engine.plugins.render import ( Order, RenderPlugin, @@ -16,7 +16,6 @@ from engine.plugins.render import ( ) from engine.plugins.timing import Delta, TimePlugin from engine.plugins.pygame import Display, Keyboard, PygamePlugin -from random import random # Initialisation @@ -35,24 +34,32 @@ def spawn_sprites(world: World) -> None: """ Ajoute des sprites au monde. """ - for i in range(100): - red = random() < 0.1 - entity = world.create_entity( - Position(random() * Display.WIDTH, random() * Display.HEIGHT), - Texture("directory.png") if red else Texture("error.png"), - Order(1 if red else 0), + + def new_button(position: Vec2, file_name: str) -> None: + world.create_entity( + Position(position), + Order(0), + Texture(file_name + ".png"), + Hoverable( + enter_callback=lambda _world, entity: entity.set( + Texture(file_name + "_hover.png"), + ), + exit_callback=lambda _world, entity: entity.set( + Texture(file_name + ".png"), + ), + ), ) - if red: - entity.set( - Clickable( - lambda world, entity: entity.set( - AnimatedSprite( - "search_directory", - lambda world, entity: print("finished !"), - ) - ) - ) - ) + + for i in range(4): + if i == 0: + file_name = "button_classique" + elif i == 1: + file_name = "button_histoire" + elif i == 2: + file_name = "button_tricheur" + else: + file_name = "button_menteur" + new_button(Vec2(Display.WIDTH / 4 * i, 20), file_name) # On ajoutant la tache diff --git a/textures/button_classique.png b/textures/button_classique.png new file mode 100644 index 0000000..71ebb5b Binary files /dev/null and b/textures/button_classique.png differ diff --git a/textures/button_classique_hover.png b/textures/button_classique_hover.png new file mode 100644 index 0000000..e73cc4c Binary files /dev/null and b/textures/button_classique_hover.png differ diff --git a/textures/button_histoire.png b/textures/button_histoire.png new file mode 100644 index 0000000..b2bc698 Binary files /dev/null and b/textures/button_histoire.png differ diff --git a/textures/button_histoire_hover.png b/textures/button_histoire_hover.png new file mode 100644 index 0000000..056e631 Binary files /dev/null and b/textures/button_histoire_hover.png differ diff --git a/textures/button_menteur.png b/textures/button_menteur.png new file mode 100644 index 0000000..1266334 Binary files /dev/null and b/textures/button_menteur.png differ diff --git a/textures/button_menteur_hover.png b/textures/button_menteur_hover.png new file mode 100644 index 0000000..f25efc8 Binary files /dev/null and b/textures/button_menteur_hover.png differ diff --git a/textures/button_tricheur.png b/textures/button_tricheur.png new file mode 100644 index 0000000..c71d66b Binary files /dev/null and b/textures/button_tricheur.png differ diff --git a/textures/button_tricheur_hover.png b/textures/button_tricheur_hover.png new file mode 100644 index 0000000..96ef297 Binary files /dev/null and b/textures/button_tricheur_hover.png differ