source: branches/active/character_customization/game/parpg/mainmenuview.py @ 766

Revision 765, 5.4 KB checked in by aspidites, 8 years ago (diff)

Patch by Aspidites

+ now possible to close options window

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