281 lines
7.2 KiB
Python
281 lines
7.2 KiB
Python
from typing import TYPE_CHECKING
|
|
|
|
import pyglet
|
|
|
|
from source.gui import widget, texture
|
|
from source.gui.scene import RoomHost
|
|
from source.gui.scene.abc import Scene
|
|
from source.network.packet import PacketSettings
|
|
|
|
if TYPE_CHECKING:
|
|
from source.gui.window import Window
|
|
|
|
|
|
class RoomCreate(Scene):
|
|
def __init__(self, window: "Window", **kwargs):
|
|
super().__init__(window, **kwargs)
|
|
|
|
self.batch_label = pyglet.graphics.Batch()
|
|
self.batch_input_background = pyglet.graphics.Batch()
|
|
self.batch_button_background = pyglet.graphics.Batch()
|
|
self.batch_checkbox = pyglet.graphics.Batch()
|
|
|
|
self.back = self.add_widget(
|
|
widget.Button,
|
|
x=20, y=20, width=0.2, height=0.1,
|
|
|
|
label_text="Retour",
|
|
|
|
style=texture.Button.Style1,
|
|
|
|
background_batch=self.batch_button_background,
|
|
label_batch=self.batch_label
|
|
)
|
|
|
|
from source.gui.scene import MainMenu
|
|
self.back.add_listener("on_click_release", lambda *_: self.window.set_scene(MainMenu))
|
|
|
|
# Username
|
|
|
|
self.add_widget(
|
|
widget.Text,
|
|
|
|
x=0.1, y=0.5,
|
|
|
|
anchor_x="center", anchor_y="center",
|
|
|
|
text="Pseudonyme",
|
|
|
|
batch=self.batch_label
|
|
)
|
|
|
|
self.input_username = self.add_widget(
|
|
widget.Input,
|
|
|
|
x=0.2, y=0.45, width=0.15, height=0.1,
|
|
|
|
style=texture.Input.Style1,
|
|
|
|
label_text="Host",
|
|
|
|
background_batch=self.batch_input_background,
|
|
label_batch=self.batch_label
|
|
)
|
|
|
|
# Grid configuration
|
|
|
|
self.add_widget(
|
|
widget.Text,
|
|
|
|
x=0.1, y=0.9,
|
|
anchor_x="center", anchor_y="center",
|
|
text=f"Largeur de la grille",
|
|
|
|
batch=self.batch_label
|
|
)
|
|
|
|
self.input_width = self.add_widget(
|
|
widget.Input,
|
|
|
|
x=0.2, y=0.86, width=0.1, height=0.08,
|
|
|
|
regex=r"\d+",
|
|
|
|
style=texture.Input.Style1,
|
|
|
|
label_text="8",
|
|
|
|
background_batch=self.batch_input_background,
|
|
label_batch=self.batch_label
|
|
)
|
|
|
|
self.add_widget(
|
|
widget.Text,
|
|
|
|
x=0.1, y=0.8,
|
|
anchor_x="center", anchor_y="center",
|
|
text=f"Longueur de la grille",
|
|
|
|
batch=self.batch_label
|
|
)
|
|
|
|
self.input_height = self.add_widget(
|
|
widget.Input,
|
|
|
|
x=0.2, y=0.76, width=0.1, height=0.08,
|
|
|
|
regex=r"\d+",
|
|
|
|
style=texture.Input.Style1,
|
|
|
|
label_text="8",
|
|
|
|
background_batch=self.batch_input_background,
|
|
label_batch=self.batch_label
|
|
)
|
|
|
|
# Tour
|
|
|
|
self.checkbox_host_start = self.add_widget(
|
|
widget.Checkbox,
|
|
|
|
x=0.4, y=0.8, width=0.05, height=0.1,
|
|
|
|
style=texture.Checkbox.Style1,
|
|
|
|
state=True,
|
|
|
|
batch=self.batch_checkbox
|
|
)
|
|
|
|
self.add_widget(
|
|
widget.Text,
|
|
|
|
x=0.46, y=0.85,
|
|
|
|
anchor_y="center",
|
|
|
|
text="Premier tour pour l'hôte",
|
|
|
|
batch=self.batch_label
|
|
)
|
|
|
|
# taille et quantité des bateaux
|
|
|
|
self.boat_size: int = 1
|
|
self.boat_size_amount: dict[int, int] = {2: 1, 3: 1, 4: 2, 5: 1}
|
|
|
|
def update_boat_size_text():
|
|
self.label_boat_size.text = f"Taille: {self.boat_size}"
|
|
self.input_boat_amount.text = str(self.boat_size_amount.get(self.boat_size, 0))
|
|
|
|
self.label_boat_recap.text = ""
|
|
for size, amount in sorted(self.boat_size_amount.items(), key=lambda v: v[0]):
|
|
self.label_boat_recap.text += f"Taille: {size}, Quantité: {amount}\n"
|
|
|
|
self.button_boat_size_previous = self.add_widget(
|
|
widget.Button,
|
|
|
|
x=0.7, y=0.8, width=0.03, height=0.1,
|
|
|
|
label_text="<",
|
|
label_font_size=25,
|
|
|
|
style=texture.Button.Style1,
|
|
|
|
background_batch=self.batch_button_background,
|
|
label_batch=self.batch_label
|
|
)
|
|
|
|
def previous_boat_size():
|
|
if self.boat_size <= 1: return
|
|
self.boat_size -= 1
|
|
update_boat_size_text()
|
|
|
|
self.button_boat_size_previous.add_listener("on_click_release", lambda *_: previous_boat_size())
|
|
|
|
self.label_boat_size = self.add_widget(
|
|
widget.Text,
|
|
|
|
x=0.8, y=0.85,
|
|
|
|
anchor_x="center", anchor_y="center",
|
|
|
|
batch=self.batch_label,
|
|
)
|
|
|
|
self.button_boat_size_next = self.add_widget(
|
|
widget.Button,
|
|
|
|
x=0.87, y=0.8, width=0.03, height=0.1,
|
|
|
|
label_text=">",
|
|
label_font_size=25,
|
|
|
|
style=texture.Button.Style1,
|
|
|
|
background_batch=self.batch_button_background,
|
|
label_batch=self.batch_label
|
|
)
|
|
|
|
def next_boat_size():
|
|
self.boat_size += 1
|
|
update_boat_size_text()
|
|
|
|
self.button_boat_size_next.add_listener("on_click_release", lambda *_: next_boat_size())
|
|
|
|
self.input_boat_amount = self.add_widget(
|
|
widget.Input,
|
|
|
|
x=0.7, y=0.68, width=0.2, height=0.08,
|
|
|
|
regex=r"\d+",
|
|
|
|
style=texture.Input.Style1,
|
|
|
|
label_text="8",
|
|
|
|
background_batch=self.batch_input_background,
|
|
label_batch=self.batch_label
|
|
)
|
|
|
|
def change_boat_amount():
|
|
quantity = int(self.input_boat_amount.text)
|
|
|
|
if quantity > 0:
|
|
self.boat_size_amount[self.boat_size] = quantity
|
|
|
|
elif self.boat_size in self.boat_size_amount:
|
|
self.boat_size_amount.pop(self.boat_size)
|
|
|
|
update_boat_size_text()
|
|
|
|
self.input_boat_amount.add_listener("on_valid_text", lambda *_: change_boat_amount())
|
|
self.input_boat_amount.add_listener("on_enter", lambda *_: change_boat_amount())
|
|
|
|
self.label_boat_recap = self.add_widget(
|
|
widget.Text,
|
|
|
|
x=0.7, y=0.60, width=0.2, height=0.1,
|
|
|
|
multiline=True,
|
|
|
|
batch=self.batch_label
|
|
)
|
|
|
|
update_boat_size_text()
|
|
|
|
# TODO: si on diminue la taille de la grille après avoir mis des bateaux de plus longue taille, faire un check
|
|
|
|
# Démarrer
|
|
|
|
self.start = self.add_widget(
|
|
widget.Button,
|
|
x=lambda widget: widget.scene.window.width - 20 - widget.width, y=20, width=0.2, height=0.1,
|
|
|
|
label_text="Continuer",
|
|
|
|
style=texture.Button.Style1,
|
|
|
|
background_batch=self.batch_button_background,
|
|
label_batch=self.batch_label
|
|
)
|
|
|
|
self.start.add_listener("on_click_release", lambda *_: self.confirm())
|
|
|
|
def confirm(self):
|
|
settings = PacketSettings(
|
|
username=self.input_username.text,
|
|
grid_width=int(self.input_width.text),
|
|
grid_height=int(self.input_height.text),
|
|
host_start=self.checkbox_host_start.state,
|
|
boats_length=[size for size, quantity in self.boat_size_amount.items() for _ in range(quantity)]
|
|
)
|
|
|
|
self.window.set_scene(RoomHost, settings=settings)
|
|
|
|
def on_draw(self):
|
|
self.batch_input_background.draw()
|
|
self.batch_button_background.draw()
|
|
self.batch_checkbox.draw()
|
|
self.batch_label.draw()
|