2020-11-27 15:33:17 +00:00
|
|
|
# Copyright (C) 2020 by ÿnérant, eichhornchen, nicomarg, charlse
|
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
2020-12-11 16:43:46 +00:00
|
|
|
|
2020-12-04 15:02:03 +00:00
|
|
|
import curses
|
2020-12-12 12:46:45 +00:00
|
|
|
from random import randint
|
2020-11-11 19:36:43 +00:00
|
|
|
from typing import List
|
|
|
|
|
2021-01-08 22:15:48 +00:00
|
|
|
from squirrelbattle.menus import Menu, MainMenu, SettingsMenu, StoreMenu,\
|
2021-01-08 22:32:47 +00:00
|
|
|
ChestMenu
|
2020-12-12 16:15:08 +00:00
|
|
|
from .display import Box, Display
|
2020-12-18 14:36:25 +00:00
|
|
|
from ..entities.player import Player
|
2020-12-18 21:24:41 +00:00
|
|
|
from ..enums import KeyValues, GameMode
|
2020-12-11 16:43:46 +00:00
|
|
|
from ..game import Game
|
2020-11-19 01:49:59 +00:00
|
|
|
from ..resources import ResourceManager
|
2020-11-27 21:19:41 +00:00
|
|
|
from ..translations import gettext as _
|
2020-11-10 19:34:22 +00:00
|
|
|
|
2020-11-08 15:36:21 +00:00
|
|
|
|
2020-11-10 17:08:06 +00:00
|
|
|
class MenuDisplay(Display):
|
2020-11-27 17:00:54 +00:00
|
|
|
"""
|
2020-12-13 20:29:25 +00:00
|
|
|
A class to display the menu objects.
|
2020-11-27 17:00:54 +00:00
|
|
|
"""
|
2020-12-18 14:07:09 +00:00
|
|
|
menu: Menu
|
2020-11-09 00:33:23 +00:00
|
|
|
position: int
|
2020-11-10 19:34:22 +00:00
|
|
|
|
2020-11-20 17:07:09 +00:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
self.menubox = Box(*args, **kwargs)
|
2020-11-10 17:08:06 +00:00
|
|
|
|
2020-11-10 19:34:22 +00:00
|
|
|
def update_menu(self, menu: Menu) -> None:
|
2020-11-10 17:08:06 +00:00
|
|
|
self.menu = menu
|
2020-11-08 15:36:21 +00:00
|
|
|
|
2020-11-10 19:34:22 +00:00
|
|
|
# Menu values are printed in pad
|
|
|
|
self.pad = self.newpad(self.trueheight, self.truewidth + 2)
|
2020-11-10 17:08:06 +00:00
|
|
|
|
2020-11-09 00:33:23 +00:00
|
|
|
def update_pad(self) -> None:
|
2020-11-10 18:40:59 +00:00
|
|
|
for i in range(self.trueheight):
|
2020-12-12 12:46:45 +00:00
|
|
|
self.addstr(self.pad, i, 0, " " + self.values[i])
|
2020-11-10 19:34:22 +00:00
|
|
|
# set a marker on the selected line
|
2020-12-12 12:46:45 +00:00
|
|
|
self.addstr(self.pad, self.menu.position, 0, " >")
|
2020-11-09 00:33:23 +00:00
|
|
|
|
2020-11-10 17:08:06 +00:00
|
|
|
def display(self) -> None:
|
2020-11-10 20:32:10 +00:00
|
|
|
cornery = 0 if self.height - 2 >= self.menu.position - 1 \
|
|
|
|
else self.trueheight - self.height + 2 \
|
|
|
|
if self.height - 2 >= self.trueheight - self.menu.position else 0
|
2020-11-10 19:34:22 +00:00
|
|
|
|
|
|
|
# Menu box
|
2020-11-20 17:07:09 +00:00
|
|
|
self.menubox.refresh(self.y, self.x, self.height, self.width)
|
2020-11-26 21:20:14 +00:00
|
|
|
self.pad.erase()
|
2020-11-10 17:08:06 +00:00
|
|
|
self.update_pad()
|
2020-12-12 12:46:45 +00:00
|
|
|
self.refresh_pad(self.pad, cornery, 0, self.y + 1, self.x + 1,
|
2020-11-10 20:20:11 +00:00
|
|
|
self.height - 2 + self.y,
|
|
|
|
self.width - 2 + self.x)
|
2020-11-10 19:34:22 +00:00
|
|
|
|
2021-01-08 10:21:40 +00:00
|
|
|
def handle_click(self, y: int, x: int, attr: int, game: Game) -> None:
|
2020-12-11 16:40:56 +00:00
|
|
|
"""
|
|
|
|
We can select a menu item with the mouse.
|
|
|
|
"""
|
2020-12-11 17:07:24 +00:00
|
|
|
self.menu.position = max(0, min(len(self.menu.values) - 1, y - 1))
|
2020-12-11 16:43:46 +00:00
|
|
|
game.handle_key_pressed(KeyValues.ENTER)
|
2020-12-11 16:40:56 +00:00
|
|
|
|
2020-11-27 20:51:54 +00:00
|
|
|
@property
|
|
|
|
def truewidth(self) -> int:
|
|
|
|
return max([len(str(a)) for a in self.values])
|
|
|
|
|
|
|
|
@property
|
|
|
|
def trueheight(self) -> int:
|
|
|
|
return len(self.values)
|
|
|
|
|
2020-11-10 18:40:59 +00:00
|
|
|
@property
|
|
|
|
def preferred_width(self) -> int:
|
|
|
|
return self.truewidth + 6
|
2020-11-10 19:34:22 +00:00
|
|
|
|
2020-11-10 18:40:59 +00:00
|
|
|
@property
|
|
|
|
def preferred_height(self) -> int:
|
|
|
|
return self.trueheight + 2
|
2020-11-10 17:08:06 +00:00
|
|
|
|
2020-11-11 13:56:00 +00:00
|
|
|
@property
|
2020-11-11 19:36:43 +00:00
|
|
|
def values(self) -> List[str]:
|
2020-11-11 13:56:00 +00:00
|
|
|
return [str(a) for a in self.menu.values]
|
|
|
|
|
2020-11-13 18:08:40 +00:00
|
|
|
|
2020-11-13 14:40:44 +00:00
|
|
|
class SettingsMenuDisplay(MenuDisplay):
|
2020-11-27 17:00:54 +00:00
|
|
|
"""
|
2020-12-13 20:29:25 +00:00
|
|
|
A class to display specifically a settingsmenu object.
|
2020-11-27 17:00:54 +00:00
|
|
|
"""
|
2020-12-18 14:07:09 +00:00
|
|
|
menu: SettingsMenu
|
|
|
|
|
|
|
|
def update(self, game: Game) -> None:
|
|
|
|
self.update_menu(game.settings_menu)
|
|
|
|
|
2020-11-13 14:40:44 +00:00
|
|
|
@property
|
|
|
|
def values(self) -> List[str]:
|
2020-11-27 21:19:41 +00:00
|
|
|
return [_(a[1][1]) + (" : "
|
2020-11-19 01:47:36 +00:00
|
|
|
+ ("?" if self.menu.waiting_for_key
|
2020-11-27 21:19:41 +00:00
|
|
|
and a == self.menu.validate() else a[1][0]
|
|
|
|
.replace("\n", "\\n"))
|
2020-11-13 18:08:40 +00:00
|
|
|
if a[1][0] else "") for a in self.menu.values]
|
|
|
|
|
2020-11-10 19:34:22 +00:00
|
|
|
|
2020-11-10 17:08:06 +00:00
|
|
|
class MainMenuDisplay(Display):
|
2020-11-27 17:00:54 +00:00
|
|
|
"""
|
2020-12-13 20:29:25 +00:00
|
|
|
A class to display specifically a mainmenu object.
|
2020-11-27 17:00:54 +00:00
|
|
|
"""
|
2020-11-10 19:34:22 +00:00
|
|
|
def __init__(self, menu: MainMenu, *args):
|
2020-11-10 17:08:06 +00:00
|
|
|
super().__init__(*args)
|
2020-11-10 09:01:31 +00:00
|
|
|
self.menu = menu
|
|
|
|
|
2021-01-10 10:54:49 +00:00
|
|
|
with open(ResourceManager.get_asset_path("ascii_art-title.txt"), "r")\
|
|
|
|
as file:
|
2020-11-10 17:08:06 +00:00
|
|
|
self.title = file.read().split("\n")
|
2020-11-10 09:01:31 +00:00
|
|
|
|
2020-11-13 18:08:40 +00:00
|
|
|
self.pad = self.newpad(max(self.rows, len(self.title) + 30),
|
2020-11-19 00:17:46 +00:00
|
|
|
max(len(self.title[0]) + 5, self.cols))
|
2020-11-13 18:08:40 +00:00
|
|
|
|
2020-12-12 12:46:45 +00:00
|
|
|
self.fg_color = curses.COLOR_WHITE
|
2020-12-11 20:17:21 +00:00
|
|
|
|
2020-11-10 18:40:59 +00:00
|
|
|
self.menudisplay = MenuDisplay(self.screen, self.pack)
|
2020-11-10 17:08:06 +00:00
|
|
|
self.menudisplay.update_menu(self.menu)
|
2020-11-10 19:34:22 +00:00
|
|
|
|
2020-11-10 18:40:59 +00:00
|
|
|
def display(self) -> None:
|
2020-11-10 19:34:22 +00:00
|
|
|
for i in range(len(self.title)):
|
2020-11-26 11:35:52 +00:00
|
|
|
self.addstr(self.pad, 4 + i, max(self.width // 2
|
2020-12-11 20:17:21 +00:00
|
|
|
- len(self.title[0]) // 2 - 1, 0), self.title[i],
|
2020-12-12 12:46:45 +00:00
|
|
|
self.fg_color)
|
2020-12-18 21:24:41 +00:00
|
|
|
msg = _("Credits")
|
|
|
|
self.addstr(self.pad, self.height - 1, self.width - 1 - len(msg), msg)
|
2020-11-26 19:58:46 +00:00
|
|
|
self.refresh_pad(self.pad, 0, 0, self.y, self.x,
|
|
|
|
self.height + self.y - 1,
|
2020-11-20 17:09:39 +00:00
|
|
|
self.width + self.x - 1)
|
2020-11-10 18:40:59 +00:00
|
|
|
menuwidth = min(self.menudisplay.preferred_width, self.width)
|
2020-11-10 19:34:22 +00:00
|
|
|
menuy, menux = len(self.title) + 8, self.width // 2 - menuwidth // 2 - 1
|
|
|
|
self.menudisplay.refresh(
|
|
|
|
menuy, menux, min(self.menudisplay.preferred_height,
|
|
|
|
self.height - menuy), menuwidth)
|
2020-12-04 13:41:59 +00:00
|
|
|
|
2020-12-18 14:07:09 +00:00
|
|
|
def update(self, game: Game) -> None:
|
|
|
|
self.menudisplay.update_menu(game.main_menu)
|
|
|
|
|
2021-01-08 10:21:40 +00:00
|
|
|
def handle_click(self, y: int, x: int, attr: int, game: Game) -> None:
|
2020-12-11 16:40:56 +00:00
|
|
|
menuwidth = min(self.menudisplay.preferred_width, self.width)
|
|
|
|
menuy, menux = len(self.title) + 8, self.width // 2 - menuwidth // 2 - 1
|
|
|
|
menuheight = min(self.menudisplay.preferred_height, self.height - menuy)
|
|
|
|
|
|
|
|
if menuy <= y < menuy + menuheight and menux <= x < menux + menuwidth:
|
2021-01-08 10:21:40 +00:00
|
|
|
self.menudisplay.handle_click(y - menuy, x - menux, attr, game)
|
2020-12-11 16:40:56 +00:00
|
|
|
|
2020-12-11 20:17:21 +00:00
|
|
|
if y <= len(self.title):
|
2020-12-12 12:46:45 +00:00
|
|
|
self.fg_color = randint(0, 1000), randint(0, 1000), randint(0, 1000)
|
2020-12-11 20:17:21 +00:00
|
|
|
|
2020-12-18 21:24:41 +00:00
|
|
|
if y == self.height - 1 and x >= self.width - 1 - len(_("Credits")):
|
|
|
|
game.state = GameMode.CREDITS
|
|
|
|
|
2020-12-04 13:41:59 +00:00
|
|
|
|
2020-12-11 17:08:10 +00:00
|
|
|
class PlayerInventoryDisplay(MenuDisplay):
|
2020-12-13 20:29:25 +00:00
|
|
|
"""
|
|
|
|
A class to handle the display of the player's inventory.
|
|
|
|
"""
|
2020-12-18 14:36:25 +00:00
|
|
|
player: Player = None
|
2020-12-18 14:15:47 +00:00
|
|
|
selected: bool = True
|
|
|
|
store_mode: bool = False
|
2021-01-08 22:15:48 +00:00
|
|
|
chest_mode: bool = False
|
2020-12-18 14:15:47 +00:00
|
|
|
|
2020-12-18 14:07:09 +00:00
|
|
|
def update(self, game: Game) -> None:
|
2020-12-18 14:36:25 +00:00
|
|
|
self.player = game.player
|
2020-12-18 14:07:09 +00:00
|
|
|
self.update_menu(game.inventory_menu)
|
2020-12-18 14:15:47 +00:00
|
|
|
self.store_mode = game.state == GameMode.STORE
|
2021-01-08 22:15:48 +00:00
|
|
|
self.chest_mode = game.state == GameMode.CHEST
|
2020-12-18 14:15:47 +00:00
|
|
|
self.selected = game.state == GameMode.INVENTORY \
|
2021-01-08 22:15:48 +00:00
|
|
|
or (self.store_mode and not game.is_in_store_menu)\
|
|
|
|
or (self.chest_mode and not game.is_in_chest_menu)
|
2020-12-18 14:07:09 +00:00
|
|
|
|
2020-12-04 13:41:59 +00:00
|
|
|
def update_pad(self) -> None:
|
2020-12-12 16:15:08 +00:00
|
|
|
self.menubox.update_title(_("INVENTORY"))
|
2020-12-04 15:28:37 +00:00
|
|
|
for i, item in enumerate(self.menu.values):
|
|
|
|
rep = self.pack[item.name.upper()]
|
2020-12-18 00:50:11 +00:00
|
|
|
selection = f"[{rep}]" if i == self.menu.position \
|
2020-12-18 14:15:47 +00:00
|
|
|
and self.selected else f" {rep} "
|
2020-12-12 16:15:08 +00:00
|
|
|
self.addstr(self.pad, i + 1, 0, selection
|
2020-12-18 00:50:11 +00:00
|
|
|
+ " " + item.translated_name.capitalize()
|
2021-01-08 10:55:25 +00:00
|
|
|
+ (f" ({item.description})" if item.description else "")
|
2020-12-18 00:50:11 +00:00
|
|
|
+ (": " + str(item.price) + " Hazels"
|
2020-12-18 14:15:47 +00:00
|
|
|
if self.store_mode else ""))
|
2020-12-04 15:28:37 +00:00
|
|
|
|
2020-12-18 14:36:25 +00:00
|
|
|
if self.store_mode:
|
|
|
|
price = f"{self.pack.HAZELNUT} {self.player.hazel} Hazels"
|
|
|
|
width = len(price) + (self.pack.tile_width - 1)
|
|
|
|
self.addstr(self.pad, self.height - 3, self.width - width - 2,
|
|
|
|
price, italic=True)
|
2020-12-04 15:28:37 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def truewidth(self) -> int:
|
|
|
|
return max(1, self.height if hasattr(self, "height") else 10)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def trueheight(self) -> int:
|
|
|
|
return 2 + super().trueheight
|
2020-12-11 17:07:24 +00:00
|
|
|
|
2021-01-08 10:21:40 +00:00
|
|
|
def handle_click(self, y: int, x: int, attr: int, game: Game) -> None:
|
2020-12-11 17:07:24 +00:00
|
|
|
"""
|
|
|
|
We can select a menu item with the mouse.
|
|
|
|
"""
|
2020-12-12 16:39:12 +00:00
|
|
|
self.menu.position = max(0, min(len(self.menu.values) - 1, y - 2))
|
2020-12-18 00:50:11 +00:00
|
|
|
game.is_in_store_menu = False
|
2020-12-11 17:07:24 +00:00
|
|
|
game.handle_key_pressed(KeyValues.ENTER)
|
2020-12-11 17:38:13 +00:00
|
|
|
|
2020-12-07 20:22:06 +00:00
|
|
|
|
2020-12-11 17:08:10 +00:00
|
|
|
class StoreInventoryDisplay(MenuDisplay):
|
2020-12-13 20:29:25 +00:00
|
|
|
"""
|
|
|
|
A class to handle the display of a merchant's inventory.
|
|
|
|
"""
|
2020-12-18 14:36:25 +00:00
|
|
|
menu: StoreMenu
|
2020-12-18 14:15:47 +00:00
|
|
|
selected: bool = False
|
|
|
|
|
2020-12-18 14:07:09 +00:00
|
|
|
def update(self, game: Game) -> None:
|
|
|
|
self.update_menu(game.store_menu)
|
2020-12-18 14:15:47 +00:00
|
|
|
self.selected = game.is_in_store_menu
|
2020-12-18 14:07:09 +00:00
|
|
|
|
2020-12-11 17:08:10 +00:00
|
|
|
def update_pad(self) -> None:
|
2020-12-12 16:15:08 +00:00
|
|
|
self.menubox.update_title(_("STALL"))
|
2020-12-11 17:08:10 +00:00
|
|
|
for i, item in enumerate(self.menu.values):
|
|
|
|
rep = self.pack[item.name.upper()]
|
2020-12-18 00:50:11 +00:00
|
|
|
selection = f"[{rep}]" if i == self.menu.position \
|
2020-12-18 14:15:47 +00:00
|
|
|
and self.selected else f" {rep} "
|
2020-12-12 16:15:08 +00:00
|
|
|
self.addstr(self.pad, i + 1, 0, selection
|
2020-12-11 17:17:08 +00:00
|
|
|
+ " " + item.translated_name.capitalize()
|
2021-01-08 10:55:25 +00:00
|
|
|
+ (f" ({item.description})" if item.description else "")
|
2020-12-11 17:17:08 +00:00
|
|
|
+ ": " + str(item.price) + " Hazels")
|
2020-12-07 19:54:53 +00:00
|
|
|
|
2020-12-18 14:36:25 +00:00
|
|
|
price = f"{self.pack.HAZELNUT} {self.menu.merchant.hazel} Hazels"
|
|
|
|
width = len(price) + (self.pack.tile_width - 1)
|
|
|
|
self.addstr(self.pad, self.height - 3, self.width - width - 2, price,
|
|
|
|
italic=True)
|
|
|
|
|
2020-12-11 17:08:10 +00:00
|
|
|
@property
|
|
|
|
def truewidth(self) -> int:
|
|
|
|
return max(1, self.height if hasattr(self, "height") else 10)
|
2020-12-07 20:22:06 +00:00
|
|
|
|
2020-12-11 17:08:10 +00:00
|
|
|
@property
|
|
|
|
def trueheight(self) -> int:
|
|
|
|
return 2 + super().trueheight
|
2020-12-11 17:38:13 +00:00
|
|
|
|
2021-01-08 10:21:40 +00:00
|
|
|
def handle_click(self, y: int, x: int, attr: int, game: Game) -> None:
|
2020-12-11 17:38:13 +00:00
|
|
|
"""
|
|
|
|
We can select a menu item with the mouse.
|
|
|
|
"""
|
2020-12-12 16:39:12 +00:00
|
|
|
self.menu.position = max(0, min(len(self.menu.values) - 1, y - 2))
|
2020-12-18 00:50:11 +00:00
|
|
|
game.is_in_store_menu = True
|
2020-12-11 17:38:13 +00:00
|
|
|
game.handle_key_pressed(KeyValues.ENTER)
|
2021-01-08 22:15:48 +00:00
|
|
|
|
2021-01-08 22:32:47 +00:00
|
|
|
|
2021-01-08 22:15:48 +00:00
|
|
|
class ChestInventoryDisplay(MenuDisplay):
|
|
|
|
"""
|
|
|
|
A class to handle the display of a merchant's inventory.
|
|
|
|
"""
|
|
|
|
menu: ChestMenu
|
|
|
|
selected: bool = False
|
|
|
|
|
|
|
|
def update(self, game: Game) -> None:
|
|
|
|
self.update_menu(game.chest_menu)
|
|
|
|
self.selected = game.is_in_chest_menu
|
|
|
|
|
|
|
|
def update_pad(self) -> None:
|
|
|
|
self.menubox.update_title(_("CHEST"))
|
|
|
|
for i, item in enumerate(self.menu.values):
|
|
|
|
rep = self.pack[item.name.upper()]
|
|
|
|
selection = f"[{rep}]" if i == self.menu.position \
|
|
|
|
and self.selected else f" {rep} "
|
|
|
|
self.addstr(self.pad, i + 1, 0, selection
|
|
|
|
+ " " + item.translated_name.capitalize())
|
|
|
|
|
|
|
|
@property
|
|
|
|
def truewidth(self) -> int:
|
|
|
|
return max(1, self.height if hasattr(self, "height") else 10)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def trueheight(self) -> int:
|
|
|
|
return 2 + super().trueheight
|
|
|
|
|
2021-01-08 22:51:47 +00:00
|
|
|
def handle_click(self, y: int, x: int, attr: int, game: Game) -> None:
|
2021-01-08 22:15:48 +00:00
|
|
|
"""
|
|
|
|
We can select a menu item with the mouse.
|
|
|
|
"""
|
|
|
|
self.menu.position = max(0, min(len(self.menu.values) - 1, y - 2))
|
|
|
|
game.is_in_chest_menu = True
|
|
|
|
game.handle_key_pressed(KeyValues.ENTER)
|