-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGUI.cpp
442 lines (387 loc) · 11.9 KB
/
GUI.cpp
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
#include "GUI.h"
#include "Graphics.h"
#include "Renderer.h"
#include "Log.h"
void GUI :: DefaultStyle :: drawLine(const glm::vec2& a, const glm::vec2& b, eEffectType fx) const
{
//glBegin(GL_LINES);
// glVertex2fv(a.array());
// glVertex2fv(b.array());
//glEnd();
Renderer::get().drawLine(glm::vec3(a, 0.0f),glm::vec3(b, 0.0f));
}
void GUI :: DefaultStyle :: drawBox(const glm::vec2& pos, const glm::vec2& size, eEffectType fx) const
{
glm::vec2 v[4] = {
glm::vec2(pos),
glm::vec2(pos) + glm::vec2(size.x, 0.0f),
glm::vec2(pos) + glm::vec2(size),
glm::vec2(pos) + glm::vec2(0.0f, size.y)
};
glBegin(GL_QUADS);
for(int i=0; i<4; i++) {
Color c;
Color c2;
switch(fx)
{
case FX_NONE:
c = Color(0.3f, 0.3f, 0.3f, 1.0f);
c2 = Color(0.7f, 0.7f, 0.7f, 1.0f);
break;
case FX_HIGHLIGHT:
c = Color(0.0f, 0.0f, 0.3f, 1.0f);
c2 = Color(0.0f, 0.0f, 0.9f, 1.0f);
break;
case FX_ACTIVE:
break;
};
//Color c(fx==FX_HIGHLIGHT?0.0f:0.5f, fx==FX_HIGHLIGHT?0.0f:0.5f, 0.5f, (i==0||i==1)?0.2f:0.9f);
//glColor4fv((i==0||i==1)?c.array():c2.array());
((i==0||i==1)?c:c2).apply();
glVertex2fv(glm::value_ptr(v[i]));
}
glEnd();
}
void GUI :: DefaultStyle :: drawText(const std::string& text, const glm::vec2& pos, glm::vec2 size, eFontType ftype, eFontAlign falign) const
{
if(size == glm::vec2(0.0f))
size = glm::vec2(Renderer::get().width()*1.0f, fontSize()*1.0f);
FTSimpleLayout layout;
layout.SetFont(font(ftype));
layout.SetLineLength(size.x);
switch(falign)
{
case ALIGN_LEFT:
layout.SetAlignment(FTGL::ALIGN_LEFT);
break;
case ALIGN_CENTER:
layout.SetAlignment(FTGL::ALIGN_CENTER);
break;
case ALIGN_RIGHT:
layout.SetAlignment(FTGL::ALIGN_RIGHT);
break;
default:
return;
};
Color::apply(Color()); // apply white color, no trans
// FTGL overrides layout positioning (bug), so use glRasterPos
glRasterPos2f(pos.x, pos.y + (size.y - fontSize()) / 2.0f);
layout.Render(text.c_str());
glRasterPos2f(0.0f, 0.0f); // reset
}
GUI :: Button :: Button(const std::string& caption, const Freq::Timed<glm::vec2>& pos, const glm::vec2& size, std::function<void(Button*)>& callback, unsigned int flags)
{
m_flags = flags;
m_sCaption = caption;
m_bHighlighted = false;
m_xCallback = callback;
position(pos);
Object::size(size);
}
int GUI::Button :: logic(GUI* gui, unsigned int advance, Style* style, Input* input)
{
m_bHighlighted = collision(
glm::vec2(
input->getMouseX(),
Renderer::get().height() - input->getMouseY()
),
glm::vec2(1.0f)
);
if(m_bHighlighted && input->mouseLeftClickf()) {
gui->focus(this);
m_xCallback(this);
}
return m_bHighlighted?1:0;
}
void GUI::Button :: render(const Style* style) const
{
glm::vec2 pos = m_pos.get();
//bool hl = collision(
// glm::vec2(
// input->getMouseX(),
// Renderer::get().height() - input->getMouseY()
// ),
// glm::vec2(1.0f)
//);
//style->drawBox(pos, m_size,
// hl? Style::FX_HIGHLIGHT: Style::FX_NONE);
//style->drawText(m_sCaption, pos, m_size,
// hl ? Style::FONT_BOLD : Style::FONT_REGULAR,
// Style::ALIGN_CENTER);
style->drawBox(pos, m_size,
m_bHighlighted? Style::FX_HIGHLIGHT: Style::FX_NONE);
style->drawText(m_sCaption, pos, m_size,
m_bHighlighted ? Style::FONT_BOLD : Style::FONT_REGULAR,
Style::ALIGN_CENTER);
}
bool GUI::Button :: collision(const glm::vec2& obj, const glm::vec2& size) const
{
glm::vec2 my_pos = m_pos.get();
return GUI::collision(my_pos, m_size, obj, size);
}
GUI :: ToggleButton :: ToggleButton(const std::string& caption, const Freq::Timed<glm::vec2>& pos, const glm::vec2& size, std::function<void(ToggleButton*)>& callback, unsigned int flags)
{
m_flags = flags;
m_sCaption = caption;
m_bHighlighted = false;
m_bActivated = false;
m_xCallback = callback;
position(pos);
Object::size(size);
}
int GUI::ToggleButton :: logic(GUI* gui, unsigned int advance, Style* style, Input* input)
{
m_bHighlighted = collision(
glm::vec2(
input->getMouseX(),
Renderer::get().height() - input->getMouseY()
),
glm::vec2(1.0f)
);
if(m_bHighlighted && input->mouseLeftClickf()) {
//m_bActivated = !m_bActivated;
//m_xCallback(this);
gui->focus(this);
activate(!activated());
}
return m_bHighlighted?1:0;
}
void GUI::ToggleButton :: activate(bool b)
{
if(b == m_bActivated) // don't retrigger
return;
m_bActivated = b;
m_xCallback(this);
}
void GUI::ToggleButton :: render(const Style* style) const
{
glm::vec2 pos = m_pos.get();
style->drawBox(pos, m_size,
( m_bHighlighted || m_bActivated ) ? Style::FX_HIGHLIGHT: Style::FX_NONE);
style->drawText(m_sCaption, pos, m_size,
( m_bHighlighted || m_bActivated ) ? Style::FONT_BOLD : Style::FONT_REGULAR,
Style::ALIGN_CENTER);
}
bool GUI::ToggleButton :: collision(const glm::vec2& obj, const glm::vec2& size) const
{
glm::vec2 my_pos = m_pos.get();
return GUI::collision(my_pos, m_size, obj, size);
}
GUI::Menu :: Menu(std::list<Option> options, const Freq::Timed<glm::vec2>& pos, const glm::vec2& size, unsigned int flags)
{
m_flags = flags;
//for(auto itr = options.begin();
// itr != options.end();
// ++itr)
//{
// m_Options.push_back(*itr);
//}
m_Options = move(options);
//std::copy(options.begin(), options.end(), m_Options.begin());
position(pos);
Object::size(size);
m_SelectedOption = m_Options.begin();
m_vDirection = glm::vec2(0.0, -1.0);
}
int GUI::Menu:: logic(GUI* gui, unsigned int advance, Style* style, Input* input)
{
int affected = 0;
glm::vec2 my_pos = m_pos.get();
glm::vec2 pos = my_pos;
glm::vec2 mouse_pos(
input->getMouseX(),
Renderer::get().height() - input->getMouseY()
);
glm::vec2 mouse_size(1.0f);
glm::vec2 list_size(
m_size.x,
m_size.y * (float)m_Options.size()
);
if(GUI::collision(
pos + (m_vDirection * m_size) * (float)(m_Options.size()-1),
list_size,
mouse_pos,
mouse_size))
{
if(input->mouseLeftClickf())
{
gui->focus(this);
for(list<Option>::iterator itr = m_Options.begin();
itr != m_Options.end();
++itr)
{
if(GUI::collision(pos,
m_size,
mouse_pos,
mouse_size))
{
itr->call(this);
affected++;
break;
}
pos += m_vDirection * m_size;
}
}
}
pos = my_pos;
for(list<Option>::iterator itr = m_Options.begin();
itr != m_Options.end();
++itr)
{
if(GUI::collision(pos,
m_size,
mouse_pos,
mouse_size))
{
m_SelectedOption = itr;
affected++;
break;
}
pos += m_vDirection * m_size;
}
return affected;
}
void GUI::Menu:: render(const Style* style) const
{
glm::vec2 pos = m_pos.get();
//style->drawBox(pos, glm::vec2(m_vDirection * (m_size * (float)m_Options.size()) ), Style::FX_HIGHLIGHT);
for(list<Option>::const_iterator itr = m_Options.begin();
itr != m_Options.end();
++itr)
{
style->drawBox(pos, m_size, itr == m_SelectedOption ? Style::FX_HIGHLIGHT: Style::FX_NONE);
style->drawText(itr->caption(), pos, m_size,
itr == m_SelectedOption ? Style::FONT_BOLD : Style::FONT_REGULAR,
Style::ALIGN_CENTER);
pos += m_vDirection * m_size;
}
}
bool GUI::Menu :: collision(const glm::vec2& obj, const glm::vec2& size) const
{
return GUI::collision(m_pos.get(), glm::vec2(m_size * (float)m_Options.size()), obj, size);
}
int GUI::Dialog::logic(GUI* gui, unsigned int advance, Style* style, Input* input)
{
return 0;
}
void GUI::Dialog::render(const Style* style) const
{
glm::vec2 size = Renderer::get().size();
glm::vec2 v[4] = {
glm::vec2(0.0f),
glm::vec2(size.x, 0.0f),
glm::vec2(size),
glm::vec2(0.0f, size.y)
};
glBegin(GL_QUADS);
for(int i=0; i<4; i++) {
Color c(0.0f, 0.0f, 0.0f, 0.5f);
c.apply();
glVertex2fv(glm::value_ptr(v[i]));
}
glEnd();
//style->drawText();
}
int GUI :: logic(unsigned int advance, Input* input)
{
if(m_spDialog)
return m_spDialog->logic(this, advance, m_spStyle.get(), input);
int affected = 0;
for(vector<shared_ptr<Object>>::iterator itr = m_Objects.begin();
itr != m_Objects.end();
)
{
if((*itr)->flags() & GUI::F_REMOVE)
{
itr = m_Objects.erase(itr);
continue;
}
if(!(*itr)->hidden())
affected += (*itr)->logic(this, advance, m_spStyle.get(), input);
++itr;
}
if(!affected && input->mouseAnyf())
{
// clear temp items
for(vector<shared_ptr<Object>>::iterator itr = m_Objects.begin();
itr != m_Objects.end();
)
{
if((*itr)->flags() & GUI::F_AUTOHIDE)
(*itr)->hide(true);
else if((*itr)->flags() & GUI::F_AUTODEACTIVATE)
(*itr)->activate(false);
else if((*itr)->flags() & GUI::F_AUTOREMOVE || (*itr)->flags() & GUI::F_REMOVE)
{
itr = m_Objects.erase(itr);
continue;
}
++itr;
}
}
return affected;
}
void GUI :: render() const
{
// push wireframe state
bool w;
if(w = Renderer::get().wireframe())
Renderer::get().wireframe(false);
bool shaders_paused = Renderer::get().pauseShaders();
bool lighting_paused = Renderer::get().pauseLighting();
Renderer::get().shaders(Renderer::UNBIND_SHADERS);
for(vector<shared_ptr<Object>>::const_iterator itr = m_Objects.cbegin();
itr != m_Objects.cend();
++itr)
{
if(!((*itr)->hidden() && (*itr)->position().hasElapsed()))
(*itr)->render(m_spStyle.get());
}
if(m_spDialog)
{
m_spDialog->render(m_spStyle.get());
style()->drawText(string("This is a status message."), glm::vec2(0.0f), glm::vec2(0.0f),
Style::FONT_REGULAR, Style::ALIGN_CENTER);
}
//restore wireframe state
if(w)
Renderer::get().wireframe(true);
Renderer::get().resumeLighting(lighting_paused);
Renderer::get().resumeShaders(shaders_paused);
}
bool GUI :: collision(
const glm::vec2& a_pos,
const glm::vec2& a_size,
const glm::vec2& b_pos,
const glm::vec2& b_size)
{
if ((a_pos.x > b_pos.x + b_size.x - 1.0f)||
(a_pos.y > b_pos.y + b_size.y - 1.0f)||
(b_pos.x > a_pos.x + a_size.x - 1.0f)||
(b_pos.y > a_pos.y + a_size.y - 1.0f))
{
return false;
}
return true;
}
void GUI :: focus(Object* obj)
{
for(vector<shared_ptr<Object>>::iterator itr = m_Objects.begin();
itr != m_Objects.end();
)
{
if(itr->get() != obj)
{
if((*itr)->flags() & GUI::F_AUTOHIDE)
(*itr)->hide(true);
else if((*itr)->flags() & GUI::F_AUTODEACTIVATE)
(*itr)->activate(false);
else if((*itr)->flags() & GUI::F_AUTOREMOVE || (*itr)->flags() & GUI::F_AUTOREMOVE)
{
itr = m_Objects.erase(itr);
continue;
}
}
++itr;
}
}