source: branches/active/character_customization/game/parpg/gui/menus.py @ 833

Revision 813, 6.1 KB checked in by aspidites, 9 years ago (diff)

Patch by Aspidites:

-updated all calls to settings.paths to the more readable settings.system_path and settings.user_path properties

  • renamed sys_path to system_path for consistency
Line 
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
16import logging
17
18from fife.extensions import pychan
19from dialogs import RestartDialog
20
21logger = logging.getLogger('menus')
22
23class ContextMenu(object):
24    def __init__(self, engine, menu_items, pos):
25        """@type engine: engine.Engine
26           @param engine: An instance of the class Engine from engine.py
27           @type menu_items: list
28           @param menu_items: A list of items containing the name and
29                              text for the menu item and callback
30                              i.e. [["menu", "Some text",  Callback]
31           @type pos: (int, int)
32           @param pos: Screen position to use
33           @return: None"""
34        self.vbox = pychan.widgets.VBox(position=pos)
35        events_to_map = {}
36        for item in menu_items:
37            p = pychan.widgets.Button(name=item[0], text=unicode(item[1]))
38            self.vbox.addChild(p)
39            events_to_map [item[0]] = self.actionDecorator(*item[2:])
40        self.vbox.mapEvents(events_to_map)
41   
42    def show(self):
43        """Shows the context menu"""
44        self.vbox.show()
45    def hide(self):
46        """Hides the context menu"""
47        self.vbox.hide()
48       
49    def actionDecorator (self,func, *args, **kwargs):
50        """This function is supposed to add some generic that should be
51        executed before and/or after an action is fired through the
52        context menu.
53       
54        @type func: Any callable
55        @param func: The original action function
56        @param args: Unpacked list of positional arguments
57        @param kwargs: Unpacked list of keyword arguments
58        @return: A wrapped version of func"""
59        def decoratedFunc ():
60            """ This is the actual wrapped version of func, that is returned.
61            It takes no external arguments, so it can safely be passed around
62            as a callback."""
63            # some stuff that we do before the actual action is executed
64            self.hide()
65            # run the action function, and record the return value
66            ret_val = func (*args,**kwargs)
67            # we can eventually add some post-action code here, if needed (e.g. logging)
68            pass       
69            # return the value, as if the original function was called
70            return ret_val
71        return decoratedFunc
72       
73class SettingsMenu(object):
74    def __init__(self, engine, settings):
75        self.engine = engine
76        self.settings = settings
77
78        width = self.settings.fife.ScreenWidth
79        height = self.settings.fife.ScreenHeight
80
81        # available options
82        screen_modes = self.engine.getDeviceCaps().getSupportedScreenModes()
83        resolutions = list(set([(mode.getWidth(), mode.getHeight())
84                                for mode in screen_modes]))
85        self.resolutions = ["{0}x{1}".format(item[0], item[1])
86                            for item in sorted(resolutions)[1:]]
87
88        self.render_backends = ['OpenGL', 'SDL']
89        self.lighting_models = range(3)
90
91        # selected options
92        self.resolution = "{0}x{1}".format(width, height)
93        self.render_backend = self.settings.fife.RenderBackend
94        self.lighting_model = self.settings.fife.Lighting
95        self.fullscreen = self.settings.fife.FullScreen
96        self.sound = self.settings.fife.EnableSound
97
98        self.window = pychan.loadXML('gui/settings_menu.xml')
99        self.restart_dialog = RestartDialog()
100        self.window.mapEvents({'okButton': self.save,
101                               'cancelButton': self.hide,
102                               'defaultButton': self.reset})
103        self.initializeWidgets()
104        self.fillWidgets()
105
106    def initializeWidgets(self):
107        initial_data = {'screen_resolution': self.resolutions,
108                        'render_backend': self.render_backends,
109                        'lighting_model': self.lighting_models}
110
111        self.window.distributeInitialData(initial_data)
112
113
114    def fillWidgets(self):
115        resolution = self.resolutions.index(self.resolution)
116        backend = self.render_backends.index(self.render_backend)
117        lighting = self.lighting_models.index(self.lighting_model)
118
119        self.window.distributeData({'screen_resolution': resolution,
120                                    'render_backend': backend,
121                                    'lighting_model': lighting,
122                                    'enable_fullscreen': self.fullscreen,
123                                    'enable_sound': self.sound})
124
125    def show(self):
126        self.window.show()
127
128    def hide(self):
129        self.window.hide()
130
131    def reset(self):
132        self.settings.read(self.settings.system_path)
133        self.fillWidgets()
134
135    def save(self):
136        (resolution, backend, lighting,
137         fullscreen, sound) = self.window.collectData('screen_resolution',
138                                                      'render_backend',
139                                                      'lighting_model', 
140                                                      'enable_fullscreen',
141                                                      'enable_sound')
142
143        width, height = self.resolutions[resolution].split('x')
144        self.settings.fife.ScreenWidth = width
145        self.settings.fife.ScreenHeight = height
146        self.settings.fife.RenderBackend = self.render_backends[backend]
147        self.settings.fife.Lighting = self.lighting_models[lighting]
148        self.settings.fife.FullScreen = fullscreen
149        self.settings.fife.EnableSound = sound
150        self.settings.write()
151       
152        self.restart_dialog.show()
153        self.hide()
Note: See TracBrowser for help on using the repository browser.