|
1
|
|
|
|
|
|
|
|
|
|
2
|
|
|
from __future__ import absolute_import |
|
3
|
|
|
|
|
4
|
|
|
import sys |
|
5
|
|
|
|
|
6
|
|
|
import numpy as np |
|
|
|
|
|
|
7
|
|
|
|
|
8
|
|
|
from tcod.tcod import _cdata |
|
9
|
|
|
from tcod.libtcod import ffi, lib |
|
10
|
|
|
from tcod.libtcod import BKGND_DEFAULT, BKGND_SET |
|
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
if sys.version_info[0] == 2: # Python 2 |
|
14
|
|
|
def _fmt(string): |
|
|
|
|
|
|
15
|
|
|
if not isinstance(string, unicode): |
|
|
|
|
|
|
16
|
|
|
string = string.decode('latin-1') |
|
17
|
|
|
return string.replace(u'%', u'%%') |
|
18
|
|
|
else: |
|
19
|
|
|
def _fmt(string): |
|
20
|
|
|
"""Return a string that escapes 'C printf' side effects.""" |
|
21
|
|
|
return string.replace('%', '%%') |
|
22
|
|
|
|
|
23
|
|
|
|
|
24
|
|
|
class _ChBufferArray(np.ndarray): |
|
25
|
|
|
"""Numpy subclass designed to access libtcod's character buffer. |
|
26
|
|
|
|
|
27
|
|
|
This class needs to modify the char_t.cf attribute as a side effect so that |
|
28
|
|
|
libtcod will select the correct characters on flush. |
|
29
|
|
|
""" |
|
30
|
|
|
|
|
31
|
|
|
def __new__(cls, ch_array, cf_array): |
|
32
|
|
|
self = ch_array.view(cls) |
|
33
|
|
|
self._cf_array = cf_array |
|
34
|
|
|
return self |
|
35
|
|
|
|
|
36
|
|
|
def __array_finalize__(self, obj): |
|
37
|
|
|
if obj is None: |
|
38
|
|
|
return |
|
39
|
|
|
self._cf_array = None |
|
40
|
|
|
|
|
41
|
|
|
def __repr__(self): |
|
42
|
|
|
return repr(self.view(np.ndarray)) |
|
|
|
|
|
|
43
|
|
|
|
|
44
|
|
|
def __getitem__(self, index): |
|
45
|
|
|
"""Slicing this array also slices its _cf_array attribute.""" |
|
46
|
|
|
array = np.ndarray.__getitem__(self, index) |
|
47
|
|
|
if self._cf_array is None or array.size == 1: |
|
48
|
|
|
return array.view(np.ndarray) |
|
49
|
|
|
array._cf_array = self._cf_array[index] |
|
50
|
|
|
return array |
|
51
|
|
|
|
|
52
|
|
|
def _covert_ch_to_cf(self, index, ch_arr): |
|
53
|
|
|
"""Apply a set of Unicode variables to libtcod's special format. |
|
54
|
|
|
|
|
55
|
|
|
_cf_array should be the same shape as ch_arr after being sliced by |
|
56
|
|
|
index. |
|
57
|
|
|
""" |
|
58
|
|
|
if lib.TCOD_ctx.max_font_chars == 0: |
|
59
|
|
|
return # libtcod not initialized |
|
60
|
|
|
ch_table = ffi.buffer( |
|
61
|
|
|
lib.TCOD_ctx.ascii_to_tcod[0:lib.TCOD_ctx.max_font_chars]) |
|
62
|
|
|
ch_table = np.frombuffer(ch_table, np.intc) |
|
63
|
|
|
self._cf_array[index] = ch_table[ch_arr.ravel()].reshape(ch_arr.shape) |
|
64
|
|
|
|
|
65
|
|
|
def __setitem__(self, index, value): |
|
66
|
|
|
"""Properly set up the char_t.cf variables as a side effect.""" |
|
67
|
|
|
np.ndarray.__setitem__(self, index, value) |
|
68
|
|
|
if self._cf_array is not None: |
|
69
|
|
|
self._covert_ch_to_cf(index, self[index]) |
|
70
|
|
|
|
|
71
|
|
|
|
|
72
|
|
|
class Console(object): |
|
73
|
|
|
""" |
|
74
|
|
|
Args: |
|
75
|
|
|
width (int): Width of the new Console. |
|
76
|
|
|
height (int): Height of the new Console. |
|
77
|
|
|
|
|
78
|
|
|
.. versionadded:: 2.0 |
|
79
|
|
|
""" |
|
80
|
|
|
|
|
81
|
|
|
def __init__(self, width, height): |
|
82
|
|
|
self.cdata = ffi.gc(lib.TCOD_console_new(width, height), |
|
83
|
|
|
lib.TCOD_console_delete) |
|
84
|
|
|
self._init_setup_console_data() |
|
85
|
|
|
|
|
86
|
|
|
@classmethod |
|
87
|
|
|
def _from_cdata(cls, cdata): |
|
|
|
|
|
|
88
|
|
|
if isinstance(cdata, cls): |
|
89
|
|
|
return cdata |
|
90
|
|
|
self = object.__new__(cls) |
|
91
|
|
|
self.cdata = cdata |
|
92
|
|
|
self._init_setup_console_data() |
|
93
|
|
|
return self |
|
94
|
|
|
|
|
95
|
|
|
def _init_setup_console_data(self): |
|
96
|
|
|
"""Setup numpy arrays over libtcod data buffers.""" |
|
97
|
|
|
import numpy |
|
|
|
|
|
|
98
|
|
|
if self.cdata == ffi.NULL: |
|
99
|
|
|
self._console_data = lib.TCOD_ctx.root |
|
100
|
|
|
else: |
|
101
|
|
|
self._console_data = ffi.cast('TCOD_console_data_t *', self.cdata) |
|
102
|
|
|
|
|
103
|
|
|
def unpack_color(image_cdata): |
|
104
|
|
|
"""return a (height, width, 3) shaped array from an image struct""" |
|
105
|
|
|
color_data = lib.TCOD_image_get_colors(image_cdata) |
|
106
|
|
|
color_buffer = ffi.buffer(color_data[0:self.width * self.height]) |
|
107
|
|
|
array = np.frombuffer(color_buffer, np.uint8) |
|
108
|
|
|
return array.reshape((self.height, self.width, 3)) |
|
109
|
|
|
|
|
110
|
|
|
self._fg = unpack_color(self._console_data.fg_colors) |
|
111
|
|
|
self._bg = unpack_color(self._console_data.bg_colors) |
|
112
|
|
|
|
|
113
|
|
|
buf = self._console_data.ch_array |
|
114
|
|
|
buf = ffi.buffer(buf[0:self.width * self.height]) |
|
115
|
|
|
self._ch = np.frombuffer(buf, np.intc).reshape((self.height, |
|
116
|
|
|
self.width)) |
|
117
|
|
|
|
|
118
|
|
|
@property |
|
119
|
|
|
def width(self): |
|
120
|
|
|
"""int: The width of this Console. (read-only)""" |
|
121
|
|
|
return lib.TCOD_console_get_width(self.cdata) |
|
122
|
|
|
|
|
123
|
|
|
@property |
|
124
|
|
|
def height(self): |
|
125
|
|
|
"""int: The height of this Console. (read-only)""" |
|
126
|
|
|
return lib.TCOD_console_get_height(self.cdata) |
|
127
|
|
|
|
|
128
|
|
|
@property |
|
129
|
|
|
def bg(self): |
|
130
|
|
|
"""A numpy uint8 array with the shape (height, width, 3). |
|
131
|
|
|
|
|
132
|
|
|
You can change the consoles background colors by using this array. |
|
133
|
|
|
|
|
134
|
|
|
Index this array with ``console.bg[y, x, channel]`` |
|
135
|
|
|
""" |
|
136
|
|
|
return self._bg |
|
137
|
|
|
|
|
138
|
|
|
@property |
|
139
|
|
|
def fg(self): |
|
140
|
|
|
"""A numpy uint8 array with the shape (height, width, 3). |
|
141
|
|
|
|
|
142
|
|
|
You can change the consoles foreground colors by using this array. |
|
143
|
|
|
|
|
144
|
|
|
Index this array with ``console.fg[y, x, channel]`` |
|
145
|
|
|
""" |
|
146
|
|
|
return self._fg |
|
147
|
|
|
|
|
148
|
|
|
@property |
|
149
|
|
|
def ch(self): |
|
150
|
|
|
"""A numpy int array with the shape (height, width). |
|
151
|
|
|
|
|
152
|
|
|
You can change the consoles character codes by using this array. |
|
153
|
|
|
|
|
154
|
|
|
Index this array with ``console.ch[y, x]`` |
|
155
|
|
|
""" |
|
156
|
|
|
return self._ch |
|
157
|
|
|
|
|
158
|
|
|
@property |
|
159
|
|
|
def default_bg(self): |
|
160
|
|
|
"""Tuple[int, int, int]: The default background color.""" |
|
161
|
|
|
color = self._console_data.back |
|
162
|
|
|
return color.r, color.g, color.b |
|
163
|
|
|
@default_bg.setter |
|
164
|
|
|
def default_bg(self, color): |
|
|
|
|
|
|
165
|
|
|
self._console_data.back = color |
|
166
|
|
|
|
|
167
|
|
|
@property |
|
168
|
|
|
def default_fg(self): |
|
169
|
|
|
"""Tuple[int, int, int]: The default foreground color.""" |
|
170
|
|
|
color = self._console_data.fore |
|
171
|
|
|
return color.r, color.g, color.b |
|
172
|
|
|
@default_fg.setter |
|
173
|
|
|
def default_fg(self, color): |
|
|
|
|
|
|
174
|
|
|
self._console_data.fore = color |
|
175
|
|
|
|
|
176
|
|
|
@property |
|
177
|
|
|
def default_bg_blend(self): |
|
178
|
|
|
"""int: The default blending mode.""" |
|
179
|
|
|
return self._console_data.bkgnd_flag |
|
180
|
|
|
@default_bg_blend.setter |
|
181
|
|
|
def default_bg_blend(self, value): |
|
|
|
|
|
|
182
|
|
|
self._console_data.bkgnd_flag = value |
|
183
|
|
|
|
|
184
|
|
|
@property |
|
185
|
|
|
def default_alignment(self): |
|
186
|
|
|
"""int: The default text alignment.""" |
|
187
|
|
|
return self._console_data.alignment |
|
188
|
|
|
@default_alignment.setter |
|
189
|
|
|
def default_alignment(self, value): |
|
|
|
|
|
|
190
|
|
|
self._console_data.alignment = value |
|
191
|
|
|
|
|
192
|
|
|
def clear(self): |
|
193
|
|
|
"""Reset this console to its default colors and the space character. |
|
194
|
|
|
""" |
|
195
|
|
|
lib.TCOD_console_clear(self.cdata) |
|
196
|
|
|
|
|
197
|
|
|
def put_char(self, x, y, ch, bg_blend=BKGND_DEFAULT): |
|
198
|
|
|
"""Draw the character c at x,y using the default colors and a blend mode. |
|
|
|
|
|
|
199
|
|
|
|
|
200
|
|
|
Args: |
|
201
|
|
|
x (int): Character x position from the left. |
|
202
|
|
|
y (int): Character y position from the top. |
|
203
|
|
|
ch (int): Character code to draw. Must be in integer form. |
|
204
|
|
|
bg_blend (int): Blending mode to use, defaults to BKGND_DEFAULT. |
|
205
|
|
|
""" |
|
206
|
|
|
lib.TCOD_console_put_char(self.cdata, x, y, ch, bg_blend) |
|
207
|
|
|
|
|
208
|
|
|
def print_(self, x, y, string, bg_blend=BKGND_DEFAULT, alignment=None): |
|
209
|
|
|
"""Print a color formatted string on a console. |
|
210
|
|
|
|
|
211
|
|
|
Args: |
|
212
|
|
|
x (int): Character x position from the left. |
|
213
|
|
|
y (int): Character y position from the top. |
|
214
|
|
|
string (Text): A unicode string optionaly using color codes. |
|
215
|
|
|
""" |
|
216
|
|
|
alignment = self.default_alignment if alignment is None else alignment |
|
217
|
|
|
|
|
218
|
|
|
lib.TCOD_console_print_ex_utf(self.cdata, x, y, |
|
219
|
|
|
bg_blend, alignment, _fmt(string)) |
|
220
|
|
|
|
|
221
|
|
|
def print_rect(self, x, y, width, height, string, |
|
222
|
|
|
bg_blend=BKGND_DEFAULT, alignment=None): |
|
223
|
|
|
"""Print a string constrained to a rectangle. |
|
224
|
|
|
|
|
225
|
|
|
If h > 0 and the bottom of the rectangle is reached, |
|
226
|
|
|
the string is truncated. If h = 0, |
|
227
|
|
|
the string is only truncated if it reaches the bottom of the console. |
|
228
|
|
|
|
|
229
|
|
|
Returns: |
|
230
|
|
|
int: The number of lines of text once word-wrapped. |
|
231
|
|
|
""" |
|
232
|
|
|
alignment = self.default_alignment if alignment is None else alignment |
|
233
|
|
|
return lib.TCOD_console_print_rect_ex_utf(self.cdata, |
|
234
|
|
|
x, y, width, height, bg_blend, alignment, _fmt(string)) |
|
235
|
|
|
|
|
236
|
|
|
def get_height_rect(self, x, y, width, height, string): |
|
237
|
|
|
"""Return the height of this text once word-wrapped into this rectangle. |
|
|
|
|
|
|
238
|
|
|
|
|
239
|
|
|
Returns: |
|
240
|
|
|
int: The number of lines of text once word-wrapped. |
|
241
|
|
|
""" |
|
242
|
|
|
return lib.TCOD_console_get_height_rect_utf( |
|
243
|
|
|
self.cdata, x, y, width, height, _fmt(string)) |
|
244
|
|
|
|
|
245
|
|
|
def rect(self, x, y, width, height, clear, bg_blend=BKGND_DEFAULT): |
|
246
|
|
|
"""Draw a the background color on a rect optionally clearing the text. |
|
247
|
|
|
|
|
248
|
|
|
If clr is True the affected tiles are changed to space character. |
|
249
|
|
|
""" |
|
250
|
|
|
lib.TCOD_console_rect(self.cdata, x, y, width, height, clear, bg_blend) |
|
251
|
|
|
|
|
252
|
|
|
def hline(self, x, y, width, bg_blend=BKGND_DEFAULT): |
|
253
|
|
|
"""Draw a horizontal line on the console. |
|
254
|
|
|
|
|
255
|
|
|
This always uses the character 196, the horizontal line character. |
|
256
|
|
|
""" |
|
257
|
|
|
lib.TCOD_console_hline(self.cdata, x, y, width, bg_blend) |
|
258
|
|
|
|
|
259
|
|
|
def vline(self, x, y, height, bg_blend=BKGND_DEFAULT): |
|
260
|
|
|
"""Draw a vertical line on the console. |
|
261
|
|
|
|
|
262
|
|
|
This always uses the character 179, the vertical line character. |
|
263
|
|
|
""" |
|
264
|
|
|
lib.TCOD_console_vline(self.cdata, x, y, height, bg_blend) |
|
265
|
|
|
|
|
266
|
|
|
def print_frame(self, x, y, width, height, string='', |
|
267
|
|
|
clear=True, bg_blend=BKGND_DEFAULT): |
|
268
|
|
|
"""Draw a framed rectangle with optinal text. |
|
269
|
|
|
|
|
270
|
|
|
This uses the default background color and blend mode to fill the |
|
271
|
|
|
rectangle and the default foreground to draw the outline. |
|
272
|
|
|
|
|
273
|
|
|
string will be printed on the inside of the rectangle, word-wrapped. |
|
274
|
|
|
|
|
275
|
|
|
Note: |
|
276
|
|
|
This method does not support Unicode outside of the 0-255 range. |
|
277
|
|
|
""" |
|
278
|
|
|
lib.TCOD_console_print_frame(self.cdata, x, y, width, height, |
|
279
|
|
|
clear, bg_blend, string.encode('latin-1')) |
|
280
|
|
|
|
|
281
|
|
|
def blit(self, x, y, width, height, |
|
282
|
|
|
dest, dest_x, dest_y, fg_alpha=1.0, bg_alpha=1.0): |
|
283
|
|
|
"""Blit this console from x,y,w,h to the console dst at xdst,ydst.""" |
|
284
|
|
|
lib.TCOD_console_blit(self.cdata, x, y, width, height, |
|
285
|
|
|
_cdata(dest), dest_x, dest_y, fg_alpha, bg_alpha) |
|
286
|
|
|
|
|
287
|
|
|
def set_key_color(self, color): |
|
288
|
|
|
"""Set a consoles blit transparent color.""" |
|
289
|
|
|
lib.TCOD_console_set_key_color(self.cdata, color) |
|
290
|
|
|
|
|
291
|
|
|
def __enter__(self): |
|
292
|
|
|
"""Context manage the root console. |
|
293
|
|
|
|
|
294
|
|
|
When the root console is used in a context, the grapical window will |
|
295
|
|
|
close once the context is left as if :any:`tcod.console_delete` was |
|
296
|
|
|
called on it. |
|
297
|
|
|
|
|
298
|
|
|
This is useful for some Python IDE's, such as IDLE. |
|
299
|
|
|
""" |
|
300
|
|
|
if self.cdata != ffi.NULL: |
|
301
|
|
|
raise NotImplementedError('Only the root console has a context.') |
|
302
|
|
|
return self |
|
303
|
|
|
|
|
304
|
|
|
def __exit__(self, *args): |
|
305
|
|
|
"""Closes the graphical window on exit. |
|
306
|
|
|
|
|
307
|
|
|
Some tcod functions may have undefined behaviour after this point. |
|
308
|
|
|
""" |
|
309
|
|
|
lib.TCOD_console_delete(self.cdata) |
|
310
|
|
|
|
|
311
|
|
|
def __bool__(self): |
|
312
|
|
|
"""Mimic libtcod behavior.""" |
|
313
|
|
|
return self.cdata != ffi.NULL |
|
314
|
|
|
|
|
315
|
|
|
__nonzero__ = __bool__ |
|
316
|
|
|
|
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.