Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
blob: 13e6d5788779a399c482035ac6da1f4d82bdb99d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/*
   client/audiosettingsmenu.cc
   This file is part of the Osirion project and is distributed under
   the terms of the GNU General Public License version 2
*/

#include "client/audiosettingsmenu.h"
#include "client/closeiconbutton.h"
#include "core/cvar.h"
#include "ui/button.h"
#include "ui/checkbox.h"
#include "ui/slider.h"
#include "ui/iconbutton.h"
#include "ui/inputbox.h"
#include "ui/label.h"
#include "ui/ui.h"
#include "math/functions.h"

#include <iostream>

namespace client {
	
AudioSettingsMenu::AudioSettingsMenu(ui::Widget *parent, const char *label) : ui::Window(parent)
{
	set_label(label);
	set_border(true);
	set_background(true);
	set_font(ui::root()->font_small());
	
	// window title
	_titlelabel = new ui::Label(this);
	_titlelabel->set_label("title");
	_titlelabel->set_background(false);
	_titlelabel->set_border(false);
	_titlelabel->set_font(ui::root()->font_large());
	_titlelabel->set_alignment(ui::AlignCenter);
	_titlelabel->set_text("AUDIO SETTINGS");
	
	// close button
	_closebutton = new CloseIconButton(_titlelabel);
	
	// content frame
	_frame = new ui::Widget(this);
	_frame->set_label("frame");
	_frame->set_background(true);
	_frame->set_border(true);	
	_frame->set_focus();
	
	// master volume
	_mastervolumelabel = new ui::Label(_frame);
	_mastervolumelabel->set_label("snd_volume");
	_mastervolumelabel->set_background(false);
	_mastervolumelabel->set_border(false);
	_mastervolumelabel->set_text("Master volume");
	
	_mastervolumeslider = new ui::Slider(_frame);
	_mastervolumeslider->set_background(false);
	_mastervolumeslider->set_border(true);
	_mastervolumeslider->set_range(0.0f, 100.0f);
	
	// engine volume
	_enginevolumelabel = new ui::Label(_frame);
	_enginevolumelabel->set_label("snd_engines");
	_enginevolumelabel->set_background(false);
	_enginevolumelabel->set_border(false);
	_enginevolumelabel->set_text("Engine volume");
	
	_enginevolumeslider = new ui::Slider(_frame);
	_enginevolumeslider->set_background(false);
	_enginevolumeslider->set_border(true);
	_enginevolumeslider->set_range(0.0f, 100.0f);
	
	// options label
	_optionslabel = new ui::Label(_frame);
	_optionslabel->set_label("options");
	_optionslabel->set_background(false);
	_optionslabel->set_border(false);
	_optionslabel->set_text("Options");
	
	// doppler effect checkbox
	_dopplercheckbox = new ui::Checkbox(_frame);
	_dopplercheckbox->set_label("snd_doppler");
	_dopplercheckbox->set_text("Enable doppler effect (requires sound restart)");
	_dopplercheckbox->set_border(true);

	// apply button
	_applybutton = new ui::Button(_frame, "Apply");
	_applybutton->set_label("apply");

	// reset button
	_resetbutton = new ui::Button(_frame, "Reset");
	_resetbutton->set_label("reset");

}

AudioSettingsMenu::~AudioSettingsMenu()
{
}

void AudioSettingsMenu::show()
{
	refresh();
	ui::Window::show();
}

void AudioSettingsMenu::refresh()
{
	math::Color color;
	
	// master volume
	if (core::Cvar *cv_volume = core::Cvar::find("snd_volume"))
	{
		float v = cv_volume->value();
		math::clamp(v, 0.0f, 1.0f);
		_mastervolumeslider->set_value(v * 100.0f);
		
	}
	else
	{
		_mastervolumeslider->set_value(0.0f);
	}
	
	// engine volume
	if (core::Cvar *cv_engines = core::Cvar::find("snd_engines"))
	{
		float v = cv_engines->value();
		math::clamp(v, 0.0f, 1.0f);
		_enginevolumeslider->set_value(v * 100.0f);
		
	}
	else
	{
		_enginevolumeslider->set_value(0.0f);
	}
	
	// doppler effect
	if (core::Cvar *cv_doppler = core::Cvar::find("snd_doppler"))
	{
		float v = cv_doppler->value();
		_dopplercheckbox->set_value(v > 0.0f);
	}
	else
	{
		_dopplercheckbox->set_value(false);
	}
}

void AudioSettingsMenu::apply()
{
	// set master volume
	if (core::Cvar *cv_volume = core::Cvar::find("snd_volume"))
	{
		float v = _mastervolumeslider->value();
		cv_volume->assign(v * 0.01f);
	}
	
	// set engine volume
	if (core::Cvar *cv_engines = core::Cvar::find("snd_engines"))
	{
		float v = _enginevolumeslider->value();
		cv_engines->assign(v * 0.01f);
	}
	
	// set doppler effect
	if (core::Cvar *cv_doppler = core::Cvar::find("snd_doppler"))
	{		
		float v = (_dopplercheckbox->value() ? 1.0f : 0.0f);
		cv_doppler->assign(v * 0.01f);
	}
}

void AudioSettingsMenu::resize()
{
	const float padding = ui::UI::padding;
	const float margin = ui::UI::margin;
		
	// resize title label
	_titlelabel->set_size(width() - padding * 2.0f, _titlelabel->font()->height());	
	_titlelabel->set_location(padding, padding);

	// resize close button
	_closebutton->set_size(_titlelabel->font()->height(), _titlelabel->font()->height());
	_closebutton->set_location(_titlelabel->width() - _closebutton->width(), 0);
	
	// resize content frame
	_frame->set_location(_titlelabel->left(), _titlelabel->bottom() + padding);	
	_frame->set_size(_titlelabel->width(), height() - _titlelabel->bottom() - padding * 2.0f);
	
	//---- inside _frame ----
	
	// resize master volume label
	_mastervolumelabel->set_location(padding, padding);
	_mastervolumelabel->set_size(ui::UI::elementsize.width(), _mastervolumelabel->font()->height() + margin);
	// resize master volume slider
	_mastervolumeslider->set_location(_mastervolumelabel->right() + padding, _mastervolumelabel->top());
	_mastervolumeslider->set_size(_frame->width() - _mastervolumelabel->right() - 2.0f * padding, _mastervolumelabel->height());
	
	// resize master volume label
	_enginevolumelabel->set_location(padding, _mastervolumelabel->bottom() + margin);
	_enginevolumelabel->set_size(ui::UI::elementsize.width(), _enginevolumelabel->font()->height() + margin);
	// resize master volume slider
	_enginevolumeslider->set_location(_enginevolumelabel->right() + padding, _enginevolumelabel->top());
	_enginevolumeslider->set_size(_frame->width() - _enginevolumelabel->right() - 2.0f * padding, _enginevolumelabel->height());
	
	// options label
	_optionslabel->set_location(padding, _enginevolumelabel->bottom() + margin);
	_optionslabel->set_size(ui::UI::elementsize.width(), _optionslabel->font()->height() + margin);
	
	// doppler effect checkbox
	_dopplercheckbox->set_location(_optionslabel->right() + padding, _optionslabel->top());
	_dopplercheckbox->set_size(_enginevolumeslider->width(), _optionslabel->height());
	
	// resize apply button
	_applybutton->set_size(ui::UI::elementsize);
	_applybutton->set_location(
		(_frame->width() - 2.0f *  ui::UI::elementsize.width() - padding) * 0.5f,
		_frame->height() - _applybutton->height() - padding
	);
	
	// resize reset button
	_resetbutton->set_size(ui::UI::elementsize);
	_resetbutton->set_location(_applybutton->right() + padding, _applybutton->top());
	
	
}

bool AudioSettingsMenu::on_emit(ui::Widget *sender, const ui::Widget::Event event, void *data)
{
	if (sender == _closebutton)
	{
		if (event == ui::Widget::EventButtonClicked)
		{
			parent()->hide();
			return true;
		}
	} 
	else if (sender == _applybutton)
	{
		if (event == ui::Widget::EventButtonClicked)
		{
			apply();
			return true;
		}
	}
	else if (sender == _resetbutton)
	{
		if (event == ui::Widget::EventButtonClicked)
		{
			refresh();
			return true;
		}
	}
	
	return Window::on_emit(sender, event, data);
}

} // namespace client