Package fife :: Package extensions :: Package pychan :: Module internal
[hide private]
[frames] | no frames]

Source Code for Module fife.extensions.pychan.internal

  1  # -*- coding: utf-8 -*- 
  2   
  3  # #################################################################### 
  4  #  Copyright (C) 2005-2011 by the FIFE team 
  5  #  http://www.fifengine.net 
  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.guimanager: 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 @todo: delete calls occur even if the widget is not in self.allWidgets 106 we can't rely on a perfect show/hide/show/hide chain here (which we do!) 107 108 this method is called way too frequent and unmanaged as that we can 109 ever hope to achieve a strict show/hide/show chain 110 111 weak ref should only be deleted if there is one, because 112 hiding/showing a widget is a different process then updating 113 the self.allWidgets datastructure 114 115 the assert check will trigger on a frequent basis, thus 116 I added it but uncommented it for further notice 117 """ 118 self.hook.remove_widget( widget.real_widget ) 119 120 # triggered on a regular basis 121 # assert widget not in self.allWidgets, "KeyError: pychan tried to delete this widget: %s" % widget 122 123 if widget not in self.allWidgets: 124 return 125 del self.allWidgets[ widget ]
126
127 - def getConsole(self):
128 """ 129 Gets a reference to the console 130 """ 131 return self.hook.console
132
133 - def getDefaultFont(self):
134 """ 135 Returns the default font 136 """ 137 return self.fonts['default']
138
139 - def setDefaultFont(self,name):
140 self.fonts['default'] = self.getFont(name)
141
142 - def getFont(self,name):
143 """ 144 B{pending deprecation} 145 146 Returns a GuiFont identified by its name. 147 148 @param name: A string identifier from the font definitions in pychans config files. 149 """ 150 if in_fife: 151 font = self.fonts.get(name) 152 if isinstance(font,guichan.GuiFont): 153 return font 154 if hasattr(font,"font") and isinstance(getattr(font,"font"),guichan.GuiFont): 155 return font.font 156 raise InitializationError("Couldn't find the font '%s' - did you forget loading a .fontdef?" % str(name)) 157 else: 158 return self.hook.get_font(name)
159
160 - def createFont(self, path="", size=0, glyphs=""):
161 """ 162 Creates and returns a GuiFont from the GUI Manager 163 """ 164 return self.hook.create_font(path,size,glyphs)
165
166 - def releaseFont(self, font):
167 """ 168 Releases a font from memory. Expects a guichan.GuiFont. 169 170 @todo This needs to be tested. Also should add a way to release 171 a font by name (fonts.Font). 172 """ 173 if not isinstance(font,guichan.GuiFont): 174 raise InitializationError("PyChan Manager expected a guichan.GuiFont instance, not %s." % repr(font)) 175 self.hook.release_font(font)
176
177 - def addFont(self,font):
178 """ 179 B{deprecated} 180 181 Add a font to the font registry. It's not necessary to call this directly. 182 But it expects a L{Font} instance and throws an L{InitializationError} 183 otherwise. 184 185 @param font: A L{Font} instance. 186 """ 187 if not isinstance(font,fonts.Font): 188 raise InitializationError("PyChan Manager expected a fonts.Font instance, not %s." % repr(font)) 189 self.fonts[font.name] = font
190
191 - def addStyle(self,name,style):
192 style = self._remapStyleKeys(style) 193 194 for k,v in self.styles.get('default',{}).items(): 195 style[k] = style.get(k,v) 196 self.styles[name] = style
197
198 - def stylize(self,widget, style, **kwargs):
199 style = self.styles[style] 200 for k,v in style.get('default',{}).items(): 201 v = kwargs.get(k,v) 202 setattr(widget,k,v) 203 204 cls = widget.__class__ 205 for applicable,specstyle in style.items(): 206 if not isinstance(applicable,tuple): 207 applicable = (applicable,) 208 if cls in applicable: 209 for k,v in specstyle.items(): 210 v = kwargs.get(k,v) 211 setattr(widget,k,v)
212
213 - def _remapStyleKeys(self,style):
214 """ 215 Translate style selectors to tuples of widget classes. (internal) 216 """ 217 # Remap class names, create copy: 218 def _toClass(class_): 219 if class_ == "default": 220 return class_ 221 222 if type(class_) == type(widgets.Widget) and issubclass(class_,widgets.Widget): 223 return class_ 224 if not widgets.WIDGETS.has_key(str(class_)): 225 raise InitializationError("Can't resolve %s to a widget class." % repr(class_)) 226 return widgets.WIDGETS[str(class_)]
227 228 style_copy = {} 229 for k,v in style.items(): 230 if isinstance(k,tuple): 231 new_k = tuple(map(_toClass,k)) 232 else: 233 new_k = _toClass(k) 234 style_copy[new_k] = v 235 return style_copy
236
237 - def loadImage(self,filename):
238 if not filename: 239 raise InitializationError("Empty Image file.") 240 return self.hook.load_image(filename)
241 242 # Default Widget style. 243 244 DEFAULT_STYLE = { 245 'default' : { 246 'border_size': 0, 247 'margins': (0,0), 248 'base_color' : guichan.Color(28,28,28), 249 'foreground_color' : guichan.Color(255,255,255), 250 'background_color' : guichan.Color(50,50,50), 251 'selection_color' : guichan.Color(80,80,80), 252 'font' : 'default' 253 }, 254 'Button' : { 255 'border_size': 2, 256 'margins' : (5,2), 257 'min_size' : (15,10), 258 }, 259 'CheckBox' : { 260 'border_size': 0, 261 }, 262 'RadioButton' : { 263 'border_size': 0, 264 'background_color' : guichan.Color(0,0,0), 265 }, 266 'Label' : { 267 'border_size': 0, 268 'background_color' : guichan.Color(50,50,50,0) 269 }, 270 'ListBox' : { 271 'border_size': 0, 272 }, 273 'Window' : { 274 'border_size': 0, 275 'margins': (5,5), 276 'opaque' : 1, 277 'padding':2, 278 'titlebar_height' : 12, 279 'background_image' : None, 280 }, 281 'TextBox' : { 282 }, 283 ('Container','HBox','VBox') : { 284 'border_size': 0, 285 'margins': (0,0), 286 'padding':2, 287 'opaque' : 1, 288 'background_image' : None, 289 } 290 } 291