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

Revision 742, 7.6 KB checked in by technomage, 8 years ago (diff)

Patch by Technomage

  • Added a loadFonts method to PARPGApplication which is called upon instantiation and loads all .fontdef files used to define fonts for PyChan?;
  • 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.basicapplication import ApplicationBase
23
24from parpg.gamemodel import GameModel
25from parpg.mainmenuview import MainMenuView
26from parpg import console
27from parpg.mainmenucontroller import MainMenuController
28from parpg.common.listeners.event_listener import EventListener
29from parpg.common.listeners.key_listener import KeyListener
30from parpg.common.listeners.mouse_listener import MouseListener
31from parpg.common.listeners.command_listener import CommandListener
32from parpg.common.listeners.console_executor import ConsoleExecuter
33from parpg.common.listeners.widget_listener import WidgetListener
34
35class KeyFilter(fife.IKeyFilter):
36    """
37    This is the implementation of the fife.IKeyFilter class.
38   
39    Prevents any filtered keys from being consumed by guichan.
40    """
41    def __init__(self, keys):
42        fife.IKeyFilter.__init__(self)
43        self._keys = keys
44
45    def isFiltered(self, event):
46        """Checks if an key is filtered"""
47        return event.getKey().getValue() in self._keys
48
49class ApplicationListener(KeyListener,
50                        MouseListener,
51                        ConsoleExecuter,
52                        CommandListener,
53                        WidgetListener):   
54    """Basic listener for PARPG"""
55       
56    def __init__(self, event_listener, engine, view, model):
57        """Initialize the instance.
58           @type engine: fife.engine
59           @param engine: ???
60           @type view: viewbase.ViewBase
61           @param view: View that draws the current state
62           @type model: GameModel
63           @param model: The game model"""
64        KeyListener.__init__(self, event_listener)
65        MouseListener.__init__(self, event_listener)
66        ConsoleExecuter.__init__(self, event_listener)
67        CommandListener.__init__(self, event_listener)
68        WidgetListener.__init__(self, event_listener)       
69        self.engine = engine
70        self.view = view
71        self.model = model
72        keyfilter = KeyFilter([fife.Key.ESCAPE])
73        keyfilter.__disown__()       
74       
75        engine.getEventManager().setKeyFilter(keyfilter)
76        self.quit = False
77        self.about_window = None
78        self.console = console.Console(self)
79
80    def quitGame(self):
81        """Forces a quit game on next cycle.
82           @return: None"""
83        self.quit = True
84
85    def onConsoleCommand(self, command):
86        """
87        Called on every console comand, delegates calls  to the a console
88        object, implementing the callbacks
89        @type command: string
90        @param command: the command to run
91        @return: result
92        """
93        return self.console.handleConsoleCommand(command)
94
95    def onCommand(self, command):
96        """Enables the game to be closed via the 'X' button on the window frame
97           @type command: fife.Command
98           @param command: The command to read.
99           @return: None"""
100        if(command.getCommandType() == fife.CMD_QUIT_GAME):
101            self.quit = True
102            command.consume()
103
104class PARPGApplication(ApplicationBase):
105    """Main Application class
106       We use an MVC model model
107       self.gamesceneview is our view,self.model is our model
108       self.controller is the controller"""
109       
110    def __init__(self, setting):
111        """Initialise the instance.
112           @return: None"""
113        super(PARPGApplication, self).__init__(setting)
114        pychan.init(self.engine, debug = True)
115        #self.engine.getModel(self)
116        self.model = GameModel(self.engine, setting)
117        self.model.maps_file = self._setting.get("PARPG", "MapsFile")
118        self.model.readMapFiles()
119        self.model.object_db_file = self._setting.get("PARPG", 
120                                                      "ObjectDatabaseFile") 
121        self.model.readObjectDB()
122        self.model.agents_directory = self._setting.get("PARPG",
123                                                        "AgentsDirectory") 
124        self.model.getAgentImportFiles()
125        self.model.all_agents_file = self._setting.get("PARPG", "AllAgentsFile")
126        self.model.readAllAgents()
127        self.model.dialogues_directory = self._setting.get("PARPG", 
128                                                           "DialoguesDirectory")
129        self.model.getDialogues()
130        self.view = MainMenuView(self.engine, self.model)
131        self.fonts_directory = self._setting.get("PARPG", "FontsDirectory")
132        self.loadFonts(self.fonts_directory)
133        self.event_listener = EventListener(self.engine)
134        self.controllers = []
135        controller = MainMenuController(self.engine, 
136                                                        self.view, 
137                                                        self.model,
138                                                        self)
139        #controller.initHud()
140        self.controllers.append(controller)
141        self.listener = ApplicationListener(self.event_listener,
142                                            self.engine, 
143                                            self.view, 
144                                            self.model)
145        #start_map = self._setting.get("PARPG", "Map")
146        #self.model.changeMap(start_map)
147
148    def loadFonts(self, fonts_directory):
149        file_names = os.listdir(fonts_directory)
150        for file_name in file_names:
151            base_name, extension = os.path.splitext(file_name)
152            if extension == '.fontdef':
153                pychan.loadFont(file_name)
154
155    def createListener(self):
156        """ __init__ takes care of the event listener, basicapplication's
157           createListener is only harmful. Without overriding it, the program
158           quit's on esc press, rather than invoking the main menu.
159        """
160        pass
161
162    def pushController(self, controller):
163        """Adds a controller to the list to be the current active one."""
164        self.controllers[-1].pause(True)
165        self.controllers.append(controller)
166   
167    def popController(self):
168        """Removes and returns the current active controller, unless its the last one"""
169        ret_controller = None
170        if self.controllers.count > 1:
171            ret_controller = self.controllers.pop()
172            self.controllers[-1].pause(False)
173        ret_controller.onStop()
174        return ret_controller
175   
176    def switchController(self, controller):
177        """Clears the controller list and adds a controller to be the current active one"""
178        for old_controller in self.controllers:
179            old_controller.onStop()
180        self.controllers = []
181        self.controllers.append(controller)
182   
183    def _pump(self):
184        """Main game loop.
185           There are in fact 2 main loops, this one and the one in GameSceneView.
186           @return: None"""
187        if self.listener.quit:
188            self.breakRequested = True #pylint: disable-msg=C0103
189        else:
190            for controller in self.controllers:
191                controller.pump()
Note: See TracBrowser for help on using the repository browser.