comparison engine/extensions/basicapplication.py @ 129:9a1529f9625e

* Indentation patch by GreyGhost
author mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
date Thu, 07 Aug 2008 15:46:46 +0000
parents 860d81602a2a
children d2f1e81fbe2c
comparison
equal deleted inserted replaced
128:6e1fd3571440 129:9a1529f9625e
11 from serializers.xmlanimation import XMLAnimationLoader 11 from serializers.xmlanimation import XMLAnimationLoader
12 12
13 class ExitEventListener(fife.IKeyListener): 13 class ExitEventListener(fife.IKeyListener):
14 """ 14 """
15 Default, rudimentary event listener. 15 Default, rudimentary event listener.
16 16
17 Will cause the application to quit on pressing ESC. 17 Will cause the application to quit on pressing ESC.
18 """ 18 """
19 def __init__(self, app): 19 def __init__(self, app):
20 self.app = app 20 self.app = app
21 self.engine = app.engine 21 self.engine = app.engine
27 27
28 def keyPressed(self, evt): 28 def keyPressed(self, evt):
29 keyval = evt.getKey().getValue() 29 keyval = evt.getKey().getValue()
30 if keyval == fife.Key.ESCAPE: 30 if keyval == fife.Key.ESCAPE:
31 self.app.quit() 31 self.app.quit()
32 32
33 def keyReleased(self, evt): 33 def keyReleased(self, evt):
34 pass 34 pass
35 35
36 class ApplicationBase(object): 36 class ApplicationBase(object):
37 """ 37 """
38 ApplicationBase is an extendable class that provides a basic environment for a FIFE-based client. 38 ApplicationBase is an extendable class that provides a basic environment for a FIFE-based client.
39 39
40 The unextended application reads in and initializes engine settings, sets up a simple event 40 The unextended application reads in and initializes engine settings, sets up a simple event
41 listener, and pumps the engine while listening for a quit message. Specialized applications can 41 listener, and pumps the engine while listening for a quit message. Specialized applications can
42 modify settings.py to change initial engine settings. They can provide their own event listener 42 modify settings.py to change initial engine settings. They can provide their own event listener
43 by overriding L{createListener}. And they can override the L{_pump} method 43 by overriding L{createListener}. And they can override the L{_pump} method
44 to define runtime behavior of the application. 44 to define runtime behavior of the application.
45 45
46 """ 46 """
47 def __init__(self): 47 def __init__(self):
48 self.engine = fife.Engine() 48 self.engine = fife.Engine()
49 49
50 self.loadSettings() 50 self.loadSettings()
51 self.initLogging() 51 self.initLogging()
52 52
53 self.engine.init() 53 self.engine.init()
54 54
55 self._animationloader = XMLAnimationLoader(self.engine.getImagePool(), self.engine.getVFS()) 55 self._animationloader = XMLAnimationLoader(self.engine.getImagePool(), self.engine.getVFS())
56 self.engine.getAnimationPool().addResourceLoader(self._animationloader) 56 self.engine.getAnimationPool().addResourceLoader(self._animationloader)
57 57
58 self.quitRequested = False 58 self.quitRequested = False
59 self.breakRequested = False 59 self.breakRequested = False
60 self.returnValues = [] 60 self.returnValues = []
61 61
62 def loadSettings(self): 62 def loadSettings(self):
64 Load the settings from a python file and load them into the engine. 64 Load the settings from a python file and load them into the engine.
65 Called in the ApplicationBase constructor. 65 Called in the ApplicationBase constructor.
66 """ 66 """
67 import settings 67 import settings
68 self.settings = settings 68 self.settings = settings
69 69
70 engineSetting = self.engine.getSettings() 70 engineSetting = self.engine.getSettings()
71 engineSetting.setDefaultFontGlyphs(settings.FontGlyphs) 71 engineSetting.setDefaultFontGlyphs(settings.FontGlyphs)
72 engineSetting.setDefaultFontPath(settings.Font) 72 engineSetting.setDefaultFontPath(settings.Font)
73 engineSetting.setDefaultFontSize(12) 73 engineSetting.setDefaultFontSize(12)
74 engineSetting.setBitsPerPixel(settings.BitsPerPixel) 74 engineSetting.setBitsPerPixel(settings.BitsPerPixel)
87 pass 87 pass
88 try: 88 try:
89 engineSetting.setImageChunkingSize(settings.ImageChunkSize) 89 engineSetting.setImageChunkingSize(settings.ImageChunkSize)
90 except: 90 except:
91 pass 91 pass
92 92
93 def initLogging(self): 93 def initLogging(self):
94 """ 94 """
95 Initialize the LogManager. 95 Initialize the LogManager.
96 """ 96 """
97 self.log = fifelog.LogManager(self.engine, self.settings.LogToPrompt, self.settings.LogToFile) 97 self.log = fifelog.LogManager(self.engine, self.settings.LogToPrompt, self.settings.LogToFile)
100 100
101 def createListener(self): 101 def createListener(self):
102 """ 102 """
103 This creates a default event listener, which will just close the program 103 This creates a default event listener, which will just close the program
104 after pressing ESC. 104 after pressing ESC.
105 105
106 You should override this method to provide your own event handling. 106 You should override this method to provide your own event handling.
107 """ 107 """
108 return ExitEventListener(self) 108 return ExitEventListener(self)
109 109
110 def run(self): 110 def run(self):
114 eventlistener = self.createListener() 114 eventlistener = self.createListener()
115 self.engine.initializePumping() 115 self.engine.initializePumping()
116 retval = self.mainLoop() 116 retval = self.mainLoop()
117 self.engine.finalizePumping() 117 self.engine.finalizePumping()
118 return retval 118 return retval
119 119
120 def mainLoop(self): 120 def mainLoop(self):
121 """ 121 """
122 The programs main loop. 122 The programs main loop.
123 123
124 Do not override this, instead provide your own L{_pump} method. 124 Do not override this, instead provide your own L{_pump} method.
125 You can call this recursively, e.g. to provide synchronous 125 You can call this recursively, e.g. to provide synchronous
126 Dialogs :-) and break out of the current mainLoop by calling 126 Dialogs :-) and break out of the current mainLoop by calling
127 L{breakFromMainLoop}. It will return the argument passed 127 L{breakFromMainLoop}. It will return the argument passed
128 to L{breakFromMainLoop}. 128 to L{breakFromMainLoop}.
137 self._pump() 137 self._pump()
138 138
139 if self.breakRequested: 139 if self.breakRequested:
140 self.breakRequested = False 140 self.breakRequested = False
141 break 141 break
142 142
143 return self.returnValues.pop() 143 return self.returnValues.pop()
144 144
145 def breakFromMainLoop(self,returnValue): 145 def breakFromMainLoop(self,returnValue):
146 """ 146 """
147 Break from the currently running L{mainLoop}. 147 Break from the currently running L{mainLoop}.
148 148
149 The passed argument will be returned by the mainLoop. 149 The passed argument will be returned by the mainLoop.
150 """ 150 """
151 self.returnValues[-1] = returnValue 151 self.returnValues[-1] = returnValue
152 self.breakRequested = True 152 self.breakRequested = True
153 153
154 154
155 def _pump(self): 155 def _pump(self):
156 """ 156 """
157 Application pump. 157 Application pump.
158 158
159 Derived classes can specialize this for unique behavior. 159 Derived classes can specialize this for unique behavior.
160 This is called every frame. 160 This is called every frame.
161 """ 161 """
162 162
163 def quit(self): 163 def quit(self):