Mercurial > fife-parpg
comparison engine/python/fife/extensions/pychan/internal.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 compat import guichan, in_fife | |
25 import widgets | |
26 from fife.extensions import fife_timer as timer | |
27 import fonts | |
28 from exceptions import * | |
29 from traceback import print_exc | |
30 | |
31 def get_manager(): | |
32 """ | |
33 Get the manager from inside pychan. | |
34 | |
35 To avoid cyclic imports write:: | |
36 from internal import get_manager | |
37 """ | |
38 return Manager.manager | |
39 | |
40 def screen_width(): | |
41 return get_manager().hook.screen_width | |
42 | |
43 def screen_height(): | |
44 return get_manager().hook.screen_height | |
45 | |
46 class Manager(object): | |
47 manager = None | |
48 | |
49 def __init__(self, hook, debug = False, compat_layout = False): | |
50 super(Manager,self).__init__() | |
51 self.hook = hook | |
52 self.debug = debug | |
53 self.compat_layout = compat_layout | |
54 self.unicodePolicy = ('ignore',) | |
55 | |
56 if in_fife: | |
57 if not hook.engine.getEventManager(): | |
58 raise InitializationError("No event manager installed.") | |
59 if not hook.engine.getGuiManager(): | |
60 raise InitializationError("No GUI manager installed.") | |
61 timer.init(hook.engine.getTimeManager()) | |
62 | |
63 self.fonts = {} | |
64 #glyphs = ' abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.,!?-+/:();%`\'*#=[]"' | |
65 self.fonts['default'] = hook.default_font | |
66 | |
67 self.styles = {} | |
68 self.addStyle('default',DEFAULT_STYLE) | |
69 | |
70 Manager.manager = self | |
71 | |
72 # Setup synchronous dialogs | |
73 self.mainLoop = None | |
74 self.breakFromMainLoop = None | |
75 self.can_execute = False | |
76 | |
77 import weakref | |
78 self.allWidgets = weakref.WeakKeyDictionary() | |
79 | |
80 # Autopos | |
81 from autoposition import placeWidget | |
82 self.placeWidget = placeWidget | |
83 | |
84 def setupModalExecution(self,mainLoop,breakFromMainLoop): | |
85 """ | |
86 Setup synchronous execution of dialogs. | |
87 """ | |
88 self.mainLoop = mainLoop | |
89 self.breakFromMainLoop = breakFromMainLoop | |
90 self.can_execute = True | |
91 | |
92 def show(self,widget): | |
93 """ | |
94 Shows a widget on screen. Used by L{Widget.show} - do not use directly. | |
95 """ | |
96 self.placeWidget(widget, widget.position_technique) | |
97 assert widget not in self.allWidgets | |
98 self.allWidgets[ widget ] = 1 | |
99 self.hook.add_widget( widget.real_widget ) | |
100 | |
101 def hide(self,widget): | |
102 """ | |
103 Hides a widget again. Used by L{Widget.hide} - do not use directly. | |
104 """ | |
105 self.hook.remove_widget( widget.real_widget ) | |
106 del self.allWidgets[ widget ] | |
107 | |
108 def setDefaultFont(self,name): | |
109 self.fonts['default'] = self.getFont(name) | |
110 | |
111 def getFont(self,name): | |
112 """ | |
113 B{pending deprecation} | |
114 | |
115 Returns a GuiFont identified by its name. | |
116 | |
117 @param name: A string identifier from the font definitions in pychans config files. | |
118 """ | |
119 if in_fife: | |
120 font = self.fonts.get(name) | |
121 if isinstance(font,guichan.GuiFont): | |
122 return font | |
123 if hasattr(font,"font") and isinstance(getattr(font,"font"),guichan.GuiFont): | |
124 return font.font | |
125 raise InitializationError("Couldn't find the font '%s' - did you forget loading a .fontdef?" % str(name)) | |
126 else: | |
127 return self.hook.get_font(name) | |
128 | |
129 def addFont(self,font): | |
130 """ | |
131 B{deprecated} | |
132 | |
133 Add a font to the font registry. It's not necessary to call this directly. | |
134 But it expects a L{Font} instance and throws an L{InitializationError} | |
135 otherwise. | |
136 | |
137 @param font: A L{Font} instance. | |
138 """ | |
139 if not isinstance(font,fonts.Font): | |
140 raise InitializationError("PyChan Manager expected a fonts.Font instance, not %s." % repr(font)) | |
141 self.fonts[font.name] = font | |
142 | |
143 def addStyle(self,name,style): | |
144 style = self._remapStyleKeys(style) | |
145 | |
146 for k,v in self.styles.get('default',{}).items(): | |
147 style[k] = style.get(k,v) | |
148 self.styles[name] = style | |
149 | |
150 def stylize(self,widget, style, **kwargs): | |
151 style = self.styles[style] | |
152 for k,v in style.get('default',{}).items(): | |
153 v = kwargs.get(k,v) | |
154 setattr(widget,k,v) | |
155 | |
156 cls = widget.__class__ | |
157 for applicable,specstyle in style.items(): | |
158 if not isinstance(applicable,tuple): | |
159 applicable = (applicable,) | |
160 if cls in applicable: | |
161 for k,v in specstyle.items(): | |
162 v = kwargs.get(k,v) | |
163 setattr(widget,k,v) | |
164 | |
165 def _remapStyleKeys(self,style): | |
166 """ | |
167 Translate style selectors to tuples of widget classes. (internal) | |
168 """ | |
169 # Remap class names, create copy: | |
170 def _toClass(class_): | |
171 if class_ == "default": | |
172 return class_ | |
173 | |
174 if type(class_) == type(widgets.Widget) and issubclass(class_,widgets.Widget): | |
175 return class_ | |
176 if not widgets.WIDGETS.has_key(str(class_)): | |
177 raise InitializationError("Can't resolve %s to a widget class." % repr(class_)) | |
178 return widgets.WIDGETS[str(class_)] | |
179 | |
180 style_copy = {} | |
181 for k,v in style.items(): | |
182 if isinstance(k,tuple): | |
183 new_k = tuple(map(_toClass,k)) | |
184 else: | |
185 new_k = _toClass(k) | |
186 style_copy[new_k] = v | |
187 return style_copy | |
188 | |
189 def loadImage(self,filename): | |
190 if not filename: | |
191 raise InitializationError("Empty Image file.") | |
192 return self.hook.load_image(filename) | |
193 | |
194 # Default Widget style. | |
195 | |
196 DEFAULT_STYLE = { | |
197 'default' : { | |
198 'border_size': 0, | |
199 'margins': (0,0), | |
200 'base_color' : guichan.Color(28,28,28), | |
201 'foreground_color' : guichan.Color(255,255,255), | |
202 'background_color' : guichan.Color(50,50,50), | |
203 'selection_color' : guichan.Color(80,80,80), | |
204 'font' : 'default' | |
205 }, | |
206 'Button' : { | |
207 'border_size': 2, | |
208 'margins' : (5,2), | |
209 'min_size' : (15,10), | |
210 }, | |
211 'CheckBox' : { | |
212 'border_size': 0, | |
213 }, | |
214 'RadioButton' : { | |
215 'border_size': 0, | |
216 'background_color' : guichan.Color(0,0,0), | |
217 }, | |
218 'Label' : { | |
219 'border_size': 0, | |
220 'background_color' : guichan.Color(50,50,50,0) | |
221 }, | |
222 'ClickLabel' : { | |
223 'border_size': 0, | |
224 }, | |
225 'ListBox' : { | |
226 'border_size': 0, | |
227 }, | |
228 'Window' : { | |
229 'border_size': 0, | |
230 'margins': (5,5), | |
231 'opaque' : 1, | |
232 'padding':2, | |
233 'titlebar_height' : 12, | |
234 'background_image' : None, | |
235 }, | |
236 'TextBox' : { | |
237 }, | |
238 ('Container','HBox','VBox') : { | |
239 'border_size': 0, | |
240 'margins': (0,0), | |
241 'padding':2, | |
242 'opaque' : 1, | |
243 'background_image' : None, | |
244 } | |
245 } |