squirrel-battle/squirrelbattle/display/display.py

109 lines
3.1 KiB
Python
Raw Normal View History

2020-11-06 14:34:24 +00:00
import curses
2020-11-10 19:43:30 +00:00
from typing import Any, Optional, Union
2020-11-08 22:40:03 +00:00
2020-11-19 01:18:08 +00:00
from squirrelbattle.display.texturepack import TexturePack
from squirrelbattle.tests.screen import FakePad
2020-11-06 14:34:24 +00:00
class Display:
2020-11-10 19:34:22 +00:00
x: int
y: int
width: int
height: int
pad: Any
2020-11-10 19:34:22 +00:00
2020-11-10 19:43:30 +00:00
def __init__(self, screen: Any, pack: Optional[TexturePack] = None):
2020-11-06 14:34:24 +00:00
self.screen = screen
2020-11-10 19:43:30 +00:00
self.pack = pack or TexturePack.get_pack("ascii")
2020-11-08 22:40:03 +00:00
def newpad(self, height: int, width: int) -> Union[FakePad, Any]:
2020-11-08 22:48:26 +00:00
return curses.newpad(height, width) if self.screen else FakePad()
def init_pair(self, number: int, foreground: int, background: int) -> None:
return curses.init_pair(number, foreground, background) \
if self.screen else None
def color_pair(self, number: int) -> int:
return curses.color_pair(number) if self.screen else 0
def resize(self, y: int, x: int, height: int, width: int,
resize_pad: bool = True) -> None:
self.x = x
self.y = y
self.width = width
self.height = height
if hasattr(self, "pad") and resize_pad:
2020-11-20 17:06:43 +00:00
self.pad.resize(self.height + 1, self.width + 1)
def refresh(self, *args, resize_pad: bool = True) -> None:
if len(args) == 4:
self.resize(*args, resize_pad)
self.display()
2020-11-10 19:34:22 +00:00
def display(self) -> None:
raise NotImplementedError
@property
def rows(self) -> int:
return curses.LINES if self.screen else 42
@property
def cols(self) -> int:
return curses.COLS if self.screen else 42
class VerticalSplit(Display):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.pad = self.newpad(self.rows, 1)
@property
def width(self) -> int:
return 1
@width.setter
def width(self, val: Any) -> None:
pass
def display(self) -> None:
for i in range(self.height):
self.pad.addstr(i, 0, "")
2020-11-20 17:06:43 +00:00
self.pad.refresh(0, 0, self.y, self.x, self.y + self.height - 1, self.x)
class HorizontalSplit(Display):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.pad = self.newpad(1, self.cols)
@property
def height(self) -> int:
return 1
@height.setter
def height(self, val: Any) -> None:
pass
def display(self) -> None:
for i in range(self.width):
self.pad.addstr(0, i, "")
2020-11-20 17:06:43 +00:00
self.pad.refresh(0, 0, self.y, self.x, self.y, self.x + self.width - 1)
2020-11-20 17:09:39 +00:00
2020-11-20 17:06:43 +00:00
class Box(Display):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.pad = self.newpad(self.rows, self.cols)
2020-11-20 17:09:39 +00:00
2020-11-20 17:06:43 +00:00
def display(self) -> None:
self.pad.addstr(0, 0, "" + "" * (self.width - 2) + "")
for i in range(1, self.height - 1):
self.pad.addstr(i, 0, "")
self.pad.addstr(i, self.width - 1, "")
2020-11-20 17:09:39 +00:00
self.pad.addstr(self.height - 1, 0, "" + "" * (self.width - 2) + "")
self.pad.refresh(0, 0, self.y, self.x, self.y + self.height - 1,
self.x + self.width - 1)