simplified the in_bbox function in the widgets

This commit is contained in:
Faraphel 2023-03-09 23:21:41 +01:00
parent 13aa6312b8
commit cb4e7cafe3
3 changed files with 12 additions and 7 deletions

View file

@ -7,7 +7,7 @@ A faire :
- mode d'emploi (video + pdf) expliquant le fonctionnement - mode d'emploi (video + pdf) expliquant le fonctionnement
2. Bonus : 2. Bonus :
- animations de fin, mettre la musique, ... - animations de fin
- sauvegarder les paramètres dans un fichier - sauvegarder les paramètres dans un fichier
3. Bug : 3. Bug :

View file

@ -4,7 +4,7 @@ from source.gui.better_pyglet import Sprite, Label
from source.gui.texture.abc import Style from source.gui.texture.abc import Style
from source.gui.widget.abc import BoxWidget from source.gui.widget.abc import BoxWidget
from source.type import Distance from source.type import Distance
from source.utils import dict_filter_prefix, in_bbox from source.utils import dict_filter_prefix
if TYPE_CHECKING: if TYPE_CHECKING:
from source.gui.scene.abc import Scene from source.gui.scene.abc import Scene
@ -128,5 +128,5 @@ class Scroller(BoxWidget):
self._refresh() self._refresh()
def on_mouse_drag(self, x, y, dx, dy, buttons, modifiers): def on_mouse_drag(self, x, y, dx, dy, buttons, modifiers):
if in_bbox((x, y), self.bbox): if self.in_bbox((x, y)):
self._refresh_cursor(x - self.x) self._refresh_cursor(x - self.x)

View file

@ -2,7 +2,7 @@ from abc import ABC
from typing import TYPE_CHECKING, Optional from typing import TYPE_CHECKING, Optional
from source.gui.widget.abc import Widget from source.gui.widget.abc import Widget
from source.type import Distance from source.type import Distance, Point2D
from source.utils import in_bbox from source.utils import in_bbox
if TYPE_CHECKING: if TYPE_CHECKING:
@ -114,6 +114,11 @@ class BoxWidget(Widget, ABC):
def center(self) -> tuple[float, float]: def center(self) -> tuple[float, float]:
return self.center_x, self.center_y return self.center_x, self.center_y
# function
def in_bbox(self, point: Point2D) -> bool:
return in_bbox(point, self.bbox)
# event # event
def on_mouse_motion(self, x: int, y: int, dx: int, dy: int): def on_mouse_motion(self, x: int, y: int, dx: int, dy: int):
@ -129,7 +134,7 @@ class BoxWidget(Widget, ABC):
rel_x, rel_y = x - self.x, y - self.y rel_x, rel_y = x - self.x, y - self.y
old_hovering = self.hovering old_hovering = self.hovering
self.hovering = in_bbox((x, y), self.bbox) self.hovering = self.in_bbox((x, y))
if old_hovering != self.hovering: # if the hover changed if old_hovering != self.hovering: # if the hover changed
# call the hover changed event # call the hover changed event
@ -143,7 +148,7 @@ class BoxWidget(Widget, ABC):
def on_mouse_press(self, x: int, y: int, button: int, modifiers: int): def on_mouse_press(self, x: int, y: int, button: int, modifiers: int):
rel_x, rel_y = x - self.x, y - self.y rel_x, rel_y = x - self.x, y - self.y
self.activated = in_bbox((x, y), self.bbox) self.activated = self.in_bbox((x, y))
self.trigger_event("on_activate_change", rel_x, rel_y, button, modifiers) self.trigger_event("on_activate_change", rel_x, rel_y, button, modifiers)
if self.activated: # if the click was inside the widget if self.activated: # if the click was inside the widget
@ -162,7 +167,7 @@ class BoxWidget(Widget, ABC):
old_click: bool = self.clicking old_click: bool = self.clicking
self.clicking = False # the widget is no longer clicked self.clicking = False # the widget is no longer clicked
if not in_bbox((x, y), self.bbox): return # if the release was not in the collision, ignore if not self.in_bbox((x, y)): return # if the release was not in the collision, ignore
if old_click: # if this button was the one hovered when the click was pressed if old_click: # if this button was the one hovered when the click was pressed
self.trigger_event("on_click_change", rel_x, rel_y, button, modifiers) self.trigger_event("on_click_change", rel_x, rel_y, button, modifiers)