forked from danilw/nanogui-GLES-wasm
-
Notifications
You must be signed in to change notification settings - Fork 2
/
widget.cpp
255 lines (222 loc) · 7.21 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
/*
src/widget.cpp -- Base class of all widgets
NanoGUI was developed by Wenzel Jakob <[email protected]>.
The widget drawing code is based on the NanoVG demo application
by Mikko Mononen.
All rights reserved. Use of this source code is governed by a
BSD-style license that can be found in the LICENSE.txt file.
*/
#include <nanogui/widget.h>
#include <nanogui/layout.h>
#include <nanogui/theme.h>
#include <nanogui/window.h>
#include <nanogui/opengl.h>
#include <nanogui/screen.h>
#include <nanogui/serializer/core.h>
NAMESPACE_BEGIN(nanogui)
Widget::Widget(Widget *parent)
: mParent(nullptr), mTheme(nullptr), mLayout(nullptr),
mPos(Vector2i::Zero()), mSize(Vector2i::Zero()),
mFixedSize(Vector2i::Zero()), mVisible(true), mEnabled(true),
mFocused(false), mMouseFocus(false), mTooltip(""), mFontSize(-1.0f),
mIconExtraScale(1.0f), mCursor(Cursor::Arrow) {
if (parent)
parent->addChild(this);
}
Widget::~Widget() {
for (auto child : mChildren) {
if (child)
child->decRef();
}
}
void Widget::setTheme(Theme *theme) {
if (mTheme.get() == theme)
return;
mTheme = theme;
for (auto child : mChildren)
child->setTheme(theme);
}
int Widget::fontSize() const {
return (mFontSize < 0 && mTheme) ? mTheme->mStandardFontSize : mFontSize;
}
Vector2i Widget::preferredSize(NVGcontext *ctx) const {
if (mLayout)
return mLayout->preferredSize(ctx, this);
else
return mSize;
}
void Widget::performLayout(NVGcontext *ctx) {
if (mLayout) {
mLayout->performLayout(ctx, this);
} else {
for (auto c : mChildren) {
Vector2i pref = c->preferredSize(ctx), fix = c->fixedSize();
c->setSize(Vector2i(
fix[0] ? fix[0] : pref[0],
fix[1] ? fix[1] : pref[1]
));
c->performLayout(ctx);
}
}
}
Widget *Widget::findWidget(const Vector2i &p) {
for (auto it = mChildren.rbegin(); it != mChildren.rend(); ++it) {
Widget *child = *it;
if (child->visible() && child->contains(p - mPos))
return child->findWidget(p - mPos);
}
return contains(p) ? this : nullptr;
}
bool Widget::mouseButtonEvent(const Vector2i &p, int button, bool down, int modifiers) {
for (auto it = mChildren.rbegin(); it != mChildren.rend(); ++it) {
Widget *child = *it;
if (child->visible() && child->contains(p - mPos) &&
child->mouseButtonEvent(p - mPos, button, down, modifiers))
return true;
}
if (button == GLFW_MOUSE_BUTTON_1 && down && !mFocused)
requestFocus();
return false;
}
bool Widget::mouseMotionEvent(const Vector2i &p, const Vector2i &rel, int button, int modifiers) {
for (auto it = mChildren.rbegin(); it != mChildren.rend(); ++it) {
Widget *child = *it;
if (!child->visible())
continue;
bool contained = child->contains(p - mPos), prevContained = child->contains(p - mPos - rel);
if (contained != prevContained)
child->mouseEnterEvent(p, contained);
if ((contained || prevContained) &&
child->mouseMotionEvent(p - mPos, rel, button, modifiers))
return true;
}
return false;
}
bool Widget::scrollEvent(const Vector2i &p, const Vector2f &rel) {
for (auto it = mChildren.rbegin(); it != mChildren.rend(); ++it) {
Widget *child = *it;
if (!child->visible())
continue;
if (child->contains(p - mPos) && child->scrollEvent(p - mPos, rel))
return true;
}
return false;
}
bool Widget::mouseDragEvent(const Vector2i &, const Vector2i &, int, int) {
return false;
}
bool Widget::mouseEnterEvent(const Vector2i &, bool enter) {
mMouseFocus = enter;
return false;
}
bool Widget::focusEvent(bool focused) {
mFocused = focused;
return false;
}
bool Widget::keyboardEvent(int, int, int, int) {
return false;
}
bool Widget::keyboardCharacterEvent(unsigned int) {
return false;
}
void Widget::addChild(int index, Widget * widget) {
assert(index <= childCount());
mChildren.insert(mChildren.begin() + index, widget);
widget->incRef();
widget->setParent(this);
widget->setTheme(mTheme);
}
void Widget::addChild(Widget * widget) {
addChild(childCount(), widget);
}
void Widget::removeChild(const Widget *widget) {
mChildren.erase(std::remove(mChildren.begin(), mChildren.end(), widget), mChildren.end());
widget->decRef();
}
void Widget::removeChild(int index) {
Widget *widget = mChildren[index];
mChildren.erase(mChildren.begin() + index);
widget->decRef();
}
int Widget::childIndex(Widget *widget) const {
auto it = std::find(mChildren.begin(), mChildren.end(), widget);
if (it == mChildren.end())
return -1;
return (int) (it - mChildren.begin());
}
Window *Widget::window() {
Widget *widget = this;
while (true) {
if (!widget)
throw std::runtime_error(
"Widget:internal error (could not find parent window)");
Window *window = dynamic_cast<Window *>(widget);
if (window)
return window;
widget = widget->parent();
}
}
Screen *Widget::screen() {
Widget *widget = this;
while (true) {
if (!widget)
throw std::runtime_error(
"Widget:internal error (could not find parent screen)");
Screen *screen = dynamic_cast<Screen *>(widget);
if (screen)
return screen;
widget = widget->parent();
}
}
void Widget::requestFocus() {
Widget *widget = this;
while (widget->parent())
widget = widget->parent();
((Screen *) widget)->updateFocus(this);
}
void Widget::draw(NVGcontext *ctx) {
#if NANOGUI_SHOW_WIDGET_BOUNDS
nvgStrokeWidth(ctx, 1.0f);
nvgBeginPath(ctx);
nvgRect(ctx, mPos.x() - 0.5f, mPos.y() - 0.5f, mSize.x() + 1, mSize.y() + 1);
nvgStrokeColor(ctx, nvgRGBA(255, 0, 0, 255));
nvgStroke(ctx);
#endif
if (mChildren.empty())
return;
nvgSave(ctx);
nvgTranslate(ctx, mPos.x(), mPos.y());
for (auto child : mChildren) {
if (child->visible()) {
nvgSave(ctx);
nvgIntersectScissor(ctx, child->mPos.x(), child->mPos.y(), child->mSize.x(), child->mSize.y());
child->draw(ctx);
nvgRestore(ctx);
}
}
nvgRestore(ctx);
}
void Widget::save(Serializer &s) const {
s.set("position", mPos);
s.set("size", mSize);
s.set("fixedSize", mFixedSize);
s.set("visible", mVisible);
s.set("enabled", mEnabled);
s.set("focused", mFocused);
s.set("tooltip", mTooltip);
s.set("fontSize", mFontSize);
s.set("cursor", (int) mCursor);
}
bool Widget::load(Serializer &s) {
if (!s.get("position", mPos)) return false;
if (!s.get("size", mSize)) return false;
if (!s.get("fixedSize", mFixedSize)) return false;
if (!s.get("visible", mVisible)) return false;
if (!s.get("enabled", mEnabled)) return false;
if (!s.get("focused", mFocused)) return false;
if (!s.get("tooltip", mTooltip)) return false;
if (!s.get("fontSize", mFontSize)) return false;
if (!s.get("cursor", mCursor)) return false;
return true;
}
NAMESPACE_END(nanogui)