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