source: branches/active/character_customization/game/parpg/application.py @ 766

Revision 766, 9.0 KB checked in by aspidites, 9 years ago (diff)

Patch by Aspidites

+ forgot to update settings.ini to include quotes for FontGlyphs?

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