@@ -184,17 +184,29 @@ class Color(object):
184184 """
185185
186186 def __new__ (cls , * rgb ):
187+ self = object .__new__ (cls )
187188 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 ]
190193 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 )
194197 self .r , self .g , self .b = rgb
195198 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
198210 raise TypeError ('Parameters must be (r,g,b) or (int), got: %a' %
199211 repr (rgb ))
200212
@@ -234,6 +246,11 @@ def _set_b(self, value):
234246 g = property (_get_g , _set_g )
235247 b = property (_get_b , _set_b )
236248
249+ def __eq__ (self , other ):
250+ return (self ._r == other ._r and
251+ self ._g == other ._g and
252+ self ._b == other ._b )
253+
237254 def __repr__ (self ):
238255 return '<%s[%i, %i, %i]>' % (self .__class__ .__name__ ,
239256 self ._r , self ._g , self ._b )
@@ -307,51 +324,129 @@ def _get_slice(self, slice_x, slice_y):
307324 slice_x = slice (slice_y , slice_y + 1 )
308325 return self .__class__ (self ._console , self ._range_x [slice_x ],
309326 self ._range_y [slice_y ])
310-
311- class _AttributeCh (_ConsoleAttribute ):
312-
327+
313328 def __getitem__ (self , key ):
314329 if isinstance (key [0 ], slice ) or isinstance (key [1 ], slice ):
315330 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' )
319349
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 ):
323359 _lib .TCOD_console_set_char (self ._console .tcod_console , x , y , ch )
324360
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+
325383 class _AttributeFG (_ConsoleAttribute ):
326384
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 ):
332386 return Color .from_int (
333387 _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 ):
338390 _lib .TDL_console_set_fg (self ._console .tcod_console , x , y , fg )
339391
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+
340417 class _AttributeBG (_ConsoleAttribute ):
341418
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)
349438
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)
355450
356451
357452 def __init__ (self ):
@@ -373,7 +468,6 @@ def _normalizePoint(self, x, y):
373468 # cast to int, always faster than type checking
374469 x = int (x )
375470 y = int (y )
376-
377471 # handle negative indexes
378472 return self ._range_x [x ], self ._range_y [y ]
379473
@@ -840,7 +934,7 @@ def move(self, x, y):
840934 @param y: Y position to place the cursor.
841935 @see: L{get_cursor}, L{print_str}, L{write}
842936 """
843- self ._cursor = self ._normalizePoint ( x , y )
937+ self ._cursor = range ( self .width )[ x ], range ( self . height )[ y ]
844938
845939 def scroll (self , x , y ):
846940 """Scroll the contents of the console in the direction of x,y.
@@ -1153,8 +1247,24 @@ def __getitem__(self, key):
11531247 x = self ._range_x [x ]
11541248 y = self ._range_y [y ]
11551249 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+
11581268
11591269 def __repr__ (self ):
11601270 return "<Console (Width=%i Height=%i)>" % (self .width , self .height )
@@ -1204,7 +1314,10 @@ def __init__(self, console, x, y, width, height):
12041314 _BaseConsole .__init__ (self )
12051315 assert isinstance (console , (Console , Window )), 'console parameter must be a Console or Window instance, got %s' % repr (console )
12061316 self .parent = console
1207-
1317+ if width is None :
1318+ width = console .width
1319+ if height is None :
1320+ height = console .height
12081321 slice_x = slice (x , x + width )
12091322 slice_y = slice (y , y + height )
12101323 self ._range_x = console ._range_x [slice_x ]
@@ -1238,7 +1351,8 @@ def height(self):
12381351 def _translate (self , x , y ):
12391352 """Convertion x and y to their position on the root Console"""
12401353 # 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 ])
12421356
12431357 def clear (self , fg = Ellipsis , bg = Ellipsis ):
12441358 # inherit docstring
@@ -1271,23 +1385,21 @@ def draw_char(self, x, y, char, fg=Ellipsis, bg=Ellipsis):
12711385
12721386 def draw_rect (self , x , y , width , height , string , fg = Ellipsis , bg = Ellipsis ):
12731387 # 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 )
12811393
12821394 def draw_frame (self , x , y , width , height , string , fg = Ellipsis , bg = Ellipsis ):
12831395 # inherit docstring
12841396 x , y , width , height = self ._normalizeRect (x , y , width , height )
1397+ x , y = self ._translate (x , y )
12851398 if fg is Ellipsis :
12861399 fg = self ._default_fg
12871400 if bg is Ellipsis :
12881401 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 )
12911403
12921404 def get_char (self , x , y ):
12931405 # inherit docstring
@@ -1317,6 +1429,21 @@ def __getitem__(self, key):
13171429 _lib .TDL_console_get_fg (self .console .tcod_console , x , y ),
13181430 _lib .TDL_console_get_bg (self .console .tcod_console , x , y ))
13191431
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+
13201447
13211448 def __repr__ (self ):
13221449 return "<Window(X=%i Y=%i Width=%i Height=%i)>" % (self .x , self .y ,
0 commit comments