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

Revision 753, 9.3 KB checked in by aspidites, 8 years ago (diff)

Patch by Aspidites:

  • disabled options button as functionality was previously defined in fife's Setting object. Rather than reimplement in settings.py, I think it would be wiser to implement in the gui package or similar.
  • continued further integration of settings.py in rest of project
  • 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.gamemodel import GameModel
26from parpg.mainmenuview import MainMenuView
27from parpg import console
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
36class 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
50class 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        KeyListener.__init__(self, event_listener)
66        MouseListener.__init__(self, event_listener)
67        ConsoleExecuter.__init__(self, event_listener)
68        CommandListener.__init__(self, event_listener)
69        WidgetListener.__init__(self, event_listener)       
70        self.engine = engine
71        self.view = view
72        self.model = model
73        keyfilter = KeyFilter([fife.Key.ESCAPE])
74        keyfilter.__disown__()       
75       
76        engine.getEventManager().setKeyFilter(keyfilter)
77        self.quit = False
78        self.about_window = None
79        self.console = console.Console(self)
80
81    def quitGame(self):
82        """Forces a quit game on next cycle.
83           @return: None"""
84        self.quit = True
85
86    def onConsoleCommand(self, command):
87        """
88        Called on every console comand, delegates calls  to the a console
89        object, implementing the callbacks
90        @type command: string
91        @param command: the command to run
92        @return: result
93        """
94        return self.console.handleConsoleCommand(command)
95
96    def onCommand(self, command):
97        """Enables the game to be closed via the 'X' button on the window frame
98           @type command: fife.Command
99           @param command: The command to read.
100           @return: None"""
101        if(command.getCommandType() == fife.CMD_QUIT_GAME):
102            self.quit = True
103            command.consume()
104
105class PARPGApplication(ApplicationBase):
106    """Main Application class
107       We use an MVC model model
108       self.gamesceneview is our view,self.model is our model
109       self.controller is the controller"""
110       
111    def __init__(self, setting):
112        """Initialise the instance.
113           @return: None"""
114        self._setting = setting
115        self.engine = fife.Engine()
116        self.loadSettings()
117        self.engine.init()
118        self._animationloader = XMLAnimationLoader(self.engine.getImagePool(),
119                                                   self.engine.getVFS())
120        self.engine.getAnimationPool().addResourceLoader(self._animationloader)
121
122        pychan.init(self.engine, debug = True)
123        #self.engine.getModel(self)
124        self.model = GameModel(self.engine, setting)
125        self.model.maps_file = self._setting.parpg.MapsFile
126        self.model.readMapFiles()
127        self.model.object_db_file = self._setting.parpg.ObjectDatabaseFile
128        self.model.readObjectDB()
129        self.model.agents_directory = self._setting.parpg.AgentsDirectory
130        self.model.getAgentImportFiles()
131        self.model.all_agents_file = self._setting.parpg.AllAgentsFile
132        self.model.readAllAgents()
133        self.model.dialogues_directory = self._setting.parpg.DialoguesDirectory
134        self.model.getDialogues()
135        self.view = MainMenuView(self.engine, self.model)
136        self.fonts_directory = self._setting.fife.FontsDirectory
137        self.loadFonts(self.fonts_directory)
138        self.event_listener = EventListener(self.engine)
139        self.controllers = []
140        controller = MainMenuController(self.engine, 
141                                                        self.view, 
142                                                        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.