|
10 | 10 | import warnings |
11 | 11 | from os import PathLike |
12 | 12 | from pathlib import Path |
13 | | -from typing import Any, Iterable |
| 13 | +from typing import Any, Iterable, overload |
14 | 14 |
|
15 | 15 | import numpy as np |
16 | 16 | from numpy.typing import ArrayLike, NDArray |
@@ -552,6 +552,7 @@ def __deprecate_defaults( # noqa: C901, PLR0912 |
552 | 552 | stacklevel=3, |
553 | 553 | ) |
554 | 554 |
|
| 555 | + @deprecated("Switch to using keywords and then replace with 'console.print(...)'") |
555 | 556 | def print_( |
556 | 557 | self, |
557 | 558 | x: int, |
@@ -579,6 +580,7 @@ def print_( |
579 | 580 | alignment = self.default_alignment if alignment is None else alignment |
580 | 581 | lib.TCOD_console_printf_ex(self.console_c, x, y, bg_blend, alignment, _fmt(string)) |
581 | 582 |
|
| 583 | + @deprecated("Switch to using keywords and then replace with 'console.print(...)'") |
582 | 584 | def print_rect( # noqa: PLR0913 |
583 | 585 | self, |
584 | 586 | x: int, |
@@ -973,55 +975,159 @@ def __str__(self) -> str: |
973 | 975 | """Return a simplified representation of this consoles contents.""" |
974 | 976 | return "<{}>".format("\n ".join("".join(chr(c) for c in line) for line in self._tiles["ch"])) |
975 | 977 |
|
| 978 | + @overload |
| 979 | + @deprecated( |
| 980 | + "Switch all parameters to keywords such as 'console.print(x=..., y=..., text=..., width=..., height=..., fg=..., bg=..., bg_blend=..., alignment=...)'" |
| 981 | + "\n'string' keyword should be renamed to `text`" |
| 982 | + ) |
| 983 | + def print( |
| 984 | + self, |
| 985 | + x: int, |
| 986 | + y: int, |
| 987 | + text: str, |
| 988 | + fg: tuple[int, int, int] | None = None, |
| 989 | + bg: tuple[int, int, int] | None = None, |
| 990 | + bg_blend: int = tcod.constants.BKGND_SET, |
| 991 | + alignment: int = tcod.constants.LEFT, |
| 992 | + *, |
| 993 | + string: str = "", |
| 994 | + ) -> int: ... |
| 995 | + |
| 996 | + @overload |
| 997 | + def print( |
| 998 | + self, |
| 999 | + x: int, |
| 1000 | + y: int, |
| 1001 | + text: str, |
| 1002 | + *, |
| 1003 | + width: int | None = None, |
| 1004 | + height: int | None = None, |
| 1005 | + fg: tuple[int, int, int] | None = None, |
| 1006 | + bg: tuple[int, int, int] | None = None, |
| 1007 | + bg_blend: int = tcod.constants.BKGND_SET, |
| 1008 | + alignment: int = tcod.constants.LEFT, |
| 1009 | + ) -> int: ... |
| 1010 | + |
976 | 1011 | def print( # noqa: PLR0913 |
977 | 1012 | self, |
978 | 1013 | x: int, |
979 | 1014 | y: int, |
980 | | - string: str, |
| 1015 | + text: str = "", |
981 | 1016 | fg: tuple[int, int, int] | None = None, |
982 | 1017 | bg: tuple[int, int, int] | None = None, |
983 | 1018 | bg_blend: int = tcod.constants.BKGND_SET, |
984 | 1019 | alignment: int = tcod.constants.LEFT, |
985 | | - ) -> None: |
986 | | - r"""Print a string on a console with manual line breaks. |
| 1020 | + *, |
| 1021 | + width: int | None = None, |
| 1022 | + height: int | None = None, |
| 1023 | + string: str = "", |
| 1024 | + ) -> int: |
| 1025 | + r"""Print a string of Unicode text on this console. |
987 | 1026 |
|
988 | | - `x` and `y` are the starting tile, with ``0,0`` as the upper-left |
989 | | - corner of the console. |
| 1027 | + Prefer using keywords for this method call to avoid ambiguous parameters. |
990 | 1028 |
|
991 | | - `string` is a Unicode string which may include color control |
992 | | - characters. Strings which are too long will be truncated until the |
993 | | - next newline character ``"\n"``. |
| 1029 | + Args: |
| 1030 | + x: Starting X coordinate, with the left-most tile as zero. |
| 1031 | + y: Starting Y coordinate, with the top-most tile as zero. |
| 1032 | + text: A Unicode string which may include color control characters. |
| 1033 | + width: Width in tiles to constrain the printing region. |
| 1034 | + If a `width` is given then `text` will have automatic word wrapping applied to it. |
| 1035 | + A `width` of `None` means `text` will only have manual line breaks. |
| 1036 | + height: Height in tiles to constrain the printing region. |
| 1037 | + fg: RGB tuple to use as the foreground color, or `None` to leave the foreground unchanged. |
| 1038 | + Tuple values should be 0-255. |
| 1039 | + bg: RGB tuple to use as the background color, or `None` to leave the foreground unchanged. |
| 1040 | + Tuple values should be 0-255. |
| 1041 | + bg_blend: Background blend type used by libtcod. |
| 1042 | + Typically starts with `libtcodpy.BKGND_*`. |
| 1043 | + alignment: One of `libtcodpy.LEFT`, `libtcodpy.CENTER`, or `libtcodpy.RIGHT` |
| 1044 | + string: Older deprecated name of the `text` parameter. |
994 | 1045 |
|
995 | | - `fg` and `bg` are the foreground text color and background tile color |
996 | | - respectfully. This is a 3-item tuple with (r, g, b) color values from |
997 | | - 0 to 255. These parameters can also be set to `None` to leave the |
998 | | - colors unchanged. |
| 1046 | + Returns: |
| 1047 | + The height of `text` in lines via word wrapping and line breaks. |
999 | 1048 |
|
1000 | | - `bg_blend` is the blend type used by libtcod. |
| 1049 | + Example:: |
1001 | 1050 |
|
1002 | | - `alignment` can be `tcod.LEFT`, `tcod.CENTER`, or `tcod.RIGHT`. |
| 1051 | + >>> from tcod import libtcodpy |
| 1052 | + >>> console = tcod.console.Console(20, 1) |
| 1053 | + >>> console.clear(ch=ord('·')) |
| 1054 | + >>> console.print(x=0, y=0, text="left") |
| 1055 | + 1 |
| 1056 | + >>> console.print(x=console.width, y=0, text="right", alignment=libtcodpy.RIGHT) |
| 1057 | + 1 |
| 1058 | + >>> console.print(x=10, y=0, text="center", alignment=libtcodpy.CENTER) |
| 1059 | + 1 |
| 1060 | + >>> print(console) |
| 1061 | + <left···center··right> |
| 1062 | +
|
| 1063 | + >>> console = tcod.console.Console(20, 4) |
| 1064 | + >>> console.clear(ch=ord('·')) |
| 1065 | + >>> console.print(x=1, y=1, text="words within bounds", width=8) |
| 1066 | + 3 |
| 1067 | + >>> print(console) |
| 1068 | + <···················· |
| 1069 | + ·words·············· |
| 1070 | + ·within············· |
| 1071 | + ·bounds·············> |
| 1072 | + >>> WHITE = (255, 255, 255) |
| 1073 | + >>> BLACK = (0, 0, 0) |
| 1074 | + >>> console.print(x=0, y=0, text="Black text on white background", fg=BLACK, bg=WHITE) |
| 1075 | + 1 |
1003 | 1076 |
|
1004 | 1077 | .. versionadded:: 8.5 |
1005 | 1078 |
|
1006 | 1079 | .. versionchanged:: 9.0 |
| 1080 | +
|
1007 | 1081 | `fg` and `bg` now default to `None` instead of white-on-black. |
1008 | 1082 |
|
1009 | 1083 | .. versionchanged:: 13.0 |
| 1084 | +
|
1010 | 1085 | `x` and `y` are now always used as an absolute position for negative values. |
| 1086 | +
|
| 1087 | + .. versionchanged:: Unreleased |
| 1088 | +
|
| 1089 | + Added `text` parameter to replace `string`. |
| 1090 | +
|
| 1091 | + Added `width` and `height` keyword parameters to bind text to a rectangle and replace other print functions. |
| 1092 | + Right-aligned text with `width=None` now treats the `x` coordinate as a past-the-end index, this will shift |
| 1093 | + the text of older calls to the left by 1 tile. |
| 1094 | +
|
| 1095 | + Now returns the number of lines printed via word wrapping. |
1011 | 1096 | """ |
1012 | | - string_ = string.encode("utf-8") |
1013 | | - lib.TCOD_console_printn( |
1014 | | - self.console_c, |
1015 | | - x, |
1016 | | - y, |
1017 | | - len(string_), |
1018 | | - string_, |
1019 | | - (fg,) if fg is not None else ffi.NULL, |
1020 | | - (bg,) if bg is not None else ffi.NULL, |
1021 | | - bg_blend, |
1022 | | - alignment, |
| 1097 | + if width is not None and width <= 0: |
| 1098 | + return 0 |
| 1099 | + if width is None and alignment == tcod.constants.LEFT: # Fix alignment |
| 1100 | + width = 0x100000 |
| 1101 | + if width is None and alignment == tcod.constants.CENTER: # Fix center alignment |
| 1102 | + x -= 0x100000 |
| 1103 | + width = 0x200000 |
| 1104 | + if width is None and alignment == tcod.constants.RIGHT: # Fix right alignment |
| 1105 | + x -= 0x100000 |
| 1106 | + width = 0x100000 |
| 1107 | + rgb_fg = ffi.new("TCOD_ColorRGB*", fg) if fg is not None else ffi.NULL |
| 1108 | + rgb_bg = ffi.new("TCOD_ColorRGB*", bg) if bg is not None else ffi.NULL |
| 1109 | + utf8 = (string or text).encode("utf-8") |
| 1110 | + return _check( |
| 1111 | + int( |
| 1112 | + lib.TCOD_printn_rgb( |
| 1113 | + self.console_c, |
| 1114 | + { |
| 1115 | + "x": x, |
| 1116 | + "y": y, |
| 1117 | + "width": width or 0, |
| 1118 | + "height": height or 0, |
| 1119 | + "fg": rgb_fg, |
| 1120 | + "bg": rgb_bg, |
| 1121 | + "flag": bg_blend, |
| 1122 | + "alignment": alignment, |
| 1123 | + }, |
| 1124 | + len(utf8), |
| 1125 | + utf8, |
| 1126 | + ) |
| 1127 | + ) |
1023 | 1128 | ) |
1024 | 1129 |
|
| 1130 | + @deprecated("Switch to using keywords and then replace with 'console.print(...)'") |
1025 | 1131 | def print_box( # noqa: PLR0913 |
1026 | 1132 | self, |
1027 | 1133 | x: int, |
@@ -1144,9 +1250,9 @@ def draw_frame( # noqa: PLR0913 |
1144 | 1250 | >>> console.draw_frame(x=6, y=0, width=3, height=3, decoration="123456789") |
1145 | 1251 | >>> console.draw_frame(x=9, y=0, width=3, height=3, decoration="/-\\| |\\-/") |
1146 | 1252 | >>> console.draw_frame(x=0, y=3, width=12, height=3) |
1147 | | - >>> console.print_box(x=0, y=3, width=12, height=1, string=" Title ", alignment=libtcodpy.CENTER) |
| 1253 | + >>> console.print(x=0, y=3, width=12, height=1, string=" Title ", alignment=libtcodpy.CENTER) |
1148 | 1254 | 1 |
1149 | | - >>> console.print_box(x=0, y=5, width=12, height=1, string="┤Lower├", alignment=libtcodpy.CENTER) |
| 1255 | + >>> console.print(x=0, y=5, width=12, height=1, string="┤Lower├", alignment=libtcodpy.CENTER) |
1150 | 1256 | 1 |
1151 | 1257 | >>> print(console) |
1152 | 1258 | <┌─┐╔═╗123/-\ |
|
0 commit comments