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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
|
/*
client/playersettingsmenu.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/playersettingsmenu.h"
#include "client/closeiconbutton.h"
#include "core/cvar.h"
#include "ui/button.h"
#include "ui/colorpicker.h"
#include "ui/iconbutton.h"
#include "ui/inputbox.h"
#include "ui/label.h"
#include "ui/ui.h"
#include <iostream>
namespace client {
PlayerSettingsMenu::PlayerSettingsMenu(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("PLAYER 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();
// player name
_playernamelabel = new ui::Label(_frame);
_playernamelabel->set_background(false);
_playernamelabel->set_border(false);
_playernamelabel->set_text("Name");
_playernameinput = new ui::InputBox(_frame);
_playernameinput ->set_label("cl_name");
_playernameinput->set_background(false);
_playernameinput->set_border(true);
_playernameinput->set_alignment(ui::AlignLeft | ui::AlignVCenter);
_playernameinput->set_max(64);
// primary color
_primarycolorlabel = new ui::Label(_frame);
_primarycolorlabel ->set_label("cl_color");
_primarycolorlabel->set_background(false);
_primarycolorlabel->set_border(false);
_primarycolorlabel->set_text("Primary color");
_primarycolorpicker = new ui::ColorPicker(_frame);
_primarycolorpicker->set_background(false);
_primarycolorpicker->set_border(true);
// secondary color
_secondarycolorlabel = new ui::Label(_frame);
_secondarycolorlabel ->set_label("cl_colorsecond");
_secondarycolorlabel->set_background(false);
_secondarycolorlabel->set_border(false);
_secondarycolorlabel->set_text("Secondary color");
_secondarycolorpicker = new ui::ColorPicker(_frame);
_secondarycolorpicker->set_background(false);
_secondarycolorpicker->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");
}
PlayerSettingsMenu::~PlayerSettingsMenu()
{
}
void PlayerSettingsMenu::refresh()
{
math::Color color;
// player name
core::Cvar *cv_playername = core::Cvar::find("cl_name");
if (cv_playername)
{
_playernameinput->set_text(cv_playername->str());
}
else
{
_playernameinput->clear();
}
// player primary color
core::Cvar *cv_primarycolor = core::Cvar::find("cl_color");
if (cv_primarycolor)
{
std::stringstream str(cv_primarycolor->str());
if (!(str >> color))
{
color.assign(1.0f);
}
}
else
{
color.assign(1.0f);
}
_primarycolorpicker->set_color(color);
// player secondary color
core::Cvar *cv_secondarycolor = core::Cvar::find("cl_colorsecond");
if (cv_secondarycolor)
{
std::stringstream str(cv_secondarycolor->str());
if (!(str >> color))
{
color.assign(0.5f);
}
}
else
{
color.assign(0.5f);
}
_secondarycolorpicker->set_color(color);
}
void PlayerSettingsMenu::show()
{
refresh();
ui::Window::show();
}
void PlayerSettingsMenu::apply()
{
// verify player name
std::string playername(_playernameinput->text());
aux::strip_quotes(playername);
aux::trim(playername);
if (!playername.size())
{
playername.assign("Player");
}
_playernameinput->set_text(playername);
// apply player name
core::Cvar *cv_playername = core::Cvar::find("cl_name");
if (cv_playername)
{
cv_playername->assign(playername);
}
// apply player primary color
core::Cvar *cv_primarycolor = core::Cvar::find("cl_color");
if (cv_primarycolor)
{
math::Color color(_primarycolorpicker->color());
std::stringstream str;
str << color;
cv_primarycolor->assign(str.str());
}
// apply player secondary color
core::Cvar *cv_secondarycolor = core::Cvar::find("cl_colorsecond");
if (cv_secondarycolor)
{
math::Color color(_secondarycolorpicker->color());
std::stringstream str;
str << color;
cv_secondarycolor->assign(str.str());
}
}
void PlayerSettingsMenu::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 player name label
_playernamelabel->set_location(padding, padding);
_playernamelabel->set_size(ui::UI::elementsize.width(), _playernamelabel->font()->height() + margin);
// resize player name inputbox
_playernameinput->set_location(_playernamelabel->right() + padding, _playernamelabel->top());
_playernameinput->set_size(_frame->width() - _playernamelabel->right() - 2.0f * padding, _playernamelabel->height());
// resize primary color label
_primarycolorlabel->set_size(ui::UI::elementsize.width(), _primarycolorlabel->font()->height() + margin);
_primarycolorlabel->set_location(padding, _playernamelabel->bottom() + margin);
// resize primary color picker
_primarycolorpicker->set_size(_playernameinput->width(), ui::UI::elementsize.height() * 4.0f);
_primarycolorpicker->set_location(_primarycolorlabel->right() + padding, _primarycolorlabel->top());
// resize secondary color label
_secondarycolorlabel->set_size(ui::UI::elementsize.width(), _primarycolorlabel->font()->height() + margin);
_secondarycolorlabel->set_location(padding,_primarycolorpicker->bottom() + margin);
// resize secondary color picker
_secondarycolorpicker->set_size(_playernameinput->width(), ui::UI::elementsize.height() * 4.0f);
_secondarycolorpicker->set_location(_secondarycolorlabel->right() + padding, _secondarycolorlabel->top());
// 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 PlayerSettingsMenu::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
|