diff --git a/main.pyw b/main.pyw index 73d06ad..36c8cf5 100644 --- a/main.pyw +++ b/main.pyw @@ -1,98 +1,8 @@ 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 -# Test Scene - - -class TestScene(Scene): - def __init__(self, window: "Window"): - super().__init__(window) - - # loading resource - - 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") - - texture_atlas = pyglet.image.atlas.TextureAtlas() - region_button_normal = texture_atlas.add(texture_button_normal) - region_button_hover = texture_atlas.add(texture_button_hover) - region_button_click = texture_atlas.add(texture_button_click) - region_input_normal = texture_atlas.add(texture_input_normal) - region_input_active = texture_atlas.add(texture_input_active) - region_input_error = texture_atlas.add(texture_input_error) - - self.background_batch = pyglet.graphics.Batch() - self.label_batch = pyglet.graphics.Batch() - - # the widgets - - self.fps_display = self.add_widget(FPSDisplay, color=(255, 255, 255, 127)) - - background = self.add_widget( - Image, - - x=0, y=0, width=1, height=1, - - image=region_input_normal, - batch=self.background_batch, - ) - - button = self.add_widget( - Button, - - x=0.5, y=0.5, width=0.5, height=0.5, - - texture_normal=region_button_normal, - texture_hover=region_button_hover, - texture_click=region_button_click, - - label_text="Hello World !", - - background_batch=self.background_batch, - label_batch=self.label_batch, - ) - - button.on_pressed = lambda button, modifiers: "pass" - button.on_release = lambda button, modifiers: window.set_scene(TestScene2) - - input_ = self.add_widget( - Input, - - x=0.1, y=0.2, width=0.4, height=0.1, - - texture_normal=region_input_normal, - texture_active=region_input_active, - texture_error=region_input_error, - - # 4 numéros de 1 à 3 chiffres séparés par des points (IP), optionnellement suivi - # de deux points ainsi que de 1 à 5 chiffres (port) - regex=r"\d{1,3}(\.\d{1,3}){3}(:\d{1,5})?", - - background_batch=self.background_batch, - label_batch=self.label_batch, - ) - - def on_draw(self): - self.background_batch.draw() - self.label_batch.draw() - self.fps_display.draw() - - -class TestScene2(Scene): - def __init__(self, window: "Window"): - super().__init__(window) - - def on_draw(self): - self.window.clear() - # Create a new window window = Window(resizable=True, vsync=True) diff --git a/source/gui/scene/MainMenu.py b/source/gui/scene/MainMenu.py index 1a0dc90..9ec521e 100644 --- a/source/gui/scene/MainMenu.py +++ b/source/gui/scene/MainMenu.py @@ -2,9 +2,9 @@ from typing import TYPE_CHECKING import pyglet -from source.gui.scene import RoomJoin, RoomCreate +from source.gui.scene import RoomJoin, RoomCreate, Settings from source.gui.scene.abc import Scene -from source.gui.widget import Image, Text, Button +from source.gui.widget import Image, Text, Button, FPSDisplay if TYPE_CHECKING: from source.gui.window import Window @@ -27,7 +27,7 @@ class MainMenu(Scene): self.title = self.add_widget( Text, - x=0.05, y=0.85, + x=50, y=0.85, text="Bataille Navale", font_size=50 ) @@ -35,7 +35,7 @@ class MainMenu(Scene): self.game_create = self.add_widget( Button, - x=0.05, y=0.3, width=0.3, height=0.1, + x=50, y=0.45, width=0.3, height=0.1, label_text="Créer une salle", label_font_size=20, @@ -50,7 +50,7 @@ class MainMenu(Scene): self.game_join = self.add_widget( Button, - x=0.05, y=0.15, width=0.3, height=0.1, + x=50, y=0.3, width=0.3, height=0.1, label_text="Rejoindre une salle", label_font_size=20, @@ -62,8 +62,28 @@ class MainMenu(Scene): self.game_join.on_release = lambda button, modifiers: self.window.set_scene(RoomJoin) + self.settings = self.add_widget( + Button, + + x=50, y=0.15, width=0.3, height=0.1, + + label_text="Paramètres", + label_font_size=20, + + texture_normal=texture_button_normal, + texture_hover=texture_button_hover, + texture_click=texture_button_click + ) + + self.settings.on_release = lambda button, modifiers: self.window.set_scene(Settings) + + self.fps_display = self.add_widget(FPSDisplay, color=(255, 255, 255, 180)) + def on_draw(self): self.background.draw() self.title.draw() self.game_create.draw() self.game_join.draw() + self.settings.draw() + + self.fps_display.draw() diff --git a/source/gui/scene/RoomCreate.py b/source/gui/scene/RoomCreate.py index f080a19..3fee1d7 100644 --- a/source/gui/scene/RoomCreate.py +++ b/source/gui/scene/RoomCreate.py @@ -25,7 +25,7 @@ class RoomCreate(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", diff --git a/source/gui/scene/Settings.py b/source/gui/scene/Settings.py new file mode 100644 index 0000000..f1f4f94 --- /dev/null +++ b/source/gui/scene/Settings.py @@ -0,0 +1,29 @@ +from typing import TYPE_CHECKING + +import pyglet + +from source.gui.scene.abc import Scene +from source.gui.widget import Checkbox + +if TYPE_CHECKING: + from source.gui.window import Window + + +class Settings(Scene): + def __init__(self, window: "Window", *args, **kwargs): + super().__init__(window, *args, **kwargs) + + texture_tick_disabled = pyglet.image.load("./assets/image/checkbox/disabled.png") + texture_tick_enabled = pyglet.image.load("./assets/image/checkbox/enabled.png") + + self.checkbox = self.add_widget( + Checkbox, + + x=0.45, y=0.45, width=0.1, height=0.1, + + texture_disabled=texture_tick_disabled, + texture_enabled=texture_tick_enabled, + ) + + def on_draw(self): + self.checkbox.draw() diff --git a/source/gui/scene/__init__.py b/source/gui/scene/__init__.py index 0e4be90..1a6051f 100644 --- a/source/gui/scene/__init__.py +++ b/source/gui/scene/__init__.py @@ -1,4 +1,5 @@ from .RoomCreate import RoomCreate from .RoomJoin import RoomJoin +from .Settings import Settings from .MainMenu import MainMenu diff --git a/source/gui/widget/Button.py b/source/gui/widget/Button.py index a13b44c..d2d7bf9 100644 --- a/source/gui/widget/Button.py +++ b/source/gui/widget/Button.py @@ -4,7 +4,7 @@ import pyglet from source.gui.sprite import Sprite from source.gui.widget.abc import BoxWidget -from source.type import Percentage +from source.type import Distance from source.utils import dict_prefix if TYPE_CHECKING: @@ -21,10 +21,10 @@ class Button(BoxWidget): texture_normal: pyglet.image.AbstractImage, - x: Percentage = 0, - y: Percentage = 0, - width: Percentage = None, - height: Percentage = None, + x: Distance = 0, + y: Distance = 0, + width: Distance = None, + height: Distance = None, texture_hover: pyglet.image.AbstractImage = None, texture_click: pyglet.image.AbstractImage = None, diff --git a/source/gui/widget/Checkbox.py b/source/gui/widget/Checkbox.py new file mode 100644 index 0000000..3b71e70 --- /dev/null +++ b/source/gui/widget/Checkbox.py @@ -0,0 +1,73 @@ +from typing import TYPE_CHECKING + +import pyglet.image + +from source.gui.sprite import Sprite +from source.gui.widget.abc import BoxWidget +from source.type import Distance + + +if TYPE_CHECKING: + from source.gui.scene.abc import Scene + + +class Checkbox(BoxWidget): + def __init__(self, scene: "Scene", + + texture_disabled: pyglet.image.AbstractImage, + texture_enabled: pyglet.image.AbstractImage, + + x: Distance = 0, + y: Distance = 0, + width: Distance = None, + height: Distance = None, + + state: bool = False, + + **kwargs): + super().__init__(scene, x, y, width, height) + + self._texture_disabled = texture_disabled + self._texture_enabled = texture_enabled + + self.tick = Sprite(img=self._texture_disabled, **kwargs) + + self.state = state + + self._refresh_size() + + # refreshing + + @property + def tick_texture(self): + return self._texture_enabled if self.state else self._texture_disabled + + def _refresh_tick(self): + self.tick.image = self.tick_texture + + def _refresh_size(self): + self.tick.x, self.tick.y = self.x, self.y + self.tick.width, self.tick.height = self.width, self.height + + # property + + @property + def state(self): + return self._state + + @state.setter + def state(self, state: bool): + self._state = state + self._refresh_tick() + + # event + + def on_resize(self, width: int, height: int): + self._refresh_size() + + def on_release(self, button: int, modifiers: int): + # lorsque le bouton est enclenché, inverse son état + self.state = not self.state + + def draw(self): + self.tick.draw() diff --git a/source/gui/widget/FPSDisplay.py b/source/gui/widget/FPSDisplay.py index 5ce6726..015a7f5 100644 --- a/source/gui/widget/FPSDisplay.py +++ b/source/gui/widget/FPSDisplay.py @@ -5,7 +5,7 @@ from typing import TYPE_CHECKING import pyglet if TYPE_CHECKING: - from source.gui.scene import Scene + from source.gui.scene.abc import Scene class FPSDisplay(Widget): diff --git a/source/gui/widget/Image.py b/source/gui/widget/Image.py index ad517b7..95a1b9c 100644 --- a/source/gui/widget/Image.py +++ b/source/gui/widget/Image.py @@ -4,8 +4,7 @@ import pyglet.image from source.gui.sprite import Sprite from source.gui.widget.abc import BoxWidget -from source.type import Percentage - +from source.type import Distance if TYPE_CHECKING: from source.gui.scene.abc import Scene @@ -16,10 +15,10 @@ class Image(BoxWidget): image: pyglet.image.AbstractImage, - x: Percentage = 0, - y: Percentage = 0, - width: Percentage = None, - height: Percentage = None, + x: Distance = 0, + y: Distance = 0, + width: Distance = None, + height: Distance = None, *args, **kwargs): super().__init__(scene, x, y, width, height) diff --git a/source/gui/widget/Input.py b/source/gui/widget/Input.py index c00cf05..042d518 100644 --- a/source/gui/widget/Input.py +++ b/source/gui/widget/Input.py @@ -5,7 +5,7 @@ import pyglet.image from source.gui.sprite import Sprite from source.gui.widget.abc import BoxWidget -from source.type import Percentage +from source.type import Distance from source.utils import dict_prefix if TYPE_CHECKING: @@ -21,10 +21,10 @@ class Input(BoxWidget): regex: Optional[str | re.Pattern] = None, - x: Percentage = 0, - y: Percentage = 0, - width: Percentage = None, - height: Percentage = None, + x: Distance = 0, + y: Distance = 0, + width: Distance = None, + height: Distance = None, *args, **kwargs): super().__init__(scene, x, y, width, height) diff --git a/source/gui/widget/Text.py b/source/gui/widget/Text.py index 2f1d215..505300e 100644 --- a/source/gui/widget/Text.py +++ b/source/gui/widget/Text.py @@ -4,7 +4,7 @@ from source.gui.widget.abc import BoxWidget import pyglet -from source.type import Percentage +from source.type import Distance if TYPE_CHECKING: from source.gui.scene.abc import Scene @@ -16,10 +16,10 @@ class Text(BoxWidget): """ 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, *args, **kwargs): super().__init__(scene, x, y, width, height) diff --git a/source/gui/widget/__init__.py b/source/gui/widget/__init__.py index b0432a8..86070be 100644 --- a/source/gui/widget/__init__.py +++ b/source/gui/widget/__init__.py @@ -3,3 +3,4 @@ from .FPSDisplay import FPSDisplay from .Button import Button from .Input import Input from .Image import Image +from .Checkbox import Checkbox