comparison src/parpg/mainmenuview.py @ 0:1fd2201f5c36

Initial commit of parpg-core.
author M. George Hansen <technopolitica@gmail.com>
date Sat, 14 May 2011 01:12:35 -0700
parents
children d60f1dab8469
comparison
equal deleted inserted replaced
-1:000000000000 0:1fd2201f5c36
1 # This file is part of PARPG.
2
3 # PARPG is free software: you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation, either version 3 of the License, or
6 # (at your option) any later version.
7
8 # PARPG is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
12
13 # You should have received a copy of the GNU General Public License
14 # along with PARPG. If not, see <http://www.gnu.org/licenses/
15
16 import os
17
18 from fife.extensions import pychan
19
20 from viewbase import ViewBase
21 from parpg.gui.filebrowser import FileBrowser
22 from parpg.gui.menus import SettingsMenu
23
24 class MainMenuView(ViewBase):
25 """View that is used to display the main menu"""
26
27 def __init__(self, engine, model):
28 """Constructor for MainMenuView
29 @param engine: A fife.Engine instance
30 @type engine: fife.Engine
31 @param model: a script.GameModel instance
32 @type model: script.GameModel
33 """
34 ViewBase.__init__(self, engine, model)
35 self.quit_window = None
36 self.new_game_callback = None
37 self.load_game_callback = None
38 self.quit_callback = None
39 self.main_menu = None
40 self.character_screen = None
41 self.gui_path = os.path.join(self.model.settings.system_path,
42 self.model.settings.parpg.GuiPath)
43
44 def showMenu(self):
45 """"Shows the main menu"""
46 self.main_menu_background.show()
47 self.main_menu.show()
48
49 def hideMenu(self):
50 """"Hides the main menu"""
51 self.main_menu.hide()
52 self.main_menu_background.hide()
53
54 def initalizeMainMenu(self, new_game, load_game, quit_game):
55 """Initialized the main menu and sets the callbacks"""
56 # Set a simple background to display the main screen.
57 self.main_menu_background = pychan.loadXML(os.path.join(self.gui_path,
58 'main_menu_background.xml'))
59
60 # Initialize the main menu screen.
61 screen_mode = self.engine.getRenderBackend().getCurrentScreenMode()
62 self.main_menu_background.width = screen_mode.getWidth()
63 self.main_menu_background.height = screen_mode.getHeight()
64 self.main_menu = pychan.loadXML(os.path.join(self.gui_path,
65 'main_menu.xml'))
66
67 # Setup images for variables widgets
68 self.main_menu.background_image = os.path.join(self.gui_path,
69 'notebook',
70 'notebook_background.png')
71 quit_button = self.main_menu.findChild(name='quitButton')
72 quit_button.up_image = os.path.join(self.gui_path, 'notebook', 'tabs',
73 'tab2_bg_dark_bottom.png')
74 quit_button.hover_image = os.path.join(self.gui_path, 'notebook',
75 'tabs',
76 'tab2_bg_normal_bottom.png')
77 quit_button.down_image = os.path.join(self.gui_path, 'notebook',
78 'tabs',
79 'tab2_bg_normal_bottom.png')
80
81 self.main_menu.adaptLayout()
82 self.new_game_callback = new_game
83 self.load_game_callback = load_game
84 self.quit_callback = quit_game
85 menu_events = {}
86 menu_events["newButton"] = self.newGame
87 menu_events["loadButton"] = self.loadGame
88 menu_events["settingsButton"] = self.displaySettings
89 menu_events["quitButton"] = self.quitGame
90 self.main_menu.mapEvents(menu_events)
91
92 self.initializeQuitDialog()
93 self.initializeSettingsMenu()
94
95 def newGame(self):
96 """Called when user request to start a new game.
97 @return: None"""
98 self.new_game_callback()
99
100 def loadGame(self):
101 """ Called when the user wants to load a game.
102 @return: None"""
103 load_browser = FileBrowser(self.engine,
104 self.model.settings,
105 self.load_game_callback,
106 gui_xml_path=os.path.join(self.gui_path,
107 'loadbrowser.xml'),
108 save_file=False,
109 extensions=('.dat'))
110 load_browser.showBrowser()
111
112 def initializeQuitDialog(self):
113 """Creates the quit confirmation dialog
114 @return: None"""
115
116 self.quit_window = pychan.widgets.Window(title=unicode("Quit?"), \
117 min_size=(200,0))
118
119 hbox = pychan.widgets.HBox()
120 are_you_sure = "Are you sure you want to quit?"
121 label = pychan.widgets.Label(text=unicode(are_you_sure))
122 yes_button = pychan.widgets.Button(name="yes_button",
123 text=unicode("Yes"),
124 min_size=(90,20),
125 max_size=(90,20))
126 no_button = pychan.widgets.Button(name="no_button",
127 text=unicode("No"),
128 min_size=(90,20),
129 max_size=(90,20))
130
131 self.quit_window.addChild(label)
132 hbox.addChild(yes_button)
133 hbox.addChild(no_button)
134 self.quit_window.addChild(hbox)
135
136 events_to_map = { "yes_button": self.quit_callback,
137 "no_button": self.quit_window.hide }
138
139 self.quit_window.mapEvents(events_to_map)
140
141
142 def quitGame(self):
143 """Called when user requests to quit game.
144 @return: None"""
145 self.quit_window.show()
146
147 def initializeSettingsMenu(self):
148 self.settings_menu = SettingsMenu(self.engine, self.model.settings)
149
150 def displaySettings(self):
151 self.settings_menu.show()