comparison src/parpg/application.py @ 0:1fd2201f5c36

Initial commit of parpg-core.
author M. George Hansen <technopolitica@gmail.com>
date Sat, 14 May 2011 01:12:35 -0700
parents
children d60f1dab8469
comparison
equal deleted inserted replaced
-1:000000000000 0:1fd2201f5c36
1 # This program is free software: you can redistribute it and/or modify
2 # it under the terms of the GNU General Public License as published by
3 # the Free Software Foundation, either version 3 of the License, or
4 # (at your option) any later version.
5
6 # This program is distributed in the hope that it will be useful,
7 # but WITHOUT ANY WARRANTY; without even the implied warranty of
8 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 # GNU General Public License for more details.
10
11 # You should have received a copy of the GNU General Public License
12 # along with this program. If not, see <http://www.gnu.org/licenses/>.
13 """This module contains the main Application class
14 and the basic Listener for PARPG """
15
16 import os
17 import sys
18
19 from fife import fife
20 from fife.extensions import pychan
21 from fife.extensions.serializers.xmlanimation import XMLAnimationLoader
22 from fife.extensions.basicapplication import ApplicationBase
23
24 from parpg import console
25 from parpg.font import PARPGFont
26 from parpg.gamemodel import GameModel
27 from parpg.mainmenuview import MainMenuView
28 from parpg.mainmenucontroller import MainMenuController
29 from parpg.common.listeners.event_listener import EventListener
30 from parpg.common.listeners.key_listener import KeyListener
31 from parpg.common.listeners.mouse_listener import MouseListener
32 from parpg.common.listeners.command_listener import CommandListener
33 from parpg.common.listeners.console_executor import ConsoleExecuter
34 from parpg.common.listeners.widget_listener import WidgetListener
35
36 class KeyFilter(fife.IKeyFilter):
37 """
38 This is the implementation of the fife.IKeyFilter class.
39
40 Prevents any filtered keys from being consumed by guichan.
41 """
42 def __init__(self, keys):
43 fife.IKeyFilter.__init__(self)
44 self._keys = keys
45
46 def isFiltered(self, event):
47 """Checks if an key is filtered"""
48 return event.getKey().getValue() in self._keys
49
50 class ApplicationListener(KeyListener,
51 MouseListener,
52 ConsoleExecuter,
53 CommandListener,
54 WidgetListener):
55 """Basic listener for PARPG"""
56
57 def __init__(self, event_listener, engine, view, model):
58 """Initialize the instance.
59 @type engine: fife.engine
60 @param engine: ???
61 @type view: viewbase.ViewBase
62 @param view: View that draws the current state
63 @type model: GameModel
64 @param model: The game model"""
65
66 KeyListener.__init__(self, event_listener)
67 MouseListener.__init__(self, event_listener)
68 ConsoleExecuter.__init__(self, event_listener)
69 CommandListener.__init__(self, event_listener)
70 WidgetListener.__init__(self, event_listener)
71 self.engine = engine
72 self.view = view
73 self.model = model
74 keyfilter = KeyFilter([fife.Key.ESCAPE])
75 keyfilter.__disown__()
76
77 engine.getEventManager().setKeyFilter(keyfilter)
78 self.quit = False
79 self.about_window = None
80 self.console = console.Console(self)
81
82 def quitGame(self):
83 """Forces a quit game on next cycle.
84 @return: None"""
85 self.quit = True
86
87 def onConsoleCommand(self, command):
88 """
89 Called on every console comand, delegates calls to the a console
90 object, implementing the callbacks
91 @type command: string
92 @param command: the command to run
93 @return: result
94 """
95 return self.console.handleConsoleCommand(command)
96
97 def onCommand(self, command):
98 """Enables the game to be closed via the 'X' button on the window frame
99 @type command: fife.Command
100 @param command: The command to read.
101 @return: None"""
102 if(command.getCommandType() == fife.CMD_QUIT_GAME):
103 self.quit = True
104 command.consume()
105
106 class PARPGApplication(ApplicationBase):
107 """Main Application class
108 We use an MVC model model
109 self.gamesceneview is our view,self.model is our model
110 self.controller is the controller"""
111
112 def __init__(self, setting):
113 """Initialise the instance.
114 @return: None"""
115 self._setting = setting
116 self.engine = fife.Engine()
117 self.loadSettings()
118 self.engine.init()
119 self._animationloader = XMLAnimationLoader(self.engine.getImagePool(),
120 self.engine.getVFS())
121 self.engine.getAnimationPool().addResourceLoader(self._animationloader)
122
123 pychan.init(self.engine, debug = True)
124 pychan.setupModalExecution(self.mainLoop,self.breakFromMainLoop)
125
126 self.quitRequested = False
127 self.breakRequested = False
128 self.returnValues = []
129 #self.engine.getModel(self)
130 self.model = GameModel(self.engine, setting)
131 self.model.readMapFiles()
132 self.model.readObjectDB()
133 self.model.getAgentImportFiles()
134 self.model.readAllAgents()
135 self.model.getDialogues()
136 self.view = MainMenuView(self.engine, self.model)
137 self.loadFonts()
138 self.event_listener = EventListener(self.engine)
139 self.controllers = []
140 controller = MainMenuController(self.engine, self.view, self.model,
141 self)
142 #controller.initHud()
143 self.controllers.append(controller)
144 self.listener = ApplicationListener(self.event_listener,
145 self.engine,
146 self.view,
147 self.model)
148 #start_map = self._setting.fife.get("PARPG", "Map")
149 #self.model.changeMap(start_map)
150
151 def loadFonts(self):
152 # add the fonts path to the system path to import font definitons
153 sys.path.insert(0, os.path.join(self._setting.system_path,
154 self._setting.fife.FontsPath))
155 from oldtypewriter import fontdefs
156
157 for fontdef in fontdefs:
158 pychan.internal.get_manager().addFont(PARPGFont(fontdef,
159 self._setting))
160
161
162 def loadSettings(self):
163 """
164 Load the settings from a python file and load them into the engine.
165 Called in the ApplicationBase constructor.
166 """
167
168 engineSetting = self.engine.getSettings()
169 engineSetting.setDefaultFontGlyphs(self._setting.fife.FontGlyphs)
170 engineSetting.setDefaultFontPath(os.path.join(self._setting.system_path,
171 self._setting.fife.FontsPath,
172 self._setting.fife.Font))
173 engineSetting.setDefaultFontSize(self._setting.fife.DefaultFontSize)
174 engineSetting.setBitsPerPixel(self._setting.fife.BitsPerPixel)
175 engineSetting.setInitialVolume(self._setting.fife.InitialVolume)
176 engineSetting.setSDLRemoveFakeAlpha(self._setting.fife.SDLRemoveFakeAlpha)
177 engineSetting.setScreenWidth(self._setting.fife.ScreenWidth)
178 engineSetting.setScreenHeight(self._setting.fife.ScreenHeight)
179 engineSetting.setRenderBackend(self._setting.fife.RenderBackend)
180 engineSetting.setFullScreen(self._setting.fife.FullScreen)
181 engineSetting.setVideoDriver(self._setting.fife.VideoDriver)
182 engineSetting.setLightingModel(self._setting.fife.Lighting)
183 engineSetting.setColorKeyEnabled(self._setting.fife.ColorKeyEnabled)
184
185 engineSetting.setColorKey(*[int(digit)
186 for digit in self._setting.fife.ColorKey])
187
188 engineSetting.setWindowTitle(self._setting.fife.WindowTitle)
189 engineSetting.setWindowIcon(os.path.join(self._setting.system_path,
190 self._setting.fife.IconsPath,
191 self._setting.fife.WindowIcon))
192
193 def createListener(self):
194 """ __init__ takes care of creating an event listener, so
195 basicapplication's createListener is harmful. Without
196 overriding it, the program quit's on esc press, rather than
197 invoking the main menu
198 """
199 pass
200
201 def pushController(self, controller):
202 """Adds a controller to the list to be the current active one."""
203 self.controllers[-1].pause(True)
204 self.controllers.append(controller)
205
206 def popController(self):
207 """Removes and returns the current active controller, unless its the last one"""
208 ret_controller = None
209 if self.controllers.count > 1:
210 ret_controller = self.controllers.pop()
211 self.controllers[-1].pause(False)
212 ret_controller.onStop()
213 return ret_controller
214
215 def switchController(self, controller):
216 """Clears the controller list and adds a controller to be the current active one"""
217 for old_controller in self.controllers:
218 old_controller.onStop()
219 self.controllers = []
220 self.controllers.append(controller)
221
222 def _pump(self):
223 """Main game loop.
224 There are in fact 2 main loops, this one and the one in GameSceneView.
225 @return: None"""
226 if self.listener.quit:
227 self.breakRequested = True #pylint: disable-msg=C0103
228 else:
229 for controller in self.controllers:
230 controller.pump()