Module Settings
[hide private]
[frames] | no frames]

Source Code for Module Settings

  1  ##################################################################### 
  2  # -*- coding: iso-8859-1 -*-                                        # 
  3  #                                                                   # 
  4  # Frets on Fire                                                     # 
  5  # Copyright (C) 2006 Sami Kyöstilä                                  # 
  6  #                                                                   # 
  7  # This program is free software; you can redistribute it and/or     # 
  8  # modify it under the terms of the GNU General Public License       # 
  9  # as published by the Free Software Foundation; either version 2    # 
 10  # of the License, or (at your option) any later version.            # 
 11  #                                                                   # 
 12  # This program is distributed in the hope that it will be useful,   # 
 13  # but WITHOUT ANY WARRANTY; without even the implied warranty of    # 
 14  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the     # 
 15  # GNU General Public License for more details.                      # 
 16  #                                                                   # 
 17  # You should have received a copy of the GNU General Public License # 
 18  # along with this program; if not, write to the Free Software       # 
 19  # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,        # 
 20  # MA  02110-1301, USA.                                              # 
 21  ##################################################################### 
 22   
 23  import Menu 
 24  from Language import _ 
 25  import Dialogs 
 26  import Config 
 27  import Mod 
 28  import Audio 
 29   
 30  import pygame 
 31   
32 -class ConfigChoice(Menu.Choice):
33 - def __init__(self, config, section, option, autoApply = False):
34 self.config = config 35 self.section = section 36 self.option = option 37 self.changed = False 38 self.value = None 39 self.autoApply = autoApply 40 o = config.prototype[section][option] 41 v = config.get(section, option) 42 if isinstance(o.options, dict): 43 values = o.options.values() 44 values.sort() 45 try: 46 valueIndex = values.index(o.options[v]) 47 except KeyError: 48 valueIndex = 0 49 elif isinstance(o.options, list): 50 values = o.options 51 try: 52 valueIndex = values.index(v) 53 except ValueError: 54 valueIndex = 0 55 else: 56 raise RuntimeError("No usable options for %s.%s." % (section, option)) 57 Menu.Choice.__init__(self, text = o.text, callback = self.change, values = values, valueIndex = valueIndex)
58
59 - def change(self, value):
60 o = self.config.prototype[self.section][self.option] 61 62 if isinstance(o.options, dict): 63 for k, v in o.options.items(): 64 if v == value: 65 value = k 66 break 67 68 self.changed = True 69 self.value = value 70 71 if self.autoApply: 72 self.apply()
73
74 - def apply(self):
75 if self.changed: 76 self.config.set(self.section, self.option, self.value)
77
78 -class VolumeConfigChoice(ConfigChoice):
79 - def __init__(self, engine, config, section, option, autoApply = False):
80 ConfigChoice.__init__(self, config, section, option, autoApply) 81 self.engine = engine
82
83 - def change(self, value):
84 ConfigChoice.change(self, value) 85 sound = self.engine.data.screwUpSound 86 sound.setVolume(self.value) 87 sound.play()
88
89 -class KeyConfigChoice(Menu.Choice):
90 - def __init__(self, engine, config, section, option):
91 self.engine = engine 92 self.config = config 93 self.section = section 94 self.option = option 95 self.changed = False 96 self.value = None 97 Menu.Choice.__init__(self, text = "", callback = self.change)
98
99 - def getText(self, selected):
100 def keycode(k): 101 try: 102 return int(k) 103 except: 104 return getattr(pygame, k)
105 o = self.config.prototype[self.section][self.option] 106 v = self.config.get(self.section, self.option) 107 return "%s: %s" % (o.text, pygame.key.name(keycode(v)).capitalize())
108
109 - def change(self):
110 o = self.config.prototype[self.section][self.option] 111 112 if isinstance(o.options, dict): 113 for k, v in o.options.items(): 114 if v == value: 115 value = k 116 break 117 118 key = Dialogs.getKey(self.engine, _("Press a key for '%s' or Escape to cancel.") % (o.text)) 119 120 if key: 121 self.config.set(self.section, self.option, key) 122 self.engine.input.reloadControls()
123
124 - def apply(self):
125 pass
126 127
128 -class SettingsMenu(Menu.Menu):
129 - def __init__(self, engine):
130 applyItem = [(_("Apply New Settings"), self.applySettings)] 131 132 modSettings = [ 133 ConfigChoice(engine.config, "mods", "mod_" + m) for m in Mod.getAvailableMods(engine) 134 ] + applyItem 135 136 gameSettings = [ 137 (_("Mod settings"), modSettings), 138 ConfigChoice(engine.config, "game", "language"), 139 ConfigChoice(engine.config, "game", "leftymode", autoApply = True), 140 ConfigChoice(engine.config, "game", "tapping", autoApply = True), 141 ConfigChoice(engine.config, "game", "uploadscores", autoApply = True), 142 ] 143 gameSettingsMenu = Menu.Menu(engine, gameSettings + applyItem) 144 145 keySettings = [ 146 (_("Test Keys"), lambda: Dialogs.testKeys(engine)), 147 KeyConfigChoice(engine, engine.config, "player", "key_action1"), 148 KeyConfigChoice(engine, engine.config, "player", "key_action2"), 149 KeyConfigChoice(engine, engine.config, "player", "key_1"), 150 KeyConfigChoice(engine, engine.config, "player", "key_2"), 151 KeyConfigChoice(engine, engine.config, "player", "key_3"), 152 KeyConfigChoice(engine, engine.config, "player", "key_4"), 153 KeyConfigChoice(engine, engine.config, "player", "key_5"), 154 KeyConfigChoice(engine, engine.config, "player", "key_left"), 155 KeyConfigChoice(engine, engine.config, "player", "key_right"), 156 KeyConfigChoice(engine, engine.config, "player", "key_up"), 157 KeyConfigChoice(engine, engine.config, "player", "key_down"), 158 KeyConfigChoice(engine, engine.config, "player", "key_cancel"), 159 ] 160 keySettingsMenu = Menu.Menu(engine, keySettings) 161 162 modes = engine.video.getVideoModes() 163 modes.reverse() 164 Config.define("video", "resolution", str, "640x480", text = _("Video Resolution"), options = ["%dx%d" % (m[0], m[1]) for m in modes]) 165 videoSettings = [ 166 ConfigChoice(engine.config, "video", "resolution"), 167 ConfigChoice(engine.config, "video", "fullscreen"), 168 ConfigChoice(engine.config, "video", "fps"), 169 ConfigChoice(engine.config, "video", "multisamples"), 170 #ConfigChoice(engine.config, "opengl", "svgshaders"), # shaders broken at the moment 171 ConfigChoice(engine.config, "opengl", "svgquality"), 172 ConfigChoice(engine.config, "video", "fontscale"), 173 ] 174 videoSettingsMenu = Menu.Menu(engine, videoSettings + applyItem) 175 176 volumeSettings = [ 177 VolumeConfigChoice(engine, engine.config, "audio", "guitarvol"), 178 VolumeConfigChoice(engine, engine.config, "audio", "songvol"), 179 VolumeConfigChoice(engine, engine.config, "audio", "rhythmvol"), 180 VolumeConfigChoice(engine, engine.config, "audio", "screwupvol"), 181 ] 182 volumeSettingsMenu = Menu.Menu(engine, volumeSettings + applyItem) 183 184 audioSettings = [ 185 (_("Volume Settings"), volumeSettingsMenu), 186 ConfigChoice(engine.config, "audio", "delay"), 187 ConfigChoice(engine.config, "audio", "frequency"), 188 ConfigChoice(engine.config, "audio", "bits"), 189 ConfigChoice(engine.config, "audio", "buffersize"), 190 ] 191 audioSettingsMenu = Menu.Menu(engine, audioSettings + applyItem) 192 193 settings = [ 194 (_("Game Settings"), gameSettingsMenu), 195 (_("Key Settings"), keySettingsMenu), 196 (_("Video Settings"), videoSettingsMenu), 197 (_("Audio Settings"), audioSettingsMenu), 198 ] 199 200 self.settingsToApply = settings + \ 201 videoSettings + \ 202 audioSettings + \ 203 volumeSettings + \ 204 gameSettings + \ 205 modSettings 206 207 Menu.Menu.__init__(self, engine, settings)
208
209 - def applySettings(self):
210 for option in self.settingsToApply: 211 if isinstance(option, ConfigChoice): 212 option.apply() 213 self.engine.restart()
214
215 -class GameSettingsMenu(Menu.Menu):
216 - def __init__(self, engine):
217 settings = [ 218 VolumeConfigChoice(engine, engine.config, "audio", "guitarvol", autoApply = True), 219 VolumeConfigChoice(engine, engine.config, "audio", "songvol", autoApply = True), 220 VolumeConfigChoice(engine, engine.config, "audio", "rhythmvol", autoApply = True), 221 VolumeConfigChoice(engine, engine.config, "audio", "screwupvol", autoApply = True), 222 ] 223 Menu.Menu.__init__(self, engine, settings)
224