1 /*
2 NanoGUI was developed by Wenzel Jakob <wenzel.jakob@epfl.ch>.
3 The widget drawing code is based on the NanoVG demo application
4 by Mikko Mononen.
5
6 All rights reserved. Use of this source code is governed by a
7 BSD-style license that can be found in the LICENSE.txt file.
8 */
9 /**
10 * \file nanogui/checkbox.h
11 *
12 * \brief Two-state check box Widget.
13 */
14
15 #pragma once
16
17 #include <nanogui/widget.h>
18
NAMESPACE_BEGIN(nanogui)19 NAMESPACE_BEGIN(nanogui)
20
21 /**
22 * \class CheckBox checkbox.h nanogui/checkbox.h
23 *
24 * \brief Two-state check box widget.
25 *
26 * \remark
27 * This class overrides \ref nanogui::Widget::mIconExtraScale to be ``1.2f``,
28 * which affects all subclasses of this Widget. Subclasses must explicitly
29 * set a different value if needed (e.g., in their constructor).
30 */
31 class NANOGUI_EXPORT CheckBox : public Widget {
32 public:
33 /**
34 * Adds a CheckBox to the specified ``parent``.
35 *
36 * \param parent
37 * The Widget to add this CheckBox to.
38 *
39 * \param caption
40 * The caption text of the CheckBox (default ``"Untitled"``).
41 *
42 * \param callback
43 * If provided, the callback to execute when the CheckBox is checked or
44 * unchecked. Default parameter function does nothing. See
45 * \ref nanogui::CheckBox::mPushed for the difference between "pushed"
46 * and "checked".
47 */
48 CheckBox(Widget *parent, const std::string &caption = "Untitled",
49 const std::function<void(bool)> &callback = std::function<void(bool)>());
50
51 /// The caption of this CheckBox.
52 const std::string &caption() const { return mCaption; }
53
54 /// Sets the caption of this CheckBox.
55 void setCaption(const std::string &caption) { mCaption = caption; }
56
57 /// Whether or not this CheckBox is currently checked.
58 const bool &checked() const { return mChecked; }
59
60 /// Sets whether or not this CheckBox is currently checked.
61 void setChecked(const bool &checked) { mChecked = checked; }
62
63 /// Whether or not this CheckBox is currently pushed. See \ref nanogui::CheckBox::mPushed.
64 const bool &pushed() const { return mPushed; }
65
66 /// Sets whether or not this CheckBox is currently pushed. See \ref nanogui::CheckBox::mPushed.
67 void setPushed(const bool &pushed) { mPushed = pushed; }
68
69 /// Returns the current callback of this CheckBox.
70 std::function<void(bool)> callback() const { return mCallback; }
71
72 /// Sets the callback to be executed when this CheckBox is checked / unchecked.
73 void setCallback(const std::function<void(bool)> &callback) { mCallback = callback; }
74
75 /**
76 * The mouse button callback will return ``true`` when all three conditions are met:
77 *
78 * 1. This CheckBox is "enabled" (see \ref nanogui::Widget::mEnabled).
79 * 2. ``p`` is inside this CheckBox.
80 * 3. ``button`` is ``GLFW_MOUSE_BUTTON_1`` (left mouse click).
81 *
82 * Since a mouse button event is issued for both when the mouse is pressed, as well
83 * as released, this function sets \ref nanogui::CheckBox::mPushed to ``true`` when
84 * parameter ``down == true``. When the second event (``down == false``) is fired,
85 * \ref nanogui::CheckBox::mChecked is inverted and \ref nanogui::CheckBox::mCallback
86 * is called.
87 *
88 * That is, the callback provided is only called when the mouse button is released,
89 * **and** the click location remains within the CheckBox boundaries. If the user
90 * clicks on the CheckBox and releases away from the bounds of the CheckBox,
91 * \ref nanogui::CheckBox::mPushed is simply set back to ``false``.
92 */
93 virtual bool mouseButtonEvent(const Vector2i &p, int button, bool down, int modifiers) override;
94
95 /// The preferred size of this CheckBox.
96 virtual Vector2i preferredSize(NVGcontext *ctx) const override;
97
98 /// Draws this CheckBox.
99 virtual void draw(NVGcontext *ctx) override;
100
101 /// Saves this CheckBox to the specified Serializer.
102 virtual void save(Serializer &s) const override;
103
104 /// Loads the state of the specified Serializer to this CheckBox.
105 virtual bool load(Serializer &s) override;
106
107 protected:
108 /// The caption text of this CheckBox.
109 std::string mCaption;
110
111 /**
112 * Internal tracking variable to distinguish between mouse click and release.
113 * \ref nanogui::CheckBox::mCallback is only called upon release. See
114 * \ref nanogui::CheckBox::mouseButtonEvent for specific conditions.
115 */
116 bool mPushed;
117
118 /// Whether or not this CheckBox is currently checked or unchecked.
119 bool mChecked;
120
121 /// The function to execute when \ref nanogui::CheckBox::mChecked is changed.
122 std::function<void(bool)> mCallback;
123
124 public:
125 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
126 };
127
128 NAMESPACE_END(nanogui)
129