| 1 | //************************************ bs::framework - Copyright 2018 Marko Pintera **************************************// |
| 2 | //*********** Licensed under the MIT license. See LICENSE.md for full terms. This notice is not to be removed. ***********// |
| 3 | #pragma once |
| 4 | |
| 5 | #include "BsPrerequisites.h" |
| 6 | #include "GUI/BsGUIElementContainer.h" |
| 7 | #include "Utility/BsEvent.h" |
| 8 | |
| 9 | namespace bs |
| 10 | { |
| 11 | /** @addtogroup Implementation |
| 12 | * @{ |
| 13 | */ |
| 14 | |
| 15 | /** A slider with a draggable handle that can be vertical or horizontal. */ |
| 16 | class BS_EXPORT GUISlider : public GUIElementContainer |
| 17 | { |
| 18 | public: |
| 19 | /** Name of the style for the handle button used by the slider. */ |
| 20 | static const String& getHandleStyleType(); |
| 21 | |
| 22 | /** Name of the style for the background image used by the slider. */ |
| 23 | static const String& getBackgroundStyleType(); |
| 24 | |
| 25 | /** Name of the style for the background fill image used by the slider. */ |
| 26 | static const String& getFillStyleType(); |
| 27 | |
| 28 | /** |
| 29 | * Moves the slider handle the the specified position in the handle area. |
| 30 | * |
| 31 | * @param[in] pct Position to move the handle to, in percent ranging [0.0f, 1.0f] |
| 32 | */ |
| 33 | void setPercent(float pct); |
| 34 | |
| 35 | /** Gets the current position of the slider handle, in percent ranging [0.0f, 1.0f]. */ |
| 36 | float getPercent() const; |
| 37 | |
| 38 | /** |
| 39 | * Gets the current value of the slider. This is the slider handle position percentage scaled within the current |
| 40 | * minimum and maximum range, rounded up to nearest step increment. |
| 41 | */ |
| 42 | float getValue() const; |
| 43 | |
| 44 | /** Sets a new value of the slider. This value should be within minimum and maximum range values. */ |
| 45 | void setValue(float value); |
| 46 | |
| 47 | /** |
| 48 | * Sets a minimum and maximum allow values in the input field. Set to large negative/positive values if you don't |
| 49 | * require clamping. |
| 50 | */ |
| 51 | void setRange(float min, float max); |
| 52 | |
| 53 | /** Returns the minimum value of the slider */ |
| 54 | float getRangeMinimum() const; |
| 55 | |
| 56 | /** Returns the maximum value of the slider */ |
| 57 | float getRangeMaximum() const; |
| 58 | |
| 59 | /** |
| 60 | * Sets a step that defines the minimal increment the value can be increased/decreased by. Set to zero to have no |
| 61 | * step. |
| 62 | */ |
| 63 | void setStep(float step); |
| 64 | |
| 65 | /** Gets the minimum percentual variation of the handle position */ |
| 66 | float getStep() const; |
| 67 | |
| 68 | /** @copydoc GUIElement::setTint */ |
| 69 | void setTint(const Color& color) override; |
| 70 | |
| 71 | /** Triggered when the user changes the value of the slider. */ |
| 72 | Event<void(float percent)> onChanged; |
| 73 | |
| 74 | public: // ***** INTERNAL ****** |
| 75 | /** @name Internal |
| 76 | * @{ |
| 77 | */ |
| 78 | |
| 79 | /** @copydoc GUIElementContainer::_getOptimalSize */ |
| 80 | Vector2I _getOptimalSize() const override; |
| 81 | |
| 82 | /** @} */ |
| 83 | protected: |
| 84 | GUISlider(bool horizontal, const String& styleName, const GUIDimensions& dimensions); |
| 85 | virtual ~GUISlider(); |
| 86 | |
| 87 | /** @copydoc GUIElementContainer::_updateLayoutInternal */ |
| 88 | void _updateLayoutInternal(const GUILayoutData& data) override; |
| 89 | |
| 90 | /** @copydoc GUIElementContainer::styleUpdated */ |
| 91 | void styleUpdated() override; |
| 92 | |
| 93 | /** Triggered when the slider handles moves. */ |
| 94 | void onHandleMoved(float newPosition, float newSize); |
| 95 | |
| 96 | /** @copydoc GUIElement::_commandEvent */ |
| 97 | bool _commandEvent(const GUICommandEvent& ev) override; |
| 98 | private: |
| 99 | GUISliderHandle* mSliderHandle; |
| 100 | GUITexture* mBackground; |
| 101 | GUITexture* mFillBackground; |
| 102 | bool mHorizontal; |
| 103 | float mMinRange = 0.0f; |
| 104 | float mMaxRange = 1.0f; |
| 105 | bool mHasFocus = false; |
| 106 | |
| 107 | HEvent mHandleMovedConn; |
| 108 | }; |
| 109 | |
| 110 | /** @} */ |
| 111 | |
| 112 | /** @addtogroup GUI |
| 113 | * @{ |
| 114 | */ |
| 115 | |
| 116 | /** A horizontal slider with a draggable handle. */ |
| 117 | class BS_EXPORT GUISliderHorz : public GUISlider |
| 118 | { |
| 119 | public: |
| 120 | /** Returns type name of the GUI element used for finding GUI element styles. */ |
| 121 | static const String& getGUITypeName(); |
| 122 | |
| 123 | /** |
| 124 | * Creates a new horizontal slider. |
| 125 | * |
| 126 | * @param[in] styleName Optional style to use for the element. Style will be retrieved from GUISkin of the |
| 127 | * GUIWidget the element is used on. If not specified default style is used. |
| 128 | */ |
| 129 | static GUISliderHorz* create(const String& styleName = StringUtil::BLANK); |
| 130 | |
| 131 | /** |
| 132 | * Creates a new horizontal slider. |
| 133 | * |
| 134 | * @param[in] options Options that allow you to control how is the element positioned and sized. |
| 135 | * This will override any similar options set by style. |
| 136 | * @param[in] styleName Optional style to use for the element. Style will be retrieved from GUISkin of the |
| 137 | * GUIWidget the element is used on. If not specified default style is used. |
| 138 | */ |
| 139 | static GUISliderHorz* create(const GUIOptions& options, const String& styleName = StringUtil::BLANK); |
| 140 | |
| 141 | private: |
| 142 | GUISliderHorz(const String& styleName, const GUIDimensions& dimensions); |
| 143 | }; |
| 144 | |
| 145 | /** A vertical slider with a draggable handle. */ |
| 146 | class BS_EXPORT GUISliderVert : public GUISlider |
| 147 | { |
| 148 | public: |
| 149 | /** Returns type name of the GUI element used for finding GUI element styles. */ |
| 150 | static const String& getGUITypeName(); |
| 151 | |
| 152 | /** |
| 153 | * Creates a new vertical slider. |
| 154 | * |
| 155 | * @param[in] styleName Optional style to use for the element. Style will be retrieved from GUISkin of the |
| 156 | * GUIWidget the element is used on. If not specified default style is used. |
| 157 | */ |
| 158 | static GUISliderVert* create(const String& styleName = StringUtil::BLANK); |
| 159 | |
| 160 | /** |
| 161 | * Creates a new vertical slider. |
| 162 | * |
| 163 | * @param[in] options Options that allow you to control how is the element positioned and sized. |
| 164 | * This will override any similar options set by style. |
| 165 | * @param[in] styleName Optional style to use for the element. Style will be retrieved from GUISkin of the |
| 166 | * GUIWidget the element is used on. If not specified default style is used. |
| 167 | */ |
| 168 | static GUISliderVert* create(const GUIOptions& options, const String& styleName = StringUtil::BLANK); |
| 169 | |
| 170 | private: |
| 171 | GUISliderVert(const String& styleName, const GUIDimensions& dimensions); |
| 172 | }; |
| 173 | |
| 174 | /** @} */ |
| 175 | } |