the text on Button is now properly centered
This commit is contained in:
parent
31d1069709
commit
0f0b21f9b7
4 changed files with 45 additions and 26 deletions
20
main.pyw
20
main.pyw
|
@ -13,14 +13,14 @@ class TestScene(Scene):
|
||||||
|
|
||||||
# loading resource
|
# loading resource
|
||||||
|
|
||||||
normal_texture = pyglet.image.load("./assets/image/button/test_button_normal.png")
|
texture_normal = pyglet.image.load("./assets/image/button/test_button_normal.png")
|
||||||
hover_texture = pyglet.image.load("./assets/image/button/test_button_hover.png")
|
texture_hover = pyglet.image.load("./assets/image/button/test_button_hover.png")
|
||||||
click_texture = pyglet.image.load("./assets/image/button/test_button_clicking.png")
|
texture_click = pyglet.image.load("./assets/image/button/test_button_clicking.png")
|
||||||
|
|
||||||
button_atlas = pyglet.image.atlas.TextureAtlas()
|
button_atlas = pyglet.image.atlas.TextureAtlas()
|
||||||
normal_region = button_atlas.add(normal_texture)
|
region_normal = button_atlas.add(texture_normal)
|
||||||
hover_region = button_atlas.add(hover_texture)
|
region_hover = button_atlas.add(texture_hover)
|
||||||
click_region = button_atlas.add(click_texture)
|
region_click = button_atlas.add(texture_click)
|
||||||
|
|
||||||
self.background_batch = pyglet.graphics.Batch()
|
self.background_batch = pyglet.graphics.Batch()
|
||||||
self.label_batch = pyglet.graphics.Batch()
|
self.label_batch = pyglet.graphics.Batch()
|
||||||
|
@ -33,9 +33,9 @@ class TestScene(Scene):
|
||||||
|
|
||||||
x=0.5, y=0.5, width=0.5, height=0.5,
|
x=0.5, y=0.5, width=0.5, height=0.5,
|
||||||
|
|
||||||
texture_normal=normal_region,
|
texture_normal=region_normal,
|
||||||
texture_hover=hover_region,
|
texture_hover=region_hover,
|
||||||
texture_click=click_region,
|
texture_click=region_click,
|
||||||
|
|
||||||
label_text="Hello World !",
|
label_text="Hello World !",
|
||||||
|
|
||||||
|
@ -51,8 +51,6 @@ class TestScene(Scene):
|
||||||
self.label_batch.draw()
|
self.label_batch.draw()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# Create a new window
|
# Create a new window
|
||||||
window = Window(resizable=True, vsync=False)
|
window = Window(resizable=True, vsync=False)
|
||||||
window.add_scene(TestScene)
|
window.add_scene(TestScene)
|
||||||
|
|
|
@ -38,15 +38,16 @@ class Button(BoxWidget):
|
||||||
|
|
||||||
self.background = Sprite(
|
self.background = Sprite(
|
||||||
img=self._texture_normal,
|
img=self._texture_normal,
|
||||||
x=self.x, y=self.y, width=self.width, height=self.height,
|
|
||||||
**dict_prefix("background_", kwargs)
|
**dict_prefix("background_", kwargs)
|
||||||
)
|
)
|
||||||
|
|
||||||
self.label = pyglet.text.Label(
|
self.label = pyglet.text.Label(
|
||||||
x=self.x, y=self.y, width=self.width, height=self.height,
|
anchor_x="center", anchor_y="center",
|
||||||
**dict_prefix("label_", kwargs)
|
**dict_prefix("label_", kwargs)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
self._refresh_size() # refresh the size and position for the background and label
|
||||||
|
|
||||||
# background
|
# background
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
@ -64,9 +65,20 @@ class Button(BoxWidget):
|
||||||
self._texture_normal
|
self._texture_normal
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# refresh
|
||||||
|
|
||||||
def _refresh_background(self) -> None:
|
def _refresh_background(self) -> None:
|
||||||
self.background.image = self.background_texture
|
self.background.image = self.background_texture
|
||||||
|
|
||||||
|
def _refresh_size(self) -> None:
|
||||||
|
self.background.x = self.x
|
||||||
|
self.background.y = self.y
|
||||||
|
self.background.width = self.width
|
||||||
|
self.background.height = self.height
|
||||||
|
|
||||||
|
self.label.x = self.x + (self.width / 2)
|
||||||
|
self.label.y = self.y + (self.height / 2)
|
||||||
|
|
||||||
@BoxWidget.hovering.setter
|
@BoxWidget.hovering.setter
|
||||||
def hovering(self, hovering: bool):
|
def hovering(self, hovering: bool):
|
||||||
# when the hover state is changed, update the background
|
# when the hover state is changed, update the background
|
||||||
|
@ -82,15 +94,7 @@ class Button(BoxWidget):
|
||||||
# event
|
# event
|
||||||
|
|
||||||
def on_resize(self, width: int, height: int):
|
def on_resize(self, width: int, height: int):
|
||||||
self.background.x = self.x
|
self._refresh_size()
|
||||||
self.background.y = self.y
|
|
||||||
self.background.width = self.width
|
|
||||||
self.background.height = self.height
|
|
||||||
|
|
||||||
self.label.x = self.x
|
|
||||||
self.label.y = self.y
|
|
||||||
self.label.width = self.width
|
|
||||||
self.label.height = self.height
|
|
||||||
|
|
||||||
def draw(self):
|
def draw(self):
|
||||||
"""
|
"""
|
||||||
|
|
|
@ -24,16 +24,20 @@ class Text(BoxWidget):
|
||||||
super().__init__(scene, x, y, width, height)
|
super().__init__(scene, x, y, width, height)
|
||||||
|
|
||||||
self.label = pyglet.text.Label(
|
self.label = pyglet.text.Label(
|
||||||
x=self.x, y=self.y, width=self.width, height=self.height,
|
|
||||||
*args, **kwargs
|
*args, **kwargs
|
||||||
)
|
)
|
||||||
|
|
||||||
def on_resize(self, width: int, height: int):
|
self._refresh_size()
|
||||||
|
|
||||||
|
def _refresh_size(self):
|
||||||
self.label.x = self.x
|
self.label.x = self.x
|
||||||
self.label.y = self.y
|
self.label.y = self.y
|
||||||
self.label.width = self.width
|
self.label.width = self.width
|
||||||
self.label.height = self.height
|
self.label.height = self.height
|
||||||
|
|
||||||
|
def on_resize(self, width: int, height: int):
|
||||||
|
self._refresh_size()
|
||||||
|
|
||||||
def draw(self):
|
def draw(self):
|
||||||
"""
|
"""
|
||||||
The draw function. Can be called to draw the widget, but can be ignored to draw it with batchs.
|
The draw function. Can be called to draw the widget, but can be ignored to draw it with batchs.
|
||||||
|
|
|
@ -1,12 +1,19 @@
|
||||||
from typing import Callable, Any
|
from typing import Callable, Any
|
||||||
|
|
||||||
|
|
||||||
def dict_filter(filter_func: Callable[[Any, Any], bool], dictionary: dict) -> dict:
|
def dict_filter(filter_func: Callable[[Any, Any], bool], dictionary: dict[Any, Any]) -> dict[Any, Any]:
|
||||||
"""
|
"""
|
||||||
Filter a dict object with the filter function given.
|
Filter a dict object with the filter function given.
|
||||||
:filter_func: the function to filter with
|
:filter_func: the function to filter with
|
||||||
:dictionary: the dictionary to filter
|
:dictionary: the dictionary to filter
|
||||||
:return: the filtered dictionary
|
:return: the filtered dictionary
|
||||||
|
|
||||||
|
Example :
|
||||||
|
filter_func = lambda key, value: key.startswith("valeur")
|
||||||
|
dictionary = {"valeur1": 1, "valeur2": 2, "clé1": None}
|
||||||
|
|
||||||
|
result = {"valeur1": 1, "valeur2": 2}
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
@ -17,12 +24,18 @@ def dict_filter(filter_func: Callable[[Any, Any], bool], dictionary: dict) -> di
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
def dict_prefix(prefix: str, dictionary: dict) -> dict:
|
def dict_prefix(prefix: str, dictionary: dict[str, Any]) -> dict[str, Any]:
|
||||||
"""
|
"""
|
||||||
Take only the keys that start with the prefix, and remove this prefix from the keys.
|
Take only the keys that start with the prefix, and remove this prefix from the keys.
|
||||||
:prefix: the prefix to use
|
:prefix: the prefix to use
|
||||||
:dictionary: the dictionary to filter
|
:dictionary: the dictionary to filter
|
||||||
:return: the dictionary with the prefix
|
:return: the dictionary with the prefix
|
||||||
|
|
||||||
|
Example:
|
||||||
|
prefix = "button"
|
||||||
|
dictionary = {"button1": 1, "button2": 2, "label1": None}
|
||||||
|
|
||||||
|
result = {"1": 1, "2": 2}
|
||||||
"""
|
"""
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
|
Loading…
Reference in a new issue