Skip to content

Commit 0bee024

Browse files
committed
Add frame decoration options and deprecate older handling of indexes.
1 parent 6df1b1f commit 0bee024

File tree

2 files changed

+61
-20
lines changed

2 files changed

+61
-20
lines changed

CHANGELOG.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@ v2.0.0
88

99
Unreleased
1010
------------------
11+
Added
12+
- Added the *decoration* parameter to *Console.draw_frame*.
13+
You may use this parameter to designate custom glyphs as the frame border.
14+
15+
Deprecated
16+
- The handling of negative indexes given to console drawing and printing
17+
functions will be changed to be used as absolute coordinates in the future.
1118

1219
12.5.1 - 2021-05-30
1320
-------------------

tcod/console.py

Lines changed: 54 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import os
77
import pathlib
88
import warnings
9-
from typing import Any, Iterable, Optional, Tuple, Union
9+
from typing import Any, Iterable, Optional, Sequence, Tuple, Union
1010

1111
import numpy as np
1212
from typing_extensions import Literal
@@ -925,8 +925,9 @@ def __str__(self) -> str:
925925
def _pythonic_index(self, x: int, y: int) -> Tuple[int, int]:
926926
if __debug__ and (x < 0 or y < 0):
927927
warnings.warn(
928-
"How negative indexes are handled my change in the future.",
929-
PendingDeprecationWarning,
928+
"Negative indexes will be treated as absolute coordinates instead of relative in the future."
929+
" The current behavior of indexing from the end is deprecated.",
930+
FutureWarning,
930931
stacklevel=3,
931932
)
932933
if x < 0:
@@ -948,9 +949,7 @@ def print(
948949
"""Print a string on a console with manual line breaks.
949950
950951
`x` and `y` are the starting tile, with ``0,0`` as the upper-left
951-
corner of the console. You can use negative numbers if you want to
952-
start printing relative to the bottom-right corner, but this behavior
953-
may change in future versions.
952+
corner of the console.
954953
955954
`string` is a Unicode string which may include color control
956955
characters. Strings which are too long will be truncated until the
@@ -999,9 +998,7 @@ def print_box(
999998
"""Print a string constrained to a rectangle and return the height.
1000999
10011000
`x` and `y` are the starting tile, with ``0,0`` as the upper-left
1002-
corner of the console. You can use negative numbers if you want to
1003-
start printing relative to the bottom-right corner, but this behavior
1004-
may change in future versions.
1001+
corner of the console.
10051002
10061003
`width` and `height` determine the bounds of the rectangle, the text
10071004
will automatically be word-wrapped to fit within these bounds.
@@ -1054,13 +1051,13 @@ def draw_frame(
10541051
fg: Optional[Tuple[int, int, int]] = None,
10551052
bg: Optional[Tuple[int, int, int]] = None,
10561053
bg_blend: int = tcod.constants.BKGND_SET,
1054+
*,
1055+
decoration: Union[str, Tuple[int, int, int, int, int, int, int, int, int]] = "┌─┐│ │└─┘",
10571056
) -> None:
10581057
"""Draw a framed rectangle with an optional title.
10591058
10601059
`x` and `y` are the starting tile, with ``0,0`` as the upper-left
1061-
corner of the console. You can use negative numbers if you want to
1062-
start printing relative to the bottom-right corner, but this behavior
1063-
may change in future versions.
1060+
corner of the console.
10641061
10651062
`width` and `height` determine the size of the frame.
10661063
@@ -1076,21 +1073,60 @@ def draw_frame(
10761073
10771074
`bg_blend` is the blend type used by libtcod.
10781075
1076+
`decoration` is a sequence of glyphs to use for rendering the borders.
1077+
This a str or tuple of int's with 9 items with the items arranged in
1078+
row-major order.
1079+
If a `decoration` is given then `title` can not be used because the
1080+
style for `title` is hard-coded. You can easily print along the upper
1081+
or lower border of the frame manually.
1082+
10791083
.. versionadded:: 8.5
10801084
10811085
.. versionchanged:: 9.0
10821086
`fg` and `bg` now default to `None` instead of white-on-black.
1087+
1088+
.. versionchanged:: 12.6
1089+
Added `decoration` parameter.
10831090
"""
10841091
x, y = self._pythonic_index(x, y)
1085-
title_ = title.encode("utf-8") # type: bytes
1086-
lib.TCOD_console_printn_frame(
1087-
self.console_c,
1092+
if title and decoration != "┌─┐│ │└─┘":
1093+
raise TypeError(
1094+
"The title and decoration parameters are mutually exclusive. You should print the title manually."
1095+
)
1096+
if title:
1097+
warnings.warn(
1098+
"The title parameter will be removed in the future since the style is hard-coded.",
1099+
PendingDeprecationWarning,
1100+
stacklevel=2,
1101+
)
1102+
title_ = title.encode("utf-8") # type: bytes
1103+
lib.TCOD_console_printn_frame(
1104+
self.console_c,
1105+
x,
1106+
y,
1107+
width,
1108+
height,
1109+
len(title_),
1110+
title_,
1111+
(fg,) if fg is not None else ffi.NULL,
1112+
(bg,) if bg is not None else ffi.NULL,
1113+
bg_blend,
1114+
clear,
1115+
)
1116+
return
1117+
decoration_: Sequence[int]
1118+
if isinstance(decoration, str):
1119+
decoration_ = [ord(c) for c in decoration]
1120+
else:
1121+
decoration_ = decoration
1122+
if len(decoration_) != 9:
1123+
raise TypeError(f"Decoration must have a length of 9 (len(decoration) is {len(decoration_)}.)")
1124+
lib.TCOD_console_draw_frame_rgb(
10881125
x,
10891126
y,
10901127
width,
10911128
height,
1092-
len(title_),
1093-
title_,
1129+
decoration_,
10941130
(fg,) if fg is not None else ffi.NULL,
10951131
(bg,) if bg is not None else ffi.NULL,
10961132
bg_blend,
@@ -1111,9 +1147,7 @@ def draw_rect(
11111147
"""Draw characters and colors over a rectangular region.
11121148
11131149
`x` and `y` are the starting tile, with ``0,0`` as the upper-left
1114-
corner of the console. You can use negative numbers if you want to
1115-
start printing relative to the bottom-right corner, but this behavior
1116-
may change in future versions.
1150+
corner of the console.
11171151
11181152
`width` and `height` determine the size of the rectangle.
11191153

0 commit comments

Comments
 (0)