| 1 | // Aseprite UI Library |
| 2 | // Copyright (C) 2020 Igara Studio S.A. |
| 3 | // Copyright (C) 2001-2016 David Capello |
| 4 | // |
| 5 | // This file is released under the terms of the MIT license. |
| 6 | // Read LICENSE.txt for more information. |
| 7 | |
| 8 | #ifndef UI_SLIDER_H_INCLUDED |
| 9 | #define UI_SLIDER_H_INCLUDED |
| 10 | #pragma once |
| 11 | |
| 12 | #include "obs/signal.h" |
| 13 | #include "ui/widget.h" |
| 14 | |
| 15 | namespace ui { |
| 16 | |
| 17 | class SliderDelegate { |
| 18 | public: |
| 19 | virtual ~SliderDelegate() { } |
| 20 | virtual std::string onGetTextFromValue(int value) = 0; |
| 21 | virtual int onGetValueFromText(const std::string& text) = 0; |
| 22 | }; |
| 23 | |
| 24 | class Slider : public Widget { |
| 25 | public: |
| 26 | Slider(int min, int max, int value, SliderDelegate* delegate = nullptr); |
| 27 | |
| 28 | int getMinValue() const { return m_min; } |
| 29 | int getMaxValue() const { return m_max; } |
| 30 | int getValue() const { return m_value; } |
| 31 | |
| 32 | void setRange(int min, int max); |
| 33 | void setValue(int value); |
| 34 | |
| 35 | bool isReadOnly() const { return m_readOnly; } |
| 36 | void setReadOnly(bool readOnly) { m_readOnly = readOnly; } |
| 37 | |
| 38 | void getSliderThemeInfo(int* min, int* max, int* value) const; |
| 39 | |
| 40 | std::string convertValueToText(int value) const; |
| 41 | int convertTextToValue(const std::string& text) const; |
| 42 | |
| 43 | // Signals |
| 44 | obs::signal<void()> Change; |
| 45 | obs::signal<void()> SliderReleased; |
| 46 | |
| 47 | protected: |
| 48 | // Events |
| 49 | bool onProcessMessage(Message* msg) override; |
| 50 | void onPaint(PaintEvent& ev) override; |
| 51 | |
| 52 | // New events |
| 53 | virtual void onChange(); |
| 54 | virtual void onSliderReleased(); |
| 55 | |
| 56 | private: |
| 57 | void setupSliderCursor(); |
| 58 | |
| 59 | int m_min; |
| 60 | int m_max; |
| 61 | int m_value; |
| 62 | bool m_readOnly; |
| 63 | SliderDelegate* m_delegate; |
| 64 | }; |
| 65 | |
| 66 | } // namespace ui |
| 67 | |
| 68 | #endif |
| 69 | |