forked from danilw/nanogui-GLES-wasm
-
Notifications
You must be signed in to change notification settings - Fork 2
/
button.cpp
244 lines (213 loc) · 8.52 KB
/
button.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
/*
src/button.cpp -- [Normal/Toggle/Radio/Popup] Button widget
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/button.h>
#include <nanogui/theme.h>
#include <nanogui/opengl.h>
#include <nanogui/serializer/core.h>
NAMESPACE_BEGIN(nanogui)
Button::Button(Widget *parent, const std::string &caption, int icon)
: Widget(parent), mCaption(caption), mIcon(icon),
mIconPosition(IconPosition::LeftCentered), mPushed(false),
mFlags(NormalButton), mBackgroundColor(Color(0, 0)),
mTextColor(Color(0, 0)) { }
Vector2i Button::preferredSize(NVGcontext *ctx) const {
int fontSize = mFontSize == -1 ? mTheme->mButtonFontSize : mFontSize;
nvgFontSize(ctx, fontSize);
nvgFontFace(ctx, "sans-bold");
float tw = nvgTextBounds(ctx, 0,0, mCaption.c_str(), nullptr, nullptr);
float iw = 0.0f, ih = fontSize;
if (mIcon) {
if (nvgIsFontIcon(mIcon)) {
ih *= icon_scale();
nvgFontFace(ctx, "icons");
nvgFontSize(ctx, ih);
iw = nvgTextBounds(ctx, 0, 0, utf8(mIcon).data(), nullptr, nullptr)
+ mSize.y() * 0.15f;
} else {
int w, h;
ih *= 0.9f;
nvgImageSize(ctx, mIcon, &w, &h);
iw = w * ih / h;
}
}
return Vector2i((int)(tw + iw) + 20, fontSize + 10);
}
bool Button::mouseButtonEvent(const Vector2i &p, int button, bool down, int modifiers) {
Widget::mouseButtonEvent(p, button, down, modifiers);
/* Temporarily increase the reference count of the button in case the
button causes the parent window to be destructed */
ref<Button> self = this;
if (button == GLFW_MOUSE_BUTTON_1 && mEnabled) {
bool pushedBackup = mPushed;
if (down) {
if (mFlags & RadioButton) {
if (mButtonGroup.empty()) {
for (auto widget : parent()->children()) {
Button *b = dynamic_cast<Button *>(widget);
if (b != this && b && (b->flags() & RadioButton) && b->mPushed) {
b->mPushed = false;
if (b->mChangeCallback)
b->mChangeCallback(false);
}
}
} else {
for (auto b : mButtonGroup) {
if (b != this && (b->flags() & RadioButton) && b->mPushed) {
b->mPushed = false;
if (b->mChangeCallback)
b->mChangeCallback(false);
}
}
}
}
if (mFlags & PopupButton) {
for (auto widget : parent()->children()) {
Button *b = dynamic_cast<Button *>(widget);
if (b != this && b && (b->flags() & PopupButton) && b->mPushed) {
b->mPushed = false;
if (b->mChangeCallback)
b->mChangeCallback(false);
}
}
}
if (mFlags & ToggleButton)
mPushed = !mPushed;
else
mPushed = true;
} else if (mPushed) {
if (contains(p) && mCallback)
mCallback();
if (mFlags & NormalButton)
mPushed = false;
}
if (pushedBackup != mPushed && mChangeCallback)
mChangeCallback(mPushed);
return true;
}
return false;
}
void Button::draw(NVGcontext *ctx) {
Widget::draw(ctx);
NVGcolor gradTop = mTheme->mButtonGradientTopUnfocused;
NVGcolor gradBot = mTheme->mButtonGradientBotUnfocused;
if (mPushed) {
gradTop = mTheme->mButtonGradientTopPushed;
gradBot = mTheme->mButtonGradientBotPushed;
} else if (mMouseFocus && mEnabled) {
gradTop = mTheme->mButtonGradientTopFocused;
gradBot = mTheme->mButtonGradientBotFocused;
}
nvgBeginPath(ctx);
nvgRoundedRect(ctx, mPos.x() + 1, mPos.y() + 1.0f, mSize.x() - 2,
mSize.y() - 2, mTheme->mButtonCornerRadius - 1);
if (mBackgroundColor.w() != 0) {
nvgFillColor(ctx, Color(mBackgroundColor.head<3>(), 1.f));
nvgFill(ctx);
if (mPushed) {
gradTop.a = gradBot.a = 0.8f;
} else {
double v = 1 - mBackgroundColor.w();
gradTop.a = gradBot.a = mEnabled ? v : v * .5f + .5f;
}
}
NVGpaint bg = nvgLinearGradient(ctx, mPos.x(), mPos.y(), mPos.x(),
mPos.y() + mSize.y(), gradTop, gradBot);
nvgFillPaint(ctx, bg);
nvgFill(ctx);
nvgBeginPath(ctx);
nvgStrokeWidth(ctx, 1.0f);
nvgRoundedRect(ctx, mPos.x() + 0.5f, mPos.y() + (mPushed ? 0.5f : 1.5f), mSize.x() - 1,
mSize.y() - 1 - (mPushed ? 0.0f : 1.0f), mTheme->mButtonCornerRadius);
nvgStrokeColor(ctx, mTheme->mBorderLight);
nvgStroke(ctx);
nvgBeginPath(ctx);
nvgRoundedRect(ctx, mPos.x() + 0.5f, mPos.y() + 0.5f, mSize.x() - 1,
mSize.y() - 2, mTheme->mButtonCornerRadius);
nvgStrokeColor(ctx, mTheme->mBorderDark);
nvgStroke(ctx);
int fontSize = mFontSize == -1 ? mTheme->mButtonFontSize : mFontSize;
nvgFontSize(ctx, fontSize);
nvgFontFace(ctx, "sans-bold");
float tw = nvgTextBounds(ctx, 0,0, mCaption.c_str(), nullptr, nullptr);
Vector2f center = mPos.cast<float>() + mSize.cast<float>() * 0.5f;
Vector2f textPos(center.x() - tw * 0.5f, center.y() - 1);
NVGcolor textColor =
mTextColor.w() == 0 ? mTheme->mTextColor : mTextColor;
if (!mEnabled)
textColor = mTheme->mDisabledTextColor;
if (mIcon) {
auto icon = utf8(mIcon);
float iw, ih = fontSize;
if (nvgIsFontIcon(mIcon)) {
ih *= icon_scale();
nvgFontSize(ctx, ih);
nvgFontFace(ctx, "icons");
iw = nvgTextBounds(ctx, 0, 0, icon.data(), nullptr, nullptr);
} else {
int w, h;
ih *= 0.9f;
nvgImageSize(ctx, mIcon, &w, &h);
iw = w * ih / h;
}
if (mCaption != "")
iw += mSize.y() * 0.15f;
nvgFillColor(ctx, textColor);
nvgTextAlign(ctx, NVG_ALIGN_LEFT | NVG_ALIGN_MIDDLE);
Vector2f iconPos = center;
iconPos.y() -= 1;
if (mIconPosition == IconPosition::LeftCentered) {
iconPos.x() -= (tw + iw) * 0.5f;
textPos.x() += iw * 0.5f;
} else if (mIconPosition == IconPosition::RightCentered) {
textPos.x() -= iw * 0.5f;
iconPos.x() += tw * 0.5f;
} else if (mIconPosition == IconPosition::Left) {
iconPos.x() = mPos.x() + 8;
} else if (mIconPosition == IconPosition::Right) {
iconPos.x() = mPos.x() + mSize.x() - iw - 8;
}
if (nvgIsFontIcon(mIcon)) {
nvgText(ctx, iconPos.x(), iconPos.y()+1, icon.data(), nullptr);
} else {
NVGpaint imgPaint = nvgImagePattern(ctx,
iconPos.x(), iconPos.y() - ih/2, iw, ih, 0, mIcon, mEnabled ? 0.5f : 0.25f);
nvgFillPaint(ctx, imgPaint);
nvgFill(ctx);
}
}
nvgFontSize(ctx, fontSize);
nvgFontFace(ctx, "sans-bold");
nvgTextAlign(ctx, NVG_ALIGN_LEFT | NVG_ALIGN_MIDDLE);
nvgFillColor(ctx, mTheme->mTextColorShadow);
nvgText(ctx, textPos.x(), textPos.y(), mCaption.c_str(), nullptr);
nvgFillColor(ctx, textColor);
nvgText(ctx, textPos.x(), textPos.y() + 1, mCaption.c_str(), nullptr);
}
void Button::save(Serializer &s) const {
Widget::save(s);
s.set("caption", mCaption);
s.set("icon", mIcon);
s.set("iconPosition", (int) mIconPosition);
s.set("pushed", mPushed);
s.set("flags", mFlags);
s.set("backgroundColor", mBackgroundColor);
s.set("textColor", mTextColor);
}
bool Button::load(Serializer &s) {
if (!Widget::load(s)) return false;
if (!s.get("caption", mCaption)) return false;
if (!s.get("icon", mIcon)) return false;
if (!s.get("iconPosition", mIconPosition)) return false;
if (!s.get("pushed", mPushed)) return false;
if (!s.get("flags", mFlags)) return false;
if (!s.get("backgroundColor", mBackgroundColor)) return false;
if (!s.get("textColor", mTextColor)) return false;
return true;
}
NAMESPACE_END(nanogui)