squirrel-battle/dungeonbattle/display/menudisplay.py

49 lines
1.9 KiB
Python

from dungeonbattle.menus import Menu
from typing import Any
import curses
class MenuDisplay:
position: int
def __init__(self, menu : Menu, screen: Any, height : int, width : int, topleftx: int, toplefty: int) :
self.screen = screen
self.values = menu.values
self.width = width
self.height = height
self.trueheight = len(menu.values)
self.truewidth = max(map(len,self.values))
self.topleftx = topleftx
self.toplefty = toplefty
#Menu values are printed in pad
self.pad = curses.newpad(self.trueheight,self.truewidth+1)
for i in range(self.trueheight-1) :
self.pad.addstr(i,0," " + self.values[i])
#Menu box
self.menubox = curses.newpad(self.height,self.width)
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)+"")
def update_pad(self) -> None:
for i in range(self.trueheight) :
self.pad.addstr(i,0," ")
self.pad.addstr(self.position,0,">") #set a marker on the selected line
def refresh(self, position : int) -> None:
self.position = position
if self.height-2>=position-1 :
cornery = 0
elif self.height-2 >= self.trueheight-position :
cornery = self.trueheight-self.height+2
self.menubox.refresh(0, 0, self.toplefty, self.topleftx,
self.height + self.toplefty,
self.width + self.topleftx)
self.update_pad(position)
self.pad.refresh(cornery, 0, self.toplefty+1, self.topleftx+1,
self.height-2 + self.toplefty,
self.width-2 + self.topleftx)