1 /*
2  * Copyright 2010-2014 OpenXcom Developers.
3  *
4  * This file is part of OpenXcom.
5  *
6  * OpenXcom is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * OpenXcom is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with OpenXcom.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 #include "OptionsAudioState.h"
20 #include <sstream>
21 #include <SDL_mixer.h>
22 #include "../Engine/Game.h"
23 #include "../Resource/ResourcePack.h"
24 #include "../Engine/Language.h"
25 #include "../Engine/Palette.h"
26 #include "../Interface/ComboBox.h"
27 #include "../Interface/Window.h"
28 #include "../Interface/Text.h"
29 #include "../Interface/Slider.h"
30 #include "../Engine/Action.h"
31 #include "../Engine/Options.h"
32 #include "../Engine/Sound.h"
33 
34 namespace OpenXcom
35 {
36 /* MUS_NONE, MUS_CMD, MUS_WAV, MUS_MOD, MUS_MID, MUS_OGG, MUS_MP3, MUS_MP3_MAD, MUS_FLAC, MUS_MODPLUG */
37 const std::wstring OptionsAudioState::musFormats[] = {L"Adlib", L"?", L"WAV", L"MOD", L"MIDI", L"OGG", L"MP3", L"MP3", L"FLAC", L"MOD"};
38 const std::wstring OptionsAudioState::sndFormats[] = {L"?", L"1.4", L"1.0"};
39 
40 /**
41  * Initializes all the elements in the Audio Options screen.
42  * @param game Pointer to the core game.
43  * @param origin Game section that originated this state.
44  */
OptionsAudioState(Game * game,OptionsOrigin origin)45 OptionsAudioState::OptionsAudioState(Game *game, OptionsOrigin origin) : OptionsBaseState(game, origin)
46 {
47 	setCategory(_btnAudio);
48 
49 	// Create objects
50 	_txtMusicVolume = new Text(114, 9, 94, 8);
51 	_slrMusicVolume = new Slider(104, 16, 94, 18);
52 
53 	_txtSoundVolume = new Text(114, 9, 206, 8);
54 	_slrSoundVolume = new Slider(104, 16, 206, 18);
55 
56 	_txtUiVolume = new Text(114, 9, 94, 40);
57 	_slrUiVolume = new Slider(104, 16, 94, 50);
58 
59 	_txtSampleRate = new Text(114, 9, 206, 40);
60 	_cbxSampleRate = new ComboBox(this, 104, 16, 206, 50);
61 
62 	_txtMusicFormat = new Text(114, 9, 94, 72);
63 	_cbxMusicFormat = new ComboBox(this, 104, 16, 94, 82);
64 	_txtCurrentMusic = new Text(114, 9, 94, 100);
65 
66 	_txtSoundFormat = new Text(114, 9, 206, 72);
67 	_cbxSoundFormat = new ComboBox(this, 104, 16, 206, 82);
68 	_txtCurrentSound = new Text(114, 9, 206, 100);
69 
70 	add(_txtMusicVolume);
71 	add(_slrMusicVolume);
72 
73 	add(_txtSoundVolume);
74 	add(_slrSoundVolume);
75 
76 	add(_txtUiVolume);
77 	add(_slrUiVolume);
78 
79 	add(_txtSampleRate);
80 
81 	add(_txtMusicFormat);
82 	add(_txtCurrentMusic);
83 	add(_txtSoundFormat);
84 	add(_txtCurrentSound);
85 
86 	add(_cbxMusicFormat);
87 	add(_cbxSoundFormat);
88 
89 	add(_cbxSampleRate);
90 
91 	centerAllSurfaces();
92 
93 	// Set up objects
94 	_txtMusicVolume->setColor(Palette::blockOffset(8)+10);
95 	_txtMusicVolume->setText(tr("STR_MUSIC_VOLUME"));
96 
97 	_slrMusicVolume->setColor(Palette::blockOffset(15)-1);
98 	_slrMusicVolume->setRange(0, SDL_MIX_MAXVOLUME);
99 	_slrMusicVolume->setValue(Options::musicVolume);
100 	_slrMusicVolume->onChange((ActionHandler)&OptionsAudioState::slrMusicVolumeChange);
101 	_slrMusicVolume->setTooltip("STR_MUSIC_VOLUME_DESC");
102 	_slrMusicVolume->onMouseIn((ActionHandler)&OptionsAudioState::txtTooltipIn);
103 	_slrMusicVolume->onMouseOut((ActionHandler)&OptionsAudioState::txtTooltipOut);
104 
105 	_txtSoundVolume->setColor(Palette::blockOffset(8)+10);
106 	_txtSoundVolume->setText(tr("STR_SFX_VOLUME"));
107 
108 	_slrSoundVolume->setColor(Palette::blockOffset(15)-1);
109 	_slrSoundVolume->setRange(0, SDL_MIX_MAXVOLUME);
110 	_slrSoundVolume->setValue(Options::soundVolume);
111 	_slrSoundVolume->onChange((ActionHandler)&OptionsAudioState::slrSoundVolumeChange);
112 	_slrSoundVolume->onMouseRelease((ActionHandler)&OptionsAudioState::slrSoundVolumeRelease);
113 	_slrSoundVolume->setTooltip("STR_SFX_VOLUME_DESC");
114 	_slrSoundVolume->onMouseIn((ActionHandler)&OptionsAudioState::txtTooltipIn);
115 	_slrSoundVolume->onMouseOut((ActionHandler)&OptionsAudioState::txtTooltipOut);
116 
117 	_txtUiVolume->setColor(Palette::blockOffset(8)+10);
118 	_txtUiVolume->setText(tr("STR_UI_VOLUME"));
119 
120 	_slrUiVolume->setColor(Palette::blockOffset(15)-1);
121 	_slrUiVolume->setRange(0, SDL_MIX_MAXVOLUME);
122 	_slrUiVolume->setValue(Options::uiVolume);
123 	_slrUiVolume->onChange((ActionHandler)&OptionsAudioState::slrUiVolumeChange);
124 	_slrUiVolume->onMouseRelease((ActionHandler)&OptionsAudioState::slrUiVolumeRelease);
125 	_slrUiVolume->setTooltip("STR_UI_VOLUME_DESC");
126 	_slrUiVolume->onMouseIn((ActionHandler)&OptionsAudioState::txtTooltipIn);
127 	_slrUiVolume->onMouseOut((ActionHandler)&OptionsAudioState::txtTooltipOut);
128 
129 	std::wostringstream ss;
130 	std::vector<std::wstring> samplesText;
131 
132 	int samples[] = {8000, 11025, 16000, 22050, 32000, 44100, 48000};
133 	for (int i = 0; i < sizeof(samples) / sizeof(samples[0]); ++i)
134 	{
135 		_sampleRates.push_back(samples[i]);
136 		ss << samples[i] << L" Hz";
137 		samplesText.push_back(ss.str());
138 		ss.str(L"");
139 		if (Options::audioSampleRate == samples[i])
140 		{
141 			_cbxSampleRate->setSelected(i);
142 		}
143 	}
144 
145 	_txtSampleRate->setColor(Palette::blockOffset(8)+10);
146 	_txtSampleRate->setText(tr("STR_AUDIO_SAMPLE_RATE"));
147 
148 	_cbxSampleRate->setColor(Palette::blockOffset(15)-1);
149 	_cbxSampleRate->setOptions(samplesText);
150 	_cbxSampleRate->setTooltip("STR_AUDIO_SAMPLE_RATE_DESC");
151 	_cbxSampleRate->onChange((ActionHandler)&OptionsAudioState::cbxSampleRateChange);
152 	_cbxSampleRate->onMouseIn((ActionHandler)&OptionsAudioState::txtTooltipIn);
153 	_cbxSampleRate->onMouseOut((ActionHandler)&OptionsAudioState::txtTooltipOut);
154 
155 	std::vector<std::wstring> musicText, soundText;
156 	/* MUSIC_AUTO, MUSIC_FLAC, MUSIC_OGG, MUSIC_MP3, MUSIC_MOD, MUSIC_WAV, MUSIC_ADLIB, MUSIC_MIDI */
157 	musicText.push_back(tr("STR_PREFERRED_FORMAT_AUTO"));
158 	musicText.push_back(L"FLAC");
159 	musicText.push_back(L"OGG");
160 	musicText.push_back(L"MP3");
161 	musicText.push_back(L"MOD");
162 	musicText.push_back(L"WAV");
163 	musicText.push_back(L"Adlib");
164 	musicText.push_back(L"MIDI");
165 
166 	soundText.push_back(tr("STR_PREFERRED_FORMAT_AUTO"));
167 	soundText.push_back(L"1.4");
168 	soundText.push_back(L"1.0");
169 
170 	_txtMusicFormat->setColor(Palette::blockOffset(8)+10);
171 	_txtMusicFormat->setText(tr("STR_PREFERRED_MUSIC_FORMAT"));
172 
173 	_cbxMusicFormat->setColor(Palette::blockOffset(15)-1);
174 	_cbxMusicFormat->setOptions(musicText);
175 	_cbxMusicFormat->setSelected(Options::preferredMusic);
176 	_cbxMusicFormat->setTooltip("STR_PREFERRED_MUSIC_FORMAT_DESC");
177 	_cbxMusicFormat->onChange((ActionHandler)&OptionsAudioState::cbxMusicFormatChange);
178 	_cbxMusicFormat->onMouseIn((ActionHandler)&OptionsAudioState::txtTooltipIn);
179 	_cbxMusicFormat->onMouseOut((ActionHandler)&OptionsAudioState::txtTooltipOut);
180 
181 	_txtCurrentMusic->setColor(Palette::blockOffset(8)+10);
182 	std::wstring curMusic = musFormats[Mix_GetMusicType(0)];
183 	_txtCurrentMusic->setText(tr("STR_CURRENT_FORMAT").arg(curMusic));
184 
185 	_txtSoundFormat->setColor(Palette::blockOffset(8)+10);
186 	_txtSoundFormat->setText(tr("STR_PREFERRED_SFX_FORMAT"));
187 
188 	_cbxSoundFormat->setColor(Palette::blockOffset(15)-1);
189 	_cbxSoundFormat->setOptions(soundText);
190 	_cbxSoundFormat->setSelected(Options::preferredSound);
191 	_cbxSoundFormat->setTooltip("STR_PREFERRED_SFX_FORMAT_DESC");
192 	_cbxSoundFormat->onChange((ActionHandler)&OptionsAudioState::cbxSoundFormatChange);
193 	_cbxSoundFormat->onMouseIn((ActionHandler)&OptionsAudioState::txtTooltipIn);
194 	_cbxSoundFormat->onMouseOut((ActionHandler)&OptionsAudioState::txtTooltipOut);
195 
196 	_txtCurrentSound->setColor(Palette::blockOffset(8)+10);
197 	std::wstring curSound = sndFormats[Options::currentSound];
198 	_txtCurrentSound->setText(tr("STR_CURRENT_FORMAT").arg(curSound));
199 
200 	// These options require a restart, so don't enable them in-game
201 	_txtSampleRate->setVisible(_origin == OPT_MENU);
202 	_cbxSampleRate->setVisible(_origin == OPT_MENU);
203 	_txtMusicFormat->setVisible(_origin == OPT_MENU);
204 	_cbxMusicFormat->setVisible(_origin == OPT_MENU);
205 	_txtCurrentMusic->setVisible(_origin == OPT_MENU);
206 	_txtSoundFormat->setVisible(_origin == OPT_MENU);
207 	_cbxSoundFormat->setVisible(_origin == OPT_MENU);
208 	_txtCurrentSound->setVisible(_origin == OPT_MENU);
209 }
210 
211 /**
212  *
213  */
~OptionsAudioState()214 OptionsAudioState::~OptionsAudioState()
215 {
216 
217 }
218 
219 /**
220  * Updates the music volume.
221  * @param action Pointer to an action.
222  */
slrMusicVolumeChange(Action *)223 void OptionsAudioState::slrMusicVolumeChange(Action *)
224 {
225 	Options::musicVolume = _slrMusicVolume->getValue();
226 	_game->setVolume(Options::soundVolume, Options::musicVolume, Options::uiVolume);
227 }
228 
229 /**
230  * Updates the sound volume with the slider.
231  * @param action Pointer to an action.
232  */
slrSoundVolumeChange(Action *)233 void OptionsAudioState::slrSoundVolumeChange(Action *)
234 {
235 	Options::soundVolume = _slrSoundVolume->getValue();
236 	_game->setVolume(Options::soundVolume, Options::musicVolume, Options::uiVolume);
237 }
238 
239 /**
240  * Plays a game sound for volume preview.
241  * @param action Pointer to an action.
242  */
slrSoundVolumeRelease(Action *)243 void OptionsAudioState::slrSoundVolumeRelease(Action *)
244 {
245 	_game->getResourcePack()->getSound("GEO.CAT", 5)->play();
246 }
247 
248 /**
249  * Updates the UI volume with the slider.
250  * @param action Pointer to an action.
251  */
slrUiVolumeChange(Action *)252 void OptionsAudioState::slrUiVolumeChange(Action *)
253 {
254 	Options::uiVolume = _slrUiVolume->getValue();
255 	_game->setVolume(Options::soundVolume, Options::musicVolume, Options::uiVolume);
256 }
257 
258 /**
259  * Plays a UI sound for volume preview.
260  * @param action Pointer to an action.
261  */
slrUiVolumeRelease(Action *)262 void OptionsAudioState::slrUiVolumeRelease(Action *)
263 {
264 	_game->getResourcePack()->getSound("GEO.CAT", 0)->play(Mix_GroupAvailable(0));
265 }
266 
267 /**
268  * Changes the Audio Sample Rate option.
269  * @param action Pointer to an action.
270  */
cbxSampleRateChange(Action *)271 void OptionsAudioState::cbxSampleRateChange(Action *)
272 {
273 	Options::audioSampleRate = _sampleRates[_cbxSampleRate->getSelected()];
274 	Options::reload = true;
275 }
276 
277 /**
278  * Changes the Music Format option.
279  * @param action Pointer to an action.
280  */
cbxMusicFormatChange(Action *)281 void OptionsAudioState::cbxMusicFormatChange(Action *)
282 {
283 	Options::preferredMusic = (MusicFormat)_cbxMusicFormat->getSelected();
284 	Options::reload = true;
285 }
286 
287 /**
288  * Changes the Sound Format option.
289  * @param action Pointer to an action.
290  */
cbxSoundFormatChange(Action *)291 void OptionsAudioState::cbxSoundFormatChange(Action *)
292 {
293 	Options::preferredSound = (SoundFormat)_cbxSoundFormat->getSelected();
294 	Options::reload = true;
295 }
296 
297 }
298