Mercurial > fife-parpg
comparison engine/extensions/pychan/properties.py @ 333:fee958103d58
* GuiImage now acts as Dummy Image if created without an argument.
* PyChan Image properties now allways return the GuiImage, annotated with a _source attribute.
* TwoButton.draw had a if branch that was never reached - fixed.
author | phoku@33b003aa-7bff-0310-803a-e67f0ece8222 |
---|---|
date | Mon, 24 Aug 2009 14:41:52 +0000 |
parents | 457e626296ba |
children | a9482d3d989e |
comparison
equal
deleted
inserted
replaced
332:457e626296ba | 333:fee958103d58 |
---|---|
67 self._getSetter(obj)(color) | 67 self._getSetter(obj)(color) |
68 def __get__(self, obj, objtype = None): | 68 def __get__(self, obj, objtype = None): |
69 color = self._getGetter(obj)() | 69 color = self._getGetter(obj)() |
70 return fife.Color(color.r,color.g,color.b,color.a) | 70 return fife.Color(color.r,color.g,color.b,color.a) |
71 | 71 |
72 class DummyImage(object): | |
73 def getWidth(self): return 0 | |
74 def getHeight(self): return 0 | |
75 | |
76 class ImageProperty(WrappedProperty): | 72 class ImageProperty(WrappedProperty): |
73 """ | |
74 This unifies the way Images and Image attributes are handled | |
75 inside PyChan. | |
76 """ | |
77 def __init__(self, name): | 77 def __init__(self, name): |
78 super(ImageProperty, self).__init__(name) | 78 super(ImageProperty, self).__init__(name) |
79 self.prop_name = "_prop_" + self.name.lower() | 79 self.prop_name = "_prop_" + self.name.lower() |
80 def __set__(self, obj, image): | 80 def __set__(self, obj, image): |
81 image_info = getattr(obj, self.prop_name, {}) | 81 image_info = getattr(obj, self.prop_name, {}) |
82 if not image: | 82 if not image: |
83 image_info["source"] = "" | 83 image_info["source"] = "" |
84 image_info["image"] = DummyImage() | 84 image_info["image"] = fife.GuiImage() |
85 image_info["image"]._source = "" | |
85 self._getSetter(obj)(None) | 86 self._getSetter(obj)(None) |
86 | 87 |
87 elif isinstance(image, str): | 88 elif isinstance(image, str): |
88 image_info["source"] = image | 89 image_info["source"] = image |
89 # to catch or not to catch ... | 90 # to catch or not to catch ... |
90 # we just let the NotFound exception trickle here. | 91 # we just let the NotFound exception trickle here. |
91 # depedning on complains we can catch and print a warning. | 92 # depedning on complains we can catch and print a warning. |
92 image_info["image"] = get_manager().loadImage(image) | 93 image_info["image"] = get_manager().loadImage(image) |
94 image_info["image"]._source = image | |
93 self._getSetter(obj)(image_info["image"]) | 95 self._getSetter(obj)(image_info["image"]) |
94 | 96 |
95 elif isinstance(image,fife.GuiImage): | 97 elif isinstance(image,fife.GuiImage): |
96 image_info["source"] = None | 98 # FIXME - this trickery with the hidden annotation |
99 # with an _source attribute isn't really clean. | |
100 # Is it even necessary | |
101 image_info["source"] = getattr(image,"_source","") | |
97 image_info["image"] = image | 102 image_info["image"] = image |
103 if image_info["source"]: | |
104 image_info["image"] = get_manager().loadImage(image) | |
98 self._getSetter(obj)(image_info["image"]) | 105 self._getSetter(obj)(image_info["image"]) |
99 else: | 106 else: |
100 attribute_name = "%s.%s" % (obj.__class__.__name__,self.name) | 107 attribute_name = "%s.%s" % (obj.__class__.__name__,self.name) |
101 error_message = "%s only accepts GuiImage and python strings, not '%s'" | 108 error_message = "%s only accepts GuiImage and python strings, not '%s'" |
102 raise RuntimeError(error_message % (attribute_name, repr(source))) | 109 raise RuntimeError(error_message % (attribute_name, repr(source))) |
103 | 110 |
104 setattr(obj, self.prop_name, image_info) | 111 setattr(obj, self.prop_name, image_info) |
105 | 112 |
106 def __get__(self, obj, objtype = None): | 113 def __get__(self, obj, objtype = None): |
107 return getattr(obj, self.prop_name, {}).get("image",None) | 114 d = getattr(obj, self.prop_name, {}) |
115 image = d.get("image",None) | |
116 if not image: | |
117 image = fife.GuiImage() | |
118 return image | |
108 | 119 |