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

Revision 76, 5.3 KB checked in by bretzel_parpg, 10 years ago (diff)

Added a functional options menu to the game. Right now it has options for fullscreen and sounds.

Line 
1#!/usr/bin/python
2
3# Make the actions append at the top instead of the bottom
4
5"""Import all necessary modules"""
6import fife
7import pychan
8from pychan.tools import callbackWithArguments as cbwa
9
10"""Main Hud class"""
11class Hud():
12    """
13    Arguments:
14        engine : an instance of the fife engine
15        settings : an instance of the class Setting from settings.py
16    """
17    def __init__(self, engine, settings):
18        pychan.init(engine, debug = True)
19       
20        self.hud = pychan.loadXML("gui/hud.xml")
21
22        self.settings = settings
23        self.hp = self.hud.findChild(name="HealthPoints")
24        self.ap = self.hud.findChild(name="ActionPoints")
25        self.actionsBox = self.hud.findChild(name="ActionsBox")
26        self.actionsText = []
27        self.menu_displayed = False
28
29        """Initialize and show the main HUD"""
30        self.events_to_map = {"menuButton":self.displayMenu, "saveButton":self.saveGame,
31                              "loadButton":self.loadGame}
32        self.hud.mapEvents(self.events_to_map) 
33        self.hud.show()
34       
35        """Initalize the main menu"""
36        self.main_menu = pychan.loadXML("gui/hud_main_menu.xml")
37        self.menu_events = {"resumeButton":self.hideMenu, "saveButton":self.saveGame,
38                            "loadButton":self.loadGame, 
39                            "optionsButton":self.displayOptions}
40        self.main_menu.mapEvents(self.menu_events)
41
42        """Initalize the options menu"""
43        self.options_menu = pychan.loadXML("gui/hud_options.xml")
44        self.options_events = {"applyButton":self.applyOptions, "closeButton":self.options_menu.hide}
45
46        self.options_menu.distributeData({'FullscreenBox':int(self.settings.readSetting(name="FullScreen")),
47                                          'SoundsBox':int(self.settings.readSetting(name="PlaySounds"))})
48       
49        self.options_menu.mapEvents(self.options_events)
50
51
52    def refreshActionsBox(self):
53        """
54        Refresh the actions box so that it displays the contents of self.actionsText
55        """
56        self.actionsBox.items = self.actionsText
57
58
59    def addAction(self, action):
60        """
61        Add an action to the actions box.
62        All this function really does is append action to self.actionsText and then
63        call refreshActionsBox
64        """
65        self.actionsText.insert(0, action)
66        self.refreshActionsBox()
67
68    def requireRestartDialog(self):
69        """
70        Show a dialog asking the user to restart PARPG for their changes to take effect
71        """
72        require_restart_dialog = pychan.loadXML('gui/hud_require_restart.xml')
73        require_restart_dialog.mapEvents({'okButton':require_restart_dialog.hide})
74        require_restart_dialog.show()
75
76    def applyOptions(self):
77        """
78        Apply the current options
79        """
80        self.requireRestart = False
81        enable_fullscreen, enable_sound = self.options_menu.collectData('FullscreenBox', 'SoundsBox')
82
83        if (int(enable_fullscreen) != int(self.settings.readSetting('FullScreen'))):
84            self.setOption('FullScreen', int(enable_fullscreen))
85            self.requireRestart = True
86           
87        if (int(enable_sound) != int(self.settings.readSetting('PlaySounds'))):
88            self.setOption('PlaySounds', int(enable_sound))
89            self.requireRestart = True
90
91        self.settings.tree.write('settings.xml')
92        if (self.requireRestart):
93            self.requireRestartDialog()
94        self.options_menu.hide()
95
96    def setOption(self, name, value):
97        """
98        Set an option within the xml
99        """
100        element = self.settings.root_element.find(name)
101        if (element != None):
102            if (value != element.text):
103                element.text = str(value)
104        else:
105            print 'Setting,', name, 'does not exist!'
106
107    def displayOptions(self):
108        """
109        Display the options menu
110        """
111        self.options_menu.show()
112
113    def displayMenu(self):
114        """
115        Displays the main in-game menu
116        """
117        if (self.menu_displayed == False):
118            self.main_menu.show()
119            self.menu_displayed = True
120        elif (self.menu_displayed == True):
121            self.hideMenu()
122
123    def hideMenu(self):
124        """
125        Hides the main in-game menu
126        """
127        self.main_menu.hide()
128        self.menu_displayed = False
129
130    def saveGame(self):
131        """
132        Open the save game dialog
133        """
134        print "save"
135
136    def loadGame(self):
137        """
138        Open the load game dialog
139        """
140        print "load"
141
142    def setHP(self, value):
143        """
144        Set the HP display on the HUD to value
145        NOTE: This does not in any way affect the character's actual health, only
146              what is displayed on the HUD
147        """
148        self.hp.text = value
149
150    def setAP(self, value):
151        """
152        Set the AP display on the HUD to value
153        NOTE: This does no in any way affect the character's actual AP, only
154              what is displayed on the HUD
155        """
156        self.ap.text = value
157       
158    def toggleInventory(self):
159        """
160        Manually toggles the inventory button
161        """
162        button = self.hud.findChild(name="inventoryButton")
163        if (button.toggled == 0):
164            button.toggled = 1
165        else:
166            button.toggled = 0
Note: See TracBrowser for help on using the repository browser.