annotate engine/extensions/pychan/properties.py @ 336:16112ef84609

PyChan fixes: * Previous commits removed the ability to map events to unnamed widgets. Fixed.
author phoku@33b003aa-7bff-0310-803a-e67f0ece8222
date Mon, 24 Aug 2009 15:34:23 +0000
parents 162662bf5c8a
children dfd48d49c044
rev   line source
331
48c99636453e Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 235
diff changeset
1 #!/usr/bin/env python
235
4a5e8e638b0d Added the 'position_techinque' attr, so it can be used from XML.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
2 # -*- coding: utf-8 -*-
331
48c99636453e Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 235
diff changeset
3
48c99636453e Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 235
diff changeset
4 # ####################################################################
48c99636453e Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 235
diff changeset
5 # Copyright (C) 2005-2009 by the FIFE team
48c99636453e Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 235
diff changeset
6 # http://www.fifengine.de
48c99636453e Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 235
diff changeset
7 # This file is part of FIFE.
48c99636453e Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 235
diff changeset
8 #
48c99636453e Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 235
diff changeset
9 # FIFE is free software; you can redistribute it and/or
48c99636453e Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 235
diff changeset
10 # modify it under the terms of the GNU Lesser General Public
48c99636453e Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 235
diff changeset
11 # License as published by the Free Software Foundation; either
48c99636453e Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 235
diff changeset
12 # version 2.1 of the License, or (at your option) any later version.
48c99636453e Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 235
diff changeset
13 #
48c99636453e Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 235
diff changeset
14 # This library is distributed in the hope that it will be useful,
48c99636453e Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 235
diff changeset
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
48c99636453e Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 235
diff changeset
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
48c99636453e Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 235
diff changeset
17 # Lesser General Public License for more details.
48c99636453e Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 235
diff changeset
18 #
48c99636453e Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 235
diff changeset
19 # You should have received a copy of the GNU Lesser General Public
48c99636453e Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 235
diff changeset
20 # License along with this library; if not, write to the
48c99636453e Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 235
diff changeset
21 # Free Software Foundation, Inc.,
48c99636453e Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 235
diff changeset
22 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
48c99636453e Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 235
diff changeset
23 # ####################################################################
48c99636453e Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 235
diff changeset
24
235
4a5e8e638b0d Added the 'position_techinque' attr, so it can be used from XML.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
25 import fife
332
457e626296ba Working on the XXX_image attributes to force consistent
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 331
diff changeset
26 from exceptions import RuntimeError
457e626296ba Working on the XXX_image attributes to force consistent
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 331
diff changeset
27
457e626296ba Working on the XXX_image attributes to force consistent
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 331
diff changeset
28 def get_manager():
457e626296ba Working on the XXX_image attributes to force consistent
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 331
diff changeset
29 import pychan
457e626296ba Working on the XXX_image attributes to force consistent
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 331
diff changeset
30 return pychan.manager
235
4a5e8e638b0d Added the 'position_techinque' attr, so it can be used from XML.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
31
4a5e8e638b0d Added the 'position_techinque' attr, so it can be used from XML.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
32 """
4a5e8e638b0d Added the 'position_techinque' attr, so it can be used from XML.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
33 Property bindings
4a5e8e638b0d Added the 'position_techinque' attr, so it can be used from XML.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
34 =================
4a5e8e638b0d Added the 'position_techinque' attr, so it can be used from XML.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
35
4a5e8e638b0d Added the 'position_techinque' attr, so it can be used from XML.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
36 This module contains a set of property bindings for
4a5e8e638b0d Added the 'position_techinque' attr, so it can be used from XML.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
37 the widgets, factored out to de-clutter the Widget.
4a5e8e638b0d Added the 'position_techinque' attr, so it can be used from XML.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
38
4a5e8e638b0d Added the 'position_techinque' attr, so it can be used from XML.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
39 """
4a5e8e638b0d Added the 'position_techinque' attr, so it can be used from XML.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
40 class WrappedProperty(object):
4a5e8e638b0d Added the 'position_techinque' attr, so it can be used from XML.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
41 def __init__(self, name):
4a5e8e638b0d Added the 'position_techinque' attr, so it can be used from XML.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
42 self.name = name
4a5e8e638b0d Added the 'position_techinque' attr, so it can be used from XML.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
43 def _getSetter(self,obj):
4a5e8e638b0d Added the 'position_techinque' attr, so it can be used from XML.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
44 setter_name = 'set' + self.name
4a5e8e638b0d Added the 'position_techinque' attr, so it can be used from XML.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
45 return getattr(obj.real_widget,setter_name)
4a5e8e638b0d Added the 'position_techinque' attr, so it can be used from XML.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
46 def _getGetter(self,obj):
4a5e8e638b0d Added the 'position_techinque' attr, so it can be used from XML.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
47 getter_name = 'get' + self.name
4a5e8e638b0d Added the 'position_techinque' attr, so it can be used from XML.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
48 return getattr(obj.real_widget,getter_name)
4a5e8e638b0d Added the 'position_techinque' attr, so it can be used from XML.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
49
4a5e8e638b0d Added the 'position_techinque' attr, so it can be used from XML.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
50
4a5e8e638b0d Added the 'position_techinque' attr, so it can be used from XML.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
51 class ColorProperty(WrappedProperty):
4a5e8e638b0d Added the 'position_techinque' attr, so it can be used from XML.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
52 """
4a5e8e638b0d Added the 'position_techinque' attr, so it can be used from XML.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
53 A color property. Fakes a color attribute of a guichan widget.
4a5e8e638b0d Added the 'position_techinque' attr, so it can be used from XML.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
54 This accepts either tuples of the colors (r,g,b)
4a5e8e638b0d Added the 'position_techinque' attr, so it can be used from XML.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
55 or L{fife.Color} objects.
4a5e8e638b0d Added the 'position_techinque' attr, so it can be used from XML.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
56
4a5e8e638b0d Added the 'position_techinque' attr, so it can be used from XML.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
57 Color objects have value semantics in this case.
4a5e8e638b0d Added the 'position_techinque' attr, so it can be used from XML.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
58 """
4a5e8e638b0d Added the 'position_techinque' attr, so it can be used from XML.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
59 def __init__(self, name):
4a5e8e638b0d Added the 'position_techinque' attr, so it can be used from XML.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
60 super(ColorProperty, self).__init__(name)
4a5e8e638b0d Added the 'position_techinque' attr, so it can be used from XML.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
61 def __set__(self, obj, color):
4a5e8e638b0d Added the 'position_techinque' attr, so it can be used from XML.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
62 if isinstance(color, tuple):
4a5e8e638b0d Added the 'position_techinque' attr, so it can be used from XML.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
63 color = fife.Color(*color)
4a5e8e638b0d Added the 'position_techinque' attr, so it can be used from XML.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
64 else:
4a5e8e638b0d Added the 'position_techinque' attr, so it can be used from XML.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
65 # Force a copy to get value semantics
4a5e8e638b0d Added the 'position_techinque' attr, so it can be used from XML.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
66 color = fife.Color(color.r,color.g,color.b,color.a)
4a5e8e638b0d Added the 'position_techinque' attr, so it can be used from XML.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
67 self._getSetter(obj)(color)
4a5e8e638b0d Added the 'position_techinque' attr, so it can be used from XML.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
68 def __get__(self, obj, objtype = None):
4a5e8e638b0d Added the 'position_techinque' attr, so it can be used from XML.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
69 color = self._getGetter(obj)()
4a5e8e638b0d Added the 'position_techinque' attr, so it can be used from XML.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
70 return fife.Color(color.r,color.g,color.b,color.a)
4a5e8e638b0d Added the 'position_techinque' attr, so it can be used from XML.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
71
332
457e626296ba Working on the XXX_image attributes to force consistent
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 331
diff changeset
72 class ImageProperty(WrappedProperty):
333
fee958103d58 * GuiImage now acts as Dummy Image if created without an argument.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 332
diff changeset
73 """
fee958103d58 * GuiImage now acts as Dummy Image if created without an argument.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 332
diff changeset
74 This unifies the way Images and Image attributes are handled
fee958103d58 * GuiImage now acts as Dummy Image if created without an argument.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 332
diff changeset
75 inside PyChan.
fee958103d58 * GuiImage now acts as Dummy Image if created without an argument.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 332
diff changeset
76 """
332
457e626296ba Working on the XXX_image attributes to force consistent
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 331
diff changeset
77 def __init__(self, name):
457e626296ba Working on the XXX_image attributes to force consistent
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 331
diff changeset
78 super(ImageProperty, self).__init__(name)
457e626296ba Working on the XXX_image attributes to force consistent
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 331
diff changeset
79 self.prop_name = "_prop_" + self.name.lower()
457e626296ba Working on the XXX_image attributes to force consistent
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 331
diff changeset
80 def __set__(self, obj, image):
457e626296ba Working on the XXX_image attributes to force consistent
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 331
diff changeset
81 image_info = getattr(obj, self.prop_name, {})
457e626296ba Working on the XXX_image attributes to force consistent
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 331
diff changeset
82 if not image:
457e626296ba Working on the XXX_image attributes to force consistent
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 331
diff changeset
83 image_info["source"] = ""
333
fee958103d58 * GuiImage now acts as Dummy Image if created without an argument.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 332
diff changeset
84 image_info["image"] = fife.GuiImage()
fee958103d58 * GuiImage now acts as Dummy Image if created without an argument.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 332
diff changeset
85 image_info["image"]._source = ""
332
457e626296ba Working on the XXX_image attributes to force consistent
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 331
diff changeset
86 self._getSetter(obj)(None)
457e626296ba Working on the XXX_image attributes to force consistent
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 331
diff changeset
87
457e626296ba Working on the XXX_image attributes to force consistent
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 331
diff changeset
88 elif isinstance(image, str):
457e626296ba Working on the XXX_image attributes to force consistent
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 331
diff changeset
89 image_info["source"] = image
457e626296ba Working on the XXX_image attributes to force consistent
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 331
diff changeset
90 # to catch or not to catch ...
457e626296ba Working on the XXX_image attributes to force consistent
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 331
diff changeset
91 # we just let the NotFound exception trickle here.
457e626296ba Working on the XXX_image attributes to force consistent
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 331
diff changeset
92 # depedning on complains we can catch and print a warning.
457e626296ba Working on the XXX_image attributes to force consistent
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 331
diff changeset
93 image_info["image"] = get_manager().loadImage(image)
334
a9482d3d989e PyChan fixes:
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 333
diff changeset
94 image_info["image"].source = image
332
457e626296ba Working on the XXX_image attributes to force consistent
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 331
diff changeset
95 self._getSetter(obj)(image_info["image"])
457e626296ba Working on the XXX_image attributes to force consistent
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 331
diff changeset
96
457e626296ba Working on the XXX_image attributes to force consistent
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 331
diff changeset
97 elif isinstance(image,fife.GuiImage):
333
fee958103d58 * GuiImage now acts as Dummy Image if created without an argument.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 332
diff changeset
98 # FIXME - this trickery with the hidden annotation
fee958103d58 * GuiImage now acts as Dummy Image if created without an argument.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 332
diff changeset
99 # with an _source attribute isn't really clean.
fee958103d58 * GuiImage now acts as Dummy Image if created without an argument.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 332
diff changeset
100 # Is it even necessary
334
a9482d3d989e PyChan fixes:
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 333
diff changeset
101 image_info["source"] = getattr(image,"source","")
332
457e626296ba Working on the XXX_image attributes to force consistent
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 331
diff changeset
102 image_info["image"] = image
333
fee958103d58 * GuiImage now acts as Dummy Image if created without an argument.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 332
diff changeset
103 if image_info["source"]:
335
162662bf5c8a PyChan fixes:
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 334
diff changeset
104 image_info["image"] = get_manager().loadImage(image_info["source"])
332
457e626296ba Working on the XXX_image attributes to force consistent
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 331
diff changeset
105 self._getSetter(obj)(image_info["image"])
457e626296ba Working on the XXX_image attributes to force consistent
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 331
diff changeset
106 else:
457e626296ba Working on the XXX_image attributes to force consistent
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 331
diff changeset
107 attribute_name = "%s.%s" % (obj.__class__.__name__,self.name)
457e626296ba Working on the XXX_image attributes to force consistent
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 331
diff changeset
108 error_message = "%s only accepts GuiImage and python strings, not '%s'"
334
a9482d3d989e PyChan fixes:
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 333
diff changeset
109 raise RuntimeError(error_message % (attribute_name, repr(image)))
332
457e626296ba Working on the XXX_image attributes to force consistent
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 331
diff changeset
110
457e626296ba Working on the XXX_image attributes to force consistent
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 331
diff changeset
111 setattr(obj, self.prop_name, image_info)
457e626296ba Working on the XXX_image attributes to force consistent
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 331
diff changeset
112
457e626296ba Working on the XXX_image attributes to force consistent
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 331
diff changeset
113 def __get__(self, obj, objtype = None):
333
fee958103d58 * GuiImage now acts as Dummy Image if created without an argument.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 332
diff changeset
114 d = getattr(obj, self.prop_name, {})
fee958103d58 * GuiImage now acts as Dummy Image if created without an argument.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 332
diff changeset
115 image = d.get("image",None)
fee958103d58 * GuiImage now acts as Dummy Image if created without an argument.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 332
diff changeset
116 if not image:
fee958103d58 * GuiImage now acts as Dummy Image if created without an argument.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 332
diff changeset
117 image = fife.GuiImage()
334
a9482d3d989e PyChan fixes:
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 333
diff changeset
118 image.source = ""
333
fee958103d58 * GuiImage now acts as Dummy Image if created without an argument.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 332
diff changeset
119 return image
332
457e626296ba Working on the XXX_image attributes to force consistent
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 331
diff changeset
120