squirrel-battle/squirrelbattle/display/menudisplay.py

103 lines
3.5 KiB
Python
Raw Normal View History

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-11-11 19:36:43 +00:00
from typing import List
2020-11-19 01:18:08 +00:00
from squirrelbattle.menus import Menu, MainMenu
from .display import Display, Box
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
class MenuDisplay(Display):
2020-11-09 00:33:23 +00:00
position: int
2020-11-10 19:34:22 +00:00
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.menubox = Box(*args, **kwargs)
2020-11-10 19:34:22 +00:00
def update_menu(self, menu: Menu) -> None:
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)
for i in range(self.trueheight):
self.addstr(self.pad, i, 0, " " + self.values[i])
2020-11-09 00:33:23 +00:00
def update_pad(self) -> None:
for i in range(self.trueheight):
self.addstr(self.pad, i, 0, " " + self.values[i])
2020-11-10 19:34:22 +00:00
# set a marker on the selected line
self.addstr(self.pad, self.menu.position, 0, ">")
2020-11-09 00:33:23 +00:00
def display(self) -> None:
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
self.menubox.refresh(self.y, self.x, self.height, self.width)
self.pad.erase()
self.update_pad()
self.refresh_pad(self.pad, cornery, 0, self.y + 1, self.x + 2,
self.height - 2 + self.y,
self.width - 2 + self.x)
2020-11-10 19:34:22 +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)
@property
def preferred_width(self) -> int:
return self.truewidth + 6
2020-11-10 19:34:22 +00:00
@property
def preferred_height(self) -> int:
return self.trueheight + 2
@property
2020-11-11 19:36:43 +00:00
def values(self) -> List[str]:
return [str(a) for a in self.menu.values]
2020-11-13 18:08:40 +00:00
class SettingsMenuDisplay(MenuDisplay):
@property
def values(self) -> List[str]:
2020-11-27 21:19:41 +00:00
return [_(a[1][1]) + (" : "
+ ("?" 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
class MainMenuDisplay(Display):
2020-11-10 19:34:22 +00:00
def __init__(self, menu: MainMenu, *args):
super().__init__(*args)
self.menu = menu
2020-11-19 01:49:59 +00:00
with open(ResourceManager.get_asset_path("ascii_art.txt"), "r") as file:
self.title = file.read().split("\n")
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
self.menudisplay = MenuDisplay(self.screen, self.pack)
self.menudisplay.update_menu(self.menu)
2020-11-10 19:34:22 +00:00
def display(self) -> None:
2020-11-10 19:34:22 +00:00
for i in range(len(self.title)):
self.addstr(self.pad, 4 + i, max(self.width // 2
- len(self.title[0]) // 2 - 1, 0), self.title[i])
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)
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)