diff --git a/NOTE.md b/NOTE.md index 6387271..4de26d0 100644 --- a/NOTE.md +++ b/NOTE.md @@ -17,6 +17,7 @@ Bug : - (incertain) Dans de rare cas (souvent en fermant brutalement la fenêtre) le processus ne s'arrête pas - Quitter pendant que l'on décide de si l'on doit charger ou non une ancienne sauvegarde fait crash l'adversaire - Les champs invalides n'empêchent pas de faire l'action + - Si le port est déjà utilisé, le jeu n'indique par l'erreur à l'hote Autre : - Tester sur Linux diff --git a/source/gui/position.py b/source/gui/position.py deleted file mode 100644 index e71de9a..0000000 --- a/source/gui/position.py +++ /dev/null @@ -1,37 +0,0 @@ -from typing import Callable - - -# pourcentage - - -def w_percent(value: float) -> Callable: # positionne en pourcentage la largeur - return lambda widget: int(widget.scene.window.width * (value / 100)) - - -def h_percent(value: float) -> Callable: # positionne en pourcentage la hauteur - return lambda widget: int(widget.scene.window.height * (value / 100)) - - -# pixel - - -def right(px: int) -> Callable: # positionne depuis la droite - return lambda widget: widget.scene.window.width - px - - -def up(px: int) -> Callable: # positionne depuis le haut - return lambda widget: widget.scene.window.height - px - - -def right_content(px: int) -> Callable: # positionne depuis la droite avec la taille du widget compris - return lambda widget: widget.scene.window.width - widget.width - px - - -def up_content(px: int) -> Callable: # positionne depuis le haut avec la taille du widget compris - return lambda widget: widget.scene.window.height - widget.height - px - - -# raccourci - -w_full = w_percent(100) -h_full = h_percent(100) diff --git a/source/gui/position/Unit.py b/source/gui/position/Unit.py new file mode 100644 index 0000000..55eaf31 --- /dev/null +++ b/source/gui/position/Unit.py @@ -0,0 +1,29 @@ +from typing import Callable, TYPE_CHECKING + +from source.gui.position import Value +from source.type import DistanceFunc + +if TYPE_CHECKING: + pass + + +class Unit: + """ + Cette classe représente une unité de position (px, vw, ...). + """ + + def __init__(self, converter: Callable[[float], DistanceFunc]): + self.converter = converter + + def __mul__(self, other: float): # opérateur * + """ + Lorsque que cet object est multiplié avec une valeur, renvoie un objet Value + utilisant le convertisseur de l'unité. + :param other: l'autre élément utilisé dans la multiplication. + :return: l'objet Value + """ + + return Value(self.converter(other)) + + def __rmul__(self, other): # opérateur *, lorsque cet objet est situé à droite de l'opération + return self.__mul__(other) diff --git a/source/gui/position/Value.py b/source/gui/position/Value.py new file mode 100644 index 0000000..5b6771d --- /dev/null +++ b/source/gui/position/Value.py @@ -0,0 +1,47 @@ +from typing import TYPE_CHECKING + +from source.type import DistanceFunc + +if TYPE_CHECKING: + from source.gui.widget.abc import BoxWidget + + +class Value: + """ + Une valeur utilisée pour calculer la position des objets. + Elle utilise une fonction permettant à partir d'un widget, d'obtenir une position dans la fenêtre + """ + + def __init__(self, calc: DistanceFunc): + self.calc = calc # fonction calculant la position depuis la taille d'un widget + + def __add__(self, other) -> "Value": # opérateur + + """ + Créer un nouvel objet Value correspondant à l'addition des deux opérandes + :param other: l'autre élément utilisé dans la multiplication. + :return: le nouvel objet Value + """ + return self.__class__(lambda widget: self.calc(widget) + other.calc(widget)) + + def __radd__(self, other): # opérateur +, lorsque cet objet est situé à droite de l'opération + return self.__add__(other) + + def __sub__(self, other) -> "Value": # opérateur - + """ + Créer un nouvel objet Value correspondant à la soustraction des deux opérandes + :param other: l'autre élément utilisé dans la multiplication. + :return: le nouvel objet Value + """ + return self.__class__(lambda widget: self.calc(widget) - other.calc(widget)) + + def __rsub__(self, other): # opérateur -, lorsque cet objet est situé à droite de l'opération + # similaire à __sub__, mais c'est cette valeur qui est soustraite à l'autre + return self.__class__(lambda widget: other.calc(widget) - self.calc(widget)) + + def __call__(self, widget: "BoxWidget") -> int: # lorsque cet objet est appelé comme une fonction + """ + Calcul pour le widget en paramètre la valeur associé. + :param widget: Le widget à utiliser pour les calculs + :return: la position correspondant à ce widget. + """ + return self.calc(widget) diff --git a/source/gui/position/__init__.py b/source/gui/position/__init__.py new file mode 100644 index 0000000..f84efb2 --- /dev/null +++ b/source/gui/position/__init__.py @@ -0,0 +1,31 @@ +from .Value import Value +from .Unit import Unit + +px = Unit(lambda value: (lambda widget: value)) # PiXel +vw = Unit(lambda value: (lambda widget: int(widget.scene.window.width * (value / 100)))) # Viewport Width +vh = Unit(lambda value: (lambda widget: int(widget.scene.window.height * (value / 100)))) # Viewport Height +ww = Unit(lambda value: (lambda widget: int(widget.width * (value / 100)))) # Widget Width +wh = Unit(lambda value: (lambda widget: int(widget.height * (value / 100)))) # Widget Height + + +vw_full, vh_full = 100*vw, 100*vh +vw_center, vh_center = 50*vw, 50*vh + +ww_full, wh_full = 100*ww, 100*wh +ww_center, wh_center = 50*ww, 50*wh + + +def real_right(value: Value) -> Value: # positionne depuis la droite avec la taille du widget compris + return vw_full - value + + +def real_top(value: Value) -> Value: # positionne depuis le haut avec la taille du widget compris + return vh_full - value + + +def right(value: Value) -> Value: # positionne depuis la droite + return real_right(value) - ww_full + + +def top(value: Value) -> Value: # positionne depuis le haut + return real_top(value) - wh_full diff --git a/source/gui/scene/Game.py b/source/gui/scene/Game.py index 3e5a0e0..5d87bde 100644 --- a/source/gui/scene/Game.py +++ b/source/gui/scene/Game.py @@ -4,7 +4,7 @@ from datetime import datetime, timedelta from pathlib import Path from typing import TYPE_CHECKING, Optional -from source.gui.position import w_percent, h_percent +from source.gui.position import vw, vh from source.path import path_save, path_history from source.core.enums import BombState from source.core.error import InvalidBombPosition, PositionAlreadyShot @@ -50,7 +50,7 @@ class Game(BaseGame): self.chat_log = self.add_widget( widget.Text, - x=10, y=45, width=w_percent(40), + x=10, y=45, width=40*vw, text="", anchor_x="left", anchor_y="bottom", @@ -60,7 +60,7 @@ class Game(BaseGame): self.chat_input = self.add_widget( widget.Input, - x=10, y=10, width=w_percent(50), height=30, + x=10, y=10, width=50*vw, height=30, type_regex=".{0,60}", @@ -79,7 +79,7 @@ class Game(BaseGame): self.button_save = self.add_widget( widget.Button, - x=w_percent(70), y=0, width=w_percent(15), height=h_percent(10), + x=70*vw, y=0, width=15*vw, height=10*vh, label_text="Sauvegarder", @@ -104,7 +104,7 @@ class Game(BaseGame): self.label_state = self.add_widget( widget.Text, - x=w_percent(50), y=h_percent(15), + x=50*vw, y=15*vh, anchor_x="center", diff --git a/source/gui/scene/GameError.py b/source/gui/scene/GameError.py index f77588d..82c591a 100644 --- a/source/gui/scene/GameError.py +++ b/source/gui/scene/GameError.py @@ -1,7 +1,7 @@ from typing import TYPE_CHECKING from source.gui import widget, texture -from source.gui.position import w_full, h_percent, w_percent, right_content +from source.gui.position import vw_full, vw_center, vh_center, right, px, vw, vh from source.gui.scene.abc import Scene @@ -16,7 +16,7 @@ class GameError(Scene): self.label = self.add_widget( widget.Text, - x=w_percent(50), y=h_percent(50), width=w_full, + x=vw_center, y=vh_center, width=vw_full, anchor_x="center", @@ -29,7 +29,7 @@ class GameError(Scene): self.back = self.add_widget( widget.Button, - x=right_content(20), y=20, width=w_percent(20), height=h_percent(10), + x=right(20*px), y=20, width=20*vw, height=10*vh, label_text="Retour", diff --git a/source/gui/scene/GameLoad.py b/source/gui/scene/GameLoad.py index 1c111ca..b19d7d4 100644 --- a/source/gui/scene/GameLoad.py +++ b/source/gui/scene/GameLoad.py @@ -2,7 +2,7 @@ from pathlib import Path from typing import TYPE_CHECKING from source.gui import widget, texture -from source.gui.position import w_percent, w_full, h_percent, right_content +from source.gui.position import vw_full, vw, vh, right, px, vw_center, vh_center from source.gui.scene.abc import Scene from source.network import Host from source.utils import path_ctime_str @@ -20,7 +20,7 @@ class GameLoad(Scene): self.label = self.add_widget( widget.Text, - x=w_percent(50), y=h_percent(50), width=w_full, + x=vw_center, y=vh_center, width=vw_full, anchor_x="center", @@ -35,7 +35,7 @@ class GameLoad(Scene): self.refuse = self.add_widget( widget.Button, - x=20, y=20, width=w_percent(20), height=h_percent(10), + x=20, y=20, width=20*vw, height=10*vh, label_text="Refuser", @@ -47,7 +47,7 @@ class GameLoad(Scene): self.accept = self.add_widget( widget.Button, - x=right_content(20), y=20, width=w_percent(20), height=h_percent(10), + x=right(20*px), y=20, width=20*vw, height=10*vh, label_text="Accepter", diff --git a/source/gui/scene/GameQuit.py b/source/gui/scene/GameQuit.py index 0d80c30..ac6b990 100644 --- a/source/gui/scene/GameQuit.py +++ b/source/gui/scene/GameQuit.py @@ -1,7 +1,7 @@ from typing import TYPE_CHECKING from source.gui import widget, texture -from source.gui.position import w_full, h_full, w_percent, h_percent +from source.gui.position import vw_full, vh_full, vh_center, vw_center, vw, vh from source.gui.scene.abc.Popup import Popup from source.network.packet import PacketQuit @@ -19,7 +19,7 @@ class GameQuit(Popup): self.background = self.add_widget( widget.Image, - x=0, y=0, width=w_full, height=h_full, + x=0, y=0, width=vw_full, height=vh_full, image=texture.Popup.Style1.background ) @@ -27,7 +27,7 @@ class GameQuit(Popup): self.text = self.add_widget( widget.Text, - x=w_percent(50), y=h_percent(50), width=w_full, + x=vw_center, y=vh_center, width=vw_full, anchor_x="center", @@ -39,7 +39,7 @@ class GameQuit(Popup): self.cancel = self.add_widget( widget.Button, - x=w_percent(20), y=h_percent(20), width=w_percent(20), height=h_percent(10), + x=20*vw, y=20*vh, width=20*vw, height=10*vh, label_text="Annuler", @@ -50,7 +50,7 @@ class GameQuit(Popup): self.confirm = self.add_widget( widget.Button, - x=w_percent(60), y=h_percent(20), width=w_percent(20), height=h_percent(10), + x=60*vw, y=20*vh, width=20*vw, height=10*vh, label_text="Confirmer", diff --git a/source/gui/scene/GameResult.py b/source/gui/scene/GameResult.py index 901bc97..2e94953 100644 --- a/source/gui/scene/GameResult.py +++ b/source/gui/scene/GameResult.py @@ -3,7 +3,7 @@ from typing import TYPE_CHECKING import pyglet.clock from source.gui import texture, widget, sound -from source.gui.position import w_full, h_full +from source.gui.position import vw_full, vh_full from source.gui.scene.abc.Popup import Popup if TYPE_CHECKING: @@ -20,7 +20,7 @@ class GameResult(Popup): self.image = self.add_widget( widget.Image, - x=0, y=0, width=w_full, height=h_full, + x=0, y=0, width=vw_full, height=vh_full, image=texture.Result.Style1.get("victory" if won else "defeat") ) diff --git a/source/gui/scene/GameSave.py b/source/gui/scene/GameSave.py index 032a75a..5d75430 100644 --- a/source/gui/scene/GameSave.py +++ b/source/gui/scene/GameSave.py @@ -1,7 +1,7 @@ from typing import TYPE_CHECKING from source.gui import widget, texture -from source.gui.position import h_full, w_full, w_percent, h_percent +from source.gui.position import vh_full, vw_full, vw, vh from source.gui.scene.abc.Popup import Popup from source.network.packet import PacketResponseSave @@ -19,7 +19,7 @@ class GameSave(Popup): self.background = self.add_widget( widget.Image, - x=0, y=0, width=w_full, height=h_full, + x=0, y=0, width=vw_full, height=vh_full, image=texture.Popup.Style1.background ) @@ -27,7 +27,7 @@ class GameSave(Popup): self.text = self.add_widget( widget.Text, - x=w_percent(50), y=h_percent(50), + x=50*vw, y=50*vh, anchor_x="center", @@ -38,7 +38,7 @@ class GameSave(Popup): self.refuse = self.add_widget( widget.Button, - x=w_percent(20), y=h_percent(20), width=w_percent(20), height=h_percent(10), + x=20*vw, y=20*vh, width=20*vw, height=10*vh, label_text="Refuser", @@ -49,7 +49,7 @@ class GameSave(Popup): self.accept = self.add_widget( widget.Button, - x=w_percent(60), y=h_percent(20), width=w_percent(20), height=h_percent(10), + x=60*vw, y=20*vh, width=20*vw, height=10*vh, label_text="Accepter", diff --git a/source/gui/scene/GameWaitLoad.py b/source/gui/scene/GameWaitLoad.py index 0a02cd7..801097e 100644 --- a/source/gui/scene/GameWaitLoad.py +++ b/source/gui/scene/GameWaitLoad.py @@ -2,7 +2,7 @@ from pathlib import Path from typing import TYPE_CHECKING from source.gui import widget -from source.gui.position import w_percent, h_percent, w_full +from source.gui.position import vw_full, vh, vw from source.gui.scene.abc import Scene from source.utils import path_ctime_str @@ -17,7 +17,7 @@ class GameWaitLoad(Scene): self.label = self.add_widget( widget.Text, - x=w_percent(50), y=h_percent(50), width=w_full, + x=50*vw, y=50*vh, width=vw_full, anchor_x="center", diff --git a/source/gui/scene/HistoryGame.py b/source/gui/scene/HistoryGame.py index 9d2c601..bf165bf 100644 --- a/source/gui/scene/HistoryGame.py +++ b/source/gui/scene/HistoryGame.py @@ -3,7 +3,7 @@ from pathlib import Path from typing import TYPE_CHECKING from source.gui import widget, texture -from source.gui.position import h_percent, w_percent +from source.gui.position import vw, vh, vw_center from source.gui.scene.abc import BaseGame if TYPE_CHECKING: @@ -41,7 +41,7 @@ class HistoryGame(BaseGame): self.previous = self.add_widget( widget.Button, - x=w_percent(20), y=h_percent(10), width=w_percent(20), height=h_percent(10), + x=20*vw, y=10*vh, width=20*vw, height=10*vh, label_text="Précédent", @@ -52,7 +52,7 @@ class HistoryGame(BaseGame): self.next = self.add_widget( widget.Button, - x=w_percent(60), y=h_percent(10), width=w_percent(20), height=h_percent(10), + x=60*vw, y=10*vh, width=20*vw, height=10*vh, label_text="Suivant", @@ -63,7 +63,7 @@ class HistoryGame(BaseGame): self.text_move = self.add_widget( widget.Text, - x=w_percent(50), y=h_percent(12), + x=vw_center, y=12*vh, anchor_x="center", diff --git a/source/gui/scene/HistoryMenu.py b/source/gui/scene/HistoryMenu.py index 642b8bc..fd16bff 100644 --- a/source/gui/scene/HistoryMenu.py +++ b/source/gui/scene/HistoryMenu.py @@ -2,7 +2,7 @@ import math from pathlib import Path from typing import TYPE_CHECKING -from source.gui.position import w_percent, h_percent +from source.gui.position import vw, vh, top from source.path import path_history from source.gui import widget, texture from source.gui.scene.abc import Scene @@ -20,7 +20,7 @@ class HistoryMenu(Scene): self.back = self.add_widget( widget.Button, - x=20, y=20, width=w_percent(20), height=h_percent(10), + x=20, y=20, width=20*vw, height=10*vh, label_text="Retour", @@ -37,7 +37,7 @@ class HistoryMenu(Scene): self.title = self.add_widget( widget.Text, - x=w_percent(50), y=h_percent(80), + x=50*vw, y=80*vh, anchor_x="center", @@ -49,7 +49,7 @@ class HistoryMenu(Scene): button = self.add_widget( widget.Button, - x=w_percent(25), y=h_percent(75 - ((i+1) * 9)), width=w_percent(50), height=h_percent(8), + x=25*vw, y=top((25 + (i*9))*vh), width=50*vw, height=8*vh, label_text=path.stem, @@ -66,7 +66,7 @@ class HistoryMenu(Scene): # si nous ne sommes pas à la première page, ajoute un bouton "précédent". self.previous = self.add_widget( widget.Button, - x=w_percent(10), y=h_percent(45), width=w_percent(10), height=h_percent(10), + x=10*vw, y=45*vh, width=10*vw, height=10*vh, label_text="Précédent", @@ -82,7 +82,7 @@ class HistoryMenu(Scene): # si nous ne sommes pas à la dernière page, ajoute un bouton "suivant". self.next = self.add_widget( widget.Button, - x=w_percent(80), y=h_percent(45), width=w_percent(10), height=h_percent(10), + x=80*vw, y=45*vh, width=10*vw, height=10*vh, label_text="Suivant", diff --git a/source/gui/scene/MainMenu.py b/source/gui/scene/MainMenu.py index eb786d8..58c01c4 100644 --- a/source/gui/scene/MainMenu.py +++ b/source/gui/scene/MainMenu.py @@ -1,6 +1,6 @@ from typing import TYPE_CHECKING -from source.gui.position import w_full, h_full, h_percent, w_percent +from source.gui.position import vw_full, vh_full, vw, vh from source.gui.scene.abc import Scene from source.gui import widget, scene, texture @@ -15,7 +15,7 @@ class MainMenu(Scene): self.background = self.add_widget( widget.Image, - x=0, y=0, width=w_full, height=h_full, + x=0, y=0, width=vw_full, height=vh_full, image=texture.Background.main ) @@ -23,7 +23,7 @@ class MainMenu(Scene): self.title = self.add_widget( widget.Text, - x=50, y=h_percent(85), + x=50, y=85*vh, text="Bataille Navale", font_size=50 @@ -31,7 +31,7 @@ class MainMenu(Scene): self.game_create = self.add_widget( widget.Button, - x=50, y=h_percent(50), width=w_percent(30), height=h_percent(10), + x=50, y=50*vh, width=30*vw, height=10*vh, label_text="Créer une salle", label_font_size=20, @@ -44,7 +44,7 @@ class MainMenu(Scene): self.game_join = self.add_widget( widget.Button, - x=50, y=h_percent(35), width=w_percent(30), height=h_percent(10), + x=50, y=35*vh, width=30*vw, height=10*vh, label_text="Rejoindre une salle", label_font_size=20, @@ -57,7 +57,7 @@ class MainMenu(Scene): self.history = self.add_widget( widget.Button, - x=50, y=h_percent(20), width=w_percent(30), height=h_percent(10), + x=50, y=20*vh, width=30*vw, height=10*vh, label_text="Historique", label_font_size=20, @@ -70,7 +70,7 @@ class MainMenu(Scene): self.settings = self.add_widget( widget.Button, - x=50, y=h_percent(5), width=w_percent(30), height=h_percent(10), + x=50, y=5*vh, width=30*vw, height=10*vh, label_text="Paramètres", label_font_size=20, diff --git a/source/gui/scene/RoomCreate.py b/source/gui/scene/RoomCreate.py index dd90743..b9c7652 100644 --- a/source/gui/scene/RoomCreate.py +++ b/source/gui/scene/RoomCreate.py @@ -1,7 +1,7 @@ from typing import TYPE_CHECKING from source.gui import widget, texture, regex -from source.gui.position import w_percent, h_percent, right_content +from source.gui.position import vw, vh, right, px from source.gui.scene import RoomHost from source.gui.scene.abc import Scene from source.network.packet import PacketSettings @@ -16,7 +16,7 @@ class RoomCreate(Scene): self.back = self.add_widget( widget.Button, - x=20, y=20, width=w_percent(20), height=h_percent(10), + x=20, y=20, width=20*vw, height=10*vh, label_text="Retour", @@ -31,7 +31,7 @@ class RoomCreate(Scene): self.add_widget( widget.Text, - x=w_percent(10), y=h_percent(65), + x=10*vw, y=65*vh, anchor_x="center", anchor_y="center", @@ -41,7 +41,7 @@ class RoomCreate(Scene): self.input_port = self.add_widget( widget.Input, - x=w_percent(20), y=h_percent(60), width=w_percent(15), height=h_percent(10), + x=20*vw, y=60*vh, width=15*vw, height=10*vh, style=texture.Input.Style1, @@ -56,7 +56,7 @@ class RoomCreate(Scene): self.add_widget( widget.Text, - x=w_percent(10), y=h_percent(50), + x=10*vw, y=50*vh, anchor_x="center", anchor_y="center", @@ -66,7 +66,7 @@ class RoomCreate(Scene): self.input_username = self.add_widget( widget.Input, - x=w_percent(20), y=h_percent(45), width=w_percent(15), height=h_percent(10), + x=20*vw, y=45*vh, width=15*vw, height=10*vh, type_regex=regex.username_type, check_regex=regex.username_check, @@ -81,7 +81,7 @@ class RoomCreate(Scene): self.add_widget( widget.Text, - x=w_percent(10), y=h_percent(90), + x=10*vw, y=90*vh, anchor_x="center", anchor_y="center", text=f"Largeur de la grille" ) @@ -89,7 +89,7 @@ class RoomCreate(Scene): self.input_width = self.add_widget( widget.Input, - x=w_percent(20), y=h_percent(86), width=w_percent(10), height=h_percent(8), + x=20*vw, y=86*vh, width=10*vw, height=8*vh, type_regex=regex.number(min_length=0, max_length=4), check_regex=regex.number(min_length=1, max_length=4), @@ -102,7 +102,7 @@ class RoomCreate(Scene): self.add_widget( widget.Text, - x=w_percent(10), y=h_percent(80), + x=10*vw, y=80*vh, anchor_x="center", anchor_y="center", text=f"Longueur de la grille" ) @@ -110,7 +110,7 @@ class RoomCreate(Scene): self.input_height = self.add_widget( widget.Input, - x=w_percent(20), y=h_percent(76), width=w_percent(10), height=h_percent(8), + x=20*vw, y=76*vh, width=10*vw, height=8*vh, type_regex=regex.number(min_length=0, max_length=4), check_regex=regex.number(min_length=1, max_length=4), @@ -125,7 +125,7 @@ class RoomCreate(Scene): self.checkbox_host_start = self.add_widget( widget.Checkbox, - x=w_percent(40), y=h_percent(80), width=w_percent(5), height=h_percent(10), + x=40*vw, y=80*vh, width=5*vw, height=10*vh, style=texture.Checkbox.Style1, @@ -135,7 +135,7 @@ class RoomCreate(Scene): self.add_widget( widget.Text, - x=w_percent(46), y=h_percent(85), + x=46*vw, y=85*vh, anchor_y="center", @@ -158,7 +158,7 @@ class RoomCreate(Scene): self.button_boat_size_previous = self.add_widget( widget.Button, - x=w_percent(70), y=h_percent(80), width=w_percent(3), height=h_percent(10), + x=70*vw, y=80*vh, width=3*vw, height=10*vh, label_text="<", label_font_size=25, @@ -176,7 +176,7 @@ class RoomCreate(Scene): self.label_boat_size = self.add_widget( widget.Text, - x=w_percent(80), y=h_percent(85), + x=80*vw, y=85*vh, anchor_x="center", anchor_y="center" ) @@ -184,7 +184,7 @@ class RoomCreate(Scene): self.button_boat_size_next = self.add_widget( widget.Button, - x=w_percent(87), y=h_percent(80), width=w_percent(3), height=h_percent(10), + x=87*vw, y=80*vh, width=3*vw, height=10*vh, label_text=">", label_font_size=25, @@ -201,7 +201,7 @@ class RoomCreate(Scene): self.input_boat_amount = self.add_widget( widget.Input, - x=w_percent(70), y=h_percent(68), width=w_percent(20), height=h_percent(8), + x=70*vw, y=68*vh, width=20*vw, height=8*vh, type_regex=regex.number(min_length=0, max_length=4), check_regex=regex.number(min_length=1, max_length=4), @@ -228,7 +228,7 @@ class RoomCreate(Scene): self.label_boat_recap = self.add_widget( widget.Text, - x=w_percent(70), y=h_percent(60), width=w_percent(20), height=h_percent(10), + x=70*vw, y=60*vh, width=20*vw, height=10*vh, multiline=True ) @@ -239,7 +239,7 @@ class RoomCreate(Scene): self.start = self.add_widget( widget.Button, - x=right_content(20), y=20, width=w_percent(20), height=h_percent(10), + x=right(20*px), y=20, width=20*vw, height=10*vh, label_text="Continuer", diff --git a/source/gui/scene/RoomHost.py b/source/gui/scene/RoomHost.py index 6a448b7..df7ecdb 100644 --- a/source/gui/scene/RoomHost.py +++ b/source/gui/scene/RoomHost.py @@ -3,7 +3,7 @@ from typing import TYPE_CHECKING import requests from source import network -from source.gui.position import w_percent, h_percent +from source.gui.position import vw, vh from source.gui.scene.abc import Scene from source.gui import widget, texture from source.utils.thread import in_pyglet_context, StoppableThread @@ -23,7 +23,7 @@ class RoomHost(Scene): self.back = self.add_widget( widget.Button, - x=20, y=20, width=w_percent(20), height=h_percent(10), + x=20, y=20, width=20*vw, height=10*vh, label_text="Retour", @@ -35,7 +35,7 @@ class RoomHost(Scene): self.label_ip = self.add_widget( widget.Text, - x=w_percent(50), y=h_percent(55), + x=50*vw, y=55*vh, anchor_x="center", anchor_y="center", font_size=20 @@ -44,7 +44,7 @@ class RoomHost(Scene): self.description = self.add_widget( widget.Text, - x=w_percent(50), y=h_percent(45), + x=50*vw, y=45*vh, anchor_x="center", anchor_y="center", text="En attente d'un second joueur..." diff --git a/source/gui/scene/RoomJoin.py b/source/gui/scene/RoomJoin.py index 493347c..ba72d18 100644 --- a/source/gui/scene/RoomJoin.py +++ b/source/gui/scene/RoomJoin.py @@ -1,7 +1,7 @@ from typing import TYPE_CHECKING, Optional from source import network -from source.gui.position import w_percent, h_percent +from source.gui.position import vw, vh from source.gui.scene.abc import Scene from source.gui import widget, texture, regex @@ -15,7 +15,7 @@ class RoomJoin(Scene): self.back = self.add_widget( widget.Button, - x=20, y=20, width=w_percent(20), height=h_percent(10), + x=20, y=20, width=20*vw, height=10*vh, label_text="Retour", @@ -28,7 +28,7 @@ class RoomJoin(Scene): self.entry_username = self.add_widget( widget.Input, - x=w_percent(40), y=h_percent(55), width=w_percent(20), height=h_percent(10), + x=40*vw, y=55*vh, width=20*vw, height=10*vh, type_regex=regex.username_type, check_regex=regex.username_check, @@ -42,7 +42,7 @@ class RoomJoin(Scene): self.entry_ip = self.add_widget( widget.Input, - x=w_percent(40), y=h_percent(45), width=w_percent(13), height=h_percent(10), + x=40*vw, y=45*vh, width=13*vw, height=10*vh, type_regex=regex.ipv4_type, check_regex=regex.ipv4_check, @@ -54,7 +54,7 @@ class RoomJoin(Scene): self.entry_port = self.add_widget( widget.Input, - x=w_percent(53), y=h_percent(45), width=w_percent(7), height=h_percent(10), + x=53*vw, y=45*vh, width=7*vw, height=10*vh, type_regex=regex.port_type, check_regex=regex.port_check, @@ -66,7 +66,7 @@ class RoomJoin(Scene): self.connect = self.add_widget( widget.Button, - x=w_percent(40), y=h_percent(35), width=w_percent(20), height=h_percent(10), + x=40*vw, y=35*vh, width=20*vw, height=10*vh, label_text="Se connecter", @@ -77,7 +77,7 @@ class RoomJoin(Scene): self.status = self.add_widget( widget.Text, - x=w_percent(50), y=h_percent(25), + x=50*vw, y=25*vh, anchor_x="center", ) diff --git a/source/gui/scene/Settings.py b/source/gui/scene/Settings.py index a178f7d..38cfc03 100644 --- a/source/gui/scene/Settings.py +++ b/source/gui/scene/Settings.py @@ -1,7 +1,7 @@ from typing import TYPE_CHECKING from source.gui import widget, texture -from source.gui.position import w_full, h_full, w_percent, h_percent +from source.gui.position import vw_full, vh_full, vw, vh from source.gui.scene.abc.Popup import Popup if TYPE_CHECKING: @@ -15,14 +15,14 @@ class Settings(Popup): self.background = self.add_widget( widget.Image, - x=0, y=0, width=w_full, height=h_full, + x=0, y=0, width=vw_full, height=vh_full, image=texture.Popup.Style1.background ) self.back = self.add_widget( widget.Button, - x=20, y=20, width=w_percent(20), height=h_percent(10), + x=20, y=20, width=20*vw, height=10*vh, label_text="Retour", @@ -34,7 +34,7 @@ class Settings(Popup): self.checkbox = self.add_widget( widget.Checkbox, - x=w_percent(45), y=h_percent(45), width=w_percent(10), height=h_percent(10), + x=45*vw, y=45*vh, width=10*vw, height=10*vh, style=texture.Checkbox.Style1 ) @@ -45,7 +45,7 @@ class Settings(Popup): self.scroller = self.add_widget( widget.Scroller, - x=w_percent(30), y=h_percent(20), width=w_percent(30), height=h_percent(10), + x=30*vw, y=20*vh, width=30*vw, height=10*vh, style=texture.Scroller.Style1, diff --git a/source/gui/scene/abc/BaseGame.py b/source/gui/scene/abc/BaseGame.py index 3ac7c1c..6773259 100644 --- a/source/gui/scene/abc/BaseGame.py +++ b/source/gui/scene/abc/BaseGame.py @@ -2,7 +2,7 @@ from abc import ABC from typing import TYPE_CHECKING from source.gui import widget, texture -from source.gui.position import right_content, h_percent, w_percent, w_full, h_full +from source.gui.position import right, vw, vh, vw_full, vh_full, px from source.gui.scene.abc import Scene from source.type import Point2D @@ -35,7 +35,7 @@ class BaseGame(Scene, ABC): self.background = self.add_widget( widget.Image, - x=0, y=0, width=w_full, height=h_full, + x=0, y=0, width=vw_full, height=vh_full, image=texture.Background.game, ) @@ -43,7 +43,7 @@ class BaseGame(Scene, ABC): self.grid_ally = self.add_widget( widget.GameGrid, - x=75, y=h_percent(25), width=w_percent(35), height=h_percent(50), + x=75, y=25*vh, width=35*vw, height=50*vh, boats_length=self.boats_length, @@ -57,7 +57,7 @@ class BaseGame(Scene, ABC): self.grid_enemy = self.add_widget( widget.GameGrid, - x=right_content(75), y=h_percent(25), width=w_percent(35), height=h_percent(50), + x=right(75*px), y=25*vh, width=35*vw, height=50*vh, grid_style=texture.Grid.Style1, boat_style=texture.Grid.Boat.Style1, @@ -69,7 +69,7 @@ class BaseGame(Scene, ABC): self.add_widget( widget.Text, - x=w_percent(27), y=h_percent(99.5), + x=27*vw, y=99.5*vh, text=self.name_ally, font_size=20, @@ -79,7 +79,7 @@ class BaseGame(Scene, ABC): self.add_widget( widget.Text, - x=w_percent(73), y=h_percent(99.5), + x=73*vw, y=99.5*vh, text=self.name_enemy, font_size=20, @@ -89,7 +89,7 @@ class BaseGame(Scene, ABC): self.score_ally = self.add_widget( widget.Text, - x=w_percent(44), y=h_percent(99.5), + x=44*vw, y=99.5*vh, text="0", font_size=25, @@ -99,7 +99,7 @@ class BaseGame(Scene, ABC): self.score_enemy = self.add_widget( widget.Text, - x=w_percent(56), y=h_percent(99.5), + x=56*vw, y=99.5*vh, text="0", font_size=25, @@ -109,7 +109,7 @@ class BaseGame(Scene, ABC): self.button_quit = self.add_widget( widget.Button, - x=w_percent(85), y=0, width=w_percent(15), height=h_percent(10), + x=85*vw, y=0, width=15*vw, height=10*vh, label_text="Quitter", diff --git a/source/gui/widget/abc/BoxWidget.py b/source/gui/widget/abc/BoxWidget.py index 4a847ad..16649f7 100644 --- a/source/gui/widget/abc/BoxWidget.py +++ b/source/gui/widget/abc/BoxWidget.py @@ -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 Distance, Percentage +from source.type import Distance from source.utils import in_bbox if TYPE_CHECKING: @@ -33,15 +33,13 @@ class BoxWidget(Widget, ABC): # property - def _getter_distance(self, max_distance: int, raw_distance: Distance) -> int: + def _getter_distance(self, 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 @@ -50,7 +48,7 @@ class BoxWidget(Widget, ABC): @property def x(self) -> int: - return self._getter_distance(self.scene.window.width, self._x) + return self._getter_distance(self._x) @x.setter def x(self, x: Distance): @@ -58,7 +56,7 @@ class BoxWidget(Widget, ABC): @property def y(self) -> int: - return self._getter_distance(self.scene.window.height, self._y) + return self._getter_distance(self._y) @y.setter def y(self, y: Distance): @@ -82,7 +80,7 @@ class BoxWidget(Widget, ABC): @property def width(self) -> int: - return self._getter_distance(self.scene.window.width, self._width) + return self._getter_distance(self._width) @width.setter def width(self, width: Optional[Distance]): @@ -90,7 +88,7 @@ class BoxWidget(Widget, ABC): @property def height(self) -> int: - return self._getter_distance(self.scene.window.height, self._height) + return self._getter_distance(self._height) @height.setter def height(self, height: Optional[Distance]): diff --git a/source/type.py b/source/type.py index 2dc0786..e44e637 100644 --- a/source/type.py +++ b/source/type.py @@ -1,10 +1,10 @@ -from typing import Union, Callable, Any +from typing import Union, Callable + Point2D = tuple[int, int] # a 2D point BBox = tuple[int, int, int, int] # a boundary box -Percentage = float # a percentage, represented as a number between 0 and 1 ColorRGB = tuple[int, int, int] # a RGB Color ColorRGBA = tuple[int, int, int, int] # a RGBA Color -DistanceFunction = Callable[[Any], int] # a function that return a distance -Distance = Union[int, Percentage, DistanceFunction] # a distance, represented by a number, a percentage or a function +DistanceFunc = Callable[["BoxWidget"], int] # a function that return a position / distance +Distance = Union[int, DistanceFunc] # a position / distance, represented by a number or a function