/
GuiWindowDef.cpp
451 lines (382 loc) · 9.56 KB
/
GuiWindowDef.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
442
443
444
445
446
447
448
449
450
451
#include "GuiWindowDef.h"
#include "parser/DefTokeniser.h"
#include "string/convert.h"
#include "itextstream.h"
#include <limits>
#include <boost/algorithm/string/split.hpp>
#include <boost/algorithm/string/classification.hpp>
#include <boost/algorithm/string/trim.hpp>
#include <boost/algorithm/string/case_conv.hpp>
#include <boost/algorithm/string/replace.hpp>
#include "GuiScript.h"
namespace gui
{
GuiWindowDef::GuiWindowDef(Gui& owner) :
_owner(owner),
_renderableText(*this),
_textChanged(true),
visible(true),
forecolor(1,1,1,1),
hovercolor(1,1,1,1),
backcolor(0,0,0,0),
bordercolor(0,0,0,0),
matcolor(1,1,1,1),
rotate(0),
textscale(1),
textalign(0),
textalignx(0),
textaligny(0),
forceaspectwidth(640),
forceaspectheight(480),
noclip(false),
notime(false),
nocursor(false),
nowrap(false),
time(0)
{}
Gui& GuiWindowDef::getGui() const
{
return _owner;
}
// Returns a GUI expression, which can be a number, a string or a formula ("gui::objVisible" == 1).
std::string GuiWindowDef::getExpression(parser::DefTokeniser& tokeniser)
{
std::string returnValue = tokeniser.nextToken();
if (returnValue == "(")
{
// Assemble token until closing brace found
std::size_t depth = 1;
while (depth > 0 && tokeniser.hasMoreTokens())
{
std::string token = tokeniser.nextToken();
if (token == ")") depth--;
returnValue += token;
}
}
// Strip quotes
boost::algorithm::trim_if(returnValue, boost::algorithm::is_any_of("\""));
return returnValue;
}
Vector4 GuiWindowDef::parseVector4(parser::DefTokeniser& tokeniser)
{
// Collect tokens until all four components are parsed
std::vector<std::string> comp;
while (comp.size() < 4 && tokeniser.hasMoreTokens())
{
std::string token = getExpression(tokeniser);
if (token == ",") continue;
if (token.find(',') != std::string::npos)
{
std::vector<std::string> parts;
boost::algorithm::split(parts, token, boost::algorithm::is_any_of(","));
for (std::size_t i = 0; i < parts.size(); ++i)
{
comp.push_back(boost::algorithm::trim_copy(parts[i]));
}
continue;
}
// TODO: Catch GUI expressions
comp.push_back(token);
}
if (comp.size() != 4)
{
throw parser::ParseException("Couldn't parse Vector4, not enough components found.");
}
return Vector4(string::convert<float>(comp[0]),
string::convert<float>(comp[1]),
string::convert<float>(comp[2]),
string::convert<float>(comp[3]));
}
float GuiWindowDef::parseFloat(parser::DefTokeniser& tokeniser)
{
// TODO: Catch GUI expressions
return string::convert<float>(getExpression(tokeniser));
}
int GuiWindowDef::parseInt(parser::DefTokeniser& tokeniser)
{
// TODO: Catch GUI expressions
return string::convert<int>(getExpression(tokeniser));
}
std::string GuiWindowDef::parseString(parser::DefTokeniser& tokeniser)
{
// TODO: Catch GUI expressions
return getExpression(tokeniser);
}
bool GuiWindowDef::parseBool(parser::DefTokeniser& tokeniser)
{
// TODO: Catch GUI expressions
return string::convert<int>(getExpression(tokeniser)) != 0;
}
void GuiWindowDef::addWindow(const GuiWindowDefPtr& window)
{
children.push_back(window);
}
void GuiWindowDef::constructFromTokens(parser::DefTokeniser& tokeniser)
{
// The windowDef keyword has already been parsed, so expect a name plus an opening brace here
name = tokeniser.nextToken();
tokeniser.assertNextToken("{");
while (tokeniser.hasMoreTokens())
{
std::string token = tokeniser.nextToken();
boost::algorithm::to_lower(token);
if (token == "rect")
{
rect = parseVector4(tokeniser);
}
else if (token == "visible")
{
visible = parseBool(tokeniser);
}
else if (token == "notime")
{
notime = parseBool(tokeniser);
}
else if (token == "forecolor")
{
forecolor = parseVector4(tokeniser);
}
else if (token == "backcolor")
{
backcolor = parseVector4(tokeniser);
}
else if (token == "bordercolor")
{
bordercolor = parseVector4(tokeniser);
}
else if (token == "matcolor")
{
matcolor = parseVector4(tokeniser);
}
else if (token == "rotate")
{
rotate = parseFloat(tokeniser);
}
else if (token == "text")
{
setText(parseString(tokeniser));
}
else if (token == "font")
{
font = parseString(tokeniser);
// Cut off the "fonts/" part
boost::algorithm::replace_first(font, "fonts/", "");
}
else if (token == "textscale")
{
textscale = parseFloat(tokeniser);
}
else if (token == "textalign")
{
textalign = parseInt(tokeniser);
}
else if (token == "textalignx")
{
textalignx = parseFloat(tokeniser);
}
else if (token == "textaligny")
{
textaligny = parseFloat(tokeniser);
}
else if (token == "forceaspectwidth")
{
forceaspectwidth = parseFloat(tokeniser);
}
else if (token == "forceaspectheight")
{
forceaspectheight = parseFloat(tokeniser);
}
else if (token == "background")
{
background = parseString(tokeniser);
}
else if (token == "noevents")
{
noevents = parseBool(tokeniser);
}
else if (token == "nocursor")
{
nocursor = parseBool(tokeniser);
}
else if (token == "noclip")
{
noclip = parseBool(tokeniser);
}
else if (token == "nowrap")
{
nowrap = parseBool(tokeniser);
}
else if (token == "modal")
{
noevents = parseBool(tokeniser);
}
else if (token == "menugui")
{
menugui = parseBool(tokeniser);
}
else if (token == "windowdef")
{
// Child windowdef
GuiWindowDefPtr window(new GuiWindowDef(_owner));
window->constructFromTokens(tokeniser);
addWindow(window);
}
else if (token == "ontime")
{
std::string timeStr = tokeniser.nextToken();
// Check the time for validity
std::size_t onTime = string::convert<std::size_t>(
timeStr, std::numeric_limits<std::size_t>::max()
);
if (onTime == std::numeric_limits<std::size_t>::max())
{
rWarning() << "Invalid time encountered in onTime event in "
<< name << ": " << timeStr << std::endl;
}
// Allocate a new GuiScript
GuiScriptPtr script(new GuiScript(*this));
script->constructFromTokens(tokeniser);
_timedEvents.insert(TimedEventMap::value_type(onTime, script));
}
else if (token == "onnamedevent")
{
std::string eventName = tokeniser.nextToken();
// Parse the script
GuiScriptPtr script(new GuiScript(*this));
script->constructFromTokens(tokeniser);
// TODO: Save event
}
else if (token == "onevent")
{
GuiScriptPtr script(new GuiScript(*this));
script->constructFromTokens(tokeniser);
// TODO
}
else if (token == "onesc")
{
GuiScriptPtr script(new GuiScript(*this));
script->constructFromTokens(tokeniser);
// TODO
}
else if (token == "onmouseenter" || token == "onmouseexit")
{
GuiScriptPtr script(new GuiScript(*this));
script->constructFromTokens(tokeniser);
// TODO
}
else if (token == "onaction")
{
GuiScriptPtr script(new GuiScript(*this));
script->constructFromTokens(tokeniser);
// TODO
}
else if (token == "float" || token == "definefloat")
{
// TODO: Add variable
std::string variableName = tokeniser.nextToken();
}
else if (token == "definevec4")
{
// TODO: Add variable
std::string variableName = tokeniser.nextToken();
parseVector4(tokeniser);
}
else if (token == "}")
{
break;
}
else
{
rWarning() << "Unknown token encountered in GUI: " << token << std::endl;
}
}
}
const std::string& GuiWindowDef::getText() const
{
return _text;
}
void GuiWindowDef::setText(const std::string& newText)
{
_text = newText;
_textChanged = true;
}
RenderableText& GuiWindowDef::getRenderableText()
{
if (_textChanged)
{
// Text has changed, refresh the renderable
_textChanged = false;
// (Re-)compile the renderable
_renderableText.recompile();
}
return _renderableText;
}
void GuiWindowDef::update(const std::size_t timeStep, bool updateChildren)
{
if (!notime)
{
std::size_t oldTime = time;
// Update this windowDef's time
time += timeStep;
// Be sure to include the ontime 0 event the first time
if (oldTime > 0)
{
oldTime++;
}
// Check events whose time is within [oldTime..time]
for (TimedEventMap::const_iterator i = _timedEvents.lower_bound(oldTime);
i != _timedEvents.end() && i != _timedEvents.upper_bound(time); ++i)
{
i->second->execute();
}
}
// Update children regardless of this windowDef's notime setting
if (updateChildren)
{
for (ChildWindows::const_iterator i = children.begin(); i != children.end(); ++i)
{
(*i)->update(timeStep, updateChildren);
}
}
}
void GuiWindowDef::initTime(const std::size_t toTime, bool updateChildren)
{
this->time = toTime;
if (updateChildren)
{
for (ChildWindows::const_iterator i = children.begin(); i != children.end(); ++i)
{
(*i)->initTime(toTime, updateChildren);
}
}
}
GuiWindowDefPtr GuiWindowDef::findWindowDef(const std::string& windowName)
{
// First look at all direct children
for (ChildWindows::const_iterator i = children.begin(); i != children.end(); ++i)
{
if ((*i)->name == windowName)
{
return (*i);
}
}
// Not found, ask each child to search for the windowDef
for (ChildWindows::const_iterator i = children.begin(); i != children.end(); ++i)
{
GuiWindowDefPtr window = (*i)->findWindowDef(windowName);
if (window != NULL) return window;
}
// Not found
return GuiWindowDefPtr();
}
void GuiWindowDef::pepareRendering(bool prepareChildren)
{
// Triggers a re-compilation of the text VBOs, if necessary
getRenderableText();
if (!prepareChildren) return;
for (ChildWindows::const_iterator i = children.begin(); i != children.end(); ++i)
{
(*i)->pepareRendering(prepareChildren);
}
}
} // namespace