batchs are now automatically handled by the scene and the widgets

This commit is contained in:
Faraphel 2023-03-02 21:34:21 +01:00
parent 3aa9eaf541
commit 3caaa8180d
18 changed files with 92 additions and 294 deletions

View file

@ -3,11 +3,16 @@ A faire :
- Sauvegarde / Quitter
- Historique / Replay
- Police d'écriture
- Documenter
- Gérer les erreurs (quitter en cours de connexion, ...)
- Documenter
- Changer les images, rajouter les fonds, ...
- Voir si les event listener intégré à pyglet sont plus pratique que l'event propagation (?)
- Faire une scène incluant par défaut les boutons "Retour" (?)
- Faire des scènes "pop-up" (?)
Bug :

View file

@ -37,15 +37,6 @@ class Game(Scene):
self.grid_width = grid_width
self.grid_height = grid_height
self.batch_label = pyglet.graphics.Batch()
self.batch_button_background = pyglet.graphics.Batch()
self.batch_input_background = pyglet.graphics.Batch()
self.batch_grid_background = pyglet.graphics.Batch()
self.batch_grid_line = pyglet.graphics.Batch()
self.batch_grid_cursor = pyglet.graphics.Batch()
self.batch_grid_boat = pyglet.graphics.Batch()
self.batch_grid_bomb = pyglet.graphics.Batch()
self.background = self.add_widget(
widget.Image,
@ -64,13 +55,7 @@ class Game(Scene):
grid_style=texture.Grid.Style1,
boat_style=texture.Grid.Boat.Style1,
bomb_style=texture.Grid.Bomb.Style1,
rows=self.grid_height, columns=self.grid_width,
background_batch=self.batch_grid_background,
line_batch=self.batch_grid_line,
cursor_batch=self.batch_grid_cursor,
boat_batch=self.batch_grid_boat,
bomb_batch=self.batch_grid_bomb
rows=self.grid_height, columns=self.grid_width
)
def board_ally_ready(widget):
@ -87,13 +72,7 @@ class Game(Scene):
grid_style=texture.Grid.Style1,
boat_style=texture.Grid.Boat.Style1,
bomb_style=texture.Grid.Bomb.Style1,
rows=self.grid_height, columns=self.grid_width,
background_batch=self.batch_grid_background,
line_batch=self.batch_grid_line,
cursor_batch=self.batch_grid_cursor,
boat_batch=self.batch_grid_boat,
bomb_batch=self.batch_grid_bomb
rows=self.grid_height, columns=self.grid_width
)
def board_enemy_bomb(widget, cell: Point2D):
@ -112,9 +91,7 @@ class Game(Scene):
text=self.name_ally,
font_size=20,
anchor_x="center", anchor_y="center",
batch=self.batch_label,
anchor_x="center", anchor_y="center"
)
self.add_widget(
@ -124,9 +101,7 @@ class Game(Scene):
text=self.name_enemy,
font_size=20,
anchor_x="center", anchor_y="center",
batch=self.batch_label,
anchor_x="center", anchor_y="center"
)
self.score_ally = self.add_widget(
@ -136,9 +111,7 @@ class Game(Scene):
text="0",
font_size=25,
anchor_x="center", anchor_y="center",
batch=self.batch_label,
anchor_x="center", anchor_y="center"
)
self.score_enemy = self.add_widget(
@ -148,9 +121,7 @@ class Game(Scene):
text="0",
font_size=25,
anchor_x="center", anchor_y="center",
batch=self.batch_label,
anchor_x="center", anchor_y="center"
)
self.chat_log = self.add_widget(
@ -160,9 +131,7 @@ class Game(Scene):
text="",
anchor_x="left",
multiline=True,
batch=self.batch_label,
multiline=True
)
self.chat_input = self.add_widget(
@ -170,10 +139,7 @@ class Game(Scene):
x=10, y=10, width=0.5, height=30,
style=texture.Button.Style1,
background_batch=self.batch_input_background,
label_batch=self.batch_label,
style=texture.Button.Style1
)
def send_chat(widget):
@ -194,10 +160,7 @@ class Game(Scene):
label_text="Sauvegarder",
style=texture.Button.Style1,
background_batch=self.batch_button_background,
label_batch=self.batch_label,
style=texture.Button.Style1
)
self.button_quit = self.add_widget(
@ -207,10 +170,7 @@ class Game(Scene):
label_text="Quitter",
style=texture.Button.Style1,
background_batch=self.batch_button_background,
label_batch=self.batch_label,
style=texture.Button.Style1
)
self.label_state = self.add_widget(
@ -220,9 +180,7 @@ class Game(Scene):
anchor_x="center",
font_size=20,
batch=self.batch_label
font_size=20
)
self.board_ally = core.Board(rows=self.grid_height, columns=self.grid_width)
@ -314,7 +272,7 @@ class Game(Scene):
# l'opposant va rejouer, ce n'est donc pas notre tour
self.my_turn = False
else:
# si la bombe a bien été placé, affiche la sur la grille visuel allié
# si la bombe a bien été placé, affiche-la sur la grille visuel allié
self.grid_ally.place_bomb(packet.position, bomb_state.success)
# c'est à notre tour si l'opposant à loupé sa bombe
self.my_turn = not bomb_state.success
@ -351,18 +309,3 @@ class Game(Scene):
# si cette bombe a touché le dernier bateau, alors l'on a gagné
self.game_end(won=True)
return True # coupe la connexion
# event
def on_draw(self):
self.background.draw()
self.batch_button_background.draw()
self.batch_input_background.draw()
self.batch_grid_background.draw()
self.batch_grid_boat.draw()
self.batch_grid_bomb.draw()
self.batch_grid_line.draw()
self.batch_grid_cursor.draw()
self.batch_label.draw()

View file

@ -1,8 +1,5 @@
from typing import TYPE_CHECKING
import pyglet
from source.gui.scene import Result
from source.gui.scene.abc import Scene
from source.gui import widget, scene, texture
@ -14,9 +11,6 @@ class MainMenu(Scene):
def __init__(self, window: "Window", **kwargs):
super().__init__(window, **kwargs)
self.batch_button_background = pyglet.graphics.Batch()
self.batch_label = pyglet.graphics.Batch()
self.background = self.add_widget(
widget.Image,
@ -31,9 +25,7 @@ class MainMenu(Scene):
x=50, y=0.85,
text="Bataille Navale",
font_size=50,
batch=self.batch_label
font_size=50
)
self.game_create = self.add_widget(
@ -43,10 +35,7 @@ class MainMenu(Scene):
label_text="Créer une salle",
label_font_size=20,
style=texture.Button.Style1,
background_batch=self.batch_button_background,
label_batch=self.batch_label
style=texture.Button.Style1
)
self.game_create.add_listener("on_click_release", lambda *_: self.window.set_scene(scene.RoomCreate))
@ -59,10 +48,7 @@ class MainMenu(Scene):
label_text="Rejoindre une salle",
label_font_size=20,
style=texture.Button.Style1,
background_batch=self.batch_button_background,
label_batch=self.batch_label
style=texture.Button.Style1
)
self.game_join.add_listener("on_click_release", lambda *_: self.window.set_scene(scene.RoomJoin))
@ -75,16 +61,7 @@ class MainMenu(Scene):
label_text="Paramètres",
label_font_size=20,
style=texture.Button.Style1,
background_batch=self.batch_button_background,
label_batch=self.batch_label
style=texture.Button.Style1
)
self.settings.add_listener("on_click_release", lambda *_: self.window.add_scene(scene.Settings))
def on_draw(self):
self.background.draw()
self.batch_button_background.draw()
self.batch_label.draw()

View file

@ -31,6 +31,3 @@ class Result(Scene):
def on_mouse_motion_after(self, x: int, y: int, button: int, modifiers: int):
raise StopEvent()
def on_draw(self):
self.image.draw()

View file

@ -1,7 +1,5 @@
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
@ -15,21 +13,13 @@ 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
style=texture.Button.Style1
)
from source.gui.scene import MainMenu
@ -44,9 +34,7 @@ class RoomCreate(Scene):
anchor_x="center", anchor_y="center",
text="Port",
batch=self.batch_label
text="Port"
)
self.input_port = self.add_widget(
@ -58,10 +46,7 @@ class RoomCreate(Scene):
regex=r"\d{1,5}",
label_text="52321",
background_batch=self.batch_input_background,
label_batch=self.batch_label
label_text="52321"
)
# Username
@ -73,9 +58,7 @@ class RoomCreate(Scene):
anchor_x="center", anchor_y="center",
text="Pseudonyme",
batch=self.batch_label
text="Pseudonyme"
)
self.input_username = self.add_widget(
@ -85,10 +68,7 @@ class RoomCreate(Scene):
style=texture.Input.Style1,
label_text="Host",
background_batch=self.batch_input_background,
label_batch=self.batch_label
label_text="Host"
)
# Grid configuration
@ -98,9 +78,7 @@ class RoomCreate(Scene):
x=0.1, y=0.9,
anchor_x="center", anchor_y="center",
text=f"Largeur de la grille",
batch=self.batch_label
text=f"Largeur de la grille"
)
self.input_width = self.add_widget(
@ -112,10 +90,7 @@ class RoomCreate(Scene):
style=texture.Input.Style1,
label_text="8",
background_batch=self.batch_input_background,
label_batch=self.batch_label
label_text="8"
)
self.add_widget(
@ -123,9 +98,7 @@ class RoomCreate(Scene):
x=0.1, y=0.8,
anchor_x="center", anchor_y="center",
text=f"Longueur de la grille",
batch=self.batch_label
text=f"Longueur de la grille"
)
self.input_height = self.add_widget(
@ -137,10 +110,7 @@ class RoomCreate(Scene):
style=texture.Input.Style1,
label_text="8",
background_batch=self.batch_input_background,
label_batch=self.batch_label
label_text="8"
)
# Tour
@ -152,9 +122,7 @@ class RoomCreate(Scene):
style=texture.Checkbox.Style1,
state=True,
batch=self.batch_checkbox
state=True
)
self.add_widget(
@ -164,9 +132,7 @@ class RoomCreate(Scene):
anchor_y="center",
text="Premier tour pour l'hôte",
batch=self.batch_label
text="Premier tour pour l'hôte"
)
# taille et quantité des bateaux
@ -190,10 +156,7 @@ class RoomCreate(Scene):
label_text="<",
label_font_size=25,
style=texture.Button.Style1,
background_batch=self.batch_button_background,
label_batch=self.batch_label
style=texture.Button.Style1
)
def previous_boat_size():
@ -208,9 +171,7 @@ class RoomCreate(Scene):
x=0.8, y=0.85,
anchor_x="center", anchor_y="center",
batch=self.batch_label,
anchor_x="center", anchor_y="center"
)
self.button_boat_size_next = self.add_widget(
@ -221,10 +182,7 @@ class RoomCreate(Scene):
label_text=">",
label_font_size=25,
style=texture.Button.Style1,
background_batch=self.batch_button_background,
label_batch=self.batch_label
style=texture.Button.Style1
)
def next_boat_size():
@ -242,10 +200,7 @@ class RoomCreate(Scene):
style=texture.Input.Style1,
label_text="8",
background_batch=self.batch_input_background,
label_batch=self.batch_label
label_text="8"
)
def change_boat_amount():
@ -267,9 +222,7 @@ class RoomCreate(Scene):
x=0.7, y=0.60, width=0.2, height=0.1,
multiline=True,
batch=self.batch_label
multiline=True
)
update_boat_size_text()
@ -284,10 +237,7 @@ class RoomCreate(Scene):
label_text="Continuer",
style=texture.Button.Style1,
background_batch=self.batch_button_background,
label_batch=self.batch_label
style=texture.Button.Style1
)
self.start.add_listener("on_click_release", lambda *_: self.confirm())
@ -308,9 +258,3 @@ class RoomCreate(Scene):
username=self.input_username.text,
settings=settings
)
def on_draw(self):
self.batch_input_background.draw()
self.batch_button_background.draw()
self.batch_checkbox.draw()
self.batch_label.draw()

View file

@ -1,6 +1,5 @@
from typing import TYPE_CHECKING
import pyglet
import requests
from source import network
@ -21,19 +20,13 @@ class RoomHost(Scene):
self.ip_address: str = "127.0.0.1"
self.port: int = port
self.batch_button_background = pyglet.graphics.Batch()
self.batch_label = 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
style=texture.Button.Style1
)
self.back.add_listener("on_click_release", self.button_back_callback)
@ -44,9 +37,7 @@ class RoomHost(Scene):
x=0.5, y=0.55,
anchor_x="center", anchor_y="center",
font_size=20,
batch=self.batch_label
font_size=20
)
self.description = self.add_widget(
@ -55,9 +46,7 @@ class RoomHost(Scene):
x=0.5, y=0.45,
anchor_x="center", anchor_y="center",
text="En attente d'un second joueur...",
batch=self.batch_label
text="En attente d'un second joueur..."
)
self.thread_network = network.Host(
@ -95,7 +84,3 @@ class RoomHost(Scene):
self.thread_ip.stop()
from source.gui.scene import MainMenu
self.window.set_scene(MainMenu)
def on_draw(self):
self.batch_button_background.draw()
self.batch_label.draw()

View file

@ -1,7 +1,5 @@
from typing import TYPE_CHECKING
import pyglet
from source import network
from source.gui.scene.abc import Scene
from source.gui import widget, texture
@ -14,20 +12,13 @@ class RoomJoin(Scene):
def __init__(self, window: "Window", **kwargs):
super().__init__(window, **kwargs)
self.batch_button_background = pyglet.graphics.Batch()
self.batch_input_background = pyglet.graphics.Batch()
self.batch_label = 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
style=texture.Button.Style1
)
self.back.add_listener("on_click_release", self.button_back_callback)
@ -40,10 +31,7 @@ class RoomJoin(Scene):
style=texture.Input.Style1,
label_text="Client",
background_batch=self.batch_input_background,
label_batch=self.batch_label
label_text="Client"
)
# IP / Port
@ -56,10 +44,7 @@ class RoomJoin(Scene):
style=texture.Input.Style1,
label_text="127.0.0.1",
background_batch=self.batch_input_background,
label_batch=self.batch_label
label_text="127.0.0.1"
)
self.entry_port = self.add_widget(
@ -70,10 +55,7 @@ class RoomJoin(Scene):
label_text="52321",
style=texture.Input.Style1,
background_batch=self.batch_input_background,
label_batch=self.batch_label
style=texture.Input.Style1
)
self.connect = self.add_widget(
@ -82,10 +64,7 @@ class RoomJoin(Scene):
label_text="Se connecter",
style=texture.Button.Style1,
background_batch=self.batch_button_background,
label_batch=self.batch_label
style=texture.Button.Style1
)
self.connect.add_listener("on_click_release", self.button_connect)
@ -102,8 +81,3 @@ class RoomJoin(Scene):
def button_back_callback(self, widget, *_):
from source.gui.scene import MainMenu
self.window.set_scene(MainMenu)
def on_draw(self):
self.batch_button_background.draw()
self.batch_input_background.draw()
self.batch_label.draw()

View file

@ -1,7 +1,5 @@
from typing import TYPE_CHECKING
import pyglet
from source.gui.event import StopEvent
from source.gui.scene.abc import Scene
from source.gui import widget, texture
@ -14,22 +12,13 @@ class Settings(Scene):
def __init__(self, window: "Window", **kwargs):
super().__init__(window, **kwargs)
self.batch_button_background = pyglet.graphics.Batch()
self.batch_scroller_background = pyglet.graphics.Batch()
self.batch_scroller_cursor = pyglet.graphics.Batch()
self.batch_checkbox = pyglet.graphics.Batch()
self.batch_label = 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
style=texture.Button.Style1
)
self.back.add_listener("on_click_release", lambda *_: self.window.remove_scene(self))
@ -39,9 +28,7 @@ class Settings(Scene):
x=0.45, y=0.45, width=0.1, height=0.1,
style=texture.Checkbox.Style1,
batch=self.batch_checkbox
style=texture.Checkbox.Style1
)
self.checkbox.add_listener("on_click_release",
@ -54,23 +41,9 @@ class Settings(Scene):
style=texture.Scroller.Style1,
text_transform=lambda value: round(value, 2),
background_batch=self.batch_scroller_background,
cursor_batch=self.batch_scroller_cursor,
label_batch=self.batch_label
text_transform=lambda value: round(value, 2)
)
def on_draw(self):
self.batch_button_background.draw()
self.batch_scroller_background.draw()
self.batch_scroller_cursor.draw()
self.batch_checkbox.draw()
self.batch_label.draw()
def on_mouse_press_after(self, x: int, y: int, button: int, modifiers: int):
raise StopEvent()

View file

@ -1,6 +1,8 @@
from abc import ABC
from typing import TYPE_CHECKING, Type
import pyglet
from source.gui.event import EventPropagationMixin
if TYPE_CHECKING:
@ -18,6 +20,8 @@ class Scene(ABC, EventPropagationMixin):
def __init__(self, window: "Window", **kwargs):
self.window = window
self.batch = pyglet.graphics.Batch()
self._widgets: list["Widget"] = list()
# Event propagation
@ -55,3 +59,12 @@ class Scene(ABC, EventPropagationMixin):
"""
self._widgets.clear()
# event
def on_draw(self) -> None:
"""
Draw all the objects in the scene.
"""
self.batch.draw()

View file

@ -34,12 +34,14 @@ class Button(BoxWidget):
self.background = Sprite(
img=self.style.get("normal"),
batch=self.scene.batch,
**dict_filter_prefix("background_", kwargs)
)
self.label = pyglet.text.Label(
width=None, height=None,
anchor_x="center", anchor_y="center",
batch=self.scene.batch,
**dict_filter_prefix("label_", kwargs)
)
@ -81,8 +83,3 @@ class Button(BoxWidget):
def on_resize(self, width: int, height: int):
self._refresh_size()
def draw(self):
self.background.draw()
self.label.draw()

View file

@ -31,7 +31,11 @@ class Checkbox(BoxWidget):
self.style = style
self.tick = Sprite(img=self.style.get("disabled"), **kwargs)
self.tick = Sprite(
img=self.style.get("disabled"),
batch=self.scene.batch,
**kwargs
)
self.state = state
@ -72,6 +76,3 @@ class Checkbox(BoxWidget):
def on_resize(self, width: int, height: int):
self._refresh_size()
def draw(self):
self.tick.draw()

View file

@ -44,6 +44,9 @@ class GameGrid(BoxWidget):
super().__init__(scene, x, y, width, height)
self.group_cursor = pyglet.graphics.Group(order=1)
self.group_line = pyglet.graphics.Group(order=2)
self.rows = rows
self.columns = columns
@ -62,12 +65,15 @@ class GameGrid(BoxWidget):
self.background = Sprite(
img=grid_style.get("background"),
batch=self.scene.batch,
**dict_filter_prefix("background_", kwargs)
)
self.lines: list[pyglet.shapes.Line] = [
pyglet.shapes.Line(
0, 0, 0, 0,
batch=self.scene.batch,
group=self.group_line,
**dict_filter_prefix("line_", kwargs)
)
for _ in range((self.columns - 1) + (self.rows - 1))
@ -76,7 +82,8 @@ class GameGrid(BoxWidget):
self.cursor = pyglet.shapes.Rectangle(
0, 0, 0, 0,
color=(0, 0, 0, 100),
**dict_filter_prefix("cursor_", kwargs)
batch=self.scene.batch,
group=self.group_cursor
)
self.add_listener("on_click_release", lambda _, *args: self.on_click_release(*args))
@ -175,6 +182,7 @@ class GameGrid(BoxWidget):
sprite = Sprite(
img=self.boat_style.get(form),
batch=self.scene.batch,
**self._boat_kwargs
)
sprite.rotation = rotation * 90
@ -225,6 +233,7 @@ class GameGrid(BoxWidget):
def place_bomb(self, cell: Point2D, touched: bool):
self.cell_sprites[cell] = Sprite(
img=self.bomb_style.get("touched" if touched else "missed"),
batch=self.scene.batch,
**self._bomb_kwargs
)
@ -260,9 +269,3 @@ class GameGrid(BoxWidget):
def on_resize(self, width: int, height: int):
self._refresh_size()
def draw(self):
self.background.draw()
for sprite in self.cell_sprites.values(): sprite.draw()
self.cursor.draw()
for line in self.lines: line.draw()

View file

@ -27,7 +27,11 @@ class Image(BoxWidget):
**kwargs):
super().__init__(scene, x, y, width, height)
self.image = Sprite(img=image, **kwargs)
self.image = Sprite(
img=image,
batch=self.scene.batch,
**kwargs
)
self._refresh_size()
@ -40,8 +44,3 @@ class Image(BoxWidget):
def on_resize(self, width: int, height: int):
self._refresh_size()
# draw
def draw(self):
self.image.draw()

View file

@ -40,12 +40,14 @@ class Input(BoxWidget):
self.background = Sprite(
img=self.style.get("normal"),
batch=self.scene.batch,
**dict_filter_prefix("background_", kwargs)
)
self.label = pyglet.text.Label(
width=None, height=None,
anchor_x="center", anchor_y="center",
batch=self.scene.batch,
**dict_filter_prefix("label_", kwargs)
)
@ -126,7 +128,3 @@ class Input(BoxWidget):
def on_resize(self, width: int, height: int):
self._refresh_size()
def draw(self):
self.background.draw()
self.label.draw()

View file

@ -44,16 +44,19 @@ class Scroller(BoxWidget):
self.background = Sprite(
img=self.style.get("background"),
batch=self.scene.batch,
**dict_filter_prefix("background_", kwargs)
)
self.cursor = Sprite(
img=self.style.get("cursor"),
batch=self.scene.batch,
**dict_filter_prefix("cursor_", kwargs)
)
self.label = pyglet.text.Label(
anchor_x="center", anchor_y="center",
batch=self.scene.batch,
**dict_filter_prefix("label_", kwargs)
)
@ -125,8 +128,3 @@ class Scroller(BoxWidget):
def on_resize(self, width: int, height: int):
self._refresh()
def draw(self):
self.background.draw()
self.cursor.draw()
self.label.draw()

View file

@ -27,6 +27,7 @@ class Text(BoxWidget):
self.label = pyglet.text.Label(
x=self.x, y=self.y, width=self.width, height=self.height,
batch=self.scene.batch,
**kwargs
)
@ -46,6 +47,3 @@ class Text(BoxWidget):
def on_resize(self, width: int, height: int):
self._refresh_size()
def draw(self):
self.label.draw()

View file

@ -1,4 +1,4 @@
from abc import ABC, abstractmethod
from abc import ABC
from typing import TYPE_CHECKING
from source.event import Listener
@ -18,9 +18,3 @@ class Widget(Listener, ABC):
super().__init__()
self.scene = scene
@abstractmethod
def draw(self):
"""
The draw function. Can be called to draw the widget.
"""

View file

@ -16,7 +16,6 @@ class Window(pyglet.window.Window, EventPropagationMixin): # NOQA
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._scenes: list["Scene"] = list()
# Event Propagation