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

Revision 103, 8.8 KB checked in by bretzel_parpg, 10 years ago (diff)

Added ready slots to HUD. The image on the ready slot on the HUD will alway match the one on the inventory

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