comparison engine/extensions/pychan/properties.py @ 332:457e626296ba

Working on the XXX_image attributes to force consistent behaviour - this needs a double check. Failed to reproduce #355.
author phoku@33b003aa-7bff-0310-803a-e67f0ece8222
date Mon, 24 Aug 2009 13:23:03 +0000
parents 48c99636453e
children fee958103d58
comparison
equal deleted inserted replaced
331:48c99636453e 332:457e626296ba
21 # Free Software Foundation, Inc., 21 # Free Software Foundation, Inc.,
22 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 22 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 # #################################################################### 23 # ####################################################################
24 24
25 import fife 25 import fife
26 from exceptions import RuntimeError
27
28 def get_manager():
29 import pychan
30 return pychan.manager
26 31
27 """ 32 """
28 Property bindings 33 Property bindings
29 ================= 34 =================
30 35
62 self._getSetter(obj)(color) 67 self._getSetter(obj)(color)
63 def __get__(self, obj, objtype = None): 68 def __get__(self, obj, objtype = None):
64 color = self._getGetter(obj)() 69 color = self._getGetter(obj)()
65 return fife.Color(color.r,color.g,color.b,color.a) 70 return fife.Color(color.r,color.g,color.b,color.a)
66 71
72 class DummyImage(object):
73 def getWidth(self): return 0
74 def getHeight(self): return 0
75
76 class ImageProperty(WrappedProperty):
77 def __init__(self, name):
78 super(ImageProperty, self).__init__(name)
79 self.prop_name = "_prop_" + self.name.lower()
80 def __set__(self, obj, image):
81 image_info = getattr(obj, self.prop_name, {})
82 if not image:
83 image_info["source"] = ""
84 image_info["image"] = DummyImage()
85 self._getSetter(obj)(None)
86
87 elif isinstance(image, str):
88 image_info["source"] = image
89 # to catch or not to catch ...
90 # we just let the NotFound exception trickle here.
91 # depedning on complains we can catch and print a warning.
92 image_info["image"] = get_manager().loadImage(image)
93 self._getSetter(obj)(image_info["image"])
94
95 elif isinstance(image,fife.GuiImage):
96 image_info["source"] = None
97 image_info["image"] = image
98 self._getSetter(obj)(image_info["image"])
99 else:
100 attribute_name = "%s.%s" % (obj.__class__.__name__,self.name)
101 error_message = "%s only accepts GuiImage and python strings, not '%s'"
102 raise RuntimeError(error_message % (attribute_name, repr(source)))
103
104 setattr(obj, self.prop_name, image_info)
105
106 def __get__(self, obj, objtype = None):
107 return getattr(obj, self.prop_name, {}).get("image",None)
108