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
|
/*
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 "core/cvar.h"
#include "ui/button.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 ui::IconButton(_titlelabel, "bitmaps/icons/window_close");
// 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);
// 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);
}
}
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);
}
}
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());
// 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
|