This repository has been archived by the owner on Nov 9, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
checkbox.cpp
109 lines (92 loc) · 3.38 KB
/
checkbox.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
/*
src/checkbox.cpp -- Two-state check box 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/checkbox.h>
#include <nanogui/opengl.h>
#include <nanogui/theme.h>
#include <nanogui/serializer/core.h>
NAMESPACE_BEGIN(nanogui)
CheckBox::CheckBox(Widget *parent, const std::string &caption,
const std::function<void(bool) > &callback)
: Widget(parent), mCaption(caption), mPushed(false), mChecked(false),
mCallback(callback) {
mIconExtraScale = 1.2f;// widget override
}
bool CheckBox::mouseButtonEvent(const Vector2i &p, int button, bool down,
int modifiers) {
Widget::mouseButtonEvent(p, button, down, modifiers);
if (!mEnabled)
return false;
if (button == GLFW_MOUSE_BUTTON_1) {
if (down) {
mPushed = true;
} else if (mPushed) {
if (contains(p)) {
mChecked = !mChecked;
if (mCallback)
mCallback(mChecked);
}
mPushed = false;
}
return true;
}
return false;
}
Vector2i CheckBox::preferredSize(NVGcontext *ctx) const {
if (mFixedSize != Vector2i::Zero())
return mFixedSize;
nvgFontSize(ctx, fontSize());
nvgFontFace(ctx, "sans");
return Vector2i(
nvgTextBounds(ctx, 0, 0, mCaption.c_str(), nullptr, nullptr) +
1.8f * fontSize(),
fontSize() * 1.3f);
}
void CheckBox::draw(NVGcontext *ctx) {
Widget::draw(ctx);
nvgFontSize(ctx, fontSize());
nvgFontFace(ctx, "sans");
nvgFillColor(ctx,
mEnabled ? mTheme->mTextColor : mTheme->mDisabledTextColor);
nvgTextAlign(ctx, NVG_ALIGN_LEFT | NVG_ALIGN_MIDDLE);
nvgText(ctx, mPos.x() + 1.6f * fontSize(), mPos.y() + mSize.y() * 0.5f,
mCaption.c_str(), nullptr);
NVGpaint bg = nvgBoxGradient(ctx, mPos.x() + 1.5f, mPos.y() + 1.5f,
mSize.y() - 2.0f, mSize.y() - 2.0f, 3, 3,
mPushed ? Color(0, 100) : Color(0, 32),
Color(0, 0, 0, 180));
nvgBeginPath(ctx);
nvgRoundedRect(ctx, mPos.x() + 1.0f, mPos.y() + 1.0f, mSize.y() - 2.0f,
mSize.y() - 2.0f, 3);
nvgFillPaint(ctx, bg);
nvgFill(ctx);
if (mChecked) {
nvgFontSize(ctx, mSize.y() * icon_scale());
nvgFontFace(ctx, "icons");
nvgFillColor(ctx, mEnabled ? mTheme->mIconColor
: mTheme->mDisabledTextColor);
nvgTextAlign(ctx, NVG_ALIGN_CENTER | NVG_ALIGN_MIDDLE);
nvgText(ctx, mPos.x() + mSize.y() * 0.5f + 1,
mPos.y() + mSize.y() * 0.5f, utf8(mTheme->mCheckBoxIcon).data(),
nullptr);
}
}
void CheckBox::save(Serializer &s) const {
Widget::save(s);
s.set("caption", mCaption);
s.set("pushed", mPushed);
s.set("checked", mChecked);
}
bool CheckBox::load(Serializer &s) {
if (!Widget::load(s)) return false;
if (!s.get("caption", mCaption)) return false;
if (!s.get("pushed", mPushed)) return false;
if (!s.get("checked", mChecked)) return false;
return true;
}
NAMESPACE_END(nanogui)