squirrel-battle/dungeonbattle/display/menudisplay.py

56 lines
2.1 KiB
Python
Raw Normal View History

2020-11-08 21:59:11 +00:00
from dungeonbattle.display.display import Display
2020-11-08 15:36:21 +00:00
from dungeonbattle.menus import Menu
from typing import Any
2020-11-08 21:48:50 +00:00
2020-11-08 21:59:11 +00:00
class MenuDisplay(Display):
def __init__(self, screen: Any, menu: Menu,
2020-11-08 21:48:50 +00:00
topleftx: int, toplefty: int):
2020-11-08 21:59:11 +00:00
super().__init__(screen)
2020-11-08 15:36:21 +00:00
self.values = menu.values
2020-11-08 21:59:11 +00:00
self.menu = menu
2020-11-08 15:36:21 +00:00
self.trueheight = len(menu.values)
2020-11-08 21:59:11 +00:00
self.truewidth = max(len(item.value) for item in menu.values)
2020-11-08 15:36:21 +00:00
self.topleftx = topleftx
self.toplefty = toplefty
2020-11-08 21:48:50 +00:00
# Menu values are printed in pad
2020-11-08 22:40:03 +00:00
self.pad = self.newpad(self.trueheight, self.truewidth + 1)
2020-11-08 21:48:50 +00:00
# Menu box
2020-11-08 22:40:03 +00:00
self.menubox = self.newpad(self.height, self.width)
def update_pad(self, position: int) -> None:
2020-11-08 21:48:50 +00:00
self.menubox.addstr(0, 0, "" + "" * (self.width - 2) + "")
for i in range(1, self.height - 2):
self.menubox.addstr(i, 0, "" + " " * (self.width - 2) + "")
self.menubox.addstr(self.height - 2, 0,
"" + "" * (self.width - 2) + "")
2020-11-08 15:36:21 +00:00
for i in range(self.trueheight):
self.pad.addstr(i, 0, " " + self.values[i].value)
2020-11-08 21:48:50 +00:00
for i in range(self.trueheight):
self.pad.addstr(i, 0, " ")
# set a marker in front of the selected line
self.pad.addstr(position, 0, ">")
2020-11-08 15:36:21 +00:00
2020-11-08 21:59:11 +00:00
def refresh(self) -> None:
with open("/tmp/log", "a") as f:
f.write(f"{self.width}x{self.height}\n")
self.ensure_resized(self.menubox, self.pad)
2020-11-08 21:59:11 +00:00
if self.height - 2 >= self.menu.position - 1:
2020-11-08 15:36:21 +00:00
cornery = 0
2020-11-08 21:59:11 +00:00
elif self.height - 2 >= self.trueheight - self.menu.position:
2020-11-08 21:48:50 +00:00
cornery = self.trueheight - self.height + 2
self.update_pad(self.menu.position)
2020-11-08 15:36:21 +00:00
self.menubox.refresh(0, 0, self.toplefty, self.topleftx,
2020-11-08 21:48:50 +00:00
self.height + self.toplefty,
self.width + self.topleftx)
self.pad.refresh(cornery, 0, self.toplefty + 1, self.topleftx + 1,
self.height - 3 + self.toplefty,
2020-11-08 21:48:50 +00:00
self.width - 2 + self.topleftx)