2021-01-10 09:46:17 +00:00
|
|
|
# Copyright (C) 2020-2021 by ÿnérant, eichhornchen, nicomarg, charlse
|
2020-11-27 15:33:17 +00:00
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
2020-11-06 14:34:24 +00:00
|
|
|
import curses
|
2020-12-20 16:54:33 +00:00
|
|
|
import sys
|
2020-12-12 12:46:45 +00:00
|
|
|
from typing import Any, Optional, Tuple, Union
|
2020-11-08 22:40:03 +00:00
|
|
|
|
2020-11-19 01:18:08 +00:00
|
|
|
from squirrelbattle.display.texturepack import TexturePack
|
2020-12-11 16:43:46 +00:00
|
|
|
from squirrelbattle.game import Game
|
2020-11-19 01:18:08 +00:00
|
|
|
from squirrelbattle.tests.screen import FakePad
|
2020-11-10 17:08:06 +00:00
|
|
|
|
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
|
2020-11-10 20:20:11 +00:00
|
|
|
pad: Any
|
2020-11-10 19:34:22 +00:00
|
|
|
|
2020-12-12 12:46:45 +00:00
|
|
|
_color_pairs = {(curses.COLOR_WHITE, curses.COLOR_BLACK): 0}
|
|
|
|
_colors_rgb = {}
|
|
|
|
|
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-12-13 20:29:25 +00:00
|
|
|
"""
|
|
|
|
Overwrites the native curses function of the same name.
|
|
|
|
"""
|
2020-11-08 22:48:26 +00:00
|
|
|
return curses.newpad(height, width) if self.screen else FakePad()
|
2020-11-08 23:44:08 +00:00
|
|
|
|
2020-11-26 19:04:54 +00:00
|
|
|
def truncate(self, msg: str, height: int, width: int) -> str:
|
2020-12-13 20:29:25 +00:00
|
|
|
"""
|
|
|
|
Truncates a string into a string adapted to the width and height of
|
|
|
|
the screen.
|
|
|
|
"""
|
2020-11-26 19:35:10 +00:00
|
|
|
height = max(0, height)
|
|
|
|
width = max(0, width)
|
2020-11-26 19:04:54 +00:00
|
|
|
lines = msg.split("\n")
|
|
|
|
lines = lines[:height]
|
|
|
|
lines = [line[:width] for line in lines]
|
|
|
|
return "\n".join(lines)
|
|
|
|
|
2020-12-12 12:46:45 +00:00
|
|
|
def translate_color(self, color: Union[int, Tuple[int, int, int]]) -> int:
|
|
|
|
"""
|
2020-12-13 20:29:25 +00:00
|
|
|
Translates a tuple (R, G, B) into a curses color index.
|
|
|
|
If we already have a color index, then nothing is processed.
|
2020-12-12 12:46:45 +00:00
|
|
|
If this is a tuple, we construct a new color index if non-existing
|
|
|
|
and we return this index.
|
|
|
|
The values of R, G and B must be between 0 and 1000, and not
|
|
|
|
between 0 and 255.
|
|
|
|
"""
|
|
|
|
if isinstance(color, tuple):
|
|
|
|
# The color is a tuple (R, G, B), that is potentially unknown.
|
|
|
|
# We translate it into a curses color number.
|
|
|
|
if color not in self._colors_rgb:
|
|
|
|
# The color does not exist, we create it.
|
|
|
|
color_nb = len(self._colors_rgb) + 8
|
|
|
|
self.init_color(color_nb, color[0], color[1], color[2])
|
|
|
|
self._colors_rgb[color] = color_nb
|
|
|
|
color = self._colors_rgb[color]
|
|
|
|
return color
|
|
|
|
|
|
|
|
def addstr(self, pad: Any, y: int, x: int, msg: str,
|
|
|
|
fg_color: Union[int, Tuple[int, int, int]] = curses.COLOR_WHITE,
|
|
|
|
bg_color: Union[int, Tuple[int, int, int]] = curses.COLOR_BLACK,
|
|
|
|
*, altcharset: bool = False, blink: bool = False,
|
|
|
|
bold: bool = False, dim: bool = False, invis: bool = False,
|
|
|
|
italic: bool = False, normal: bool = False,
|
|
|
|
protect: bool = False, reverse: bool = False,
|
|
|
|
standout: bool = False, underline: bool = False,
|
|
|
|
horizontal: bool = False, left: bool = False,
|
|
|
|
low: bool = False, right: bool = False, top: bool = False,
|
|
|
|
vertical: bool = False, chartext: bool = False) -> None:
|
2020-11-26 19:04:54 +00:00
|
|
|
"""
|
2020-12-13 20:29:25 +00:00
|
|
|
Displays a message onto the pad.
|
2020-11-26 19:04:54 +00:00
|
|
|
If the message is too large, it is truncated vertically and horizontally
|
2020-12-13 20:29:25 +00:00
|
|
|
The text can be bold, italic, blinking, ... if the right parameters are
|
2020-12-12 12:46:45 +00:00
|
|
|
given. These parameters are translated into curses attributes.
|
|
|
|
The foreground and background colors can be given as curses constants
|
|
|
|
(curses.COLOR_*), or by giving a tuple (R, G, B) that corresponds to
|
|
|
|
the color. R, G, B must be between 0 and 1000, and not 0 and 255.
|
2020-11-26 19:04:54 +00:00
|
|
|
"""
|
|
|
|
height, width = pad.getmaxyx()
|
2020-12-12 12:46:45 +00:00
|
|
|
# Truncate message if it is too large
|
2020-11-26 19:35:10 +00:00
|
|
|
msg = self.truncate(msg, height - y, width - x - 1)
|
|
|
|
if msg.replace("\n", "") and x >= 0 and y >= 0:
|
2020-12-12 12:46:45 +00:00
|
|
|
fg_color = self.translate_color(fg_color)
|
|
|
|
bg_color = self.translate_color(bg_color)
|
|
|
|
|
|
|
|
# Get the pair number for the tuple (fg, bg)
|
|
|
|
# If it does not exist, create it and give a new unique id.
|
|
|
|
if (fg_color, bg_color) in self._color_pairs:
|
|
|
|
pair_nb = self._color_pairs[(fg_color, bg_color)]
|
|
|
|
else:
|
|
|
|
pair_nb = len(self._color_pairs)
|
|
|
|
self.init_pair(pair_nb, fg_color, bg_color)
|
|
|
|
self._color_pairs[(fg_color, bg_color)] = pair_nb
|
|
|
|
|
|
|
|
# Compute curses attributes from the parameters
|
|
|
|
attr = self.color_pair(pair_nb)
|
|
|
|
attr |= curses.A_ALTCHARSET if altcharset else 0
|
|
|
|
attr |= curses.A_BLINK if blink else 0
|
|
|
|
attr |= curses.A_BOLD if bold else 0
|
|
|
|
attr |= curses.A_DIM if dim else 0
|
|
|
|
attr |= curses.A_INVIS if invis else 0
|
2020-12-20 16:54:33 +00:00
|
|
|
# Italic is supported since Python 3.7
|
|
|
|
italic &= sys.version_info >= (3, 7,)
|
2020-12-12 12:46:45 +00:00
|
|
|
attr |= curses.A_ITALIC if italic else 0
|
|
|
|
attr |= curses.A_NORMAL if normal else 0
|
|
|
|
attr |= curses.A_PROTECT if protect else 0
|
|
|
|
attr |= curses.A_REVERSE if reverse else 0
|
|
|
|
attr |= curses.A_STANDOUT if standout else 0
|
|
|
|
attr |= curses.A_UNDERLINE if underline else 0
|
|
|
|
attr |= curses.A_HORIZONTAL if horizontal else 0
|
|
|
|
attr |= curses.A_LEFT if left else 0
|
|
|
|
attr |= curses.A_LOW if low else 0
|
|
|
|
attr |= curses.A_RIGHT if right else 0
|
|
|
|
attr |= curses.A_TOP if top else 0
|
|
|
|
attr |= curses.A_VERTICAL if vertical else 0
|
|
|
|
attr |= curses.A_CHARTEXT if chartext else 0
|
|
|
|
|
|
|
|
return pad.addstr(y, x, msg, attr)
|
2020-11-26 11:35:52 +00:00
|
|
|
|
2020-11-10 21:30:55 +00:00
|
|
|
def init_pair(self, number: int, foreground: int, background: int) -> None:
|
2021-01-09 17:42:11 +00:00
|
|
|
foreground = foreground if self.screen and curses.can_change_color() \
|
|
|
|
and foreground < curses.COLORS \
|
|
|
|
else curses.COLOR_WHITE
|
|
|
|
background = background if self.screen and curses.can_change_color() \
|
|
|
|
and background < curses.COLORS \
|
|
|
|
else curses.COLOR_WHITE
|
2020-11-10 21:30:55 +00:00
|
|
|
return curses.init_pair(number, foreground, background) \
|
2021-01-09 17:42:11 +00:00
|
|
|
if self.screen and curses.can_change_color() \
|
|
|
|
and number < curses.COLOR_PAIRS else None
|
2020-11-10 21:30:55 +00:00
|
|
|
|
|
|
|
def color_pair(self, number: int) -> int:
|
2021-01-09 17:42:11 +00:00
|
|
|
return curses.color_pair(number) if self.screen \
|
|
|
|
and number < curses.COLOR_PAIRS else 0
|
2020-11-10 21:30:55 +00:00
|
|
|
|
2020-12-11 20:17:21 +00:00
|
|
|
def init_color(self, number: int, red: int, green: int, blue: int) -> None:
|
|
|
|
return curses.init_color(number, red, green, blue) \
|
2021-01-09 17:42:11 +00:00
|
|
|
if self.screen and curses.can_change_color() \
|
|
|
|
and number < curses.COLORS else None
|
2020-12-11 20:17:21 +00:00
|
|
|
|
2020-11-11 21:12:05 +00:00
|
|
|
def resize(self, y: int, x: int, height: int, width: int,
|
|
|
|
resize_pad: bool = True) -> None:
|
2020-12-13 20:29:25 +00:00
|
|
|
"""
|
|
|
|
Resizes a pad.
|
|
|
|
"""
|
2020-11-10 17:08:06 +00:00
|
|
|
self.x = x
|
|
|
|
self.y = y
|
|
|
|
self.width = width
|
|
|
|
self.height = height
|
2020-11-26 19:58:46 +00:00
|
|
|
if hasattr(self, "pad") and resize_pad and \
|
|
|
|
self.height >= 0 and self.width >= 0:
|
2020-12-12 20:19:55 +00:00
|
|
|
self.pad.erase()
|
2020-11-20 17:06:43 +00:00
|
|
|
self.pad.resize(self.height + 1, self.width + 1)
|
2020-11-10 17:08:06 +00:00
|
|
|
|
2020-11-11 21:12:05 +00:00
|
|
|
def refresh(self, *args, resize_pad: bool = True) -> None:
|
2020-12-13 20:29:25 +00:00
|
|
|
"""
|
|
|
|
Refreshes a pad
|
|
|
|
"""
|
2020-11-10 17:08:06 +00:00
|
|
|
if len(args) == 4:
|
2020-11-11 21:12:05 +00:00
|
|
|
self.resize(*args, resize_pad)
|
2020-11-10 17:08:06 +00:00
|
|
|
self.display()
|
2020-11-10 19:34:22 +00:00
|
|
|
|
2020-11-26 19:58:46 +00:00
|
|
|
def refresh_pad(self, pad: Any, top_y: int, top_x: int,
|
|
|
|
window_y: int, window_x: int,
|
|
|
|
last_y: int, last_x: int) -> None:
|
|
|
|
"""
|
2020-12-13 20:29:25 +00:00
|
|
|
Refreshes a pad on a part of the window.
|
2020-11-26 19:58:46 +00:00
|
|
|
The refresh starts at coordinates (top_y, top_x) from the pad,
|
|
|
|
and is drawn from (window_y, window_x) to (last_y, last_x).
|
2020-12-13 20:29:25 +00:00
|
|
|
If coordinates are invalid (negative indexes/length...), then nothing
|
2020-11-26 19:58:46 +00:00
|
|
|
is drawn and no error is raised.
|
|
|
|
"""
|
|
|
|
top_y, top_x = max(0, top_y), max(0, top_x)
|
|
|
|
window_y, window_x = max(0, window_y), max(0, window_x)
|
2020-11-26 21:32:25 +00:00
|
|
|
screen_max_y, screen_max_x = self.screen.getmaxyx() if self.screen \
|
2020-11-27 13:04:51 +00:00
|
|
|
else (42, 42)
|
2020-11-26 19:58:46 +00:00
|
|
|
last_y, last_x = min(screen_max_y - 1, last_y), \
|
|
|
|
min(screen_max_x - 1, last_x)
|
|
|
|
|
|
|
|
if last_y >= window_y and last_x >= window_x:
|
|
|
|
# Refresh the pad only if coordinates are valid
|
2020-12-18 15:40:52 +00:00
|
|
|
pad.noutrefresh(top_y, top_x, window_y, window_x, last_y, last_x)
|
2020-11-26 19:58:46 +00:00
|
|
|
|
2020-11-10 19:34:22 +00:00
|
|
|
def display(self) -> None:
|
2020-12-18 14:07:09 +00:00
|
|
|
"""
|
|
|
|
Draw the content of the display and refresh pads.
|
|
|
|
"""
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
def update(self, game: Game) -> None:
|
|
|
|
"""
|
|
|
|
The game state was updated.
|
|
|
|
Indicate what to do with the new state.
|
|
|
|
"""
|
2020-11-10 19:34:22 +00:00
|
|
|
raise NotImplementedError
|
2020-11-08 23:44:08 +00:00
|
|
|
|
2021-01-08 10:21:40 +00:00
|
|
|
def handle_click(self, y: int, x: int, attr: int, game: Game) -> None:
|
2020-12-11 16:40:56 +00:00
|
|
|
"""
|
|
|
|
A mouse click was performed on the coordinates (y, x) of the pad.
|
2020-12-13 20:29:25 +00:00
|
|
|
Maybe it should do something.
|
2020-12-11 16:40:56 +00:00
|
|
|
"""
|
|
|
|
|
2020-11-08 23:44:08 +00:00
|
|
|
@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
|
2020-11-20 15:52:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
class VerticalSplit(Display):
|
2020-12-13 20:29:25 +00:00
|
|
|
"""
|
|
|
|
A class to split the screen in two vertically with a pretty line.
|
|
|
|
"""
|
2020-11-20 15:52:04 +00:00
|
|
|
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):
|
2020-11-26 11:35:52 +00:00
|
|
|
self.addstr(self.pad, i, 0, "┃")
|
2020-11-26 19:58:46 +00:00
|
|
|
self.refresh_pad(self.pad, 0, 0, self.y, self.x,
|
|
|
|
self.y + self.height - 1, self.x)
|
2020-11-20 15:52:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
class HorizontalSplit(Display):
|
2020-12-13 20:29:25 +00:00
|
|
|
"""
|
|
|
|
A class to split the screen in two horizontally with a pretty line.
|
|
|
|
"""
|
2020-11-20 15:52:04 +00:00
|
|
|
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):
|
2020-11-26 11:35:52 +00:00
|
|
|
self.addstr(self.pad, 0, i, "━")
|
2020-11-26 19:58:46 +00:00
|
|
|
self.refresh_pad(self.pad, 0, 0, self.y, self.x, self.y,
|
|
|
|
self.x + self.width - 1)
|
2020-11-20 17:06:43 +00:00
|
|
|
|
2020-11-20 17:09:39 +00:00
|
|
|
|
2020-11-20 17:06:43 +00:00
|
|
|
class Box(Display):
|
2020-12-13 20:29:25 +00:00
|
|
|
"""
|
|
|
|
A class for pretty boxes to print menus and other content.
|
|
|
|
"""
|
2020-12-12 16:15:08 +00:00
|
|
|
title: str = ""
|
|
|
|
|
|
|
|
def update_title(self, title: str) -> None:
|
|
|
|
self.title = title
|
2020-11-20 17:06:43 +00:00
|
|
|
|
2020-11-27 16:52:26 +00:00
|
|
|
def __init__(self, *args, fg_border_color: Optional[int] = None, **kwargs):
|
2020-11-20 17:06:43 +00:00
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
self.pad = self.newpad(self.rows, self.cols)
|
2020-11-27 16:52:26 +00:00
|
|
|
self.fg_border_color = fg_border_color or curses.COLOR_WHITE
|
|
|
|
|
2020-11-20 17:06:43 +00:00
|
|
|
def display(self) -> None:
|
2020-11-27 16:52:26 +00:00
|
|
|
self.addstr(self.pad, 0, 0, "┏" + "━" * (self.width - 2) + "┓",
|
2020-12-12 12:46:45 +00:00
|
|
|
self.fg_border_color)
|
2020-11-20 17:06:43 +00:00
|
|
|
for i in range(1, self.height - 1):
|
2020-12-12 12:46:45 +00:00
|
|
|
self.addstr(self.pad, i, 0, "┃", self.fg_border_color)
|
|
|
|
self.addstr(self.pad, i, self.width - 1, "┃", self.fg_border_color)
|
2020-11-26 11:35:52 +00:00
|
|
|
self.addstr(self.pad, self.height - 1, 0,
|
2020-12-12 12:46:45 +00:00
|
|
|
"┗" + "━" * (self.width - 2) + "┛", self.fg_border_color)
|
2020-12-12 16:15:08 +00:00
|
|
|
|
|
|
|
if self.title:
|
|
|
|
self.addstr(self.pad, 0, (self.width - len(self.title) - 8) // 2,
|
|
|
|
f" == {self.title} == ", curses.COLOR_GREEN,
|
|
|
|
italic=True, bold=True)
|
|
|
|
|
2020-11-26 19:58:46 +00:00
|
|
|
self.refresh_pad(self.pad, 0, 0, self.y, self.x,
|
|
|
|
self.y + self.height - 1, self.x + self.width - 1)
|
2021-01-10 11:35:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
class MessageDisplay(Display):
|
|
|
|
"""
|
|
|
|
A class to handle the display of popup messages.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
|
|
|
|
self.box = Box(fg_border_color=curses.COLOR_RED, *args, **kwargs)
|
|
|
|
self.message = ""
|
|
|
|
self.pad = self.newpad(1, 1)
|
|
|
|
|
|
|
|
def update(self, game: Game) -> None:
|
|
|
|
self.message = game.message
|
|
|
|
|
|
|
|
def display(self) -> None:
|
|
|
|
self.box.refresh(self.y - 1, self.x - 2,
|
|
|
|
self.height + 2, self.width + 4)
|
|
|
|
self.box.display()
|
|
|
|
self.pad.erase()
|
|
|
|
self.addstr(self.pad, 0, 0, self.message, bold=True)
|
|
|
|
self.refresh_pad(self.pad, 0, 0, self.y, self.x,
|
|
|
|
self.height + self.y - 1,
|
|
|
|
self.width + self.x - 1)
|