Reworked graphics to make it more modular

This commit is contained in:
Nicolas Margulies
2020-11-10 18:08:06 +01:00
parent e522047c74
commit 17530f386c
9 changed files with 155 additions and 132 deletions

View File

@ -1,57 +1,36 @@
import curses
from typing import Any, Union
from typing import Any
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 dungeonbattle.entities.player import Player
class Display:
player: Player
def __init__(self, screen: Any, m: Map, texture: Any):
def __init__(self, screen: Any, pack: Any):
self.screen = screen
self.texture = texture
self.map = m
self.mapdisplay = MapDisplay(self.map, self.texture, self.rows * 4//5, self.cols)
self.statsdisplay = StatsDisplay(self.rows//5, self.cols, self.rows * 4//5, 0)
def refresh(self, m : Map, p : Player) -> None:
self.map = m
self.player = p
self.mapdisplay.refresh(self.map, self.player)
self.statsdisplay.refresh(self.player)
# self.menudisplay.refresh(self.position)
self.pack = pack
def newpad(self, height: int, width: int) -> Union[FakePad, Any]:
return curses.newpad(height, width) if self.screen else FakePad()
def ensure_resized(self, *pads) -> bool:
"""
If the window got resized, ensure that the pads are also resized.
"""
y, x = self.screen.getmaxyx() if self.screen else (0, 0)
for pad in pads:
pad.resize(y, x)
if self.screen and curses.is_term_resized(self.rows, self.cols):
curses.resizeterm(y, x)
return True
return False
def resize(self, y, x, height, width):
self.x = x
self.y = y
self.width = width
self.height = height
def refresh(self, *args):
if len(args) == 4:
self.resize(*args)
self.display()
def display(self):
pass
@property
def rows(self) -> int:
return curses.LINES if self.screen else 42
@property
def height(self) -> int:
return self.rows
@property
def cols(self) -> int:
return curses.COLS if self.screen else 42
@property
def width(self) -> int:
return self.cols