|
| 1 | +/* |
| 2 | + nanogui/button.h -- [Normal/Toggle/Radio/Popup] Button widget |
| 3 | +
|
| 4 | + NanoGUI was developed by Wenzel Jakob <[email protected]>. |
| 5 | + The widget drawing code is based on the NanoVG demo application |
| 6 | + by Mikko Mononen. |
| 7 | +
|
| 8 | + All rights reserved. Use of this source code is governed by a |
| 9 | + BSD-style license that can be found in the LICENSE.txt file. |
| 10 | +*/ |
| 11 | + |
| 12 | +#pragma once |
| 13 | + |
| 14 | +#include <nanogui/widget.h> |
| 15 | + |
| 16 | +NAMESPACE_BEGIN(nanogui) |
| 17 | + |
| 18 | +class NANOGUI_EXPORT Button : public Widget { |
| 19 | +public: |
| 20 | + /// Flags to specify the button behavior (can be combined with binary OR) |
| 21 | + enum Flags { |
| 22 | + NormalButton = 1, |
| 23 | + RadioButton = 2, |
| 24 | + ToggleButton = 4, |
| 25 | + PopupButton = 8 |
| 26 | + }; |
| 27 | + |
| 28 | + enum class IconPosition { |
| 29 | + Left, |
| 30 | + LeftCentered, |
| 31 | + RightCentered, |
| 32 | + Right |
| 33 | + }; |
| 34 | + |
| 35 | + Button(Widget *parent, const std::string &caption = "Untitled", int icon = 0); |
| 36 | + |
| 37 | + const std::string &caption() const { return mCaption; } |
| 38 | + void setCaption(const std::string &caption) { mCaption = caption; } |
| 39 | + |
| 40 | + const Color &backgroundColor() const { return mBackgroundColor; } |
| 41 | + void setBackgroundColor(const Color &backgroundColor) { mBackgroundColor = backgroundColor; } |
| 42 | + |
| 43 | + const Color &textColor() const { return mTextColor; } |
| 44 | + void setTextColor(const Color &textColor) { mTextColor = textColor; } |
| 45 | + |
| 46 | + int icon() const { return mIcon; } |
| 47 | + void setIcon(int icon) { mIcon = icon; } |
| 48 | + |
| 49 | + int flags() const { return mFlags; } |
| 50 | + void setFlags(int buttonFlags) { mFlags = buttonFlags; } |
| 51 | + |
| 52 | + IconPosition iconPosition() const { return mIconPosition; } |
| 53 | + void setIconPosition(IconPosition iconPosition) { mIconPosition = iconPosition; } |
| 54 | + |
| 55 | + bool pushed() const { return mPushed; } |
| 56 | + void setPushed(bool pushed) { mPushed = pushed; } |
| 57 | + |
| 58 | + /// Set the push callback (for any type of button) |
| 59 | + std::function<void()> callback() const { return mCallback; } |
| 60 | + void setCallback(const std::function<void()> &callback) { mCallback = callback; } |
| 61 | + |
| 62 | + /// Set the change callback (for toggle buttons) |
| 63 | + std::function<void(bool)> changeCallback() const { return mChangeCallback; } |
| 64 | + void setChangeCallback(const std::function<void(bool)> &callback) { mChangeCallback = callback; } |
| 65 | + |
| 66 | + /// Set the button group (for radio buttons) |
| 67 | + void setButtonGroup(const std::vector<Button *> &buttonGroup) { mButtonGroup = buttonGroup; } |
| 68 | + const std::vector<Button *> &buttonGroup() const { return mButtonGroup; } |
| 69 | + |
| 70 | + virtual Vector2i preferredSize(NVGcontext *ctx) const override; |
| 71 | + virtual bool mouseButtonEvent(const Vector2i &p, int button, bool down, int modifiers) override; |
| 72 | + virtual void draw(NVGcontext *ctx) override; |
| 73 | + |
| 74 | + virtual void save(Serializer &s) const override; |
| 75 | + virtual bool load(Serializer &s) override; |
| 76 | +protected: |
| 77 | + std::string mCaption; |
| 78 | + int mIcon; |
| 79 | + IconPosition mIconPosition; |
| 80 | + bool mPushed; |
| 81 | + int mFlags; |
| 82 | + Color mBackgroundColor; |
| 83 | + Color mTextColor; |
| 84 | + std::function<void()> mCallback; |
| 85 | + std::function<void(bool)> mChangeCallback; |
| 86 | + std::vector<Button *> mButtonGroup; |
| 87 | +}; |
| 88 | + |
| 89 | +NAMESPACE_END(nanogui) |
0 commit comments