TermOx
slider.hpp
1 #ifndef TERMOX_WIDGET_WIDGETS_SLIDER_HPP
2 #define TERMOX_WIDGET_WIDGETS_SLIDER_HPP
3 #include <memory>
4 
5 #include <signals_light/signal.hpp>
6 
7 #include <termox/painter/painter.hpp>
8 #include <termox/system/key.hpp>
9 #include <termox/system/mouse.hpp>
10 #include <termox/widget/area.hpp>
11 #include <termox/widget/layouts/horizontal.hpp>
12 #include <termox/widget/layouts/vertical.hpp>
13 #include <termox/widget/point.hpp>
14 #include <termox/widget/widget.hpp>
15 
16 namespace ox {
17 
19 
20 template <template <typename> typename Layout_t, bool Inverted>
21 class Slider : public Widget {
22  public:
24  struct Range {
25  int low;
26  int high;
27  };
28 
29  struct Parameters {
30  Range range;
31  int initial;
32  };
33 
34  public:
36  sl::Signal<void(Range)> range_changed;
37 
39  sl::Signal<void(int)> value_changed;
40 
41  public:
43 
45  Slider(Range range, int initial);
46 
48 
50  Slider(Parameters p);
51 
52  public:
54 
56  void set_range(Range x);
57 
59  [[nodiscard]] auto range() const noexcept -> Range;
60 
62 
63  void set_value(int x);
64 
66  auto value() const noexcept -> int;
67 
68  protected:
69  auto mouse_press_event(Mouse const& m) -> bool override;
70 
71  auto mouse_move_event(Mouse const& m) -> bool override;
72 
73  auto mouse_wheel_event(Mouse const& m) -> bool override;
74 
75  auto key_press_event(Key k) -> bool override;
76 
77  auto paint_event(Painter& p) -> bool override;
78 
79  private:
80  void increment(int amount = 1);
81 
82  void decrement(int amount = 1);
83 
85  [[nodiscard]] auto value_from_point(Point p) const -> int;
86 
88  [[nodiscard]] auto extra() const -> int;
89 
90  private:
91  static auto constexpr is_vertical = layout::is_vertical_v<Layout_t<Widget>>;
92 
93  Range range_;
94  int value_;
95 
96  private:
98  [[nodiscard]] static auto length(Range range) -> int;
99 
100  [[nodiscard]] static auto make_point(int length, int breadth) -> Point;
101 
102  [[nodiscard]] static auto make_area(int length_dim, int breadth_dim)
103  -> Area;
104 };
105 
107 template <template <typename> typename Layout_t, bool Inverted>
108 [[nodiscard]] auto slider(typename Slider<Layout_t, Inverted>::Range range,
109  int initial)
110  -> std::unique_ptr<Slider<Layout_t, Inverted>>;
111 
113 template <template <typename> typename Layout_t, bool Inverted>
114 [[nodiscard]] auto slider(
115  typename Slider<Layout_t, Inverted>::Parameters parameters)
116  -> std::unique_ptr<Slider<Layout_t, Inverted>>;
117 
118 using Slider_top = Slider<layout::Vertical, false>;
119 
121 [[nodiscard]] auto slider_top(Slider_top::Range range, int initial)
122  -> std::unique_ptr<Slider_top>;
123 
125 [[nodiscard]] auto slider_top(Slider_top::Parameters parameters)
126  -> std::unique_ptr<Slider_top>;
127 
128 using Slider_bottom = Slider<layout::Vertical, true>;
129 
131 [[nodiscard]] auto slider_bottom(Slider_bottom::Range range, int initial)
132  -> std::unique_ptr<Slider_bottom>;
133 
135 [[nodiscard]] auto slider_bottom(Slider_bottom::Parameters parameters)
136  -> std::unique_ptr<Slider_bottom>;
137 
138 using Slider_left = Slider<layout::Horizontal, false>;
139 
141 [[nodiscard]] auto slider_left(Slider_left::Range range, int initial)
142  -> std::unique_ptr<Slider_left>;
143 
145 [[nodiscard]] auto slider_left(Slider_left::Parameters parameters)
146  -> std::unique_ptr<Slider_left>;
147 
148 using Slider_right = Slider<layout::Horizontal, true>;
149 
151 [[nodiscard]] auto slider_right(Slider_right::Range range, int initial)
152  -> std::unique_ptr<Slider_right>;
153 
155 [[nodiscard]] auto slider_right(Slider_right::Parameters parameters)
156  -> std::unique_ptr<Slider_right>;
157 
158 } // namespace ox
159 #endif // TERMOX_WIDGET_WIDGETS_SLIDER_HPP
Contains functions to paint Glyphs to a Widget's screen area.
Definition: painter.hpp:21
Fixed range sliding display fixed at one end of the Widget.
Definition: slider.hpp:21
auto range() const noexcept -> Range
Return the currently set Range. Emits range_changed Signal.
Definition: slider.cpp:55
auto key_press_event(Key k) -> bool override
Handles Key_press_event objects.
Definition: slider.cpp:119
Slider(Range range, int initial)
Create new Slider.
Definition: slider.cpp:26
sl::Signal< void(int)> value_changed
Emitted on user input and passes along the new current value set.
Definition: slider.hpp:39
auto paint_event(Painter &p) -> bool override
Handles Paint_event objects.
Definition: slider.cpp:189
auto value() const noexcept -> int
Return the currently set value.
Definition: slider.cpp:71
void set_range(Range x)
Set a new Range for this Slider.
Definition: slider.cpp:43
auto mouse_wheel_event(Mouse const &m) -> bool override
Handles Mouse_wheel_event objects.
Definition: slider.cpp:93
sl::Signal< void(Range)> range_changed
Emitted when the [low, high] range is modified.
Definition: slider.hpp:36
auto mouse_move_event(Mouse const &m) -> bool override
Handles Mouse_move_event objects.
Definition: slider.cpp:85
auto mouse_press_event(Mouse const &m) -> bool override
Handles Mouse_press_event objects.
Definition: slider.cpp:77
void set_value(int x)
Set the current value of the Slider. Emits value_changed Signal.
Definition: slider.cpp:61
Definition: widget.hpp:31
Definition: slider.hpp:29
[low, high] inclusive range of values.
Definition: slider.hpp:24