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

Revision 90, 7.6 KB checked in by bretzel_parpg, 10 years ago (diff)

Added a few comments and changed the text on the set to defaults button

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