added on_press, on_release, on_hover_enter, on_hover_leave event on BoxWidget

This commit is contained in:
Faraphel 2023-02-12 14:04:07 +01:00
parent 9b008cbaea
commit 11476f5de8
2 changed files with 71 additions and 8 deletions

View file

@ -12,7 +12,10 @@ class TestScene(Scene):
super().__init__(window)
self.add_widget(FPSDisplay)
self.add_widget(Text, text="Hello World !", x=0.5, y=0.5, anchor_x="center", anchor_y="center")
label = self.add_widget(Text, text="Hello World !", x=0.5, y=0.5, width=0.5, height=0.5, anchor_x="center", anchor_y="center")
label.on_pressed = lambda self, button, modifiers: print("pressed", self, button, modifiers)
label.on_release = lambda self, button, modifiers: print("release", self, button, modifiers)
# Create a new window

View file

@ -3,6 +3,7 @@ from typing import TYPE_CHECKING, Optional
from source.gui.widget.abc import Widget
from source.type import Percentage
from source.utils import in_bbox
if TYPE_CHECKING:
from source.gui.scene.abc import Scene
@ -26,8 +27,11 @@ class BoxWidget(Widget, ABC):
self.width = width
self.height = height
self._hovering = False # is the button currently hovered ?
self._clicking = False # is the button currently clicked ?
@property
def x(self):
def x(self) -> int:
return self.scene.window.width * self._p_x
@x.setter
@ -35,7 +39,7 @@ class BoxWidget(Widget, ABC):
self._p_x = x
@property
def y(self):
def y(self) -> int:
return self.scene.window.height * self._p_y
@y.setter
@ -43,7 +47,7 @@ class BoxWidget(Widget, ABC):
self._p_y = y
@property
def width(self):
def width(self) -> int:
return None if self._p_width is None else self.scene.window.width * self._p_width
@width.setter
@ -51,7 +55,7 @@ class BoxWidget(Widget, ABC):
self._p_width = width
@property
def height(self):
def height(self) -> int:
return None if self._p_height is None else self.scene.window.height * self._p_height
@height.setter
@ -59,13 +63,69 @@ class BoxWidget(Widget, ABC):
self._p_height = height
@property
def xy(self):
def xy(self) -> tuple[int, int]:
return self.x, self.y
@property
def size(self):
def size(self) -> tuple[int, int]:
return self.width, self.height
@property
def bbox(self):
def bbox(self) -> tuple[int, int, int, int]:
return self.x, self.y, self.width, self.height
# event
def on_mouse_motion(self, x: int, y: int, dx: int, dy: int): # NOQA
"""
When the mouse is moved, this event is triggered.
Allow the implementation of the on_hover_enter and on_hover_leave events
:x: the x position of the mouse
:y: the y position of the mouse
:dx: the difference of the x mouse axis
:dy: the difference of the y mouse axis
"""
old_hovering = self._hovering
self._hovering = in_bbox((x, y), self.bbox)
if old_hovering != self._hovering: # if the hover changed
if self._hovering: self.on_hover_enter() # call the hover enter event
else: self.on_hover_leave() # call the hover leave event
def on_hover_enter(self):
"""
This event is called when the mouse enter the bbox of the widget
"""
def on_hover_leave(self):
"""
This event is called when the mouse leave the bbox of the widget
"""
def on_mouse_press(self, x: int, y: int, button: int, modifiers: int):
# if this button was the one hovered when the click was pressed
if not in_bbox((x, y), self.bbox): return
self._clicking = True
self.on_press(button, modifiers)
def on_mouse_release(self, x: int, y: int, button: int, modifiers: int):
old_click: bool = self._clicking
self._clicking = False
if not in_bbox((x, y), self.bbox): return
# if this button was the one hovered when the click was pressed
if old_click: self.on_release(button, modifiers)
def on_press(self, button: int, modifiers: int):
"""
This event is called when the bbox is pressed
"""
def on_release(self, button: int, modifiers: int):
"""
This event is called when the bbox is released
"""