forked from danilw/nanogui-GLES-wasm
-
Notifications
You must be signed in to change notification settings - Fork 2
/
popupbutton.cpp
102 lines (78 loc) · 3.07 KB
/
popupbutton.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
/*
src/popupbutton.cpp -- Button which launches a popup 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/popupbutton.h>
#include <nanogui/theme.h>
#include <nanogui/opengl.h>
#include <nanogui/serializer/core.h>
NAMESPACE_BEGIN(nanogui)
PopupButton::PopupButton(Widget *parent, const std::string &caption, int buttonIcon)
: Button(parent, caption, buttonIcon) {
mChevronIcon = mTheme->mPopupChevronRightIcon;
setFlags(Flags::ToggleButton | Flags::PopupButton);
Window *parentWindow = window();
mPopup = new Popup(parentWindow->parent(), window());
mPopup->setSize(Vector2i(320, 250));
mPopup->setVisible(false);
mIconExtraScale = 0.8f;// widget override
}
Vector2i PopupButton::preferredSize(NVGcontext *ctx) const {
return Button::preferredSize(ctx) + Vector2i(15, 0);
}
void PopupButton::draw(NVGcontext* ctx) {
if (!mEnabled && mPushed)
mPushed = false;
mPopup->setVisible(mPushed);
Button::draw(ctx);
if (mChevronIcon) {
auto icon = utf8(mChevronIcon);
NVGcolor textColor =
mTextColor.w() == 0 ? mTheme->mTextColor : mTextColor;
nvgFontSize(ctx, (mFontSize < 0 ? mTheme->mButtonFontSize : mFontSize) * icon_scale());
nvgFontFace(ctx, "icons");
nvgFillColor(ctx, mEnabled ? textColor : mTheme->mDisabledTextColor);
nvgTextAlign(ctx, NVG_ALIGN_LEFT | NVG_ALIGN_MIDDLE);
float iw = nvgTextBounds(ctx, 0, 0, icon.data(), nullptr, nullptr);
Vector2f iconPos(0, mPos.y() + mSize.y() * 0.5f - 1);
if (mPopup->side() == Popup::Right)
iconPos[0] = mPos.x() + mSize.x() - iw - 8;
else
iconPos[0] = mPos.x() + 8;
nvgText(ctx, iconPos.x(), iconPos.y(), icon.data(), nullptr);
}
}
void PopupButton::performLayout(NVGcontext *ctx) {
Widget::performLayout(ctx);
const Window *parentWindow = window();
int posY = absolutePosition().y() - parentWindow->position().y() + mSize.y() /2;
if (mPopup->side() == Popup::Right)
mPopup->setAnchorPos(Vector2i(parentWindow->width() + 15, posY));
else
mPopup->setAnchorPos(Vector2i(0 - 15, posY));
}
void PopupButton::setSide(Popup::Side side) {
if (mPopup->side() == Popup::Right &&
mChevronIcon == mTheme->mPopupChevronRightIcon)
setChevronIcon(mTheme->mPopupChevronLeftIcon);
else if (mPopup->side() == Popup::Left &&
mChevronIcon == mTheme->mPopupChevronLeftIcon)
setChevronIcon(mTheme->mPopupChevronRightIcon);
mPopup->setSide(side);
}
void PopupButton::save(Serializer &s) const {
Button::save(s);
s.set("chevronIcon", mChevronIcon);
}
bool PopupButton::load(Serializer &s) {
if (!Button::load(s))
return false;
if (!s.get("chevronIcon", mChevronIcon))
return false;
return true;
}
NAMESPACE_END(nanogui)