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

Revision 97, 8.4 KB checked in by bretzel_parpg, 10 years ago (diff)

Added a simple help menu that can be accessed off of the main menu

RevLine 
[74]1#!/usr/bin/python
2
3"""Import all necessary modules"""
[90]4import shutil
[74]5import fife
6import pychan
7from pychan.tools import callbackWithArguments as cbwa
8
9"""Main Hud class"""
10class Hud():
11    """
12    Arguments:
13        engine : an instance of the fife engine
[76]14        settings : an instance of the class Setting from settings.py
[74]15    """
[76]16    def __init__(self, engine, settings):
[74]17        pychan.init(engine, debug = True)
18       
19        self.hud = pychan.loadXML("gui/hud.xml")
20
[76]21        self.settings = settings
[80]22        self.hp = self.hud.findChild(name="healthPoints")
23        self.ap = self.hud.findChild(name="actionPoints")
24        self.actionsBox = self.hud.findChild(name="actionsBox")
[74]25        self.actionsText = []
26        self.menu_displayed = False
[80]27       
28        self.initializeHud()
29        self.initializeMainMenu()
30        self.initializeOptionsMenu()
[97]31        self.initializeHelpMenu()
[74]32
[80]33    def initializeHud(self):
[76]34        """Initialize and show the main HUD"""
[74]35        self.events_to_map = {"menuButton":self.displayMenu, "saveButton":self.saveGame,
36                              "loadButton":self.loadGame}
37        self.hud.mapEvents(self.events_to_map) 
[80]38
39        screen_width = int(self.settings.readSetting('ScreenWidth'))
40        self.hud.findChild(name="mainHudWindow").size = (screen_width, 65)
41
42        self.hud.findChild(name="inventoryButton").position = (screen_width-59, 7)
43       
44        actions_width = screen_width - 175 
45        self.hud.findChild(name="actionsBox").min_size = (actions_width, 0)
46        self.hud.findChild(name="actionsScrollArea").size = (actions_width, 55)
47
[74]48        self.hud.show()
[76]49       
[80]50    def initializeMainMenu(self):
[76]51        """Initalize the main menu"""
[74]52        self.main_menu = pychan.loadXML("gui/hud_main_menu.xml")
53        self.menu_events = {"resumeButton":self.hideMenu, "saveButton":self.saveGame,
54                            "loadButton":self.loadGame, 
[97]55                            "optionsButton":self.displayOptions,
56                            "helpButton":self.displayHelp}
[74]57        self.main_menu.mapEvents(self.menu_events)
58
[97]59    def initializeHelpMenu(self):
60        """Initialize the help menu"""
[80]61
[97]62        self.help_dialog = pychan.loadXML("gui/help.xml")
63        help_events = {"closeButton":self.help_dialog.hide}
64        self.help_dialog.mapEvents(help_events)
65
66        main_help_text = "Put help text here"
67
68        keybindings_text = "A : Add a test action to the actions display[br]I : Toggle the inventory screen[br]F5 : Take a screenshot [br]     (saves to <parpg>/screenshots/)[br]Q : Quit the game"
69
70        self.help_dialog.distributeInitialData({
71                "MainHelpText":main_help_text,
72                "KeybindText":keybindings_text
73                })
74
[80]75    def initializeOptionsMenu(self):
[76]76        """Initalize the options menu"""
[89]77
[76]78        self.options_menu = pychan.loadXML("gui/hud_options.xml")
[80]79        self.options_events = {"applyButton":self.applyOptions,
[89]80                               "closeButton":self.options_menu.hide,
81                               "defaultsButton":self.setToDefaults}
[76]82
[80]83        self.Resolutions = ['640x480', '800x600', '1024x768', '1280x1024', '1440x900']
84        self.RenderBackends = ['OpenGL', 'SDL']
85        self.renderNumber = 0
86        if (str(self.settings.readSetting('RenderBackend')) == "SDL"):
87            self.renderNumber = 1
88        self.options_menu.distributeInitialData({
89                'ResolutionBox': self.Resolutions,
90                'RenderBox': self.RenderBackends
91                })
92        self.options_menu.distributeData({
93                'FullscreenBox':int(self.settings.readSetting(name="FullScreen")), 
94                'SoundsBox':int(self.settings.readSetting(name="PlaySounds")),
95                'ResolutionBox':self.Resolutions.index(str(self.settings.readSetting("ScreenWidth")) + 'x' + str(self.settings.readSetting("ScreenHeight"))),
96                'RenderBox': self.renderNumber
97                })
[76]98       
99        self.options_menu.mapEvents(self.options_events)
100
[89]101    def setToDefaults(self):
[90]102        """ Reset all the options to the options in settings-dist.xml """
[89]103        shutil.copyfile('settings-dist.xml', 'settings.xml')
104        self.requireRestartDialog()
105        self.options_menu.hide()
[76]106
[97]107    def displayHelp(self):
108        """ Display the help screen """
109        self.help_dialog.show()
[89]110
[97]111
[74]112    def refreshActionsBox(self):
113        """
114        Refresh the actions box so that it displays the contents of self.actionsText
115        """
116        self.actionsBox.items = self.actionsText
117
118
119    def addAction(self, action):
120        """
121        Add an action to the actions box.
122        All this function really does is append action to self.actionsText and then
123        call refreshActionsBox
124        """
[76]125        self.actionsText.insert(0, action)
[74]126        self.refreshActionsBox()
127
[76]128    def requireRestartDialog(self):
129        """
130        Show a dialog asking the user to restart PARPG for their changes to take effect
131        """
132        require_restart_dialog = pychan.loadXML('gui/hud_require_restart.xml')
133        require_restart_dialog.mapEvents({'okButton':require_restart_dialog.hide})
134        require_restart_dialog.show()
135
136    def applyOptions(self):
137        """
138        Apply the current options
139        """
140        self.requireRestart = False
[80]141        enable_fullscreen, enable_sound, screen_resolution, render_backend = self.options_menu.collectData('FullscreenBox', 'SoundsBox', 'ResolutionBox', 'RenderBox')
[76]142
143        if (int(enable_fullscreen) != int(self.settings.readSetting('FullScreen'))):
144            self.setOption('FullScreen', int(enable_fullscreen))
145            self.requireRestart = True
146           
147        if (int(enable_sound) != int(self.settings.readSetting('PlaySounds'))):
148            self.setOption('PlaySounds', int(enable_sound))
149            self.requireRestart = True
150
[80]151        if (screen_resolution != self.Resolutions.index(str(self.settings.readSetting("ScreenWidth")) + 'x' + str(self.settings.readSetting("ScreenHeight")))):
152            self.setOption('ScreenWidth', int(self.Resolutions[screen_resolution].partition('x')[0]))
153            self.setOption('ScreenHeight', int(self.Resolutions[screen_resolution].partition('x')[2]))
154            self.requireRestart = True
155       
156        if (render_backend == 0):
157            render_backend = 'OpenGL'
158        else:
159            render_backend = 'SDL'
160
161        if (render_backend != str(self.settings.readSetting("RenderBackend"))):
162            self.setOption('RenderBackend', render_backend)
163            self.requireRestart = True
164
[76]165        self.settings.tree.write('settings.xml')
166        if (self.requireRestart):
167            self.requireRestartDialog()
168        self.options_menu.hide()
169
170    def setOption(self, name, value):
171        """
172        Set an option within the xml
173        """
174        element = self.settings.root_element.find(name)
175        if (element != None):
176            if (value != element.text):
177                element.text = str(value)
178        else:
179            print 'Setting,', name, 'does not exist!'
180
[74]181    def displayOptions(self):
[76]182        """
183        Display the options menu
184        """
185        self.options_menu.show()
[74]186
187    def displayMenu(self):
188        """
189        Displays the main in-game menu
190        """
191        if (self.menu_displayed == False):
192            self.main_menu.show()
193            self.menu_displayed = True
194        elif (self.menu_displayed == True):
195            self.hideMenu()
196
197    def hideMenu(self):
198        """
199        Hides the main in-game menu
200        """
201        self.main_menu.hide()
202        self.menu_displayed = False
203
204    def saveGame(self):
205        """
206        Open the save game dialog
207        """
208        print "save"
209
210    def loadGame(self):
211        """
212        Open the load game dialog
213        """
214        print "load"
215
216    def setHP(self, value):
217        """
218        Set the HP display on the HUD to value
[90]219        NOTE: This does not in any way affect the character's actual health, only what is displayed on the HUD
[74]220        """
221        self.hp.text = value
222
223    def setAP(self, value):
224        """
225        Set the AP display on the HUD to value
226        NOTE: This does no in any way affect the character's actual AP, only
227              what is displayed on the HUD
228        """
229        self.ap.text = value
230       
231    def toggleInventory(self):
232        """
233        Manually toggles the inventory button
234        """
235        button = self.hud.findChild(name="inventoryButton")
236        if (button.toggled == 0):
237            button.toggled = 1
238        else:
239            button.toggled = 0
Note: See TracBrowser for help on using the repository browser.