added a RoomCreate and a RoomJoin scene

This commit is contained in:
Faraphel 2023-02-14 23:02:44 +01:00
parent 3db2ea3c9a
commit e5ce93a207
10 changed files with 225 additions and 8 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 60 KiB

View file

Before

Width:  |  Height:  |  Size: 7.9 KiB

After

Width:  |  Height:  |  Size: 7.9 KiB

View file

@ -1,5 +1,6 @@
import pyglet import pyglet
from source.gui.scene import MainMenu
from source.gui.scene.abc import Scene from source.gui.scene.abc import Scene
from source.gui.widget import FPSDisplay, Button, Image, Input from source.gui.widget import FPSDisplay, Button, Image, Input
from source.gui.window import Window 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_normal = pyglet.image.load("./assets/image/button/normal.png")
texture_button_hover = pyglet.image.load("./assets/image/button/hovering.png") texture_button_hover = pyglet.image.load("./assets/image/button/hovering.png")
texture_button_click = pyglet.image.load("./assets/image/button/clicking.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_active = pyglet.image.load("./assets/image/input/active.png")
texture_input_error = pyglet.image.load("./assets/image/input/error.png") texture_input_error = pyglet.image.load("./assets/image/input/error.png")
@ -95,7 +96,7 @@ class TestScene2(Scene):
# Create a new window # Create a new window
window = Window(resizable=True, vsync=False) window = Window(resizable=True, vsync=False)
window.add_scene(TestScene) window.add_scene(MainMenu)
# Start the event loop # Start the event loop
pyglet.app.run(interval=0) pyglet.app.run(interval=0)

View file

@ -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()

View file

@ -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()

View file

@ -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()

View file

@ -0,0 +1,4 @@
from .RoomCreate import RoomCreate
from .RoomJoin import RoomJoin
from .MainMenu import MainMenu

View file

@ -46,6 +46,8 @@ class Input(BoxWidget):
**dict_prefix("label_", kwargs) **dict_prefix("label_", kwargs)
) )
self._refresh_size()
# background # background
@property @property
@ -92,20 +94,28 @@ class Input(BoxWidget):
self._invalid = invalid self._invalid = invalid
self._refresh_background() self._refresh_background()
@property
def text(self):
return self.label.text
@text.setter
def text(self, text: str):
self.label.text = text
# event # event
def on_key_press(self, symbol: int, modifiers: int): 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 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é 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): def on_text(self, char: str):
if not self.activated: return # ignore si ce widget est désactivé / non sélectionné 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 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): def on_resize(self, width: int, height: int):
self._refresh_size() self._refresh_size()

View file

@ -51,7 +51,7 @@ class BoxWidget(Widget, ABC):
@property @property
def width(self) -> int: 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 @width.setter
def width(self, width: Optional[Percentage]): def width(self, width: Optional[Percentage]):
@ -59,7 +59,7 @@ class BoxWidget(Widget, ABC):
@property @property
def height(self) -> int: 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 @height.setter
def height(self, height: Optional[Percentage]): def height(self, height: Optional[Percentage]):

View file

@ -5,7 +5,7 @@ import pyglet
if TYPE_CHECKING: if TYPE_CHECKING:
from source.gui.scene import Scene from source.gui.scene.abc import Scene
class Window(pyglet.window.Window): # NOQA class Window(pyglet.window.Window): # NOQA