distance can now be represented by a constant pixel value or a function that is called when the distance have to be updated

This commit is contained in:
Faraphel 2023-02-15 08:57:24 +01:00
parent e5ce93a207
commit 2dfc21b8ef
6 changed files with 53 additions and 22 deletions

10
NOTE.md
View file

@ -1,3 +1,13 @@
A faire :
Widgets:
- Slider
- Checkbox
- Grille (bataille navale)
Réseau :
- Connexion entre les joueurs
- Envoie des paramètres à l'autre joueur (largeur longueur du plateau, nombre de bateau et leur taille, ...)
- Phase de placement des bateaux

View file

@ -95,7 +95,7 @@ class TestScene2(Scene):
# Create a new window
window = Window(resizable=True, vsync=False)
window = Window(resizable=True, vsync=True)
window.add_scene(MainMenu)
# Start the event loop

View file

@ -21,7 +21,7 @@ class MainMenu(Scene):
self.background = self.add_widget(
Image,
x=0, y=0, width=1, height=1,
x=0.0, y=0.0, width=1.0, height=1.0,
image=texture_background
)

View file

@ -23,7 +23,7 @@ class RoomJoin(Scene):
self.back = self.add_widget(
Button,
x=0.02, y=0.02, width=0.2, height=0.1,
x=20, y=20, width=0.2, height=0.1,
label_text="Retour",
@ -39,7 +39,7 @@ class RoomJoin(Scene):
Input,
x=0.4, y=0.5, width=0.13, height=0.1,
regex=r"\d{1,3}(.\d{1,3}){3}",
regex=r"\d{1,3}(\.\d{1,3}){3}",
texture_normal=texture_input_normal,
texture_active=texture_input_active,

View file

@ -2,7 +2,7 @@ from abc import ABC
from typing import TYPE_CHECKING, Optional
from source.gui.widget.abc import Widget
from source.type import Percentage
from source.type import Distance, Percentage
from source.utils import in_bbox
if TYPE_CHECKING:
@ -15,10 +15,10 @@ class BoxWidget(Widget, ABC):
"""
def __init__(self, scene: "Scene",
x: Percentage = 0,
y: Percentage = 0,
width: Percentage = None,
height: Percentage = None):
x: Distance = 0,
y: Distance = 0,
width: Distance = None,
height: Distance = None):
super().__init__(scene)
# memorize the value with a percent value
@ -33,37 +33,52 @@ class BoxWidget(Widget, ABC):
# property
def _getter_distance(self, max_distance: int, raw_distance: Distance) -> int:
"""
Return the true distance in pixel from a more abstract distance
:param max_distance: the max value the distance in pixel should have
:param raw_distance: the distance object to convert to pixel
:return: the true distance in pixel
"""
if isinstance(raw_distance, Percentage): return int(max_distance * raw_distance)
if isinstance(raw_distance, int): return raw_distance
if callable(raw_distance): return raw_distance(self)
if raw_distance is None: return 0
raise TypeError(f"Invalid type for the distance : {type(raw_distance)}")
@property
def x(self) -> int:
return self.scene.window.width * self._p_x
return self._getter_distance(self.scene.window.width, self._x)
@x.setter
def x(self, x: Percentage):
self._p_x = x
def x(self, x: Distance):
self._x = x
@property
def y(self) -> int:
return self.scene.window.height * self._p_y
return self._getter_distance(self.scene.window.height, self._y)
@y.setter
def y(self, y: Percentage):
self._p_y = y
def y(self, y: Distance):
self._y = y
@property
def width(self) -> int:
return 0 if self._p_width is None else self.scene.window.width * self._p_width
return self._getter_distance(self.scene.window.width, self._width)
@width.setter
def width(self, width: Optional[Percentage]):
self._p_width = width
def width(self, width: Optional[Distance]):
self._width = width
@property
def height(self) -> int:
return 0 if self._p_height is None else self.scene.window.height * self._p_height
return self._getter_distance(self.scene.window.height, self._height)
@height.setter
def height(self, height: Optional[Percentage]):
self._p_height = height
def height(self, height: Optional[Distance]):
self._height = height
@property
def xy(self) -> tuple[int, int]:

View file

@ -1,3 +1,9 @@
from typing import Union, Callable, Any
Point2D = tuple[int, int]
BBox = tuple[int, int, int, int]
Percentage = float
Percentage = float # a percentage, represented as a number between 0 and 1
DistanceFunction = Callable[[Any], int] # a function that return a distance
# a distance, represented either by a whole number, a percentage or a function
Distance = Union[Percentage, int, DistanceFunction]