# HG changeset patch # User phoku@33b003aa-7bff-0310-803a-e67f0ece8222 # Date 1237882197 0 # Node ID 4a5e8e638b0db0462598df43f952617a01fa22e9 # Parent e4dfdf4c11fdd6c49268f64256fee11cb7b93102 Added the 'position_techinque' attr, so it can be used from XML. Factored out color properties and made them have value semantics. diff -r e4dfdf4c11fd -r 4a5e8e638b0d engine/extensions/pychan/properties.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/engine/extensions/pychan/properties.py Tue Mar 24 08:09:57 2009 +0000 @@ -0,0 +1,43 @@ +# -*- coding: utf-8 -*- +import fife + +""" +Property bindings +================= + +This module contains a set of property bindings for +the widgets, factored out to de-clutter the Widget. + +""" +class WrappedProperty(object): + def __init__(self, name): + self.name = name + def _getSetter(self,obj): + setter_name = 'set' + self.name + return getattr(obj.real_widget,setter_name) + def _getGetter(self,obj): + getter_name = 'get' + self.name + return getattr(obj.real_widget,getter_name) + + +class ColorProperty(WrappedProperty): + """ + A color property. Fakes a color attribute of a guichan widget. + This accepts either tuples of the colors (r,g,b) + or L{fife.Color} objects. + + Color objects have value semantics in this case. + """ + def __init__(self, name): + super(ColorProperty, self).__init__(name) + def __set__(self, obj, color): + if isinstance(color, tuple): + color = fife.Color(*color) + else: + # Force a copy to get value semantics + color = fife.Color(color.r,color.g,color.b,color.a) + self._getSetter(obj)(color) + def __get__(self, obj, objtype = None): + color = self._getGetter(obj)() + return fife.Color(color.r,color.g,color.b,color.a) + diff -r e4dfdf4c11fd -r 4a5e8e638b0d engine/extensions/pychan/widgets.py --- a/engine/extensions/pychan/widgets.py Mon Mar 23 16:54:16 2009 +0000 +++ b/engine/extensions/pychan/widgets.py Tue Mar 24 08:09:57 2009 +0000 @@ -12,6 +12,7 @@ import events from exceptions import * from attrs import Attr,UnicodeAttr, PointAttr,ColorAttr,BoolAttr,IntAttr,FloatAttr +from properties import ColorProperty def get_manager(): import pychan @@ -43,6 +44,8 @@ def getWidth(self): return 0 def getHeight(self): return 0 + + class Widget(object): """ This is the common widget base class, which provides most of the wrapping @@ -98,7 +101,7 @@ ATTRIBUTES = [ Attr('name'), PointAttr('position'), PointAttr('min_size'), PointAttr('size'), PointAttr('max_size'), ColorAttr('base_color'),ColorAttr('background_color'),ColorAttr('foreground_color'),ColorAttr('selection_color'), - Attr('style'), Attr('font'),IntAttr('border_size') + Attr('style'), Attr('font'),IntAttr('border_size'),Attr('position_technique'), ] DEFAULT_NAME = '__unnamed__' @@ -605,33 +608,10 @@ def _getBorderSize(self): return self.real_widget.getFrameSize() def _setBorderSize(self,size): self.real_widget.setFrameSize(size) - def _getBaseColor(self): return self.real_widget.getBaseColor() - def _setBaseColor(self,color): - if isinstance(color,type(())): - color = fife.Color(*color) - self.real_widget.setBaseColor(color) - base_color = property(_getBaseColor,_setBaseColor) - - def _getBackgroundColor(self): return self.real_widget.getBackgroundColor() - def _setBackgroundColor(self,color): - if isinstance(color,type(())): - color = fife.Color(*color) - self.real_widget.setBackgroundColor(color) - background_color = property(_getBackgroundColor,_setBackgroundColor) - - def _getForegroundColor(self): return self.real_widget.getForegroundColor() - def _setForegroundColor(self,color): - if isinstance(color,type(())): - color = fife.Color(*color) - self.real_widget.setForegroundColor(color) - foreground_color = property(_getForegroundColor,_setForegroundColor) - - def _getSelectionColor(self): return self.real_widget.getSelectionColor() - def _setSelectionColor(self,color): - if isinstance(color,type(())): - color = fife.Color(*color) - self.real_widget.setSelectionColor(color) - selection_color = property(_getSelectionColor,_setSelectionColor) + base_color = ColorProperty("BaseColor") + background_color = ColorProperty("BackgroundColor") + foreground_color = ColorProperty("ForegroundColor") + selection_color = ColorProperty("SelectionColor") def _getStyle(self): return self._style def _setStyle(self,style):