2020-11-11 19:36:43 +00:00
|
|
|
from typing import List
|
|
|
|
|
2020-11-10 09:01:31 +00:00
|
|
|
from dungeonbattle.menus import Menu, MainMenu
|
2020-11-10 17:08:06 +00:00
|
|
|
from .display import Display
|
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-09 00:33:23 +00:00
|
|
|
position: int
|
2020-11-10 19:34:22 +00:00
|
|
|
|
|
|
|
def __init__(self, *args):
|
2020-11-10 17:08:06 +00:00
|
|
|
super().__init__(*args)
|
2020-11-10 19:34:22 +00:00
|
|
|
self.menubox = self.newpad(self.rows, self.cols)
|
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-10 18:40:59 +00:00
|
|
|
self.trueheight = len(self.values)
|
|
|
|
self.truewidth = max([len(a) for a in self.values])
|
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.pad.addstr(i, 0, " " + self.values[i])
|
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-11-11 13:46:25 +00:00
|
|
|
self.pad.addstr(i, 0, " " + self.values[i])
|
2020-11-10 19:34:22 +00:00
|
|
|
# set a marker on the selected line
|
|
|
|
self.pad.addstr(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
|
|
|
|
self.menubox.addstr(0, 0, "┏" + "━" * (self.width - 2) + "┓")
|
|
|
|
for i in range(1, self.height - 1):
|
|
|
|
self.menubox.addstr(i, 0, "┃" + " " * (self.width - 2) + "┃")
|
|
|
|
self.menubox.addstr(self.height - 1, 0,
|
|
|
|
"┗" + "━" * (self.width - 2) + "┛")
|
|
|
|
|
2020-11-10 17:08:06 +00:00
|
|
|
self.menubox.refresh(0, 0, self.y, self.x,
|
2020-11-10 19:34:22 +00:00
|
|
|
self.height + self.y,
|
|
|
|
self.width + self.x)
|
2020-11-10 17:08:06 +00:00
|
|
|
self.update_pad()
|
2020-11-10 19:34:22 +00:00
|
|
|
self.pad.refresh(cornery, 0, self.y + 1, self.x + 2,
|
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
|
|
|
|
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-10 19:34:22 +00:00
|
|
|
|
2020-11-10 17:08:06 +00:00
|
|
|
class MainMenuDisplay(Display):
|
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
|
2020-11-10 17:08:06 +00:00
|
|
|
self.pad = self.newpad(self.rows, self.cols)
|
2020-11-10 09:01:31 +00:00
|
|
|
|
2020-11-10 18:40:59 +00:00
|
|
|
with open("resources/ascii_art.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-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)):
|
|
|
|
self.pad.addstr(4 + i, self.width // 2
|
|
|
|
- len(self.title[0]) // 2 - 1, self.title[i])
|
|
|
|
self.pad.refresh(0, 0, self.y, self.x, self.height, self.width)
|
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)
|