repaired display

This commit is contained in:
eichhornchen
2020-11-09 01:33:23 +01:00
parent 8d5d9d38ff
commit a4876bb7af
7 changed files with 118 additions and 65 deletions

View File

@@ -2,14 +2,28 @@ import curses
from typing import Any, Union
from dungeonbattle.tests.screen import FakePad
from dungeonbattle.display.mapdisplay import MapDisplay
from dungeonbattle.display.statsdisplay import StatsDisplay
from dungeonbattle.display.menudisplay import MenuDisplay
from dungeonbattle.interfaces import Map
from .entities.player import Player
class Display:
def __init__(self, screen: Any):
map: Map
player: Player
def __init__(self, screen: Any, texture: Any):
self.screen = screen
self.texture = texture
self.mapdisplay = MapDisplay(self.screen, self.texture, curses.LINES * 4//5, curses.COLS)
self.statsdisplay = StatsDisplay(self.screen, curses.LINES//5, curses.COLS)
def refresh(self) -> None:
raise NotImplementedError
def refresh(self, m : Map, p : Player) -> None:
self.map = m
self.player = p
self.mapdisplay.refresh(m, p, )
self.statsdisplay.refresh(self.player)
self.menudisplay.refresh(self.position)
def newpad(self, height: int, width: int) -> Union[FakePad, Any]:
return curses.newpad(height, width) if self.screen else FakePad()

View File

@@ -23,9 +23,10 @@ class MapDisplay:
for e in self.map.entities:
self.pad.addstr(e.y, e.x, self.pack.PLAYER)
def display(self, m : Map, p : Player y: int, x: int) -> None:
def display(self, m : Map, p : Player) -> None:
self.map = m
self.player = p
y, x = self.map.currenty, self.map.currentx
deltay, deltax = (self.height // 2) + 1, (self.width // 2) + 1
pminrow, pmincol = y - deltay, x - deltax
sminrow, smincol = max(-pminrow, 0), max(-pmincol, 0)

View File

@@ -1,55 +1,48 @@
from dungeonbattle.display.display import Display
from dungeonbattle.menus import Menu
from typing import Any
import curses
class MenuDisplay(Display):
def __init__(self, screen: Any, menu: Menu,
topleftx: int, toplefty: int):
super().__init__(screen)
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.menu = menu
self.width = width
self.height = height
self.trueheight = len(menu.values)
self.truewidth = max(len(item.value) for item in menu.values)
self.truewidth = max(map(len,self.values))
self.topleftx = topleftx
self.toplefty = toplefty
# Menu values are printed in pad
self.pad = self.newpad(self.trueheight, self.truewidth + 1)
#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 = self.newpad(self.height, self.width)
#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 update_pad(self, position: int) -> None:
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) + "")
for i in range(self.trueheight):
self.pad.addstr(i, 0, " " + self.values[i].value)
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, ">")
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)
if self.height - 2 >= self.menu.position - 1:
def refresh(self, position : int) -> None:
self.position = position
if self.height-2>=position-1 :
cornery = 0
elif self.height - 2 >= self.trueheight - self.menu.position:
cornery = self.trueheight - self.height + 2
self.update_pad(self.menu.position)
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.pad.refresh(cornery, 0, self.toplefty + 1, self.topleftx + 1,
self.height - 3 + self.toplefty,
self.width - 2 + 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)

View File

@@ -5,19 +5,22 @@ from dungeonbattle.entities.player import Player
class StatsDisplay(Display):
def __init__(self, screen: Any, player: Player,
self.player: Player
def __init__(self, screen: Any, height: int, width: int,
topleftx: int, toplefty: int):
super().__init__(screen)
self.width = width
self.height = height
self.topleftx = topleftx
self.toplefty = toplefty
self.player = player
self.pad = self.newpad(self.height, self.width)
self.pad = self.newpad(height, width)
def update_pad(self) -> None:
string = ""
for i in range(self.width - 1):
string = string + "-"
string = string
self.pad.addstr(0, 0, string)
string2 = "Player -- LVL {} EXP {}/{} HP {}/{}"\
.format(self.player.level, self.player.current_xp,
@@ -34,8 +37,8 @@ class StatsDisplay(Display):
string3 = string3 + " "
self.pad.addstr(2, 0, string3)
def refresh(self) -> None:
self.ensure_resized(self.pad)
def refresh(self, p : Player) -> None:
self.player = p
self.pad.clear()
self.update_pad()
self.pad.refresh(0, 0, self.toplefty, self.topleftx,