@@ -184,17 +184,29 @@ class Color(object):
184
184
"""
185
185
186
186
def __new__ (cls , * rgb ):
187
+ self = object .__new__ (cls )
187
188
if not rgb :
188
- self = object .__new__ (cls )
189
- self ._r = self ._g = self ._b = 0
189
+ return self # blank (0, 0, 0)
190
+ try : # check if int-like
191
+ rgb = int (rgb )
192
+ self ._r , self ._g , self ._b = _lib .TDL_color_int_to_array (color )[0 :3 ]
190
193
return self
191
- length = len ( rgb )
192
- if length == 3 :
193
- self = object . __new__ ( cls )
194
+ except TypeError :
195
+ pass # not an int
196
+ try : # try to unpack (r, g, b )
194
197
self .r , self .g , self .b = rgb
195
198
return self
196
- if length == 1 :
197
- return cls .from_int (int (rgb [0 ]))
199
+ except TypeError :
200
+ pass # not a tuple-like
201
+ except ValueError :
202
+ pass # not 3 items
203
+ try : # try to unpack ((r, g, b),)
204
+ (self .r , self .g , self .b ), = rgb
205
+ return self
206
+ except TypeError :
207
+ pass
208
+ except ValueError :
209
+ pass
198
210
raise TypeError ('Parameters must be (r,g,b) or (int), got: %a' %
199
211
repr (rgb ))
200
212
@@ -234,6 +246,11 @@ def _set_b(self, value):
234
246
g = property (_get_g , _set_g )
235
247
b = property (_get_b , _set_b )
236
248
249
+ def __eq__ (self , other ):
250
+ return (self ._r == other ._r and
251
+ self ._g == other ._g and
252
+ self ._b == other ._b )
253
+
237
254
def __repr__ (self ):
238
255
return '<%s[%i, %i, %i]>' % (self .__class__ .__name__ ,
239
256
self ._r , self ._g , self ._b )
@@ -307,51 +324,129 @@ def _get_slice(self, slice_x, slice_y):
307
324
slice_x = slice (slice_y , slice_y + 1 )
308
325
return self .__class__ (self ._console , self ._range_x [slice_x ],
309
326
self ._range_y [slice_y ])
310
-
311
- class _AttributeCh (_ConsoleAttribute ):
312
-
327
+
313
328
def __getitem__ (self , key ):
314
329
if isinstance (key [0 ], slice ) or isinstance (key [1 ], slice ):
315
330
return self ._get_slice (* key )
316
- x = self ._range_x [key [0 ]]
317
- y = self ._range_y [key [1 ]]
318
- return _lib .TCOD_console_get_char (self ._console .tcod_console , x , y )
331
+ return self ._get_item (self ._range_x [key [0 ]], self ._range_y [key [1 ]])
332
+
333
+ def _get_item (self , x , y ):
334
+ raise NotImplementedError ('function should be overwritten by subclass' )
335
+
336
+ def __setitem__ (self , key , value ):
337
+ if isinstance (key [0 ], slice ) or isinstance (key [1 ], slice ):
338
+ x , y = key
339
+ if not isinstance (x , slice ):
340
+ x = slice (x , x + 1 )
341
+ if not isinstance (y , slice ):
342
+ y = slice (y , y + 1 )
343
+ self ._set_range (self ._range_x [x ], self ._range_y [y ])
344
+ else :
345
+ self ._set_item (self ._range_x [key [0 ]], self ._range_y [key [1 ]], value )
346
+
347
+ def _set_item (self , x , y , value ):
348
+ raise NotImplementedError ('function should be overwritten by subclass' )
319
349
320
- def __setitem__ (self , key , ch ):
321
- x = self ._range_x [key [0 ]]
322
- y = self ._range_y [key [1 ]]
350
+ def _set_range (self , range_x , range_y , value ):
351
+ raise NotImplementedError ('function should be overwritten by subclass' )
352
+
353
+ class _AttributeCh (_ConsoleAttribute ):
354
+
355
+ def _get_item (self , x , y ):
356
+ return _lib .TCOD_console_get_char (self ._console .tcod_console , x , y )
357
+
358
+ def _set_item (self , x , y , ch ):
323
359
_lib .TCOD_console_set_char (self ._console .tcod_console , x , y , ch )
324
360
361
+ def _set_range (self , range_x , range_y , ch ):
362
+ for x in range_x :
363
+ for y in range_y :
364
+ _lib .TCOD_console_set_char (self ._console .tcod_console , x , y , ch )
365
+
366
+ # def __getitem__(self, key):
367
+ # if isinstance(key[0], slice) or isinstance(key[1], slice):
368
+ # return self._get_slice(*key)
369
+ # x = self._range_x[key[0]]
370
+ # y = self._range_y[key[1]]
371
+ # return _lib.TCOD_console_get_char(self._console.tcod_console, x, y)
372
+
373
+ # def __setitem__(self, key, ch):
374
+ # if isinstance(key[0], slice) or isinstance(key[1], slice):
375
+ # for y_ in self._range_y[key[1]]:
376
+ # for x_ in self._range_x[key[0]]:
377
+ # _lib.TCOD_console_set_char(self._console.tcod_console,
378
+ # x_, y_, ch)
379
+ # return
380
+ # _lib.TCOD_console_set_char(self._console.tcod_console,
381
+ # self._range_x[key[0]], self._range_y[key[1]], ch)
382
+
325
383
class _AttributeFG (_ConsoleAttribute ):
326
384
327
- def __getitem__ (self , key ):
328
- if isinstance (key [0 ], slice ) or isinstance (key [1 ], slice ):
329
- return self ._get_slice (* key )
330
- x = self ._range_x [key [0 ]]
331
- y = self ._range_y [key [1 ]]
385
+ def _get_item (self , x , y ):
332
386
return Color .from_int (
333
387
_lib .TDL_console_get_fg (self ._console .tcod_console , x , y ))
334
-
335
- def __setitem__ (self , key , fg ):
336
- x = self ._range_x [key [0 ]]
337
- y = self ._range_y [key [1 ]]
388
+
389
+ def _set_item (self , x , y , fg ):
338
390
_lib .TDL_console_set_fg (self ._console .tcod_console , x , y , fg )
339
391
392
+ def _set_range (self , range_x , range_y , fg ):
393
+ cdata = self ._console .tcod_console
394
+ for x in range_x :
395
+ for y in range_y :
396
+ _lib .TDL_console_set_fg (cdata , x , y , fg )
397
+
398
+ # def __getitem__(self, key):
399
+ # if isinstance(key[0], slice) or isinstance(key[1], slice):
400
+ # return self._get_slice(*key)
401
+ # x = self._range_x[key[0]]
402
+ # y = self._range_y[key[1]]
403
+ # return Color.from_int(
404
+ # _lib.TDL_console_get_fg(self._console.tcod_console, x, y))
405
+
406
+ # def __setitem__(self, key, fg):
407
+ # x = self._range_x[key[0]]
408
+ # y = self._range_y[key[1]]
409
+ # if isinstance(x, range) or isinstance(y, range):
410
+ # for y_ in y:
411
+ # for x_ in x:
412
+ # _lib.TDL_console_set_fg(self._console.tcod_console,
413
+ # x_, y_, fg)
414
+ # return
415
+ # _lib.TDL_console_set_fg(self._console.tcod_console, x, y, fg)
416
+
340
417
class _AttributeBG (_ConsoleAttribute ):
341
418
342
- def __getitem__ (self , key ):
343
- if isinstance (key [0 ], slice ) or isinstance (key [1 ], slice ):
344
- return self ._get_slice (* key )
345
- x = self ._range_x [key [0 ]]
346
- y = self ._range_y [key [1 ]]
347
- return _lib .TCOD_console_get_char_background (
348
- self ._console .tcod_console , x , y )
419
+ def _get_item (self , x , y ):
420
+ return Color .from_int (
421
+ _lib .TDL_console_get_bg (self ._console .tcod_console , x , y ))
422
+
423
+ def _set_item (self , x , y , fg ):
424
+ _lib .TDL_console_set_bg (self ._console .tcod_console , x , y , fg , 1 )
425
+
426
+ def _set_range (self , range_x , range_y , fg ):
427
+ cdata = self ._console .tcod_console
428
+ for x in range_x :
429
+ for y in range_y :
430
+ _lib .TDL_console_set_bg (cdata , x , y , bg , 1 )
431
+ # def __getitem__(self, key):
432
+ # if isinstance(key[0], slice) or isinstance(key[1], slice):
433
+ # return self._get_slice(*key)
434
+ # x = self._range_x[key[0]]
435
+ # y = self._range_y[key[1]]
436
+ # return _lib.TCOD_console_get_char_background(
437
+ # self._console.tcod_console, x, y)
349
438
350
- def __setitem__ (self , key , bg ):
351
- x = self ._range_x [key [0 ]]
352
- y = self ._range_y [key [1 ]]
353
- _lib .TCOD_console_set_char_background (
354
- self ._console .tcod_console , x , y , fg , 1 )
439
+ # def __setitem__(self, key, bg):
440
+ # x = self._range_x[key[0]]
441
+ # y = self._range_y[key[1]]
442
+ # if isinstance(x, range) or isinstance(y, range):
443
+ # for y_ in y:
444
+ # for x_ in x:
445
+ # _lib.TDL_console_set_bg(self._console.tcod_console,
446
+ # x_, y_, bg)
447
+ # return
448
+ # _lib.TDL_console_set_bg(
449
+ # self._console.tcod_console, x, y, bg, 1)
355
450
356
451
357
452
def __init__ (self ):
@@ -373,7 +468,6 @@ def _normalizePoint(self, x, y):
373
468
# cast to int, always faster than type checking
374
469
x = int (x )
375
470
y = int (y )
376
-
377
471
# handle negative indexes
378
472
return self ._range_x [x ], self ._range_y [y ]
379
473
@@ -840,7 +934,7 @@ def move(self, x, y):
840
934
@param y: Y position to place the cursor.
841
935
@see: L{get_cursor}, L{print_str}, L{write}
842
936
"""
843
- self ._cursor = self ._normalizePoint ( x , y )
937
+ self ._cursor = range ( self .width )[ x ], range ( self . height )[ y ]
844
938
845
939
def scroll (self , x , y ):
846
940
"""Scroll the contents of the console in the direction of x,y.
@@ -1153,8 +1247,24 @@ def __getitem__(self, key):
1153
1247
x = self ._range_x [x ]
1154
1248
y = self ._range_y [y ]
1155
1249
return (_lib .TCOD_console_get_char (self .tcod_console , x , y ),
1156
- _lib .TDL_console_get_fg (self .tcod_console , x , y ),
1157
- _lib .TDL_console_get_bg (self .tcod_console , x , y ))
1250
+ Color .from_int (_lib .TDL_console_get_fg (self .tcod_console , x , y )),
1251
+ Color .from_int (_lib .TDL_console_get_bg (self .tcod_console , x , y )))
1252
+
1253
+ def __setitem__ (self , key , value ):
1254
+ if isinstance (key [0 ], slice ) or isinstance (key [1 ], slice ):
1255
+ if isinstance (value , Window ):
1256
+ raise NotImplementedError ('blit stub' )
1257
+ else :
1258
+ ch , fg , bg = value
1259
+ for y in self ._range_y [key [1 ]]:
1260
+ for x in self ._range_x [key [0 ]]:
1261
+ _lib .TDL_console_put_char_ex (self .tcod_console , x , y ,
1262
+ ch , fg , bg , 1 )
1263
+ else :
1264
+ _lib .TDL_console_put_char_ex (self .tcod_console ,
1265
+ self ._range_x [key [0 ]], self ._range_y [key [1 ]], value [0 ],
1266
+ value [1 ], value [2 ], 1 )
1267
+
1158
1268
1159
1269
def __repr__ (self ):
1160
1270
return "<Console (Width=%i Height=%i)>" % (self .width , self .height )
@@ -1204,7 +1314,10 @@ def __init__(self, console, x, y, width, height):
1204
1314
_BaseConsole .__init__ (self )
1205
1315
assert isinstance (console , (Console , Window )), 'console parameter must be a Console or Window instance, got %s' % repr (console )
1206
1316
self .parent = console
1207
-
1317
+ if width is None :
1318
+ width = console .width
1319
+ if height is None :
1320
+ height = console .height
1208
1321
slice_x = slice (x , x + width )
1209
1322
slice_y = slice (y , y + height )
1210
1323
self ._range_x = console ._range_x [slice_x ]
@@ -1238,7 +1351,8 @@ def height(self):
1238
1351
def _translate (self , x , y ):
1239
1352
"""Convertion x and y to their position on the root Console"""
1240
1353
# we add our position relative to our parent and then call then next parent up
1241
- return self .parent ._translate (self ._range_x [x ], self ._range_y [y ])
1354
+
1355
+ return self .parent ._translate (x + self ._range_x [0 ], y + self ._range_y [0 ])
1242
1356
1243
1357
def clear (self , fg = Ellipsis , bg = Ellipsis ):
1244
1358
# inherit docstring
@@ -1271,23 +1385,21 @@ def draw_char(self, x, y, char, fg=Ellipsis, bg=Ellipsis):
1271
1385
1272
1386
def draw_rect (self , x , y , width , height , string , fg = Ellipsis , bg = Ellipsis ):
1273
1387
# inherit docstring
1274
- x , y , width , height = self ._normalizeRect (x , y , width , height )
1275
- if fg is Ellipsis :
1276
- fg = self ._default_fg
1277
- if bg is Ellipsis :
1278
- bg = self ._default_bg
1279
- self .parent .draw_rect (self ._range_x [x ], self ._range_y [y ], width , height ,
1280
- string , fg , bg )
1388
+ slice_x = slice (x , x + width )
1389
+ slice_y = slice (y , y + height )
1390
+ fg = _format_color (fg , self ._default_fg )
1391
+ bg = _format_color (bg , self ._default_bg )
1392
+ self [slice_x , slice_y ] = (string , fg , bg )
1281
1393
1282
1394
def draw_frame (self , x , y , width , height , string , fg = Ellipsis , bg = Ellipsis ):
1283
1395
# inherit docstring
1284
1396
x , y , width , height = self ._normalizeRect (x , y , width , height )
1397
+ x , y = self ._translate (x , y )
1285
1398
if fg is Ellipsis :
1286
1399
fg = self ._default_fg
1287
1400
if bg is Ellipsis :
1288
1401
bg = self ._default_bg
1289
- self .parent .draw_frame (self ._range_x [x ], self ._range_y [y ], width , height ,
1290
- string , fg , bg )
1402
+ self .parent .draw_frame (x , y , width , height , string , fg , bg )
1291
1403
1292
1404
def get_char (self , x , y ):
1293
1405
# inherit docstring
@@ -1317,6 +1429,21 @@ def __getitem__(self, key):
1317
1429
_lib .TDL_console_get_fg (self .console .tcod_console , x , y ),
1318
1430
_lib .TDL_console_get_bg (self .console .tcod_console , x , y ))
1319
1431
1432
+ def __setitem__ (self , key , value ):
1433
+ if isinstance (key [0 ], slice ) or isinstance (key [1 ], slice ):
1434
+ if isinstance (value , Window ):
1435
+ raise NotImplementedError ('blit stub' )
1436
+ else :
1437
+ ch , fg , bg = value
1438
+ for y in self ._range_y [key [1 ]]:
1439
+ for x in self ._range_x [key [0 ]]:
1440
+ _lib .TDL_console_put_char_ex (self .console .tcod_console ,
1441
+ x , y , ch , fg , bg , 1 )
1442
+ else :
1443
+ _lib .TDL_console_put_char_ex (self .console .tcod_console ,
1444
+ self ._range_x [key [0 ]], self ._range_y [key [1 ]], value [0 ],
1445
+ value [1 ], value [2 ], 1 )
1446
+
1320
1447
1321
1448
def __repr__ (self ):
1322
1449
return "<Window(X=%i Y=%i Width=%i Height=%i)>" % (self .x , self .y ,
0 commit comments