annotate engine/extensions/pychan/properties.py @ 358:8f64a9d1e7ab

* Cleaned up LayerTool so it follows the new python coding standards * Removed __ALL__ from __init__.py in the plugin folder
author cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
date Sat, 26 Sep 2009 09:51:11 +0000
parents dfd48d49c044
children
rev   line source
235
4a5e8e638b0d Added the 'position_techinque' attr, so it can be used from XML.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
1 # -*- coding: utf-8 -*-
331
48c99636453e Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 235
diff changeset
2
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 # 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
5 # http://www.fifengine.de
48c99636453e Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 235
diff changeset
6 # 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
7 #
48c99636453e Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 235
diff changeset
8 # 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
9 # 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
10 # 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
11 # 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
12 #
48c99636453e Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 235
diff changeset
13 # 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
14 # 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
15 # 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
16 # 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
17 #
48c99636453e Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 235
diff changeset
18 # 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
19 # 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
20 # Free Software Foundation, Inc.,
48c99636453e Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 235
diff changeset
21 # 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
22 # ####################################################################
48c99636453e Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 235
diff changeset
23
235
4a5e8e638b0d Added the 'position_techinque' attr, so it can be used from XML.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
24 import fife
332
457e626296ba Working on the XXX_image attributes to force consistent
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 331
diff changeset
25 from exceptions import RuntimeError
457e626296ba Working on the XXX_image attributes to force consistent
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 331
diff changeset
26
457e626296ba Working on the XXX_image attributes to force consistent
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 331
diff changeset
27 def get_manager():
457e626296ba Working on the XXX_image attributes to force consistent
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 331
diff changeset
28 import pychan
457e626296ba Working on the XXX_image attributes to force consistent
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 331
diff changeset
29 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
30
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 Property bindings
4a5e8e638b0d Added the 'position_techinque' attr, so it can be used from XML.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
33 =================
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 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
36 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
37
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 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
40 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
41 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
42 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
43 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
44 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
45 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
46 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
47 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
48
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 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
51 """
4a5e8e638b0d Added the 'position_techinque' attr, so it can be used from XML.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
52 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
53 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
54 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
55
4a5e8e638b0d Added the 'position_techinque' attr, so it can be used from XML.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
56 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
57 """
4a5e8e638b0d Added the 'position_techinque' attr, so it can be used from XML.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
58 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
59 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
60 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
61 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
62 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
63 else:
4a5e8e638b0d Added the 'position_techinque' attr, so it can be used from XML.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
64 # 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
65 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
66 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
67 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
68 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
69 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
70
332
457e626296ba Working on the XXX_image attributes to force consistent
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 331
diff changeset
71 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
72 """
fee958103d58 * GuiImage now acts as Dummy Image if created without an argument.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 332
diff changeset
73 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
74 inside PyChan.
fee958103d58 * GuiImage now acts as Dummy Image if created without an argument.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 332
diff changeset
75 """
332
457e626296ba Working on the XXX_image attributes to force consistent
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 331
diff changeset
76 def __init__(self, name):
457e626296ba Working on the XXX_image attributes to force consistent
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 331
diff changeset
77 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
78 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
79 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
80 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
81 if not image:
457e626296ba Working on the XXX_image attributes to force consistent
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 331
diff changeset
82 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
83 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
84 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
85 self._getSetter(obj)(None)
457e626296ba Working on the XXX_image attributes to force consistent
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 331
diff changeset
86
457e626296ba Working on the XXX_image attributes to force consistent
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 331
diff changeset
87 elif isinstance(image, str):
457e626296ba Working on the XXX_image attributes to force consistent
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 331
diff changeset
88 image_info["source"] = image
457e626296ba Working on the XXX_image attributes to force consistent
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 331
diff changeset
89 # 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
90 # 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
91 # 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
92 image_info["image"] = get_manager().loadImage(image)
334
a9482d3d989e PyChan fixes:
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 333
diff changeset
93 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
94 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
95
457e626296ba Working on the XXX_image attributes to force consistent
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 331
diff changeset
96 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
97 # 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
98 # 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
99 # Is it even necessary
334
a9482d3d989e PyChan fixes:
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 333
diff changeset
100 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
101 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
102 if image_info["source"]:
335
162662bf5c8a PyChan fixes:
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 334
diff changeset
103 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
104 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
105 else:
457e626296ba Working on the XXX_image attributes to force consistent
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 331
diff changeset
106 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
107 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
108 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
109
457e626296ba Working on the XXX_image attributes to force consistent
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 331
diff changeset
110 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
111
457e626296ba Working on the XXX_image attributes to force consistent
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 331
diff changeset
112 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
113 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
114 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
115 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
116 image = fife.GuiImage()
334
a9482d3d989e PyChan fixes:
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 333
diff changeset
117 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
118 return image
332
457e626296ba Working on the XXX_image attributes to force consistent
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 331
diff changeset
119