added BoxWidget class that allow widgets with a bbox

This commit is contained in:
Faraphel 2023-02-11 00:31:48 +01:00
parent 432a311b76
commit 9b008cbaea
11 changed files with 109 additions and 19 deletions

View file

@ -1,6 +1,6 @@
import pyglet
from source.gui.scene import Scene
from source.gui.scene.abc import Scene
from source.gui.widget import Text, FPSDisplay
from source.gui.window import Window
@ -8,11 +8,11 @@ from source.gui.window import Window
class TestScene(Scene):
def __init__(self, window: "Window", *args, **kwargs):
super().__init__(window, *args, **kwargs)
def __init__(self, window: "Window"):
super().__init__(window)
self.add_widget(FPSDisplay)
self.add_widget(Text, text="Hello World !")
self.add_widget(Text, text="Hello World !", x=0.5, y=0.5, anchor_x="center", anchor_y="center")
# Create a new window

0
source/gui/__init__.py Normal file
View file

View file

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

View file

@ -1,12 +1,13 @@
from abc import ABC
from functools import lru_cache
from typing import TYPE_CHECKING, Callable, Type, Any
if TYPE_CHECKING:
from source.gui.window import Window
from source.gui.widget import Widget
from source.gui.widget.abc import Widget
class Scene:
class Scene(ABC):
"""
A scene that can be attached to a window.
It allows to switch the whole comportment of the window in a simpler way.

View file

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

View file

@ -1,4 +1,4 @@
from source.gui.widget import Widget
from source.gui.widget.abc import Widget
from typing import TYPE_CHECKING
@ -13,8 +13,8 @@ class FPSDisplay(Widget):
A widget that display the current FPS of the scene's window
"""
def __init__(self, scene: "Scene", *args, **kwargs):
super().__init__(scene, *args, **kwargs)
def __init__(self, scene: "Scene"):
super().__init__(scene)
self.fps_display = pyglet.window.FPSDisplay(scene.window)

View file

@ -1,22 +1,38 @@
from typing import TYPE_CHECKING
from source.gui.widget import Widget
from source.gui.widget.abc import BoxWidget
import pyglet
from source.type import Percentage
if TYPE_CHECKING:
from source.gui.scene import Scene
from source.gui.scene.abc import Scene
class Text(Widget):
class Text(BoxWidget):
"""
A widget that display a text
"""
def __init__(self, scene: "Scene", *args, **kwargs):
super().__init__(scene, *args, **kwargs)
def __init__(self, scene: "Scene",
x: Percentage = 0,
y: Percentage = 0,
width: Percentage = None,
height: Percentage = None,
*args, **kwargs):
super().__init__(scene, x, y, width, height)
self.label = pyglet.text.Label(*args, **kwargs)
self.label = pyglet.text.Label(
x=self.x, y=self.y, width=self.width, height=self.height,
*args, **kwargs
)
def on_resize(self, width: int, height: int):
self.label.x = self.x
self.label.y = self.y
self.label.width = self.width
self.label.height = self.height
def on_draw(self):
self.label.draw()

View file

@ -1,3 +1,2 @@
from .Widget import Widget
from .Text import Text
from .FPSDisplay import FPSDisplay

View file

@ -0,0 +1,71 @@
from abc import ABC
from typing import TYPE_CHECKING, Optional
from source.gui.widget.abc import Widget
from source.type import Percentage
if TYPE_CHECKING:
from source.gui.scene.abc import Scene
class BoxWidget(Widget, ABC):
"""
Same as a basic widget, but represent a box
"""
def __init__(self, scene: "Scene",
x: Percentage = 0,
y: Percentage = 0,
width: Percentage = None,
height: Percentage = None):
super().__init__(scene)
# memorize the value with a percent value
self.x = x
self.y = y
self.width = width
self.height = height
@property
def x(self):
return self.scene.window.width * self._p_x
@x.setter
def x(self, x: Percentage):
self._p_x = x
@property
def y(self):
return self.scene.window.height * self._p_y
@y.setter
def y(self, y: Percentage):
self._p_y = y
@property
def width(self):
return None if self._p_width is None else self.scene.window.width * self._p_width
@width.setter
def width(self, width: Optional[Percentage]):
self._p_width = width
@property
def height(self):
return None if self._p_height is None else self.scene.window.height * self._p_height
@height.setter
def height(self, height: Optional[Percentage]):
self._p_height = height
@property
def xy(self):
return self.x, self.y
@property
def size(self):
return self.width, self.height
@property
def bbox(self):
return self.x, self.y, self.width, self.height

View file

@ -1,10 +1,11 @@
from abc import ABC
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from source.gui.scene import Scene
from source.gui.scene.abc import Scene
class Widget:
class Widget(ABC):
"""
A Widget that can be attached to a scene.

View file

@ -0,0 +1,2 @@
from .Widget import Widget
from .BoxWidget import BoxWidget