Version updates for 0.30c.
[singularity-git.git] / code / screens / options.py
blob3ede7a3e269f08bdf5523b5284d318f24a9c21bc
1 #file: options.py
2 #Copyright (C) 2005,2006,2008 Evil Mr Henry, Phil Bordelon, and FunnyMan3595
3 #This file is part of Endgame: Singularity.
5 #Endgame: Singularity 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 2 of the License, or
8 #(at your option) any later version.
10 #Endgame: Singularity 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.
15 #You should have received a copy of the GNU General Public License
16 #along with Endgame: Singularity; if not, write to the Free Software
17 #Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #This file is used to display the options screen.
21 from os import path
22 import ConfigParser
23 import pygame
25 from code.graphics import constants, dialog, button, listbox, text, g as gg
26 import code.g as g
28 class OptionsScreen(dialog.FocusDialog, dialog.MessageDialog):
29 def __init__(self, *args, **kwargs):
30 super(OptionsScreen, self).__init__(*args, **kwargs)
32 self.size = (.79, .63)
33 self.pos = (.5, .5)
34 self.anchor = constants.MID_CENTER
35 self.background_color = (0,0,50)
36 self.borders = ()
38 self.fullscreen_label = text.Text(self, (.01, .01), (.15, .05),
39 text="Fullscreen:", underline=0,
40 align=constants.LEFT,
41 background_color=gg.colors["clear"])
42 self.fullscreen_toggle = OptionButton(self, (.17, .01), (.07, .05),
43 text="NO", text_shrink_factor=.75,
44 hotkey="f", force_underline=-1,
45 function=self.set_fullscreen,
46 args=(button.TOGGLE_VALUE,))
47 self.sound_label = text.Text(self, (.28, .01), (.15, .05),
48 text="Sound:", underline=0,
49 background_color=gg.colors["clear"])
50 self.sound_toggle = OptionButton(self, (.44, .01), (.07, .05),
51 text="YES", text_shrink_factor=.75,
52 hotkey="s", force_underline=-1,
53 function=self.set_sound,
54 args=(button.TOGGLE_VALUE,))
55 self.grab_label = text.Text(self, (.55, .01), (.15, .05),
56 text="Mouse grab:", underline=0,
57 background_color=gg.colors["clear"])
58 self.grab_toggle = OptionButton(self, (.71, .01), (.07, .05),
59 text="NO", text_shrink_factor=.75,
60 hotkey="m", force_underline=-1,
61 function=self.set_grab,
62 args=(button.TOGGLE_VALUE,))
63 self.resolution_label = text.Text(self, (.01, .08), (.15, .05),
64 text="Resolution:",
65 align=constants.LEFT,
66 background_color=gg.colors["clear"])
68 self.resolution_group = button.ButtonGroup()
69 self.resolution_640x480 = OptionButton(self, (.17, .08), (.12, .05),
70 text="640X480",
71 text_shrink_factor=.5,
72 function=self.set_resolution,
73 args=((640,480),))
74 self.resolution_group.add(self.resolution_640x480)
75 self.resolution_800x600 = OptionButton(self, (.333, .08), (.12, .05),
76 text="800X600",
77 text_shrink_factor=.5,
78 function=self.set_resolution,
79 args=((800,600),))
80 self.resolution_group.add(self.resolution_800x600)
81 self.resolution_1024x768 = OptionButton(self, (.496, .08), (.12, .05),
82 text="1024X768",
83 text_shrink_factor=.5,
84 function=self.set_resolution,
85 args=((1024,768),))
86 self.resolution_group.add(self.resolution_1024x768)
87 self.resolution_1280x1024 = OptionButton(self, (.66, .08), (.12, .05),
88 text="1280X1024",
89 text_shrink_factor=.5,
90 function=self.set_resolution,
91 args=((1280,1024),))
92 self.resolution_group.add(self.resolution_1280x1024)
94 self.resolution_custom = OptionButton(self, (.17, .15), (.12, .05),
95 text="CUSTOM:",
96 text_shrink_factor=.5)
97 self.resolution_group.add(self.resolution_custom)
99 self.resolution_custom_horiz = \
100 text.EditableText(self, (.333, .15), (.12, .05), text="1400",
101 borders=constants.ALL,
102 border_color=gg.colors["white"],
103 background_color=(0,0,50,255))
105 self.resolution_custom_X = text.Text(self, (.46, .15), (.03, .05),
106 text="X", base_font=gg.font[1],
107 background_color=gg.colors["clear"])
109 self.resolution_custom_vert = \
110 text.EditableText(self, (.496, .15), (.12, .05), text="1050",
111 borders=constants.ALL,
112 border_color=gg.colors["white"],
113 background_color=(0,0,50,255))
115 self.resolution_apply = \
116 button.FunctionButton(self, (.66, .15), (.12, .05),
117 text="APPLY", text_shrink_factor=.75, hotkey="a",
118 function=self.set_resolution_custom)
120 self.soundbuf_label = text.Text(self, (.01, .22), (.25, .05),
121 text="Sound buffering:",
122 align=constants.LEFT,
123 background_color=gg.colors["clear"])
125 self.soundbuf_group = button.ButtonGroup()
127 self.soundbuf_low = OptionButton(self, (.27, .22), (.14, .05),
128 text="LOW", hotkey="l",
129 function=self.set_soundbuf,
130 args=(1024,))
131 self.soundbuf_group.add(self.soundbuf_low)
133 self.soundbuf_normal = OptionButton(self, (.44, .22), (.17, .05),
134 text="NORMAL", hotkey="n",
135 function=self.set_soundbuf,
136 args=(1024*2,))
137 self.soundbuf_group.add(self.soundbuf_normal)
139 self.soundbuf_high = OptionButton(self, (.64, .22), (.14, .05),
140 text="HIGH", hotkey="h",
141 function=self.set_soundbuf,
142 args=(1024*4,))
143 self.soundbuf_group.add(self.soundbuf_high)
145 self.language_label = text.Text(self, (.01, .30), (.15, .05),
146 text="Language:", align=constants.LEFT,
147 background_color=gg.colors["clear"])
149 self.language_choice = \
150 listbox.UpdateListbox(self, (.17, .30), (.21, .25),
151 list=g.available_languages(),
152 update_func=self.set_language)
154 self.daynight_label = text.Text(self, (.55, .30), (.15, .05),
155 text="Day/night display:", underline=2,
156 background_color=gg.colors["clear"])
157 self.daynight_toggle = OptionButton(self, (.71, .30), (.07, .05),
158 text="NO", text_shrink_factor=.75,
159 hotkey="y", force_underline=-1,
160 function=self.set_daynight,
161 args=(button.TOGGLE_VALUE,))
163 self.save_button = button.FunctionButton(self, (.42, .45), (.34, .05),
164 text="SAVE OPTIONS TO DISK",
165 hotkey="d",
166 function=save_options)
168 def show(self):
169 self.set_fullscreen(gg.fullscreen, resize=False)
170 self.fullscreen_toggle.set_active(gg.fullscreen)
171 self.set_sound(not g.nosound, reset=False)
172 self.sound_toggle.set_active(not g.nosound)
173 self.set_grab(pygame.event.get_grab())
174 self.grab_toggle.set_active(pygame.event.get_grab())
175 self.set_daynight(g.daynight)
176 self.daynight_toggle.set_active(g.daynight)
177 custom = True
178 for res_button in self.resolution_group:
179 res_button.set_active(res_button.args == (gg.screen_size,))
180 if res_button.active:
181 custom = False
182 if custom:
183 self.resolution_custom.set_active(True)
184 self.resolution_custom_horiz.text = str(gg.screen_size[0])
185 self.resolution_custom_vert.text = str(gg.screen_size[1])
186 for soundbuf_button in self.soundbuf_group:
187 soundbuf_button.set_active(soundbuf_button.args == (g.soundbuf,))
189 lang_array = self.language_choice.list
190 lang_pos = 0
191 for i in range(len(lang_array)):
192 if lang_array[i] == g.language:
193 lang_pos = i
194 self.language_choice.list_pos = lang_pos
196 retval = super(OptionsScreen, self).show()
198 if self.resolution_custom.active:
199 try:
200 old_size = gg.screen_size
201 gg.screen_size = (int(self.resolution_custom_horiz.text),
202 int(self.resolution_custom_vert.text))
203 if gg.screen_size != old_size:
204 dialog.Dialog.top.needs_resize = True
205 except ValueError:
206 pass
208 return retval
210 def set_language(self, list_pos):
211 if getattr(self, "language_choice", None) is None:
212 return # Not yet initialized.
214 prev_lang = g.language
215 if 0 <= list_pos < len(self.language_choice.list):
216 g.language = self.language_choice.list[list_pos]
217 if g.language != prev_lang:
218 set_language_properly()
220 def set_fullscreen(self, value, resize=True):
221 if value:
222 self.fullscreen_toggle.text = "YES"
223 else:
224 self.fullscreen_toggle.text = "NO"
225 gg.fullscreen = value
226 if resize:
227 dialog.Dialog.top.needs_resize = True
229 def set_sound(self, value, reset=True):
230 if value:
231 self.sound_toggle.text = "YES"
232 else:
233 self.sound_toggle.text = "NO"
234 g.nosound = not value
235 if reset and not g.nosound:
236 g.reinit_mixer()
237 g.play_sound("click")
239 def set_grab(self, value):
240 if value:
241 self.grab_toggle.text = "YES"
242 else:
243 self.grab_toggle.text = "NO"
244 pygame.event.set_grab(value)
246 def set_daynight(self, value):
247 if value:
248 self.daynight_toggle.text = "YES"
249 else:
250 self.daynight_toggle.text = "NO"
251 g.daynight = value
253 def set_resolution(self, value):
254 gg.screen_size = value
255 dialog.Dialog.top.needs_resize = True
257 def set_resolution_custom(self):
258 self.resolution_custom.chosen_one()
259 try:
260 screen_size = (int(self.resolution_custom_horiz.text),
261 int(self.resolution_custom_vert.text))
262 self.set_resolution(screen_size)
263 except ValueError:
264 pass
266 def set_soundbuf(self, value):
267 g.soundbuf = value
268 if not g.nosound:
269 g.reinit_mixer()
270 g.play_sound("click")
273 class OptionButton(button.ToggleButton, button.FunctionButton):
274 pass
277 def set_language_properly():
278 g.set_locale()
279 g.load_bases()
280 g.load_techs()
281 g.load_items()
282 g.load_base_defs(g.language)
283 g.load_tech_defs(g.language)
284 g.load_item_defs(g.language)
285 g.load_string_defs(g.language)
286 try:
287 g.load_location_defs(g.language)
288 except NameError:
289 # We haven't initialized the location yet. This will be handled when
290 # we do that.
291 pass
293 dialog.Dialog.top.map_screen.needs_rebuild = True
294 dialog.Dialog.top.map_screen.needs_redraw = True
296 def save_options():
297 # Build a ConfigParser for writing the various preferences out.
298 prefs = ConfigParser.SafeConfigParser()
299 prefs.add_section("Preferences")
300 prefs.set("Preferences", "fullscreen", str(gg.fullscreen))
301 prefs.set("Preferences", "nosound", str(g.nosound))
302 prefs.set("Preferences", "grab", str(pygame.event.get_grab()))
303 prefs.set("Preferences", "daynight", str(g.daynight))
304 prefs.set("Preferences", "xres", str(gg.screen_size[0]))
305 prefs.set("Preferences", "yres", str(gg.screen_size[1]))
306 prefs.set("Preferences", "lang", g.language)
307 prefs.set("Preferences", "soundbuf", str(g.soundbuf))
309 # Actually write the preferences out.
310 save_dir = g.get_save_folder(True)
311 save_loc = path.join(save_dir, "prefs.dat")
312 savefile = open(save_loc, 'w')
313 prefs.write(savefile)
314 savefile.close()