added BoxWidget class that allow widgets with a bbox
This commit is contained in:
parent
432a311b76
commit
9b008cbaea
11 changed files with 109 additions and 19 deletions
8
main.pyw
8
main.pyw
|
@ -1,6 +1,6 @@
|
||||||
import pyglet
|
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.widget import Text, FPSDisplay
|
||||||
from source.gui.window import Window
|
from source.gui.window import Window
|
||||||
|
|
||||||
|
@ -8,11 +8,11 @@ from source.gui.window import Window
|
||||||
|
|
||||||
|
|
||||||
class TestScene(Scene):
|
class TestScene(Scene):
|
||||||
def __init__(self, window: "Window", *args, **kwargs):
|
def __init__(self, window: "Window"):
|
||||||
super().__init__(window, *args, **kwargs)
|
super().__init__(window)
|
||||||
|
|
||||||
self.add_widget(FPSDisplay)
|
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
|
# Create a new window
|
||||||
|
|
0
source/gui/__init__.py
Normal file
0
source/gui/__init__.py
Normal file
|
@ -1 +0,0 @@
|
||||||
from .Scene import Scene
|
|
|
@ -1,12 +1,13 @@
|
||||||
|
from abc import ABC
|
||||||
from functools import lru_cache
|
from functools import lru_cache
|
||||||
from typing import TYPE_CHECKING, Callable, Type, Any
|
from typing import TYPE_CHECKING, Callable, Type, Any
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from source.gui.window import Window
|
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.
|
A scene that can be attached to a window.
|
||||||
It allows to switch the whole comportment of the window in a simpler way.
|
It allows to switch the whole comportment of the window in a simpler way.
|
1
source/gui/scene/abc/__init__.py
Normal file
1
source/gui/scene/abc/__init__.py
Normal file
|
@ -0,0 +1 @@
|
||||||
|
from .Scene import Scene
|
|
@ -1,4 +1,4 @@
|
||||||
from source.gui.widget import Widget
|
from source.gui.widget.abc import Widget
|
||||||
|
|
||||||
from typing import TYPE_CHECKING
|
from typing import TYPE_CHECKING
|
||||||
|
|
||||||
|
@ -13,8 +13,8 @@ class FPSDisplay(Widget):
|
||||||
A widget that display the current FPS of the scene's window
|
A widget that display the current FPS of the scene's window
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, scene: "Scene", *args, **kwargs):
|
def __init__(self, scene: "Scene"):
|
||||||
super().__init__(scene, *args, **kwargs)
|
super().__init__(scene)
|
||||||
|
|
||||||
self.fps_display = pyglet.window.FPSDisplay(scene.window)
|
self.fps_display = pyglet.window.FPSDisplay(scene.window)
|
||||||
|
|
||||||
|
|
|
@ -1,22 +1,38 @@
|
||||||
from typing import TYPE_CHECKING
|
from typing import TYPE_CHECKING
|
||||||
|
|
||||||
from source.gui.widget import Widget
|
from source.gui.widget.abc import BoxWidget
|
||||||
|
|
||||||
import pyglet
|
import pyglet
|
||||||
|
|
||||||
|
from source.type import Percentage
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
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
|
A widget that display a text
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, scene: "Scene", *args, **kwargs):
|
def __init__(self, scene: "Scene",
|
||||||
super().__init__(scene, *args, **kwargs)
|
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):
|
def on_draw(self):
|
||||||
self.label.draw()
|
self.label.draw()
|
||||||
|
|
|
@ -1,3 +1,2 @@
|
||||||
from .Widget import Widget
|
|
||||||
from .Text import Text
|
from .Text import Text
|
||||||
from .FPSDisplay import FPSDisplay
|
from .FPSDisplay import FPSDisplay
|
||||||
|
|
71
source/gui/widget/abc/BoxWidget.py
Normal file
71
source/gui/widget/abc/BoxWidget.py
Normal 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
|
|
@ -1,10 +1,11 @@
|
||||||
|
from abc import ABC
|
||||||
from typing import TYPE_CHECKING
|
from typing import TYPE_CHECKING
|
||||||
|
|
||||||
if 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.
|
A Widget that can be attached to a scene.
|
||||||
|
|
2
source/gui/widget/abc/__init__.py
Normal file
2
source/gui/widget/abc/__init__.py
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
from .Widget import Widget
|
||||||
|
from .BoxWidget import BoxWidget
|
Loading…
Reference in a new issue