From 9b008cbaea1ea70629c24b00c7ba28634e819128 Mon Sep 17 00:00:00 2001 From: Faraphel Date: Sat, 11 Feb 2023 00:31:48 +0100 Subject: [PATCH] added BoxWidget class that allow widgets with a bbox --- main.pyw | 8 +-- source/gui/__init__.py | 0 source/gui/scene/__init__.py | 1 - source/gui/scene/{ => abc}/Scene.py | 5 +- source/gui/scene/abc/__init__.py | 1 + source/gui/widget/FPSDisplay.py | 6 +-- source/gui/widget/Text.py | 28 ++++++++--- source/gui/widget/__init__.py | 1 - source/gui/widget/abc/BoxWidget.py | 71 +++++++++++++++++++++++++++ source/gui/widget/{ => abc}/Widget.py | 5 +- source/gui/widget/abc/__init__.py | 2 + 11 files changed, 109 insertions(+), 19 deletions(-) create mode 100644 source/gui/__init__.py rename source/gui/scene/{ => abc}/Scene.py (96%) create mode 100644 source/gui/scene/abc/__init__.py create mode 100644 source/gui/widget/abc/BoxWidget.py rename source/gui/widget/{ => abc}/Widget.py (75%) create mode 100644 source/gui/widget/abc/__init__.py diff --git a/main.pyw b/main.pyw index 7f81b20..a088e93 100644 --- a/main.pyw +++ b/main.pyw @@ -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 diff --git a/source/gui/__init__.py b/source/gui/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/source/gui/scene/__init__.py b/source/gui/scene/__init__.py index 8abb0fe..e69de29 100644 --- a/source/gui/scene/__init__.py +++ b/source/gui/scene/__init__.py @@ -1 +0,0 @@ -from .Scene import Scene diff --git a/source/gui/scene/Scene.py b/source/gui/scene/abc/Scene.py similarity index 96% rename from source/gui/scene/Scene.py rename to source/gui/scene/abc/Scene.py index 87ff02b..a1b663b 100644 --- a/source/gui/scene/Scene.py +++ b/source/gui/scene/abc/Scene.py @@ -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. diff --git a/source/gui/scene/abc/__init__.py b/source/gui/scene/abc/__init__.py new file mode 100644 index 0000000..8abb0fe --- /dev/null +++ b/source/gui/scene/abc/__init__.py @@ -0,0 +1 @@ +from .Scene import Scene diff --git a/source/gui/widget/FPSDisplay.py b/source/gui/widget/FPSDisplay.py index f952151..dd0ea51 100644 --- a/source/gui/widget/FPSDisplay.py +++ b/source/gui/widget/FPSDisplay.py @@ -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) diff --git a/source/gui/widget/Text.py b/source/gui/widget/Text.py index a22443d..efde644 100644 --- a/source/gui/widget/Text.py +++ b/source/gui/widget/Text.py @@ -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() diff --git a/source/gui/widget/__init__.py b/source/gui/widget/__init__.py index 6b82082..197d78b 100644 --- a/source/gui/widget/__init__.py +++ b/source/gui/widget/__init__.py @@ -1,3 +1,2 @@ -from .Widget import Widget from .Text import Text from .FPSDisplay import FPSDisplay diff --git a/source/gui/widget/abc/BoxWidget.py b/source/gui/widget/abc/BoxWidget.py new file mode 100644 index 0000000..9868515 --- /dev/null +++ b/source/gui/widget/abc/BoxWidget.py @@ -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 diff --git a/source/gui/widget/Widget.py b/source/gui/widget/abc/Widget.py similarity index 75% rename from source/gui/widget/Widget.py rename to source/gui/widget/abc/Widget.py index bab8e93..161e0f0 100644 --- a/source/gui/widget/Widget.py +++ b/source/gui/widget/abc/Widget.py @@ -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. diff --git a/source/gui/widget/abc/__init__.py b/source/gui/widget/abc/__init__.py new file mode 100644 index 0000000..b1a533d --- /dev/null +++ b/source/gui/widget/abc/__init__.py @@ -0,0 +1,2 @@ +from .Widget import Widget +from .BoxWidget import BoxWidget