TermOx
slider_logic.hpp
1 #ifndef TERMOX_WIDGET_WIDGETS_DETAIL_SLIDER_LOGIC_HPP
2 #define TERMOX_WIDGET_WIDGETS_DETAIL_SLIDER_LOGIC_HPP
3 #include <signals_light/signal.hpp>
4 
5 namespace ox::detail {
6 
8 class Slider_logic {
9  public:
10  using Value_t = int;
11  using Ratio_t = double;
12 
14  sl::Signal<void(Value_t)> value_changed;
15 
17  sl::Signal<void(Ratio_t)> ratio_changed;
18 
19  public:
21  Slider_logic(Value_t minimum, Value_t maximum);
22 
23  public:
25 
26  void set_minimum(Value_t min);
27 
29 
30  void set_maximum(Value_t max);
31 
33  [[nodiscard]] auto minimum() const -> Value_t;
34 
36  [[nodiscard]] auto maximum() const -> Value_t;
37 
39  void increment(Value_t amount = 1);
40 
42  void decrement(Value_t amount = 1);
43 
45  void set_ratio(Ratio_t ratio);
46 
48  [[nodiscard]] auto ratio() const -> double;
49 
51  void set_value(Value_t value);
52 
54  [[nodiscard]] auto value() const -> Value_t;
55 
57  [[nodiscard]] auto length() const -> Value_t;
58 
59  private:
60  Value_t minimum_ = 0;
61  Value_t maximum_ = 0;
62  Ratio_t ratio_ = 0.;
63 };
64 
65 } // namespace ox::detail
66 #endif // TERMOX_WIDGET_WIDGETS_DETAIL_SLIDER_LOGIC_HPP
Holds the ratio of a slider like Widget and calculates current value.
Definition: slider_logic.hpp:8
void decrement(Value_t amount=1)
Decrement the current value by amount.
Definition: slider_logic.cpp:45
auto minimum() const -> Value_t
Return the smallest possible value for the slider.
Definition: slider_logic.cpp:36
void set_maximum(Value_t max)
Set the maximum value the slider can take on.
Definition: slider_logic.cpp:27
sl::Signal< void(Value_t)> value_changed
Signal emitted every time the value is changed.
Definition: slider_logic.hpp:14
Slider_logic(Value_t minimum, Value_t maximum)
Initialize with range of acceptable values and minimum current value.
Definition: slider_logic.cpp:11
void set_ratio(Ratio_t ratio)
Directly set the ratio as a value from [0.0, 1.0].
Definition: slider_logic.cpp:50
auto value() const -> Value_t
Return the current value.
Definition: slider_logic.cpp:76
auto maximum() const -> Value_t
Return the largest possible value for the slider.
Definition: slider_logic.cpp:38
void set_value(Value_t value)
Set the current value of the slider and emit signal.
Definition: slider_logic.cpp:66
void set_minimum(Value_t min)
Set the minimum value the slider can take on.
Definition: slider_logic.cpp:18
auto length() const -> Value_t
Return the distance between the maximum and minimum.
Definition: slider_logic.cpp:82
auto ratio() const -> double
Return the position of the current value in the slider as a ratio.
Definition: slider_logic.cpp:64
void increment(Value_t amount=1)
Increment the current value by amount.
Definition: slider_logic.cpp:40
sl::Signal< void(Ratio_t)> ratio_changed
Signal emitted every time the ratio is changed.
Definition: slider_logic.hpp:17