|
| 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/button.h |
| 11 | + * |
| 12 | + * \brief Defines the [Normal/Toggle/Radio/Popup] \ref nanogui::Button widget. |
| 13 | + */ |
| 14 | + |
| 15 | +#pragma once |
| 16 | + |
| 17 | +#include <nanogui/widget.h> |
| 18 | + |
| 19 | +NAMESPACE_BEGIN(nanogui) |
| 20 | +/** |
| 21 | + * \class Button button.h nanogui/button.h |
| 22 | + * |
| 23 | + * \brief [Normal/Toggle/Radio/Popup] Button widget. |
| 24 | + */ |
| 25 | +class NANOGUI_EXPORT Button : public Widget { |
| 26 | +public: |
| 27 | + /// Flags to specify the button behavior (can be combined with binary OR) |
| 28 | + enum Flags { |
| 29 | + NormalButton = (1 << 0), ///< A normal Button. |
| 30 | + RadioButton = (1 << 1), ///< A radio Button. |
| 31 | + ToggleButton = (1 << 2), ///< A toggle Button. |
| 32 | + PopupButton = (1 << 3) ///< A popup Button. |
| 33 | + }; |
| 34 | + |
| 35 | + /// The available icon positions. |
| 36 | + enum class IconPosition { |
| 37 | + Left, ///< Button icon on the far left. |
| 38 | + LeftCentered, ///< Button icon on the left, centered (depends on caption text length). |
| 39 | + RightCentered,///< Button icon on the right, centered (depends on caption text length). |
| 40 | + Right ///< Button icon on the far right. |
| 41 | + }; |
| 42 | + |
| 43 | + /** |
| 44 | + * \brief Creates a button attached to the specified parent. |
| 45 | + * |
| 46 | + * \param parent |
| 47 | + * The \ref nanogui::Widget this Button will be attached to. |
| 48 | + * |
| 49 | + * \param caption |
| 50 | + * The name of the button (default ``"Untitled"``). |
| 51 | + * |
| 52 | + * \param icon |
| 53 | + * The icon to display with this Button. See \ref nanogui::Button::mIcon. |
| 54 | + */ |
| 55 | + Button(Widget *parent, const std::string &caption = "Untitled", int icon = 0); |
| 56 | + |
| 57 | + /// Returns the caption of this Button. |
| 58 | + const std::string &caption() const { return mCaption; } |
| 59 | + |
| 60 | + /// Sets the caption of this Button. |
| 61 | + void setCaption(const std::string &caption) { mCaption = caption; } |
| 62 | + |
| 63 | + /// Returns the background color of this Button. |
| 64 | + const Color &backgroundColor() const { return mBackgroundColor; } |
| 65 | + |
| 66 | + /// Sets the background color of this Button. |
| 67 | + void setBackgroundColor(const Color &backgroundColor) { mBackgroundColor = backgroundColor; } |
| 68 | + |
| 69 | + /// Returns the text color of the caption of this Button. |
| 70 | + const Color &textColor() const { return mTextColor; } |
| 71 | + |
| 72 | + /// Sets the text color of the caption of this Button. |
| 73 | + void setTextColor(const Color &textColor) { mTextColor = textColor; } |
| 74 | + |
| 75 | + /// Returns the icon of this Button. See \ref nanogui::Button::mIcon. |
| 76 | + int icon() const { return mIcon; } |
| 77 | + |
| 78 | + /// Sets the icon of this Button. See \ref nanogui::Button::mIcon. |
| 79 | + void setIcon(int icon) { mIcon = icon; } |
| 80 | + |
| 81 | + /// The current flags of this Button (see \ref nanogui::Button::Flags for options). |
| 82 | + int flags() const { return mFlags; } |
| 83 | + |
| 84 | + /// Sets the flags of this Button (see \ref nanogui::Button::Flags for options). |
| 85 | + void setFlags(int buttonFlags) { mFlags = buttonFlags; } |
| 86 | + |
| 87 | + /// The position of the icon for this Button. |
| 88 | + IconPosition iconPosition() const { return mIconPosition; } |
| 89 | + |
| 90 | + /// Sets the position of the icon for this Button. |
| 91 | + void setIconPosition(IconPosition iconPosition) { mIconPosition = iconPosition; } |
| 92 | + |
| 93 | + /// Whether or not this Button is currently pushed. |
| 94 | + bool pushed() const { return mPushed; } |
| 95 | + |
| 96 | + /// Sets whether or not this Button is currently pushed. |
| 97 | + void setPushed(bool pushed) { mPushed = pushed; } |
| 98 | + |
| 99 | + /// The current callback to execute (for any type of button). |
| 100 | + std::function<void()> callback() const { return mCallback; } |
| 101 | + |
| 102 | + /// Set the push callback (for any type of button). |
| 103 | + void setCallback(const std::function<void()> &callback) { mCallback = callback; } |
| 104 | + |
| 105 | + /// The current callback to execute (for toggle buttons). |
| 106 | + std::function<void(bool)> changeCallback() const { return mChangeCallback; } |
| 107 | + |
| 108 | + /// Set the change callback (for toggle buttons). |
| 109 | + void setChangeCallback(const std::function<void(bool)> &callback) { mChangeCallback = callback; } |
| 110 | + |
| 111 | + /// Set the button group (for radio buttons). |
| 112 | + void setButtonGroup(const std::vector<Button *> &buttonGroup) { mButtonGroup = buttonGroup; } |
| 113 | + |
| 114 | + /// The current button group (for radio buttons). |
| 115 | + const std::vector<Button *> &buttonGroup() const { return mButtonGroup; } |
| 116 | + |
| 117 | + /// The preferred size of this Button. |
| 118 | + virtual Vector2i preferredSize(NVGcontext *ctx) const override; |
| 119 | + |
| 120 | + /// The callback that is called when any type of mouse button event is issued to this Button. |
| 121 | + virtual bool mouseButtonEvent(const Vector2i &p, int button, bool down, int modifiers) override; |
| 122 | + |
| 123 | + /// Responsible for drawing the Button. |
| 124 | + virtual void draw(NVGcontext *ctx) override; |
| 125 | + |
| 126 | + /// Saves the state of this Button provided the given Serializer. |
| 127 | + virtual void save(Serializer &s) const override; |
| 128 | + |
| 129 | + /// Sets the state of this Button provided the given Serializer. |
| 130 | + virtual bool load(Serializer &s) override; |
| 131 | + |
| 132 | +protected: |
| 133 | + /// The caption of this Button. |
| 134 | + std::string mCaption; |
| 135 | + |
| 136 | + /** |
| 137 | + * \brief The icon of this Button (``0`` means no icon). |
| 138 | + * |
| 139 | + * \rst |
| 140 | + * The icon to display with this Button. If not ``0``, may either be a |
| 141 | + * picture icon, or one of the icons enumerated in |
| 142 | + * :ref:`file_nanogui_entypo.h`. The kind of icon (image or Entypo) |
| 143 | + * is determined by the functions :func:`nanogui::nvgIsImageIcon` and its |
| 144 | + * reciprocal counterpart :func:`nanogui::nvgIsFontIcon`. |
| 145 | + * \endrst |
| 146 | + */ |
| 147 | + int mIcon; |
| 148 | + |
| 149 | + /// The position to draw the icon at. |
| 150 | + IconPosition mIconPosition; |
| 151 | + |
| 152 | + /// Whether or not this Button is currently pushed. |
| 153 | + bool mPushed; |
| 154 | + |
| 155 | + /// The current flags of this button (see \ref nanogui::Button::Flags for options). |
| 156 | + int mFlags; |
| 157 | + |
| 158 | + /// The background color of this Button. |
| 159 | + Color mBackgroundColor; |
| 160 | + |
| 161 | + /// The color of the caption text of this Button. |
| 162 | + Color mTextColor; |
| 163 | + |
| 164 | + /// The callback issued for all types of buttons. |
| 165 | + std::function<void()> mCallback; |
| 166 | + |
| 167 | + /// The callback issued for toggle buttons. |
| 168 | + std::function<void(bool)> mChangeCallback; |
| 169 | + |
| 170 | + /// The button group for radio buttons. |
| 171 | + std::vector<Button *> mButtonGroup; |
| 172 | + |
| 173 | +public: |
| 174 | + EIGEN_MAKE_ALIGNED_OPERATOR_NEW |
| 175 | +}; |
| 176 | + |
| 177 | +NAMESPACE_END(nanogui) |
0 commit comments