button now have simpler getter and a single _update_size method

This commit is contained in:
Faraphel 2023-01-10 13:56:20 +01:00
parent eff0218c8f
commit 36e89f0e6d
5 changed files with 53 additions and 36 deletions

View file

@ -2,7 +2,8 @@ import pyglet
from source.gui.widget.Button import Button from source.gui.widget.Button import Button
from source.gui.window import Window from source.gui.window import Window
from source.gui.scene import HelloWorldScene, FPSCounterScene from source.gui.scene.debug import FPSCounterScene
from source.gui.scene import HelloWorldScene
# Créer une fenêtre # Créer une fenêtre
window = Window(resizable=True, visible=False) window = Window(resizable=True, visible=False)
@ -12,15 +13,19 @@ button_hover_image = pyglet.image.load("./assets/test_button_hover.png")
hello_world_scene = HelloWorldScene() hello_world_scene = HelloWorldScene()
button = Button( # performance and button test
50, 50, 300, 100, for x in range(10):
text="HELLO", for y in range(10):
on_release=lambda *a, **b: print(a, b), button = Button(
normal_image=button_normal_image, 200 + y * 50, x * 50, 50, 50,
hover_image=button_hover_image, text=f"{x}-{y}",
) font_size=10,
on_release=lambda self, *a, **b: print(self, a, b),
normal_image=button_normal_image,
hover_image=button_hover_image,
)
hello_world_scene.add_widget(button)
hello_world_scene.add_widget(button)
fps_counter_scene = FPSCounterScene() fps_counter_scene = FPSCounterScene()
window.add_scene(hello_world_scene, fps_counter_scene) window.add_scene(hello_world_scene, fps_counter_scene)

View file

@ -1,2 +1 @@
from .HelloWorldScene import HelloWorldScene from .HelloWorldScene import HelloWorldScene
from .FPSCounterScene import FPSCounterScene

View file

@ -0,0 +1 @@
from .FPSCounterScene import FPSCounterScene

View file

@ -6,6 +6,7 @@ from source.gui.widget.base import Widget
from source.utils import in_bbox from source.utils import in_bbox
if TYPE_CHECKING: if TYPE_CHECKING:
from typing import Self
from source.gui.scene.base import Scene from source.gui.scene.base import Scene
from source.gui.window import Window from source.gui.window import Window
@ -27,33 +28,48 @@ class Button(Widget):
): ):
# TODO: use batch # TODO: use batch
# TODO: make the label centered in the button
# TODO: use texture bin and animation to simplify the image handling ? # TODO: use texture bin and animation to simplify the image handling ?
# TODO: add an image when the mouse click ?
# TODO: make x, y, width, height, font_size optionally function to allow dynamic sizing
self.label = pyglet.text.Label(*args, **kwargs) # initialise the default value for the property
self._x, self._y, self._width, self._height = x, y, width, height
# the label used for the text
self._label = pyglet.text.Label(
anchor_x="center",
anchor_y="center",
*args, **kwargs
)
# hovering and background
self._hovering = False self._hovering = False
self._normal_sprite = pyglet.sprite.Sprite(normal_image) self._normal_sprite = pyglet.sprite.Sprite(normal_image)
self._normal_sprite.original_width = self._normal_sprite.width
self._normal_sprite.original_height = self._normal_sprite.height
self._hover_sprite = pyglet.sprite.Sprite(hover_image) self._hover_sprite = pyglet.sprite.Sprite(hover_image)
self._hover_sprite.original_width = self._hover_sprite.width
self._hover_sprite.original_height = self._hover_sprite.height
self.on_press: Optional[Callable[["Window", "Scene", int, int, int, int], None]] = on_press # the event when the button is clicked
self.on_release: Optional[Callable[["Window", "Scene", int, int, int, int], None]] = on_release self.on_press: Optional[Callable[["Self", "Window", "Scene", int, int, int, int], None]] = on_press
self.on_release: Optional[Callable[["Self", "Window", "Scene", int, int, int, int], None]] = on_release
self.x: int = x # update the size of the widget
self.y: int = y self._update_size()
self.width: int = width
self.height: int = height
# function # function
def _update_sprite_size(self, x: int = None, y: int = None, width: int = None, height: int = None): def _update_size(self):
for sprite in self._normal_sprite, self._hover_sprite: for sprite in self._normal_sprite, self._hover_sprite:
sprite.update( sprite.x = self.x
x=x, sprite.y = self.y
y=y, sprite.scale_x = self.width / sprite.original_width
scale_x=None if width is None else width / sprite.width, sprite.scale_y = self.height / sprite.original_height
scale_y=None if height is None else height / sprite.height,
) self._label.x = self.x + (self.width // 2)
self._label.y = self.y + (self.height // 2)
# button getter and setter # button getter and setter
@ -73,8 +89,7 @@ class Button(Widget):
@x.setter @x.setter
def x(self, value: int): def x(self, value: int):
self._x = value self._x = value
self.label.x = value self._update_size()
self._update_sprite_size(x=value)
@property @property
def y(self) -> int: return self._y def y(self) -> int: return self._y
@ -82,8 +97,7 @@ class Button(Widget):
@y.setter @y.setter
def y(self, value: int): def y(self, value: int):
self._y = value self._y = value
self.label.y = value self._update_size()
self._update_sprite_size(y=value)
@property @property
def width(self) -> int: return self._width def width(self) -> int: return self._width
@ -91,8 +105,7 @@ class Button(Widget):
@width.setter @width.setter
def width(self, value: int): def width(self, value: int):
self._width = value self._width = value
self.label.width = value self._update_size()
self._update_sprite_size(width=value)
@property @property
def height(self) -> int: return self._height def height(self) -> int: return self._height
@ -100,18 +113,17 @@ class Button(Widget):
@height.setter @height.setter
def height(self, value: int): def height(self, value: int):
self._height = value self._height = value
self.label.height = value self._update_size()
self._update_sprite_size(height=value)
# event # event
def on_mouse_press(self, window: "Window", scene: "Scene", x: int, y: int, button: int, modifiers: int): def on_mouse_press(self, window: "Window", scene: "Scene", x: int, y: int, button: int, modifiers: int):
if not in_bbox((x, y), self.bbox): return if not in_bbox((x, y), self.bbox): return
if self.on_press is not None: self.on_press(window, scene, x, y, button, modifiers) if self.on_press is not None: self.on_press(self, window, scene, x, y, button, modifiers)
def on_mouse_release(self, window: "Window", scene: "Scene", x: int, y: int, button: int, modifiers: int): def on_mouse_release(self, window: "Window", scene: "Scene", x: int, y: int, button: int, modifiers: int):
if not in_bbox((x, y), self.bbox): return if not in_bbox((x, y), self.bbox): return
if self.on_release is not None: self.on_release(window, scene, x, y, button, modifiers) if self.on_release is not None: self.on_release(self, window, scene, x, y, button, modifiers)
def on_mouse_motion(self, window: "Window", scene: "Scene", x: int, y: int, dx: int, dy: int): def on_mouse_motion(self, window: "Window", scene: "Scene", x: int, y: int, dx: int, dy: int):
self._hovering = in_bbox((x, y), self.bbox) self._hovering = in_bbox((x, y), self.bbox)
@ -120,4 +132,4 @@ class Button(Widget):
if (bg := self.background_sprite) is not None: if (bg := self.background_sprite) is not None:
bg.draw() bg.draw()
self.label.draw() self._label.draw()