-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
widget.cpp
358 lines (300 loc) · 7.38 KB
/
widget.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
/** @file widget.cpp Base class for widgets.
*
* @authors Copyright © 2005-2017 Jaakko Keränen <jaakko.keranen@iki.fi>
* @authors Copyright © 2005-2014 Daniel Swanson <danij@dengine.net>
*
* @par License
* GPL: http://www.gnu.org/licenses/gpl.html
*
* <small>This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. This program is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details. You should have received a copy of the GNU
* General Public License along with this program; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA</small>
*/
#include <QMap>
#include "common.h"
#include "menu/widgets/widget.h"
#include "menu/page.h"
#include "g_defs.h"
#include "hu_menu.h" // menu sounds
using namespace de;
namespace common {
namespace menu {
DENG2_PIMPL_NOREF(Widget)
{
Flags flags;
int group = 0; ///< Object group identifier.
Vector2i origin; ///< Used with the fixed layout (in the page coordinate space).
Rectanglei geometry; ///< "Physical" geometry.
Page *page = nullptr; ///< Page which owns this object (if any).
String helpInfo; ///< Additional help information displayed when the widget has focus.
int shortcut = 0; ///< DDKEY used to switch focus (@c 0= none).
int pageFontIdx = 0; ///< Index of the predefined page font to use when drawing this.
int pageColorIdx = 0; ///< Index of the predefined page color to use when drawing this.
typedef QMap<Action, ActionCallback> Actions;
Actions actions;
OnTickCallback onTickCallback = nullptr;
CommandResponder cmdResponder = nullptr;
// User data values.
QVariant userValue;
QVariant userValue2;
};
Widget::Widget() : d(new Impl)
{}
Widget::~Widget()
{}
int Widget::handleEvent(event_t const &)
{
return 0; // Not handled.
}
int Widget::handleEvent_Privileged(event_t const &)
{
return 0; // Not handled.
}
Widget &Widget::setCommandResponder(CommandResponder newResponder)
{
d->cmdResponder = newResponder;
return *this;
}
int Widget::cmdResponder(menucommand_e command)
{
if(d->cmdResponder)
{
if(int result = d->cmdResponder(*this, command))
return result;
}
else
{
if(int result = handleCommand(command))
return result;
}
return false; // Not handled.
}
void Widget::tick()
{
// Hidden widgets do not tick.
if(isHidden()) return;
// Paused widgets do not tick.
if(isPaused()) return;
// Process the tick.
if(d->onTickCallback)
{
d->onTickCallback(*this);
}
}
Widget &Widget::setOnTickCallback(OnTickCallback newCallback)
{
d->onTickCallback = newCallback;
return *this;
}
bool Widget::hasPage() const
{
return d->page != 0;
}
Widget &Widget::setPage(Page *newPage)
{
d->page = newPage;
return *this;
}
Page &Widget::page() const
{
if(d->page)
{
return *d->page;
}
/// @throw MissingPageError No Page is presently attributed.
throw MissingPageError("Widget::page", "No page is attributed");
}
Rectanglei &Widget::geometry()
{
return d->geometry;
}
Rectanglei const &Widget::geometry() const
{
return d->geometry;
}
Widget &Widget::setFixedOrigin(Vector2i const &newOrigin)
{
d->origin = newOrigin;
return *this;
}
Vector2i Widget::fixedOrigin() const
{
return d->origin;
}
Widget &Widget::setFixedX(int newX)
{
d->origin.x = newX;
return *this;
}
Widget &Widget::setFixedY(int newY)
{
d->origin.y = newY;
return *this;
}
Widget &Widget::setFlags(Flags flagsToChange, FlagOp operation)
{
applyFlagOperation(d->flags, flagsToChange, operation);
return *this;
}
Widget::Flags Widget::flags() const
{
return d->flags;
}
Widget &Widget::setGroup(int newGroup)
{
d->group = newGroup;
return *this;
}
int Widget::group() const
{
return d->group;
}
Widget &Widget::setShortcut(int ddkey)
{
if(isalnum(ddkey))
{
d->shortcut = tolower(ddkey);
}
return *this;
}
int Widget::shortcut()
{
return d->shortcut;
}
Widget &Widget::setFont(int newPageFont)
{
d->pageFontIdx = newPageFont;
return *this;
}
int Widget::font() const
{
return d->pageFontIdx;
}
Widget &Widget::setColor(int newPageColor)
{
d->pageColorIdx = newPageColor;
return *this;
}
int Widget::color() const
{
return d->pageColorIdx;
}
Widget &Widget::setHelpInfo(String newHelpInfo)
{
d->helpInfo = newHelpInfo;
return *this;
}
String const &Widget::helpInfo() const
{
return d->helpInfo;
}
int Widget::handleCommand(menucommand_e cmd)
{
if(MCMD_SELECT == cmd && isFocused() && !isDisabled())
{
S_LocalSound(SFX_MENU_ACCEPT, NULL);
if(!isActive())
{
setFlags(Active);
execAction(Activated);
}
setFlags(Active, UnsetFlags);
execAction(Deactivated);
return true;
}
return false; // Not eaten.
}
bool Widget::hasAction(Action id) const
{
return d->actions.contains(id);
}
Widget &Widget::setAction(Action id, ActionCallback callback)
{
if(callback)
{
d->actions.insert(id, callback);
}
else
{
d->actions.remove(id);
}
return *this;
}
void Widget::execAction(Action id)
{
if(hasAction(id))
{
d->actions[id](*this, id);
}
}
Widget &Widget::setUserValue(QVariant const &newValue)
{
d->userValue = newValue;
return *this;
}
QVariant const &Widget::userValue() const
{
return d->userValue;
}
Widget &Widget::setUserValue2(QVariant const &newValue)
{
d->userValue2 = newValue;
return *this;
}
QVariant const &Widget::userValue2() const
{
return d->userValue2;
}
float Widget::scrollingFadeout() const
{
const auto geom = geometry();
return scrollingFadeout(geom.topLeft.y, geom.bottomRight.y);
}
float Widget::scrollingFadeout(int yTop, int yBottom) const
{
if (page().flags() & Page::NoScroll)
{
return 1.f;
}
const float FADEOUT_LEN = 20;
const auto viewRegion = page().viewRegion();
if (yBottom < viewRegion.topLeft.y)
{
return de::max(0.f, 1.f - (viewRegion.topLeft.y - yBottom) / FADEOUT_LEN);
}
if (yTop > viewRegion.bottomRight.y)
{
return de::max(0.f, 1.f - (yTop - viewRegion.bottomRight.y) / FADEOUT_LEN);
}
return 1.f;
}
Vector4f Widget::selectionFlashColor(const Vector4f &noFlashColor) const
{
if (isFocused() && cfg.common.menuTextFlashSpeed > 0)
{
const float speed = cfg.common.menuTextFlashSpeed / 2.f;
const float t = (1 + sin(page().timer() / (float)TICSPERSEC * speed * DD_PI)) / 2;
return lerp(noFlashColor, Vector4f(Vector3f(cfg.common.menuTextFlashColor), 1.f), t);
}
return noFlashColor;
}
void Widget::pageActivated()
{}
String Widget::labelText(const String &text) // static
{
// Widget text may be replaced with Values.
if (const auto *repl = Defs().getValueById("Menu Label|" + text))
{
return repl->text;
}
return text;
}
} // namespace menu
} // namespace common