added a sprite class to simplify the modification of width and height
This commit is contained in:
parent
36e89f0e6d
commit
dcca925dbe
4 changed files with 39 additions and 21 deletions
2
main.pyw
2
main.pyw
|
@ -20,7 +20,7 @@ for x in range(10):
|
||||||
200 + y * 50, x * 50, 50, 50,
|
200 + y * 50, x * 50, 50, 50,
|
||||||
text=f"{x}-{y}",
|
text=f"{x}-{y}",
|
||||||
font_size=10,
|
font_size=10,
|
||||||
on_release=lambda self, *a, **b: print(self, a, b),
|
on_release=lambda self, *a, **b: setattr(self, "width", self.width + 10),
|
||||||
normal_image=button_normal_image,
|
normal_image=button_normal_image,
|
||||||
hover_image=button_hover_image,
|
hover_image=button_hover_image,
|
||||||
)
|
)
|
||||||
|
|
17
source/gui/sprite/Sprite.py
Normal file
17
source/gui/sprite/Sprite.py
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
import pyglet.sprite
|
||||||
|
|
||||||
|
|
||||||
|
class Sprite(pyglet.sprite.Sprite):
|
||||||
|
def __init__(self, *args, **kwargs):
|
||||||
|
super().__init__(*args, **kwargs)
|
||||||
|
|
||||||
|
self._orig_width: int = self.width
|
||||||
|
self._orig_height: int = self.height
|
||||||
|
|
||||||
|
@pyglet.sprite.Sprite.width.setter
|
||||||
|
def width(self, width: int):
|
||||||
|
self.scale_x = width / self._orig_width
|
||||||
|
|
||||||
|
@pyglet.sprite.Sprite.height.setter
|
||||||
|
def height(self, height: int):
|
||||||
|
self.scale_y = height / self._orig_height
|
1
source/gui/sprite/__init__.py
Normal file
1
source/gui/sprite/__init__.py
Normal file
|
@ -0,0 +1 @@
|
||||||
|
from .Sprite import Sprite
|
|
@ -2,6 +2,7 @@ from typing import Callable, Optional, TYPE_CHECKING
|
||||||
|
|
||||||
import pyglet
|
import pyglet
|
||||||
|
|
||||||
|
from source.gui.sprite import Sprite
|
||||||
from source.gui.widget.base import Widget
|
from source.gui.widget.base import Widget
|
||||||
from source.utils import in_bbox
|
from source.utils import in_bbox
|
||||||
|
|
||||||
|
@ -12,7 +13,7 @@ if TYPE_CHECKING:
|
||||||
|
|
||||||
|
|
||||||
class Button(Widget):
|
class Button(Widget):
|
||||||
__slots__ = ("_x", "_y", "_width", "_height", "_text", "on_press", "on_release", "_normal_image", "_hover_image",
|
__slots__ = ("on_press", "on_release", "_x", "_y", "_width", "_height", "_text", "_normal_image", "_hover_image",
|
||||||
"_hovering")
|
"_hovering")
|
||||||
|
|
||||||
def __init__(self,
|
def __init__(self,
|
||||||
|
@ -20,10 +21,15 @@ class Button(Widget):
|
||||||
y: int,
|
y: int,
|
||||||
width: int,
|
width: int,
|
||||||
height: int,
|
height: int,
|
||||||
|
|
||||||
on_press: Optional[Callable] = None,
|
on_press: Optional[Callable] = None,
|
||||||
on_release: Optional[Callable] = None,
|
on_release: Optional[Callable] = None,
|
||||||
normal_image: pyglet.image.AbstractImage = None,
|
normal_image: pyglet.image.AbstractImage = None,
|
||||||
hover_image: pyglet.image.AbstractImage = None,
|
hover_image: pyglet.image.AbstractImage = None,
|
||||||
|
|
||||||
|
batch: pyglet.graphics.Batch = None,
|
||||||
|
group: pyglet.graphics.Group = None,
|
||||||
|
|
||||||
*args, **kwargs
|
*args, **kwargs
|
||||||
):
|
):
|
||||||
|
|
||||||
|
@ -45,12 +51,8 @@ class Button(Widget):
|
||||||
# hovering and background
|
# hovering and background
|
||||||
self._hovering = False
|
self._hovering = False
|
||||||
|
|
||||||
self._normal_sprite = pyglet.sprite.Sprite(normal_image)
|
self._normal_sprite = Sprite(normal_image)
|
||||||
self._normal_sprite.original_width = self._normal_sprite.width
|
self._hover_sprite = Sprite(hover_image)
|
||||||
self._normal_sprite.original_height = self._normal_sprite.height
|
|
||||||
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
|
|
||||||
|
|
||||||
# the event when the button is clicked
|
# the event when the button is clicked
|
||||||
self.on_press: Optional[Callable[["Self", "Window", "Scene", int, int, int, int], None]] = on_press
|
self.on_press: Optional[Callable[["Self", "Window", "Scene", int, int, int, int], None]] = on_press
|
||||||
|
@ -65,8 +67,8 @@ class Button(Widget):
|
||||||
for sprite in self._normal_sprite, self._hover_sprite:
|
for sprite in self._normal_sprite, self._hover_sprite:
|
||||||
sprite.x = self.x
|
sprite.x = self.x
|
||||||
sprite.y = self.y
|
sprite.y = self.y
|
||||||
sprite.scale_x = self.width / sprite.original_width
|
sprite.width = self.width
|
||||||
sprite.scale_y = self.height / sprite.original_height
|
sprite.height = self.height
|
||||||
|
|
||||||
self._label.x = self.x + (self.width // 2)
|
self._label.x = self.x + (self.width // 2)
|
||||||
self._label.y = self.y + (self.height // 2)
|
self._label.y = self.y + (self.height // 2)
|
||||||
|
@ -87,32 +89,32 @@ class Button(Widget):
|
||||||
def x(self) -> int: return self._x
|
def x(self) -> int: return self._x
|
||||||
|
|
||||||
@x.setter
|
@x.setter
|
||||||
def x(self, value: int):
|
def x(self, x: int):
|
||||||
self._x = value
|
self._x = x
|
||||||
self._update_size()
|
self._update_size()
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def y(self) -> int: return self._y
|
def y(self) -> int: return self._y
|
||||||
|
|
||||||
@y.setter
|
@y.setter
|
||||||
def y(self, value: int):
|
def y(self, y: int):
|
||||||
self._y = value
|
self._y = y
|
||||||
self._update_size()
|
self._update_size()
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def width(self) -> int: return self._width
|
def width(self) -> int: return self._width
|
||||||
|
|
||||||
@width.setter
|
@width.setter
|
||||||
def width(self, value: int):
|
def width(self, width: int):
|
||||||
self._width = value
|
self._width = width
|
||||||
self._update_size()
|
self._update_size()
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def height(self) -> int: return self._height
|
def height(self) -> int: return self._height
|
||||||
|
|
||||||
@height.setter
|
@height.setter
|
||||||
def height(self, value: int):
|
def height(self, height: int):
|
||||||
self._height = value
|
self._height = height
|
||||||
self._update_size()
|
self._update_size()
|
||||||
|
|
||||||
# event
|
# event
|
||||||
|
@ -129,7 +131,5 @@ class Button(Widget):
|
||||||
self._hovering = in_bbox((x, y), self.bbox)
|
self._hovering = in_bbox((x, y), self.bbox)
|
||||||
|
|
||||||
def on_draw(self, window: "Window", scene: "Scene"):
|
def on_draw(self, window: "Window", scene: "Scene"):
|
||||||
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()
|
||||||
|
|
Loading…
Reference in a new issue