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.
25 from code
.graphics
import constants
, dialog
, button
, listbox
, text
, g
as gg
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)
34 self
.anchor
= constants
.MID_CENTER
35 self
.background_color
= (0,0,50)
38 self
.fullscreen_label
= text
.Text(self
, (.01, .01), (.15, .05),
39 text
="Fullscreen:", underline
=0,
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),
66 background_color
=gg
.colors
["clear"])
68 self
.resolution_group
= button
.ButtonGroup()
69 self
.resolution_640x480
= OptionButton(self
, (.17, .08), (.12, .05),
71 text_shrink_factor
=.5,
72 function
=self
.set_resolution
,
74 self
.resolution_group
.add(self
.resolution_640x480
)
75 self
.resolution_800x600
= OptionButton(self
, (.333, .08), (.12, .05),
77 text_shrink_factor
=.5,
78 function
=self
.set_resolution
,
80 self
.resolution_group
.add(self
.resolution_800x600
)
81 self
.resolution_1024x768
= OptionButton(self
, (.496, .08), (.12, .05),
83 text_shrink_factor
=.5,
84 function
=self
.set_resolution
,
86 self
.resolution_group
.add(self
.resolution_1024x768
)
87 self
.resolution_1280x1024
= OptionButton(self
, (.66, .08), (.12, .05),
89 text_shrink_factor
=.5,
90 function
=self
.set_resolution
,
92 self
.resolution_group
.add(self
.resolution_1280x1024
)
94 self
.resolution_custom
= OptionButton(self
, (.17, .15), (.12, .05),
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
,
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
,
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
,
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",
166 function
=save_options
)
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
)
178 for res_button
in self
.resolution_group
:
179 res_button
.set_active(res_button
.args
== (gg
.screen_size
,))
180 if res_button
.active
:
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
191 for i
in range(len(lang_array
)):
192 if lang_array
[i
] == g
.language
:
194 self
.language_choice
.list_pos
= lang_pos
196 retval
= super(OptionsScreen
, self
).show()
198 if self
.resolution_custom
.active
:
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
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):
222 self
.fullscreen_toggle
.text
= "YES"
224 self
.fullscreen_toggle
.text
= "NO"
225 gg
.fullscreen
= value
227 dialog
.Dialog
.top
.needs_resize
= True
229 def set_sound(self
, value
, reset
=True):
231 self
.sound_toggle
.text
= "YES"
233 self
.sound_toggle
.text
= "NO"
234 g
.nosound
= not value
235 if reset
and not g
.nosound
:
237 g
.play_sound("click")
239 def set_grab(self
, value
):
241 self
.grab_toggle
.text
= "YES"
243 self
.grab_toggle
.text
= "NO"
244 pygame
.event
.set_grab(value
)
246 def set_daynight(self
, value
):
248 self
.daynight_toggle
.text
= "YES"
250 self
.daynight_toggle
.text
= "NO"
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()
260 screen_size
= (int(self
.resolution_custom_horiz
.text
),
261 int(self
.resolution_custom_vert
.text
))
262 self
.set_resolution(screen_size
)
266 def set_soundbuf(self
, value
):
270 g
.play_sound("click")
273 class OptionButton(button
.ToggleButton
, button
.FunctionButton
):
277 def set_language_properly():
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
)
287 g
.load_location_defs(g
.language
)
289 # We haven't initialized the location yet. This will be handled when
293 dialog
.Dialog
.top
.map_screen
.needs_rebuild
= True
294 dialog
.Dialog
.top
.map_screen
.needs_redraw
= True
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
)