source: trunk/PARPG/scripts/hud.py @ 148

Revision 148, 11.4 KB checked in by meggie_parpg, 10 years ago (diff)

Added primarily saving and loading functionality. Saving uses a pickle.
Currently, this is my-save.pic and is hard coded in world.py. We should
allow the user to specify a file, but that can come later and is
primarily a UI thing. You can also load (again, from the same pickle).

Right now, saving and loading only looks at the PC's position and the
position of NPCs on maps. That's really all the state we have to save.
We can add more functions later, it should be fairly modular.

Small Changes:

  • Changed the functionality of the door floor tiles because changing the layer they are on will be very difficult
  • Got rid of an extraneous function call in world
  • Property svn:eol-style set to native
Line 
1#!/usr/bin/python
2
3#   This file is part of PARPG.
4
5#   PARPG is free software: you can redistribute it and/or modify
6#   it under the terms of the GNU General Public License as published by
7#   the Free Software Foundation, either version 3 of the License, or
8#   (at your option) any later version.
9
10#   PARPG is distributed in the hope that it will be useful,
11#   but WITHOUT ANY WARRANTY; without even the implied warranty of
12#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13#   GNU General Public License for more details.
14
15#   You should have received a copy of the GNU General Public License
16#   along with PARPG.  If not, see <http://www.gnu.org/licenses/>.
17
18import shutil, fife, pychan
19from pychan.tools import callbackWithArguments as cbwa
20
21
22class Hud():
23    """Main Hud class"""
24    def __init__(self, engine, settings):
25        """Initialise the instance.
26           @type engine: fife.Engine
27           @param engine: An instance of the fife engine
28           @type settings: settings.Setting
29           @param settings: The settings data
30           @return: None"""
31        pychan.init(engine, debug = True)
32        # TODO: perhaps this should not be hard-coded here
33        self.hud = pychan.loadXML("gui/hud.xml")
34        self.settings = settings
35        self.actionsBox = self.hud.findChild(name="actionsBox")
36        self.actionsText = []
37        self.menu_displayed = False
38        self.initializeHud()
39        self.initializeMainMenu()
40        self.initializeOptionsMenu()
41        self.initializeHelpMenu()
42
43    def initializeHud(self):
44        """Initialize and show the main HUD
45           @return: None"""
46        self.events_to_map = {"menuButton":self.displayMenu,
47                              "loadButton":self.loadGame}
48        self.hud.mapEvents(self.events_to_map) 
49        # set HUD size accoriding to screen size
50        screen_width = int(self.settings.readSetting('ScreenWidth'))
51        self.hud.findChild(name="mainHudWindow").size = (screen_width, 65)
52        self.hud.findChild(name="inventoryButton").position = (screen_width-59, 7)
53        # add ready slots
54        ready1 = self.hud.findChild(name='hudReady1')
55        ready2 = self.hud.findChild(name='hudReady2')
56        ready3 = self.hud.findChild(name='hudReady3')
57        ready4 = self.hud.findChild(name='hudReady4')
58        actions_scroll_area = self.hud.findChild(name='actionsScrollArea')
59        # annoying code that is both essential and boring to enter
60        if (screen_width == 1440):
61            ready1.position = (screen_width-1235, 7)
62            ready2.position = (screen_width-1175, 7)
63            ready3.position = (screen_width-215, 7)
64            ready4.position = (screen_width-155, 7)
65            actions_scroll_area.position = (325, 5)
66            actions_width = screen_width - 550
67        elif (screen_width == 1280):
68            ready1.position = (screen_width-1075, 7)
69            ready2.position = (screen_width-1015, 7)
70            ready3.position = (screen_width-215, 7)
71            ready4.position = (screen_width-155, 7)
72            actions_scroll_area.position = (325, 5)
73            actions_width = screen_width - 550
74        elif (screen_width == 1024):
75            ready1.position = (screen_width-820, 7)
76            ready2.position = (screen_width-760, 7)
77            ready3.position = (screen_width-215, 7)
78            ready4.position = (screen_width-155, 7)
79            actions_scroll_area.position = (325, 5)
80            actions_width = screen_width - 550
81        elif (screen_width == 800):
82            ready1.position = (screen_width-640, 7)
83            ready2.position = (screen_width-580, 7)
84            ready3.position = (screen_width-185, 7)
85            ready4.position = (screen_width-125, 7)
86            actions_scroll_area.position = (280, 5)
87            actions_width = screen_width - 475
88        else:
89            ready1.position = (screen_width-475, 7)
90            ready2.position = (screen_width-420, 7)
91            ready3.position = (screen_width-175, 7)
92            ready4.position = (screen_width-120, 7)
93            actions_scroll_area.position = (280, 5)
94            actions_width = screen_width - 465
95        # and finally add an actions box
96        self.hud.findChild(name="actionsBox").min_size = (actions_width, 0)
97        actions_scroll_area.min_size = (actions_width, 55)
98        actions_scroll_area.max_size = (actions_width, 55)
99        # now it should be OK to display it all
100        self.hud.show()
101
102    def refreshActionsBox(self):
103        """Refresh the actions box so that it displays the contents of
104           self.actionsText
105           @return: None"""
106        self.actionsBox.items = self.actionsText
107
108    def addAction(self, action):
109        """Add an action to the actions box.
110           @type action: ???
111           @param action: ???
112           @return: None"""
113        self.actionsText.insert(0, action)
114        self.refreshActionsBox()
115
116    def showHUD(self):
117        """Show the HUD.
118           @return: None"""
119        self.hud.show()
120
121    def hideHUD(self):
122        """Hide the HUD.
123           @return: None"""
124        self.hud.hide()
125       
126    def initializeMainMenu(self):
127        """Initalize the main menu.
128           @return: None"""
129        self.main_menu = pychan.loadXML("gui/hud_main_menu.xml")
130        self.menu_events = {"resumeButton":self.hideMenu,
131                            "loadButton":self.loadGame, 
132                            "optionsButton":self.displayOptions,
133                            "helpButton":self.displayHelp}
134        self.main_menu.mapEvents(self.menu_events)
135
136    def displayMenu(self):
137        """Displays the main in-game menu.
138           @return: None"""
139        if (self.menu_displayed == False):
140            self.main_menu.show()
141            self.menu_displayed = True
142        elif (self.menu_displayed == True):
143            self.hideMenu()
144
145    def hideMenu(self):
146        """Hides the main in-game menu.
147           @return: None"""
148        self.main_menu.hide()
149        self.menu_displayed = False
150
151    def initializeHelpMenu(self):
152        """Initialize the help menu
153           @return: None"""
154        self.help_dialog = pychan.loadXML("gui/help.xml")
155        help_events = {"closeButton":self.help_dialog.hide}
156        self.help_dialog.mapEvents(help_events)
157        main_help_text = "Put help text here"
158        k_text = " A : Add a test action to the actions display"
159        k_text+="[br] I : Toggle the inventory screen"
160        k_text+="[br]F5 : Take a screenshot"
161        k_text+="[br]     (saves to <parpg>/screenshots/)"
162        k_text+="[br] M : Toggle music on/off"
163        k_text+="[br] Q : Quit the game"
164        self.help_dialog.distributeInitialData({
165                "MainHelpText":main_help_text,
166                "KeybindText":k_text
167                })
168
169    def displayHelp(self):
170        """Display the help screen.
171           @return: None"""
172        self.help_dialog.show()
173
174    def initializeOptionsMenu(self):
175        """Initalize the options menu.
176           @return: None"""
177        self.options_menu = pychan.loadXML("gui/hud_options.xml")
178        self.options_events = {"applyButton":self.applyOptions,
179                               "closeButton":self.options_menu.hide,
180                               "defaultsButton":self.setToDefaults}
181        self.Resolutions = ['640x480', '800x600',
182                            '1024x768', '1280x1024', '1440x900']
183        self.RenderBackends = ['OpenGL', 'SDL']
184        self.renderNumber = 0
185        if (str(self.settings.readSetting('RenderBackend')) == "SDL"):
186            self.renderNumber = 1
187        self.options_menu.distributeInitialData({
188                'ResolutionBox': self.Resolutions,
189                'RenderBox': self.RenderBackends
190                })
191        # TODO: fix bad line length here
192        self.options_menu.distributeData({
193                'FullscreenBox':int(self.settings.readSetting(name="FullScreen")), 
194                'SoundsBox':int(self.settings.readSetting(name="PlaySounds")),
195                'ResolutionBox':self.Resolutions.index(str(self.settings.readSetting("ScreenWidth")) + 'x' + str(self.settings.readSetting("ScreenHeight"))),
196                'RenderBox': self.renderNumber
197                })
198        self.options_menu.mapEvents(self.options_events)
199
200    def requireRestartDialog(self):
201        """Show a dialog asking the user to restart PARPG in order for their
202           changes to take effect.
203           @return: None"""
204        require_restart_dialog = pychan.loadXML('gui/hud_require_restart.xml')
205        require_restart_dialog.mapEvents({'okButton':require_restart_dialog.hide})
206        require_restart_dialog.show()
207
208    def applyOptions(self):
209        """Apply the current options.
210           @return: None"""
211        # TODO: line lengths here are horrible
212        # TODO: add comments
213        self.requireRestart = False
214        enable_fullscreen, enable_sound, screen_resolution, render_backend = self.options_menu.collectData('FullscreenBox', 'SoundsBox', 'ResolutionBox', 'RenderBox')
215
216        if (int(enable_fullscreen) != int(self.settings.readSetting('FullScreen'))):
217            self.setOption('FullScreen', int(enable_fullscreen))
218            self.requireRestart = True
219           
220        if (int(enable_sound) != int(self.settings.readSetting('PlaySounds'))):
221            self.setOption('PlaySounds', int(enable_sound))
222            self.requireRestart = True
223
224        if (screen_resolution != self.Resolutions.index(str(self.settings.readSetting("ScreenWidth")) + 'x' + str(self.settings.readSetting("ScreenHeight")))):
225            self.setOption('ScreenWidth', int(self.Resolutions[screen_resolution].partition('x')[0]))
226            self.setOption('ScreenHeight', int(self.Resolutions[screen_resolution].partition('x')[2]))
227            self.requireRestart = True
228       
229        if (render_backend == 0):
230            render_backend = 'OpenGL'
231        else:
232            render_backend = 'SDL'
233
234        if (render_backend != str(self.settings.readSetting("RenderBackend"))):
235            self.setOption('RenderBackend', render_backend)
236            self.requireRestart = True
237
238        self.settings.tree.write('settings.xml')
239        if (self.requireRestart):
240            self.requireRestartDialog()
241        self.options_menu.hide()
242
243    def setOption(self, name, value):
244        """Set an option within the xml.
245           @type name: ???
246           @param name: The name?
247           @type value: ???
248           @param value: The value?
249           @return: None"""
250        element = self.settings.root_element.find(name)
251        if(element != None):
252            if(value != element.text):
253                element.text = str(value)
254        else:
255            print 'Setting,', name, 'does not exist!'
256
257    def setToDefaults(self):
258        """Reset all the options to the options in settings-dist.xml.
259           @return: None"""
260        shutil.copyfile('settings-dist.xml', 'settings.xml')
261        self.requireRestartDialog()
262        self.options_menu.hide()
263
264    def displayOptions(self):
265        """Display the options menu.
266           @return: None"""
267        self.options_menu.show()
268   
269    def saveGame(self):
270        """Open the save game dialog.
271           @return: None"""
272        print "Save not yet enabled"
273
274    def loadGame(self):
275        """Open the load game dialog.
276           @return: None"""
277        print "Load not yet enabled"
278       
279    def toggleInventory(self):
280        """Manually toggles the inventory button.
281           @return: None"""
282        button = self.hud.findChild(name="inventoryButton")
283        if(button.toggled == 0):
284            button.toggled = 1
285        else:
286            button.toggled = 0
287
Note: See TracBrowser for help on using the repository browser.