added a Checkbox widget
This commit is contained in:
parent
2dfc21b8ef
commit
928d35b97b
12 changed files with 151 additions and 118 deletions
90
main.pyw
90
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)
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -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",
|
||||
|
||||
|
|
29
source/gui/scene/Settings.py
Normal file
29
source/gui/scene/Settings.py
Normal file
|
@ -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()
|
|
@ -1,4 +1,5 @@
|
|||
from .RoomCreate import RoomCreate
|
||||
from .RoomJoin import RoomJoin
|
||||
from .Settings import Settings
|
||||
|
||||
from .MainMenu import MainMenu
|
||||
|
|
|
@ -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,
|
||||
|
|
73
source/gui/widget/Checkbox.py
Normal file
73
source/gui/widget/Checkbox.py
Normal file
|
@ -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()
|
|
@ -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):
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
|
@ -3,3 +3,4 @@ from .FPSDisplay import FPSDisplay
|
|||
from .Button import Button
|
||||
from .Input import Input
|
||||
from .Image import Image
|
||||
from .Checkbox import Checkbox
|
||||
|
|
Loading…
Reference in a new issue