-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathui_main_menu_panel.py
More file actions
70 lines (52 loc) · 2.05 KB
/
ui_main_menu_panel.py
File metadata and controls
70 lines (52 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
from __future__ import annotations
import tcod.event
from tcod.event import KeySym
from game.constants import COLOR_GRAY, COLOR_WHITE, TITLE, DIRECTION_KEYS
from game.managers.event_manager import Event
from game.ui.ui_button import UIButton
from game.ui.ui_panel import UIPanel
class UIMainMenuPanel(UIPanel):
buttons: tuple[UIButton, ...] | None = None
def on_event(self, event: tcod.event.Event) -> None:
"""Called on events."""
match event:
case tcod.event.Quit():
raise SystemExit()
case tcod.event.KeyDown():
for i, button in enumerate(self.buttons):
button.on_event(event)
def on_draw(self, console: tcod.console.Console) -> None:
"""Called when the panel is being drawn."""
self.draw_title(console)
self.draw_buttons(console)
def draw_title(self, console: tcod.console.Console) -> None:
console.print(
console.width // 2,
3,
TITLE,
alignment = tcod.constants.CENTER,
)
def draw_buttons(self, console: tcod.console.Console) -> None:
self.buttons = (
UIButton("[n] New game", KeySym.n, self.new_game_button_clicked),
UIButton("[s] Settings", KeySym.s, self.settings_button_clicked),
UIButton("[q] Quit", KeySym.q, self.quit_button_clicked),
)
for i, button in enumerate(self.buttons):
console.print(
console.width // 2,
5 + i,
button.label,
COLOR_WHITE,
COLOR_GRAY,
alignment=tcod.constants.CENTER
)
def new_game_button_clicked(self) -> None:
"""Callback for the 'New game' button."""
Event('new_game_button_clicked', '')
def settings_button_clicked(self) -> None:
"""Callback for the 'Settings' button."""
Event('settings_button_clicked', '')
def quit_button_clicked(self) -> None:
"""Callback for 'Quit' button."""
raise SystemExit