comparison engine/python/fife/extensions/pychan/properties.py @ 378:64738befdf3b

bringing in the changes from the build_system_rework branch in preparation for the 0.3.0 release. This commit will require the Jan2010 devkit. Clients will also need to be modified to the new way to import fife.
author vtchill@33b003aa-7bff-0310-803a-e67f0ece8222
date Mon, 11 Jan 2010 23:34:52 +0000
parents
children
comparison
equal deleted inserted replaced
377:fe6fb0e0ed23 378:64738befdf3b
1 # -*- coding: utf-8 -*-
2
3 # ####################################################################
4 # Copyright (C) 2005-2009 by the FIFE team
5 # http://www.fifengine.de
6 # This file is part of FIFE.
7 #
8 # FIFE is free software; you can redistribute it and/or
9 # modify it under the terms of the GNU Lesser General Public
10 # License as published by the Free Software Foundation; either
11 # version 2.1 of the License, or (at your option) any later version.
12 #
13 # This library is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 # Lesser General Public License for more details.
17 #
18 # You should have received a copy of the GNU Lesser General Public
19 # License along with this library; if not, write to the
20 # Free Software Foundation, Inc.,
21 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 # ####################################################################
23
24 from fife import fife
25 from exceptions import RuntimeError
26
27 def get_manager():
28 from fife.extensions import pychan
29 return pychan.manager
30
31 """
32 Property bindings
33 =================
34
35 This module contains a set of property bindings for
36 the widgets, factored out to de-clutter the Widget.
37
38 """
39 class WrappedProperty(object):
40 def __init__(self, name):
41 self.name = name
42 def _getSetter(self,obj):
43 setter_name = 'set' + self.name
44 return getattr(obj.real_widget,setter_name)
45 def _getGetter(self,obj):
46 getter_name = 'get' + self.name
47 return getattr(obj.real_widget,getter_name)
48
49
50 class ColorProperty(WrappedProperty):
51 """
52 A color property. Fakes a color attribute of a guichan widget.
53 This accepts either tuples of the colors (r,g,b)
54 or L{fife.Color} objects.
55
56 Color objects have value semantics in this case.
57 """
58 def __init__(self, name):
59 super(ColorProperty, self).__init__(name)
60 def __set__(self, obj, color):
61 if isinstance(color, tuple):
62 color = fife.Color(*color)
63 else:
64 # Force a copy to get value semantics
65 color = fife.Color(color.r,color.g,color.b,color.a)
66 self._getSetter(obj)(color)
67 def __get__(self, obj, objtype = None):
68 color = self._getGetter(obj)()
69 return fife.Color(color.r,color.g,color.b,color.a)
70
71 class ImageProperty(WrappedProperty):
72 """
73 This unifies the way Images and Image attributes are handled
74 inside PyChan.
75 """
76 def __init__(self, name):
77 super(ImageProperty, self).__init__(name)
78 self.prop_name = "_prop_" + self.name.lower()
79 def __set__(self, obj, image):
80 image_info = getattr(obj, self.prop_name, {})
81 if not image:
82 image_info["source"] = ""
83 image_info["image"] = fife.GuiImage()
84 image_info["image"]._source = ""
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 image_info["image"].source = image
94 self._getSetter(obj)(image_info["image"])
95
96 elif isinstance(image,fife.GuiImage):
97 # FIXME - this trickery with the hidden annotation
98 # with an _source attribute isn't really clean.
99 # Is it even necessary
100 image_info["source"] = getattr(image,"source","")
101 image_info["image"] = image
102 if image_info["source"]:
103 image_info["image"] = get_manager().loadImage(image_info["source"])
104 self._getSetter(obj)(image_info["image"])
105 else:
106 attribute_name = "%s.%s" % (obj.__class__.__name__,self.name)
107 error_message = "%s only accepts GuiImage and python strings, not '%s'"
108 raise RuntimeError(error_message % (attribute_name, repr(image)))
109
110 setattr(obj, self.prop_name, image_info)
111
112 def __get__(self, obj, objtype = None):
113 d = getattr(obj, self.prop_name, {})
114 image = d.get("image",None)
115 if not image:
116 image = fife.GuiImage()
117 image.source = ""
118 return image
119