forked from danilw/nanogui-GLES-wasm
-
Notifications
You must be signed in to change notification settings - Fork 2
/
colorpicker.cpp
100 lines (79 loc) · 2.98 KB
/
colorpicker.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
/*
src/colorpicker.cpp -- push button with a popup to tweak a color value
This widget was contributed by Christian Schueller.
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/colorpicker.h>
#include <nanogui/layout.h>
#include <nanogui/colorwheel.h>
NAMESPACE_BEGIN(nanogui)
ColorPicker::ColorPicker(Widget *parent, const Color& color) : PopupButton(parent, "") {
setBackgroundColor(color);
Popup *popup = this->popup();
popup->setLayout(new GroupLayout());
// initialize callback to do nothing; this is for users to hook into
// receiving a new color value
mCallback = [](const Color &) {};
mFinalCallback = [](const Color &) {};
// set the color wheel to the specified color
mColorWheel = new ColorWheel(popup, color);
// set the pick button to the specified color
mPickButton = new Button(popup, "Pick");
mPickButton->setBackgroundColor(color);
mPickButton->setTextColor(color.contrastingColor());
mPickButton->setFixedSize(Vector2i(100, 20));
// set the reset button to the specified color
mResetButton = new Button(popup, "Reset");
mResetButton->setBackgroundColor(color);
mResetButton->setTextColor(color.contrastingColor());
mResetButton->setFixedSize(Vector2i(100, 20));
PopupButton::setChangeCallback([&](bool) {
if (this->mPickButton->pushed()) {
setColor(backgroundColor());
mFinalCallback(backgroundColor());
}
});
mColorWheel->setCallback([&](const Color &value) {
mPickButton->setBackgroundColor(value);
mPickButton->setTextColor(value.contrastingColor());
mCallback(value);
});
mPickButton->setCallback([this]() {
if (mPushed) {
Color value = mColorWheel->color();
setPushed(false);
setColor(value);
mFinalCallback(value);
}
});
mResetButton->setCallback([this]() {
Color bg = this->mResetButton->backgroundColor();
Color fg = this->mResetButton->textColor();
mColorWheel->setColor(bg);
mPickButton->setBackgroundColor(bg);
mPickButton->setTextColor(fg);
mCallback(bg);
mFinalCallback(bg);
});
}
Color ColorPicker::color() const {
return backgroundColor();
}
void ColorPicker::setColor(const Color& color) {
/* Ignore setColor() calls when the user is currently editing */
if (!mPushed) {
Color fg = color.contrastingColor();
setBackgroundColor(color);
setTextColor(fg);
mColorWheel->setColor(color);
mPickButton->setBackgroundColor(color);
mPickButton->setTextColor(fg);
mResetButton->setBackgroundColor(color);
mResetButton->setTextColor(fg);
}
}
NAMESPACE_END(nanogui)