diff --git a/assets/image/background/main.jpeg b/assets/image/background/main.jpeg new file mode 100644 index 0000000..930150e Binary files /dev/null and b/assets/image/background/main.jpeg differ diff --git a/assets/image/input/inputbox.png b/assets/image/input/normal.png similarity index 100% rename from assets/image/input/inputbox.png rename to assets/image/input/normal.png diff --git a/main.pyw b/main.pyw index 7c948fb..3e431d1 100644 --- a/main.pyw +++ b/main.pyw @@ -1,5 +1,6 @@ import pyglet +from source.gui.scene import MainMenu from source.gui.scene.abc import Scene from source.gui.widget import FPSDisplay, Button, Image, Input from source.gui.window import Window @@ -16,7 +17,7 @@ class TestScene(Scene): texture_button_normal = pyglet.image.load("./assets/image/button/normal.png") texture_button_hover = pyglet.image.load("./assets/image/button/hovering.png") texture_button_click = pyglet.image.load("./assets/image/button/clicking.png") - texture_input_normal = pyglet.image.load("./assets/image/input/inputbox.png") + texture_input_normal = pyglet.image.load("assets/image/input/normal.png") texture_input_active = pyglet.image.load("./assets/image/input/active.png") texture_input_error = pyglet.image.load("./assets/image/input/error.png") @@ -95,7 +96,7 @@ class TestScene2(Scene): # Create a new window window = Window(resizable=True, vsync=False) -window.add_scene(TestScene) +window.add_scene(MainMenu) # Start the event loop pyglet.app.run(interval=0) diff --git a/source/gui/scene/MainMenu.py b/source/gui/scene/MainMenu.py new file mode 100644 index 0000000..72c7765 --- /dev/null +++ b/source/gui/scene/MainMenu.py @@ -0,0 +1,69 @@ +from typing import TYPE_CHECKING + +import pyglet + +from source.gui.scene import RoomJoin, RoomCreate +from source.gui.scene.abc import Scene +from source.gui.widget import Image, Text, Button + +if TYPE_CHECKING: + from source.gui.window import Window + + +class MainMenu(Scene): + def __init__(self, window: "Window", *args, **kwargs): + super().__init__(window, *args, **kwargs) + + texture_background = pyglet.image.load("./assets/image/background/main.jpeg") + texture_button_normal = pyglet.image.load("./assets/image/button/normal.png") + texture_button_hover = pyglet.image.load("./assets/image/button/hovering.png") + texture_button_click = pyglet.image.load("./assets/image/button/clicking.png") + + self.background = self.add_widget( + Image, + x=0, y=0, width=1, height=1, + image=texture_background + ) + + self.title = self.add_widget( + Text, + x=0.05, y=0.85, + text="Bataille Navale", + font_size=50 + ) + + self.game_create = self.add_widget( + Button, + + x=0.05, y=0.3, width=0.3, height=0.1, + + label_text="Créer une salle", + label_font_size=20, + + texture_normal=texture_button_normal, + texture_hover=texture_button_hover, + texture_click=texture_button_click + ) + + self.game_create.on_release = lambda button, modifiers: self.window.set_scene(RoomCreate) + + self.game_join = self.add_widget( + Button, + + x=0.05, y=0.15, width=0.3, height=0.1, + + label_text="Rejoindre une salle", + label_font_size=20, + + texture_normal=texture_button_normal, + texture_hover=texture_button_hover, + texture_click=texture_button_click + ) + + self.game_join.on_release = lambda button, modifiers: self.window.set_scene(RoomJoin) + + def on_draw(self): + self.background.draw() + self.title.draw() + self.game_create.draw() + self.game_join.draw() diff --git a/source/gui/scene/RoomCreate.py b/source/gui/scene/RoomCreate.py new file mode 100644 index 0000000..f080a19 --- /dev/null +++ b/source/gui/scene/RoomCreate.py @@ -0,0 +1,58 @@ +from typing import TYPE_CHECKING + +import pyglet +import requests + +from source.gui.scene.abc import Scene +from source.gui.widget import Text, Button + +if TYPE_CHECKING: + from source.gui.window import Window + + +class RoomCreate(Scene): + def __init__(self, window: "Window", *args, **kwargs): + super().__init__(window, *args, **kwargs) + + r = requests.get('https://api.ipify.org') + r.raise_for_status() + ip_address: str = r.content.decode('utf8') + port: str = "6464" + + texture_button_normal = pyglet.image.load("./assets/image/button/normal.png") + texture_button_hover = pyglet.image.load("./assets/image/button/hovering.png") + texture_button_click = pyglet.image.load("./assets/image/button/clicking.png") + + self.back = self.add_widget( + Button, + x=0.02, y=0.02, width=0.2, height=0.1, + + label_text="Retour", + + texture_normal=texture_button_normal, + texture_hover=texture_button_hover, + texture_click=texture_button_click + ) + + from source.gui.scene import MainMenu + self.back.on_release = lambda button, modifiers: self.window.set_scene(MainMenu) + + self.label_ip = self.add_widget( + Text, + x=0.5, y=0.55, + anchor_x="center", anchor_y="center", + text=f"Votre IP - {ip_address}:{port}", + font_size=20 + ) + + self.description = self.add_widget( + Text, + x=0.5, y=0.45, + anchor_x="center", anchor_y="center", + text="En attente d'un second joueur..." + ) + + def on_draw(self): + self.back.draw() + self.label_ip.draw() + self.description.draw() diff --git a/source/gui/scene/RoomJoin.py b/source/gui/scene/RoomJoin.py new file mode 100644 index 0000000..1c7a7a0 --- /dev/null +++ b/source/gui/scene/RoomJoin.py @@ -0,0 +1,75 @@ +from typing import TYPE_CHECKING + +import pyglet + +from source.gui.scene.abc import Scene +from source.gui.widget import Input, Button + +if TYPE_CHECKING: + from source.gui.window import Window + + +class RoomJoin(Scene): + def __init__(self, window: "Window", *args, **kwargs): + super().__init__(window, *args, **kwargs) + + texture_button_normal = pyglet.image.load("./assets/image/button/normal.png") + texture_button_hover = pyglet.image.load("./assets/image/button/hovering.png") + texture_button_click = pyglet.image.load("./assets/image/button/clicking.png") + + texture_input_normal = pyglet.image.load("assets/image/input/normal.png") + texture_input_active = pyglet.image.load("./assets/image/input/active.png") + texture_input_error = pyglet.image.load("./assets/image/input/error.png") + + self.back = self.add_widget( + Button, + x=0.02, y=0.02, width=0.2, height=0.1, + + label_text="Retour", + + texture_normal=texture_button_normal, + texture_hover=texture_button_hover, + texture_click=texture_button_click + ) + + from source.gui.scene import MainMenu + self.back.on_release = lambda button, modifiers: self.window.set_scene(MainMenu) + + self.entry_ip = self.add_widget( + Input, + x=0.4, y=0.5, width=0.13, height=0.1, + + regex=r"\d{1,3}(.\d{1,3}){3}", + + texture_normal=texture_input_normal, + texture_active=texture_input_active, + texture_error=texture_input_error + ) + + self.entry_port = self.add_widget( + Input, + x=0.53, y=0.5, width=0.07, height=0.1, + + regex=r"\d{0,5}", + + texture_normal=texture_input_normal, + texture_active=texture_input_active, + texture_error=texture_input_error + ) + + self.connect = self.add_widget( + Button, + x=0.4, y=0.4, width=0.2, height=0.1, + + label_text="Se connecter", + + texture_normal=texture_button_normal, + texture_hover=texture_button_hover, + texture_click=texture_button_click + ) + + def on_draw(self): + self.back.draw() + self.entry_ip.draw() + self.entry_port.draw() + self.connect.draw() diff --git a/source/gui/scene/__init__.py b/source/gui/scene/__init__.py index e69de29..0e4be90 100644 --- a/source/gui/scene/__init__.py +++ b/source/gui/scene/__init__.py @@ -0,0 +1,4 @@ +from .RoomCreate import RoomCreate +from .RoomJoin import RoomJoin + +from .MainMenu import MainMenu diff --git a/source/gui/widget/Input.py b/source/gui/widget/Input.py index 174520f..c00cf05 100644 --- a/source/gui/widget/Input.py +++ b/source/gui/widget/Input.py @@ -46,6 +46,8 @@ class Input(BoxWidget): **dict_prefix("label_", kwargs) ) + self._refresh_size() + # background @property @@ -92,20 +94,28 @@ class Input(BoxWidget): self._invalid = invalid self._refresh_background() + @property + def text(self): + return self.label.text + + @text.setter + def text(self, text: str): + self.label.text = text + # event def on_key_press(self, symbol: int, modifiers: int): if not self.activated: return # ignore si ce widget est désactivé / non sélectionné if symbol == pyglet.window.key.BACKSPACE: # si la touche "supprimé" est enfoncé - self.label.text = self.label.text[0:-1] # retire le dernier caractère du texte + self.text = self.text[0:-1] # retire le dernier caractère du texte def on_text(self, char: str): if not self.activated: return # ignore si ce widget est désactivé / non sélectionné - self.label.text += char # ajoute le caractère au label + self.text += char # ajoute le caractère au label if self.regex is not None: # si il y a un regex de validation, applique le pour vérifier le texte - self.invalid = self.regex.fullmatch(self.label.text) is None + self.invalid = self.regex.fullmatch(self.text) is None def on_resize(self, width: int, height: int): self._refresh_size() diff --git a/source/gui/widget/abc/BoxWidget.py b/source/gui/widget/abc/BoxWidget.py index 7ea4503..3884cc6 100644 --- a/source/gui/widget/abc/BoxWidget.py +++ b/source/gui/widget/abc/BoxWidget.py @@ -51,7 +51,7 @@ class BoxWidget(Widget, ABC): @property def width(self) -> int: - return None if self._p_width is None else self.scene.window.width * self._p_width + return 0 if self._p_width is None else self.scene.window.width * self._p_width @width.setter def width(self, width: Optional[Percentage]): @@ -59,7 +59,7 @@ class BoxWidget(Widget, ABC): @property def height(self) -> int: - return None if self._p_height is None else self.scene.window.height * self._p_height + return 0 if self._p_height is None else self.scene.window.height * self._p_height @height.setter def height(self, height: Optional[Percentage]): diff --git a/source/gui/window/Window.py b/source/gui/window/Window.py index 2d63f7f..1f07740 100644 --- a/source/gui/window/Window.py +++ b/source/gui/window/Window.py @@ -5,7 +5,7 @@ import pyglet if TYPE_CHECKING: - from source.gui.scene import Scene + from source.gui.scene.abc import Scene class Window(pyglet.window.Window): # NOQA