TermOx
spinner.hpp
1 #ifndef TERMOX_WIDGET_WIDGET_SPINNER_HPP
2 #define TERMOX_WIDGET_WIDGET_SPINNER_HPP
3 #include <chrono>
4 #include <memory>
5 #include <utility>
6 
7 #include <termox/painter/glyph_string.hpp>
8 #include <termox/widget/widget.hpp>
9 
10 namespace ox {
11 class Painter;
12 
14 class Spinner : public Widget {
15  public:
16  using Duration_t = std::chrono::milliseconds;
17 
18  struct Parameters {
19  Glyph_string frames;
20  Duration_t period = Duration_t{100};
21  int width = 1;
22  int offset = 0;
23  };
24 
25  public:
27  explicit Spinner(Glyph_string frames,
28  Duration_t period = Duration_t{100},
29  int width = 1,
30  int offset = 0);
31 
32  explicit Spinner(Parameters p);
33 
34  public:
35  void set_frames(Glyph_string frames);
36 
37  [[nodiscard]] auto frames() const noexcept -> Glyph_string const&;
38 
39  void set_period(Duration_t period);
40 
41  [[nodiscard]] auto period() const noexcept -> Duration_t;
42 
43  void set_width(int width);
44 
45  [[nodiscard]] auto spinner_width() const noexcept -> int;
46 
47  void set_offset(int offset);
48 
49  public:
50  void start();
51 
52  void stop();
53 
54  protected:
55  auto paint_event(Painter& p) -> bool override;
56 
57  auto timer_event() -> bool override;
58 
59  private:
60  bool started_ = false;
61  Glyph_string frames_;
62  Duration_t period_;
63  int width_;
64  int index_;
65 };
66 
68 [[nodiscard]] auto spinner(
69  Glyph_string frames,
70  Spinner::Duration_t period = Spinner::Duration_t{100},
71  int width = 1,
72  int offset = 0) -> std::unique_ptr<Spinner>;
73 
75 [[nodiscard]] auto spinner(Spinner::Parameters p) -> std::unique_ptr<Spinner>;
76 
78 
80  struct Parameters {
81  Duration_t period = Duration_t{100};
82  int width = 1;
83  int offset = 0;
84  };
85 
86  explicit Spinner_cycle(Duration_t period = Duration_t{100},
87  int width = 1,
88  int offset = 0);
89 
90  explicit Spinner_cycle(Parameters p);
91 };
92 
93 [[nodiscard]] auto spinner_cycle(
94  Spinner::Duration_t period = Spinner::Duration_t{100},
95  int width = 1,
96  int offset = 0) -> std::unique_ptr<Spinner_cycle>;
97 
98 [[nodiscard]] auto spinner_cycle(Spinner_cycle::Parameters p)
99  -> std::unique_ptr<Spinner_cycle>;
100 
102  Spinner_cycle_ccw(Duration_t period = Duration_t{100},
103  int width = 1,
104  int offset = 0);
105 };
106 
108 template <typename... Args>
109 [[nodiscard]] auto spinner_cycle_ccw(Args&&... args)
110  -> std::unique_ptr<Spinner_cycle_ccw>
111 {
112  return std::make_unique<Spinner_cycle_ccw>(std::forward<Args>(args)...);
113 }
114 
116  Spinner_fall(Duration_t period = Duration_t{100},
117  int width = 1,
118  int offset = 0);
119 };
120 
122 template <typename... Args>
123 [[nodiscard]] auto spinner_fall(Args&&... args) -> std::unique_ptr<Spinner_fall>
124 {
125  return std::make_unique<Spinner_fall>(std::forward<Args>(args)...);
126 }
127 
129  Spinner_fall_two(Duration_t period = Duration_t{100},
130  int width = 1,
131  int offset = 0);
132 };
133 
135 template <typename... Args>
136 [[nodiscard]] auto spinner_fall_two(Args&&... args)
137  -> std::unique_ptr<Spinner_fall_two>
138 {
139  return std::make_unique<Spinner_fall_two>(std::forward<Args>(args)...);
140 }
141 
143  Spinner_fall_three(Duration_t period = Duration_t{100},
144  int width = 1,
145  int offset = 0);
146 };
147 
149 template <typename... Args>
150 [[nodiscard]] auto spinner_fall_three(Args&&... args)
151  -> std::unique_ptr<Spinner_fall_three>
152 {
153  return std::make_unique<Spinner_fall_three>(std::forward<Args>(args)...);
154 }
155 
157  Spinner_rise(Duration_t period = Duration_t{100},
158  int width = 1,
159  int offset = 0);
160 };
161 
163 template <typename... Args>
164 [[nodiscard]] auto spinner_rise(Args&&... args) -> std::unique_ptr<Spinner_rise>
165 {
166  return std::make_unique<Spinner_rise>(std::forward<Args>(args)...);
167 }
168 
170  Spinner_rise_two(Duration_t period = Duration_t{100},
171  int width = 1,
172  int offset = 0);
173 };
174 
176 template <typename... Args>
177 [[nodiscard]] auto spinner_rise_two(Args&&... args)
178  -> std::unique_ptr<Spinner_rise_two>
179 {
180  return std::make_unique<Spinner_rise_two>(std::forward<Args>(args)...);
181 }
182 
184  Spinner_rise_three(Duration_t period = Duration_t{100},
185  int width = 1,
186  int offset = 0);
187 };
188 
190 template <typename... Args>
191 [[nodiscard]] auto spinner_rise_three(Args&&... args)
192  -> std::unique_ptr<Spinner_rise_three>
193 {
194  return std::make_unique<Spinner_rise_three>(std::forward<Args>(args)...);
195 }
196 
198  Spinner_fill(Duration_t period = Duration_t{100},
199  int width = 1,
200  int offset = 0);
201 };
202 
204 template <typename... Args>
205 [[nodiscard]] auto spinner_fill(Args&&... args) -> std::unique_ptr<Spinner_fill>
206 {
207  return std::make_unique<Spinner_fill>(std::forward<Args>(args)...);
208 }
209 
211  Spinner_top_fill(Duration_t period = Duration_t{100},
212  int width = 1,
213  int offset = 0);
214 };
215 
217 template <typename... Args>
218 [[nodiscard]] auto spinner_top_fill(Args&&... args)
219  -> std::unique_ptr<Spinner_top_fill>
220 {
221  return std::make_unique<Spinner_top_fill>(std::forward<Args>(args)...);
222 }
223 
225  Spinner_tail(Duration_t period = Duration_t{100},
226  int width = 1,
227  int offset = 0);
228 };
229 
231 template <typename... Args>
232 [[nodiscard]] auto spinner_tail(Args&&... args) -> std::unique_ptr<Spinner_tail>
233 {
234  return std::make_unique<Spinner_tail>(std::forward<Args>(args)...);
235 }
236 
238  Spinner_switch(Duration_t period = Duration_t{100},
239  int width = 1,
240  int offset = 0);
241 };
242 
244 template <typename... Args>
245 auto spinner_switch(Args&&... args) -> std::unique_ptr<Spinner_switch>
246 {
247  return std::make_unique<Spinner_switch>(std::forward<Args>(args)...);
248 }
249 
251  Spinner_chase(Duration_t period = Duration_t{100},
252  int width = 1,
253  int offset = 0);
254 };
255 
257 template <typename... Args>
258 [[nodiscard]] auto spinner_chase(Args&&... args)
259  -> std::unique_ptr<Spinner_chase>
260 {
261  return std::make_unique<Spinner_chase>(std::forward<Args>(args)...);
262 }
263 
265  Spinner_line(Duration_t period = Duration_t{100},
266  int width = 1,
267  int offset = 0);
268 };
269 
271 template <typename... Args>
272 [[nodiscard]] auto spinner_line(Args&&... args) -> std::unique_ptr<Spinner_line>
273 {
274  return std::make_unique<Spinner_line>(std::forward<Args>(args)...);
275 }
276 
278  Spinner_block_cycle(Duration_t period = Duration_t{100},
279  int width = 1,
280  int offset = 0);
281 };
282 
284 template <typename... Args>
285 [[nodiscard]] auto spinner_block_cycle(Args&&... args)
286  -> std::unique_ptr<Spinner_block_cycle>
287 {
288  return std::make_unique<Spinner_block_cycle>(std::forward<Args>(args)...);
289 }
290 
292  Spinner_fade(Duration_t period = Duration_t{100},
293  int width = 1,
294  int offset = 0);
295 };
296 
298 template <typename... Args>
299 [[nodiscard]] auto spinner_fade(Args&&... args) -> std::unique_ptr<Spinner_fade>
300 {
301  return std::make_unique<Spinner_fade>(std::forward<Args>(args)...);
302 }
303 
306  Spinner_fade_trail(Duration_t period = Duration_t{100},
307  int width = 1,
308  int offset = 0);
309 };
310 
312 template <typename... Args>
313 [[nodiscard]] auto spinner_fade_trail(Args&&... args)
314  -> std::unique_ptr<Spinner_fade_trail>
315 {
316  return std::make_unique<Spinner_fade_trail>(std::forward<Args>(args)...);
317 }
318 
320  Spinner_quarter_circles(Duration_t period = Duration_t{100},
321  int width = 1,
322  int offset = 0);
323 };
324 
326 template <typename... Args>
327 [[nodiscard]] auto spinner_quarter_circles(Args&&... args)
328  -> std::unique_ptr<Spinner_quarter_circles>
329 {
330  return std::make_unique<Spinner_quarter_circles>(
331  std::forward<Args>(args)...);
332 }
333 
335  Spinner_triangles(Duration_t period = Duration_t{100},
336  int width = 1,
337  int offset = 0);
338 };
339 
341 template <typename... Args>
342 [[nodiscard]] auto spinner_triangles(Args&&... args)
343  -> std::unique_ptr<Spinner_triangles>
344 {
345  return std::make_unique<Spinner_triangles>(std::forward<Args>(args)...);
346 }
347 
349  Spinner_empty_triangles(Duration_t period = Duration_t{100},
350  int width = 1,
351  int offset = 0);
352 };
353 
355 template <typename... Args>
356 [[nodiscard]] auto spinner_empty_triangles(Args&&... args)
357  -> std::unique_ptr<Spinner_empty_triangles>
358 {
359  return std::make_unique<Spinner_empty_triangles>(
360  std::forward<Args>(args)...);
361 }
362 
364  Spinner_clock(Duration_t period = Duration_t{100},
365  int width = 1,
366  int offset = 0);
367 };
368 
370 template <typename... Args>
371 [[nodiscard]] auto spinner_clock(Args&&... args)
372  -> std::unique_ptr<Spinner_clock>
373 {
374  return std::make_unique<Spinner_clock>(std::forward<Args>(args)...);
375 }
376 
378  Spinner_box(Duration_t period = Duration_t{100},
379  int width = 1,
380  int offset = 0);
381 };
382 
384 template <typename... Args>
385 [[nodiscard]] auto spinner_box(Args&&... args) -> std::unique_ptr<Spinner_box>
386 {
387  return std::make_unique<Spinner_box>(std::forward<Args>(args)...);
388 }
389 
391  Spinner_cross(Duration_t period = Duration_t{100},
392  int width = 1,
393  int offset = 0);
394 };
395 
397 template <typename... Args>
398 [[nodiscard]] auto spinner_cross(Args&&... args)
399  -> std::unique_ptr<Spinner_cross>
400 {
401  return std::make_unique<Spinner_cross>(std::forward<Args>(args)...);
402 }
403 
405  public:
406  Spinner_vertical_pass(Duration_t period = Duration_t{100},
407  int width = 1,
408  int offset = 0);
409 
410  private:
411  [[nodiscard]] auto first() -> Glyph_string;
412 
413  [[nodiscard]] auto second() -> Glyph_string;
414 };
415 
417 template <typename... Args>
418 [[nodiscard]] auto spinner_vertical_pass(Args&&... args)
419  -> std::unique_ptr<Spinner_vertical_pass>
420 {
421  return std::make_unique<Spinner_vertical_pass>(std::forward<Args>(args)...);
422 }
423 
425  public:
426  Spinner_horizontal_pass(Duration_t period = Duration_t{100},
427  int width = 1,
428  int offset = 0);
429 
430  private:
431  [[nodiscard]] auto first() -> Glyph_string;
432 
433  [[nodiscard]] auto second() -> Glyph_string;
434 };
435 
437 template <typename... Args>
438 [[nodiscard]] auto spinner_horizontal_pass(Args&&... args)
439  -> std::unique_ptr<Spinner_horizontal_pass>
440 {
441  return std::make_unique<Spinner_horizontal_pass>(
442  std::forward<Args>(args)...);
443 }
444 
446  Spinner_bump(Duration_t period = Duration_t{100},
447  int width = 1,
448  int offset = 0);
449 };
450 
452 template <typename... Args>
453 [[nodiscard]] auto spinner_bump(Args&&... args) -> std::unique_ptr<Spinner_bump>
454 {
455  return std::make_unique<Spinner_bump>(std::forward<Args>(args)...);
456 }
457 
458 } // namespace ox
459 #endif // TERMOX_WIDGET_WIDGET_SPINNER_HPP
Holds a collection of Glyphs with a similar interface to std::string.
Definition: glyph_string.hpp:19
Contains functions to paint Glyphs to a Widget's screen area.
Definition: painter.hpp:21
Single cell animated spinner.
Definition: spinner.hpp:14
auto paint_event(Painter &p) -> bool override
Handles Paint_event objects.
Definition: spinner.cpp:65
auto timer_event() -> bool override
Handles Timer_event objects.
Definition: spinner.cpp:74
Spinner(Glyph_string frames, Duration_t period=Duration_t{100}, int width=1, int offset=0)
Each glyph in frames is a frame, offset is starting index into frames.
Definition: spinner.cpp:13
Definition: widget.hpp:31
Definition: spinner.hpp:18
Definition: spinner.hpp:277
Definition: spinner.hpp:377
Definition: spinner.hpp:445
Definition: spinner.hpp:250
Definition: spinner.hpp:363
Definition: spinner.hpp:390
Definition: spinner.hpp:80
Definition: spinner.hpp:101
Specific Spinners.
Definition: spinner.hpp:79
Definition: spinner.hpp:348
Best used with multiple spinners horizontally, each offset one from neighbor.
Definition: spinner.hpp:305
Definition: spinner.hpp:291
Definition: spinner.hpp:142
Definition: spinner.hpp:128
Definition: spinner.hpp:115
Definition: spinner.hpp:197
Definition: spinner.hpp:424
Definition: spinner.hpp:264
Definition: spinner.hpp:319
Definition: spinner.hpp:183
Definition: spinner.hpp:169
Definition: spinner.hpp:156
Definition: spinner.hpp:237
Definition: spinner.hpp:224
Definition: spinner.hpp:210
Definition: spinner.hpp:334
Definition: spinner.hpp:404