| 62 | #endif |
| 63 | |
| 64 | class UISlider : public UIElement { |
| 65 | public: |
| 66 | FL_NO_COPY(UISlider) |
| 67 | // If step is -1, it will be calculated as (max - min) / 100 |
| 68 | UISlider(const char *name, float value = 128.0f, float min = 1, |
| 69 | float max = 255, float step = -1.f) FL_NOEXCEPT; |
| 70 | float value() const FL_NOEXCEPT { return mImpl.value(); } |
| 71 | float value_normalized() const FL_NOEXCEPT { |
| 72 | float min = mImpl.getMin(); |
| 73 | float max = mImpl.getMax(); |
| 74 | if (fl::almost_equal(max, min, 0.0001f)) { |
| 75 | return 0; |
| 76 | } |
| 77 | return (value() - min) / (max - min); |
| 78 | } |
| 79 | float getMax() const FL_NOEXCEPT { return mImpl.getMax(); } |
| 80 | float getMin() const FL_NOEXCEPT { return mImpl.getMin(); } |
| 81 | void setValue(float value) FL_NOEXCEPT; |
| 82 | operator float() const FL_NOEXCEPT { return mImpl.value(); } |
| 83 | operator u8() const FL_NOEXCEPT { return static_cast<u8>(mImpl.value()); } |
| 84 | operator fl::u16() const FL_NOEXCEPT { return static_cast<fl::u16>(mImpl.value()); } |
| 85 | operator int() const FL_NOEXCEPT { return static_cast<int>(mImpl.value()); } |
| 86 | template <typename T> T as() const FL_NOEXCEPT { |
| 87 | return static_cast<T>(mImpl.value()); |
| 88 | } |
| 89 | |
| 90 | int as_int() const FL_NOEXCEPT { return static_cast<int>(mImpl.value()); } |
| 91 | |
| 92 | UISlider &operator=(float value) FL_NOEXCEPT { |
| 93 | mImpl.setValue(value); |
| 94 | return *this; |
| 95 | } |
| 96 | UISlider &operator=(int value) FL_NOEXCEPT { |
| 97 | mImpl.setValue(static_cast<float>(value)); |
| 98 | return *this; |
| 99 | } |
| 100 | |
| 101 | // Override setGroup to also update the implementation |
| 102 | void setGroup(const fl::string& groupName) FL_NOEXCEPT override { |
| 103 | UIElement::setGroup(groupName); |
| 104 | // Update the implementation's group if it has the method (WASM platforms) |
| 105 | mImpl.setGroup(groupName); |
| 106 | } |
| 107 | |
| 108 | |
| 109 | int onChanged(function<void(UISlider &)> callback) FL_NOEXCEPT { |
| 110 | int out = mCallbacks.add(callback); |
| 111 | mListener.addToEngineEventsOnce(); |
| 112 | return out; |
| 113 | } |
| 114 | void clearCallbacks() FL_NOEXCEPT { mCallbacks.clear(); } |
| 115 | |
| 116 | protected: |
| 117 | UISliderImpl mImpl; |
| 118 | |
| 119 | struct Listener : public EngineEvents::Listener { |
| 120 | Listener(UISlider *owner) FL_NOEXCEPT : mOwner(owner) { |
| 121 | |