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 - Connexion entre les joueurs
- Envoie des paramètres à l'autre joueur (largeur longueur du plateau, nombre de bateau et leur taille, ...) - Envoie des paramètres à l'autre joueur (largeur longueur du plateau, nombre de bateau et leur taille, ...)
- Phase de placement des bateaux - Phase de placement des bateaux

View file

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

View file

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

View file

@ -23,7 +23,7 @@ class RoomJoin(Scene):
self.back = self.add_widget( self.back = self.add_widget(
Button, 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", label_text="Retour",
@ -39,7 +39,7 @@ class RoomJoin(Scene):
Input, Input,
x=0.4, y=0.5, width=0.13, height=0.1, 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_normal=texture_input_normal,
texture_active=texture_input_active, texture_active=texture_input_active,

View file

@ -2,7 +2,7 @@ from abc import ABC
from typing import TYPE_CHECKING, Optional from typing import TYPE_CHECKING, Optional
from source.gui.widget.abc import Widget from source.gui.widget.abc import Widget
from source.type import Percentage from source.type import Distance, Percentage
from source.utils import in_bbox from source.utils import in_bbox
if TYPE_CHECKING: if TYPE_CHECKING:
@ -15,10 +15,10 @@ class BoxWidget(Widget, ABC):
""" """
def __init__(self, scene: "Scene", def __init__(self, scene: "Scene",
x: Percentage = 0, x: Distance = 0,
y: Percentage = 0, y: Distance = 0,
width: Percentage = None, width: Distance = None,
height: Percentage = None): height: Distance = None):
super().__init__(scene) super().__init__(scene)
# memorize the value with a percent value # memorize the value with a percent value
@ -33,37 +33,52 @@ class BoxWidget(Widget, ABC):
# property # 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 @property
def x(self) -> int: def x(self) -> int:
return self.scene.window.width * self._p_x return self._getter_distance(self.scene.window.width, self._x)
@x.setter @x.setter
def x(self, x: Percentage): def x(self, x: Distance):
self._p_x = x self._x = x
@property @property
def y(self) -> int: def y(self) -> int:
return self.scene.window.height * self._p_y return self._getter_distance(self.scene.window.height, self._y)
@y.setter @y.setter
def y(self, y: Percentage): def y(self, y: Distance):
self._p_y = y self._y = y
@property @property
def width(self) -> int: 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 @width.setter
def width(self, width: Optional[Percentage]): def width(self, width: Optional[Distance]):
self._p_width = width self._width = width
@property @property
def height(self) -> int: 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 @height.setter
def height(self, height: Optional[Percentage]): def height(self, height: Optional[Distance]):
self._p_height = height self._height = height
@property @property
def xy(self) -> tuple[int, int]: def xy(self) -> tuple[int, int]:

View file

@ -1,3 +1,9 @@
from typing import Union, Callable, Any
Point2D = tuple[int, int] Point2D = tuple[int, int]
BBox = tuple[int, int, 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]