forked from dalerank/imspinner
-
Notifications
You must be signed in to change notification settings - Fork 2
/
imspinner.h
4738 lines (4085 loc) · 255 KB
/
imspinner.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifndef _IMSPINNER_H_
#define _IMSPINNER_H_
/*
* The MIT License (MIT)
*
* Copyright (c) 2021-2022 Dalerank
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/
#include <functional>
#include <array>
#include <vector>
#include <cmath>
#include <map>
#include <cctype>
#ifdef __has_include
#if !__has_include(<imgui.h>)
#error "Couldn't find imgui.h in the header include path, please add it to the path!"
#endif // !<imgui.h>
#endif // __has_include
// imgui headers
#include "imgui.h"
#include "imgui_internal.h"
namespace ImSpinner
{
static const ImColor white{1.f, 1.f, 1.f, 1.f};
static const ImColor half_white{1.f, 1.f, 1.f, 0.5f};
static const ImColor red{1.f,0.f,0.f,1.f};
#define DECLPROP(name, type, def) \
struct name { \
type value = def; \
operator type() { return value; } \
name(const type& v) : value(v) {} \
};
enum SpinnerTypeT {
e_st_rainbow = 0,
e_st_angle,
e_st_dots,
e_st_ang,
e_st_vdots,
e_st_bounce_ball,
e_st_eclipse,
e_st_ingyang,
e_st_barchartsine,
e_st_count
};
using float_ptr = float *;
constexpr float PI_DIV_4 = IM_PI / 4.f;
constexpr float PI_DIV_2 = IM_PI / 2.f;
constexpr float PI_2 = IM_PI * 2.f;
template<class T> constexpr float PI_DIV(T d) { return IM_PI / (float)d; }
template<class T> constexpr float PI_2_DIV(T d) { return PI_2 / (float)d; }
DECLPROP (SpinnerType, SpinnerTypeT, e_st_rainbow)
DECLPROP (Radius, float, 16.f)
DECLPROP (Speed, float, 1.f)
DECLPROP (Thickness, float, 1.f)
DECLPROP (Color, ImColor, white)
DECLPROP (BgColor, ImColor, white)
DECLPROP (AltColor, ImColor, white)
DECLPROP (Angle, float, IM_PI)
DECLPROP (AngleMin, float, IM_PI)
DECLPROP (AngleMax, float, IM_PI)
DECLPROP (FloatPtr, float_ptr, nullptr)
DECLPROP (Dots, int, 0)
DECLPROP (MiddleDots, int, 0)
DECLPROP (MinThickness, float, 0.f)
DECLPROP (Reverse, bool, false)
DECLPROP (Delta, float, 0.f)
DECLPROP (Mode, int, 0)
#undef DECLPROP
namespace detail {
// SpinnerBegin is a function that starts a spinner widget, used to display an animation indicating that
// a task is in progress. It returns true if the widget is visible and can be used, or false if it should be skipped.
inline bool SpinnerBegin(const char *label, float radius, ImVec2 &pos, ImVec2 &size, ImVec2 ¢re, int &num_segments) {
ImGuiWindow *window = ImGui::GetCurrentWindow();
if (window->SkipItems)
return false;
ImGuiContext &g = *GImGui;
const ImGuiStyle &style = g.Style;
const ImGuiID id = window->GetID(label);
pos = window->DC.CursorPos;
// The size of the spinner is set to twice the radius, plus some padding based on the style
size = ImVec2((radius) * 2, (radius + style.FramePadding.y) * 2);
const ImRect bb(pos, ImVec2(pos.x + size.x, pos.y + size.y));
ImGui::ItemSize(bb, style.FramePadding.y);
num_segments = window->DrawList->_CalcCircleAutoSegmentCount(radius);
centre = bb.GetCenter();
// If the item cannot be added to the window, return false
if (!ImGui::ItemAdd(bb, id))
return false;
return true;
}
#define IMPLRPOP(basetype,type) basetype m_##type; \
void set##type(const basetype& v) { m_##type = v;} \
void set(type h) { m_##type = h.value;} \
template<typename First, typename... Args> \
void set(const type& h, const Args&... args) { set##type(h.value); this->template set<Args...>(args...); }
struct SpinnerConfig {
SpinnerConfig() {}
template<typename none = void> void set() {}
template<typename... Args>
SpinnerConfig(const Args&... args) { this->template set<Args...>(args...); }
IMPLRPOP(SpinnerTypeT, SpinnerType)
IMPLRPOP(float, Radius)
IMPLRPOP(float, Speed)
IMPLRPOP(float, Thickness)
IMPLRPOP(ImColor, Color)
IMPLRPOP(ImColor, BgColor)
IMPLRPOP(ImColor, AltColor)
IMPLRPOP(float, Angle)
IMPLRPOP(float, AngleMin)
IMPLRPOP(float, AngleMax)
IMPLRPOP(float_ptr, FloatPtr)
IMPLRPOP(int, Dots)
IMPLRPOP(int, MiddleDots)
IMPLRPOP(float, MinThickness)
IMPLRPOP(bool, Reverse)
IMPLRPOP(float, Delta)
IMPLRPOP(int, Mode)
};
#undef IMPLRPOP
}
#define SPINNER_HEADER(pos, size, centre, num_segments) \
ImVec2 pos, size, centre; int num_segments; \
if (!detail::SpinnerBegin(label, radius, pos, size, centre, num_segments)) { return; }; \
ImGuiWindow *window = ImGui::GetCurrentWindow(); \
auto circle = [&] (const std::function<ImVec2 (int)>& point_func, ImU32 dbc, float dth) { \
window->DrawList->PathClear(); \
for (int i = 0; i < num_segments; i++) { \
ImVec2 p = point_func(i); \
window->DrawList->PathLineTo(ImVec2(centre.x + p.x, centre.y + p.y)); \
} \
window->DrawList->PathStroke(dbc, 0, dth); \
}
inline ImColor color_alpha(ImColor c, float alpha) { c.Value.w *= alpha * ImGui::GetStyle().Alpha; return c; }
inline float damped_spring(float mass, float stiffness, float damping, float time, float a = PI_DIV_2, float b = PI_DIV_2) {
float omega = ImSqrt(stiffness / mass);
float alpha = damping / (2 * mass);
float exponent = std::exp(-alpha * time);
float cosTerm = ImCos(omega * ImSqrt(1 - alpha * alpha) * time);
float result = exponent * cosTerm;
return ((result *= a) + b);
};
inline float damped_gravity(float limtime) {
float time = 0.0f, initialHeight = 10.f, height = initialHeight, velocity = 0.f, prtime = 0.0f;
while (height >= 0.0) {
if (prtime >= limtime) { return height / 10.f; }
time += 0.01f; prtime += 0.01f;
height = initialHeight - 0.5 * 9.81f * time * time;
if (height < 0.0) { initialHeight = 0.0; time = 0.0; }
}
return 0.f;
}
inline float damped_trifolium(float limtime, float a = 0.f, float b = 1.f) {
return a * ImSin(limtime) - b * ImSin(3 * limtime);
}
inline float damped_inoutelastic(float t, float amplitude, float period) {
if( t == 0 ) return 0;
t *= 2;
if( t == 2 ) return 1;
float s;
if( amplitude < 1 ) {
amplitude = 1;
s = period / 4;
} else {
s = period / (2 * IM_PI) * std::asin( 1 / amplitude );
}
if( t < 1 ) return -0.5f * ( amplitude * ImPow(2.0f, 10.f*(t-1.f) ) * ImSin( (t-1.f-s)*(2.f*IM_PI)/period ));
return amplitude * ImPow( 2.0f, -10*(t-1) ) * ImSin( (t-1.f-s)*(2.f*IM_PI)/period ) * 0.5f + 1.f;
}
inline std::pair<float, float> damped_infinity(float t, float a) {
return std::make_pair((a * ImCos(t)) / (1 + (powf(ImSin(t), 2.0f))),
(a * ImSin(t) * ImCos(t)) / (1 + (powf(ImSin(t), 2.0f))));
};
inline float ease_inquad(float time) { return time * time; }
inline float ease_outquad(float time) { return time * (2.f - time); }
inline float ease_inoutquad(float t) { if (t < 0.5f) { return 2 * t * t; } else { return -1 + (4 - 2 * t) * t; }}
inline float ease_inoutquad(float *p) { float tr = ImMax(ImSin(p[0]) - 0.5f, 0.f) * (p[1] * 0.5f); return ease_inoutquad(tr); }
inline float ease_outcubic(float t) { float ft = t - 1; return ft * ft * ft + 1; }
inline float ease_inexpo(float t) { return t == 0 ? 0 : pow(2, 10 * (t - 1)); }
inline float ease_inoutexpo(float t) { if (t == 0) return 0; if (t == 1) return 1; if (t < 0.5f) return 0.5f * pow(2, (20 * t) - 10); return 0.5f * (2 - pow(2, -20 * t + 10)); }
inline float ease_inoutexpo(float *p) { float tr = ImMax(ImSin(p[0]) - 0.5f, 0.f) * (p[1] * 0.4f); return ease_inoutexpo(tr) * (p[1] * 0.3f); }
inline float ease_spring(float *p) { return damped_spring(1, 10.f, 1.0f, ImSin(ImFmod(p[0], p[1])), p[2], p[3]);}
inline float ease_gravity(float *p) { return damped_gravity(p[0]); }
inline float ease_infinity(float *p) { return damped_infinity(p[0], p[1]).second; }
inline float ease_inoutelastic(float *p) { return damped_inoutelastic(p[1], p[2], p[3]); }
enum ease_mode {
e_ease_none = 0,
e_ease_inoutquad = 1,
e_ease_inoutexpo = 2,
e_ease_spring = 3,
e_ease_gravity = 4,
e_ease_infinity = 5,
e_ease_elastic = 6,
};
template<typename ... Args>
inline float ease(ease_mode mode, Args ... args) {
static_assert((std::is_same_v<Args, float> && ...), "All arguments should be of type float");
float params[] = {args...};
switch (mode) {
case e_ease_inoutquad: return ease_inoutquad(params);
case e_ease_inoutexpo: return ease_inoutexpo(params);
case e_ease_spring: return ease_spring(params);
case e_ease_gravity: return ease_gravity(params);
case e_ease_infinity: return ease_infinity(params);
case e_ease_elastic: return ease_inoutelastic(params);
case e_ease_none: return (0.f);
}
return 0.f;
}
/*
const char *label: A string label for the spinner, used to identify it in ImGui.
float radius: The radius of the spinner.
float thickness: The thickness of the spinner's border.
const ImColor &color: The color of the spinner.
float speed: The speed of the spinning animation.
float ang_min: Minimum angle of spinning.
float ang_max: Maximum angle of spinning.
int arcs: Number of arcs of the spinner.
*/
inline void SpinnerRainbow(const char *label, float radius, float thickness, const ImColor &color, float speed, float ang_min = 0.f, float ang_max = PI_2, int arcs = 1, int mode = 0)
{
SPINNER_HEADER(pos, size, centre, num_segments);
const float start = ImAbs(ImSin((float)ImGui::GetTime()) * (num_segments - 5));
for (int i = 0; i < arcs; ++i)
{
const float rb = (radius / arcs) * (i + 1);
const float a_min = ImMax(ang_min, PI_2 * ((float)start) / (float)num_segments + (IM_PI / arcs) * i);
const float a_max = ImMin(ang_max, PI_2 * ((float)num_segments + 3 * (i + 1)) / (float)num_segments);
circle([&](int i) {
const float a = a_min + ((float)i / (float)num_segments) * (a_max - a_min);
const float rspeed = a + (float)ImGui::GetTime() * speed;
float pulse_factor = 1.f;
if (mode == 1) {
pulse_factor = 0.8f + 0.2f * ImSin((float)ImGui::GetTime() * 1.5f); // Pulsate between 0.8 and 1.0
}
const float pulsating_radius = rb * pulse_factor;
return ImVec2(ImCos(rspeed) * pulsating_radius, ImSin(rspeed) * pulsating_radius);
}, color_alpha(color, 1.f), thickness);
}
}
inline void SpinnerRainbowMix(const char *label, float radius, float thickness, const ImColor &color, float speed, float ang_min = 0.f, float ang_max = PI_2, int arcs = 1, int mode = 0)
{
SPINNER_HEADER(pos, size, centre, num_segments);
float out_h, out_s, out_v;
ImGui::ColorConvertRGBtoHSV(color.Value.x, color.Value.y, color.Value.z, out_h, out_s, out_v);
for (int i = 0; i < arcs; ++i)
{
const float rb = (radius / arcs) * (i + 1);
const float start = ImAbs(ImSin((float)ImGui::GetTime()) * (num_segments - 5));
const float a_min = ImMax(ang_min, PI_2 * ((float)start) / (float)num_segments + (IM_PI / arcs) * i);
const float a_max = ImMin(ang_max, PI_2 * ((float)num_segments + 3 * (i + 1)) / (float)num_segments);
const float koeff = mode ? (1.1f - 1.f / (i+1)) : 1.f;
ImColor c = ImColor::HSV(out_h + i * (1.f / arcs), out_s, out_v);
circle([&] (int i) {
const float a = a_min + ((float)i / (float)num_segments) * (a_max - a_min);
const float rspeed = a + (float)ImGui::GetTime() * speed * koeff;
return ImVec2(ImCos(rspeed) * rb, ImSin(rspeed) * rb);
}, color_alpha(c, 1.f), thickness);
}
}
// This function draws a rotating heart spinner.
inline void SpinnerRotatingHeart(const char *label, float radius, float thickness, const ImColor &color, float speed, float ang_min = 0.f)
{
// Calculate the position and size of the spinner, as well as the number of segments it will be divided into.
SPINNER_HEADER(pos, size, centre, num_segments);
// Calculate the start angle of the spinner based on the current time and speed.
const float start = (float)ImGui::GetTime() * speed;
// Modify the number of segments to ensure the heart shape is complete.
num_segments = (num_segments * 3) / 2;
// Create a lambda function to rotate points.
auto rotate = [] (const ImVec2 &point, float angle) {
const float s = ImSin(angle), c = ImCos(angle);
return ImVec2(point.x * c - point.y * s, point.x * s + point.y * c);
};
// Calculate the radius of the bottom of the heart.
const float rb = radius * ImMax(0.8f, ImSin(start * 2));
auto scale = [rb] (float v) { return v / 16.f * rb; };
// Draw the heart spinner by calling the circle function, passing in a lambda function that defines the shape of the heart.
circle([&] (int i) {
const float a = PI_2 * i / num_segments;
const float x = (scale(16) * ImPow(ImSin(a), 3));
const float y = -1.f * (scale(13) * ImCos(a) - scale(5) * ImCos(2 * a) - scale(2) * ImCos(3 * a) - ImCos(4 * a));
return rotate(ImVec2(x, y), ang_min);
}, color_alpha(color, 1.f), thickness);
}
// SpinnerAng is a function that draws a spinner widget with a given angle.
inline void SpinnerAng(const char *label, float radius, float thickness, const ImColor &color = white, const ImColor &bg = white, float speed = 2.8f, float angle = IM_PI, int mode = 0)
{
SPINNER_HEADER(pos, size, centre, num_segments); // Get the position, size, centre, and number of segments of the spinner using the SPINNER_HEADER macro.
float start = (float)ImGui::GetTime() * speed; // The start angle of the spinner is calculated based on the current time and the specified speed.
float b = 0.f;
switch (mode) {
case 1: b = damped_gravity(ImSin(start * 1.1f)) * angle; break;
case 2: radius = (0.8f + ImCos(start) * 0.2f) * radius; break;
case 3: b = damped_infinity(start * 1.1f, 1.f).second; break;
}
auto radiusmode = [radius, mode] (float a) { switch (mode) { case 4: return damped_trifolium(a) * radius; } return radius; };
circle([&] (int i) { // Draw the background of the spinner using the `circle` function, with the specified background color and thickness.
const float a = start + (i * (PI_2 / (num_segments - 1))); // Calculate the angle for each segment based on the start angle and the number of segments.
return ImVec2(ImCos(a) * radiusmode(a), ImSin(a) * radiusmode(a));
}, color_alpha(bg, 1.f), thickness);
circle([&] (int i) { // Draw the spinner itself using the `circle` function, with the specified color and thickness.
const float a = start - b + (i * angle / num_segments);
return ImVec2(ImCos(a) * radiusmode(a), ImSin(a) * radiusmode(a));
}, color_alpha(color, 1.f), thickness);
}
inline void SpinnerAng8(const char *label, float radius, float thickness, const ImColor &color = white, const ImColor &bg = white, float speed = 2.8f, float angle = IM_PI, int mode = 0, float rkoef = 0.5f)
{
SPINNER_HEADER(pos, size, centre, num_segments); // Get the position, size, centre, and number of segments of the spinner using the SPINNER_HEADER macro.
float start = (float)ImGui::GetTime() * speed; // The start angle of the spinner is calculated based on the current time and the specified speed.
float b = 0.f, kb = 1.f;
switch (mode) {
case 1: b = damped_gravity(ImSin(start * 1.1f)) * angle; break;
case 2: radius = (0.8f + ImCos(start) * 0.2f) * radius; break;
case 3: b = damped_infinity(start * 1.1f, 1.f).second; break;
case 4: b = ease_outquad(ImSin(start * 1.1f)) * angle; break;
case 5: kb = 2.f; break;
}
auto radiusmode = [radius, mode, kb] (float a, float k) { switch (mode) { case 4: return damped_trifolium(a) * radius; } return radius * k * kb; };
float centerx_save = centre.x;
centre.x = centerx_save + radius * (1.f - rkoef);
circle([&] (int i) { // Draw the spinner itself using the `circle` function, with the specified color and thickness.
const float a = start - b + (i * angle / num_segments);
return ImVec2(ImCos(a) * radiusmode(a, rkoef), ImSin(a) * radiusmode(a, rkoef));
}, color_alpha(color, 1.f), thickness);
centre.x = centerx_save - radius * rkoef;
circle([&] (int i) { // Draw the spinner itself using the `circle` function, with the specified color and thickness.
const float a = start - b + (i * angle / num_segments);
return ImVec2(ImCos(-a) * radiusmode(a, 1.f - rkoef), ImSin(-a) * radiusmode(a, 1.f - rkoef));
}, color_alpha(color, 1.f), thickness);
}
inline void SpinnerAngMix(const char *label, float radius, float thickness, const ImColor &color = white, float speed = 2.8f, float angle = IM_PI, int arcs = 4, int mode = 0)
{
SPINNER_HEADER(pos, size, centre, num_segments); // Get the position, size, centre, and number of segments of the spinner using the SPINNER_HEADER macro.
for (int i = 0; i < arcs; ++i)
{
const float koeff = (1.1f - 1.f / (i+1));
float start = (float)ImGui::GetTime() * speed * koeff; // The start angle of the spinner is calculated based on the current time and the specified speed.
radius = (mode == 2) ? (0.8f + ImCos(start) * 0.2f) * radius : radius;
const float rb = (radius / arcs) * (i + 1);
const float b = (mode == 1) ? damped_gravity(ImSin(start * 1.1f)) * angle : 0.f;
circle([&] (int i) { // Draw the spinner itself using the `circle` function, with the specified color and thickness.
const float a = start - b + (i * angle / num_segments);
return ImVec2(ImCos(a) * rb, ImSin(a) * rb);
}, color_alpha(color, 1.f), thickness);
}
}
inline void SpinnerLoadingRing(const char *label, float radius, float thickness, const ImColor &color = white, const ImColor &bg = half_white, float speed = 2.8f, int segments = 5)
{
SPINNER_HEADER(pos, size, centre, num_segments);
const float start = ImFmod((float)ImGui::GetTime() * speed, IM_PI); // Calculate the starting angle based on the current time and speed
const float bg_angle_offset = PI_2 / num_segments - 1;
num_segments *= 2; // Double the number of segments for the background ringxxxxxxx
circle([&] (int i) {
return ImVec2(ImCos(i * bg_angle_offset) * radius, ImSin(i * bg_angle_offset) * radius); // Draw the background ring
}, color_alpha(bg, 1.f), thickness);
float out_h, out_s, out_v;
ImGui::ColorConvertRGBtoHSV(color.Value.x, color.Value.y, color.Value.z, out_h, out_s, out_v); // Convert the color to HSV for variation in segment colors
const float start_ang = (start < PI_DIV_2) ? 0.f : (start - PI_DIV_2) * 4.f; // Calculate the angles and delta angle for each segment
const float angle_offset = ((start < PI_DIV_2) ? PI_2 : (PI_2 - start_ang)) / segments;
const float delta_angle = (start < PI_DIV_2) ? ImSin(start) * angle_offset : angle_offset;
for (int i = 0; i < segments; ++i) // Draw each segment of the loading ring
{
window->DrawList->PathClear();
const float begin_ang = start_ang - PI_DIV_2 + delta_angle * i;
ImColor c = ImColor::HSV(out_h + i * (1.f / segments * 2.f), out_s, out_v);
window->DrawList->PathArcTo(centre, radius, begin_ang, begin_ang + delta_angle, num_segments);
window->DrawList->PathStroke(color_alpha(c, 1.f), false, thickness);
}
}
inline void SpinnerClock(const char *label, float radius, float thickness, const ImColor &color = white, const ImColor &bg = half_white, float speed = 2.8f)
{
SPINNER_HEADER(pos, size, centre, num_segments);
const float start = (float)ImGui::GetTime() * speed;
const float bg_angle_offset = PI_2 / (num_segments - 1);
circle([&] (int i) { return ImVec2(ImCos(i * bg_angle_offset) * radius, ImSin(i * bg_angle_offset) * radius); }, color_alpha(bg, 1.f), thickness);
window->DrawList->AddLine(centre, ImVec2(centre.x + ImCos(start) * radius, centre.y + ImSin(start) * radius), color_alpha(color, 1.f), thickness * 2);
window->DrawList->AddLine(centre, ImVec2(centre.x + ImCos(start * 0.5f) * radius / 2.f, centre.y + ImSin(start * 0.5f) * radius / 2.f), color_alpha(color, 1.f), thickness * 2);
}
inline void SpinnerPulsar(const char *label, float radius, float thickness, const ImColor &bg = half_white, float speed = 2.8f, bool sequence = true, float angle = 0.f, int mode = 0)
{
SPINNER_HEADER(pos, size, centre, num_segments);
ImGuiStorage* storage = window->DC.StateStorage;
const ImGuiID radiusbId = window->GetID("##radiusb");
float radius_b = storage->GetFloat(radiusbId, 0.8f);
const float start = (float)ImGui::GetTime() * speed;
const float bg_angle_offset = PI_2 / (num_segments - 1);
float start_r = ImFmod(start, PI_DIV_2);
switch (mode) {
case 1: start_r = damped_infinity(start_r, angle).second; break;
}
float radius_k = ImSin(start_r);
float radius1 = radius_k * radius;
circle([&] (int i) {
return ImVec2(ImCos(i * bg_angle_offset) * radius1, ImSin(i * bg_angle_offset) * radius1);
}, color_alpha(bg, 1.f), thickness);
if (sequence) { radius_b -= (0.005f * speed); radius_b = ImMax(radius_k, ImMax(0.8f, radius_b)); }
else { radius_b = (1.f - radius_k); }
storage->SetFloat(radiusbId, radius_b);
float radius_tb = sequence ? ImMax(radius_k, radius_b) * radius : (radius_b * radius);
circle([&] (int i) {
return ImVec2(ImCos(i * bg_angle_offset) * radius_tb, ImSin(i * bg_angle_offset) * radius_tb);
}, color_alpha(bg, 1.f), thickness);
}
inline void SpinnerDoubleFadePulsar(const char *label, float radius, float /*thickness*/, const ImColor &bg = half_white, float speed = 2.8f)
{
SPINNER_HEADER(pos, size, centre, num_segments);
ImGuiStorage* storage = window->DC.StateStorage;
const ImGuiID radiusbId = window->GetID("##radiusb");
float radius_b = storage->GetFloat(radiusbId, 0.8f);
const float start = (float)ImGui::GetTime() * speed;
const float bg_angle_offset = PI_2_DIV(num_segments);
float start_r = ImFmod(start, PI_DIV_2);
float radius_k = ImSin(start_r);
window->DrawList->AddCircleFilled(centre, radius_k * radius, color_alpha(bg, ImMin(0.1f, radius_k)), num_segments);
radius_b = (1.f - radius_k);
storage->SetFloat(radiusbId, radius_b);
window->DrawList->AddCircleFilled(centre, radius_b * radius, color_alpha(bg, ImMin(0.3f, radius_b)), num_segments);
}
inline void SpinnerTwinPulsar(const char *label, float radius, float thickness, const ImColor &color = white, float speed = 2.8f, int rings = 2, int mode = 0)
{
SPINNER_HEADER(pos, size, centre, num_segments);
const float bg_angle_offset = PI_2 / (num_segments - 1);
const float koeff = PI_DIV(2 * rings);
float start = (float)ImGui::GetTime() * speed;
for (int num_ring = 0; num_ring < rings; ++num_ring) {
float radius_k = ImSin(ImFmod(start + (num_ring * koeff), PI_DIV_2));
float radius1 = radius_k * radius;
radius1 += ease((ease_mode)mode, start, radius);
circle([&] (int i) {
const float a = start + (i * bg_angle_offset);
return ImVec2(ImCos(a) * radius1, ImSin(a) * radius1);
}, color_alpha(color, radius_k > 0.5f ? 2.f - (radius_k * 2.f) : color.Value.w), thickness);
}
}
inline void SpinnerFadePulsar(const char *label, float radius, const ImColor &color = white, float speed = 2.8f, int rings = 2, int mode = 0)
{
SPINNER_HEADER(pos, size, centre, num_segments);
const float bg_angle_offset = PI_2_DIV(num_segments);
const float koeff = PI_DIV(2 * rings);
float start = (float)ImGui::GetTime() * speed;
for (int num_ring = 0; num_ring < rings; ++num_ring) {
float radius_k = ImSin(ImFmod(start + (num_ring * koeff), PI_DIV_2));
ImColor c = color_alpha(color, (radius_k > 0.5f) ? (2.f - (radius_k * 2.f)) : color.Value.w);
c.Value.w -= ease((ease_mode)mode, start, c.Value.w);
window->DrawList->AddCircleFilled(centre, radius_k * radius, c, num_segments);
}
}
inline void SpinnerCircularLines(const char *label, float radius, const ImColor &color = white, float speed = 1.8f, int lines = 8, int mode = 0)
{
SPINNER_HEADER(pos, size, centre, num_segments);
auto ghalf_pi = [] (float f) -> float { return ImMin(f, PI_DIV_2); };
const float start = ImFmod((float)ImGui::GetTime() * speed, IM_PI);
const float bg_angle_offset = PI_2_DIV(lines);
for (size_t j = 0; j < 3; ++j)
{
const float start_offset = j * PI_DIV(7.f);
const float rmax = ImMax(ImSin(ghalf_pi(start - start_offset)), 0.3f) * radius;
const float rmin = ImMax(ImSin(ghalf_pi(start - PI_DIV_4 - start_offset)), 0.3f) * radius;
ImColor c = color_alpha(color, 1.f - j * 0.3f);
for (size_t i = 0; i <= lines; i++)
{
float a = (i * bg_angle_offset);
a += ease((ease_mode)mode, start_offset, radius);
window->DrawList->AddLine(ImVec2(centre.x + ImCos(a) * rmin, centre.y + ImSin(a) * rmin),
ImVec2(centre.x + ImCos(a) * rmax, centre.y + ImSin(a) * rmax),
color_alpha(c, 1.f), 1.f);
}
}
}
inline void SpinnerDots(const char *label, float *nextdot, float radius, float thickness, const ImColor &color = white, float speed = 2.8f, size_t dots = 12, float minth = -1.f, int mode = 0)
{
SPINNER_HEADER(pos, size, centre, num_segments);
float start = (float)ImGui::GetTime() * speed;
const float bg_angle_offset = PI_2 / dots;
dots = ImMin(dots, (size_t)32);
const size_t mdots = dots / 2;
float def_nextdot = 0;
float &ref_nextdot = nextdot ? *nextdot : def_nextdot;
if (ref_nextdot < 0.f)
ref_nextdot = (float)dots;
auto radiusmode = [radius, mode, dots] (float a, int i) {
switch (mode) {
case 2: return damped_trifolium(a) * radius;
case 3: return (radius / dots) * i;
} return radius;
};
auto thcorrect = [&thickness, &ref_nextdot, &mdots, &minth] (size_t i) {
const float nth = minth < 0.f ? thickness / 2.f : minth;
return ImMax(nth, ImSin(((i - ref_nextdot) / mdots) * IM_PI) * thickness);
};
switch (mode) {
case 1: start = damped_infinity(start * 1.1f, 1.f).second; break;
case 4: start = ease_outquad(ImSin(ImFmod(start, IM_PI))); break;
case 5: start = ease_inoutexpo(ImSin(ImFmod(start, IM_PI))); break;
}
for (size_t i = 0; i <= dots; i++)
{
float a = start + (i * bg_angle_offset);
a = ImFmod(a, PI_2);
float th = minth < 0 ? thickness / 2.f : minth;
if (ref_nextdot + mdots < dots) {
if (i > ref_nextdot && i < ref_nextdot + mdots)
th = thcorrect(i);
} else {
if ((i > ref_nextdot && i < dots) || (i < ((int)(ref_nextdot + mdots)) % dots))
th = thcorrect(i);
}
window->DrawList->AddCircleFilled(ImVec2(centre.x + ImCos(-a) * radiusmode(a, i), centre.y + ImSin(-a) * radiusmode(a, i)), th, color_alpha(color, 1.f), 8);
}
}
inline void SpinnerVDots(const char *label, float radius, float thickness, const ImColor &color = white, const ImColor &bgcolor = white, float speed = 2.8f, size_t dots = 12, size_t mdots = 6, int mode = 0)
{
SPINNER_HEADER(pos, size, centre, num_segments);
float start = (float)ImGui::GetTime() * speed;
switch (mode) {
case 1: start += ease_inoutquad(ImSin(ImFmod(start, IM_PI))); break;
}
const float bg_angle_offset = PI_2_DIV(dots);
dots = ImMin(dots, (size_t)32);
for (size_t i = 0; i <= dots; i++)
{
float a = ImFmod(start + (i * bg_angle_offset), PI_2);
window->DrawList->AddCircleFilled(ImVec2(centre.x + ImCos(-a) * radius, centre.y + ImSin(-a) * radius), thickness / 2, color_alpha(bgcolor, 1.f), 8);
}
window->DrawList->PathClear();
const float d_ang = (mdots / (float)dots) * PI_2;
const float angle_offset = (d_ang) / dots;
for (size_t i = 0; i < dots; i++)
{
const float a = start + (i * angle_offset);
window->DrawList->PathLineTo(ImVec2(centre.x + ImCos(a) * radius, centre.y + ImSin(a) * radius));
}
window->DrawList->PathStroke(color_alpha(color, 1.f), false, thickness);
}
inline void SpinnerBounceDots(const char *label, float radius, float thickness, const ImColor &color = white, float speed = 2.8f, size_t dots = 3, int mode = 0)
{
SPINNER_HEADER(pos, size, centre, num_segments);
const float nextItemKoeff = 2.5f;
const float heightKoeff = 2.f;
const float heightSpeed = 0.8f;
const float hsize = dots * (thickness * nextItemKoeff) / 2.f - (thickness * nextItemKoeff) * 0.5f;
float start = (float)ImGui::GetTime() * speed;
const float offset = PI_DIV(dots);
for (size_t i = 0; i < dots; i++) {
float a = start + (IM_PI - i * offset);
switch (mode) {
case 1: a = damped_spring(1, 10.f, 1.0f, ImSin(ImFmod(start + i * PI_DIV(dots * 2), PI_2))); break;
case 2: a = damped_infinity((float)(start + i * PI_DIV(dots * 2)), radius).second; break;
}
float y = centre.y + ImSin(a * heightSpeed) * thickness * heightKoeff;
window->DrawList->AddCircleFilled(ImVec2(centre.x - hsize + i * (thickness * nextItemKoeff), ImMin(y, centre.y)), thickness, color_alpha(color, 1.f), 8);
}
}
inline void SpinnerZipDots(const char *label, float radius, float thickness, const ImColor &color = white, float speed = 2.8f, size_t dots = 5)
{
SPINNER_HEADER(pos, size, centre, num_segments);
const float nextItemKoeff = 3.5f;
const float heightKoeff = 2.f;
const float heightSpeed = 0.8f;
const float hsize = dots * (thickness * nextItemKoeff) / 2.f - (thickness * nextItemKoeff) * 0.5f;
const float start = (float)ImGui::GetTime() * speed;
const float offset = PI_DIV(dots);
for (size_t i = 0; i < dots; i++)
{
const float sina = ImSin((start + (IM_PI - i * offset)) * heightSpeed);
const float y = ImMin(centre.y + sina * thickness * heightKoeff, centre.y);
const float deltay = ImAbs(y - centre.y);
window->DrawList->AddCircleFilled(ImVec2(centre.x - hsize + i * (thickness * nextItemKoeff), y), thickness, color_alpha(color, 1.f), 8);
window->DrawList->AddCircleFilled(ImVec2(centre.x - hsize + i * (thickness * nextItemKoeff), y + 2 * deltay), thickness, color_alpha(color, 1.f), 8);
}
}
inline void SpinnerDotsToPoints(const char *label, float radius, float thickness, float offset_k, const ImColor &color = white, float speed = 1.8f, size_t dots = 5)
{
SPINNER_HEADER(pos, size, centre, num_segments);
const float nextItemKoeff = 3.5f;
const float hsize = dots * (thickness * nextItemKoeff) / 2.f - (thickness * nextItemKoeff) * 0.5f;
const float start = ImFmod((float)ImGui::GetTime() * speed, PI_2);
const float offset = PI_DIV(dots);
float out_h, out_s, out_v;
ImGui::ColorConvertRGBtoHSV(color.Value.x, color.Value.y, color.Value.z, out_h, out_s, out_v);
if (start < PI_DIV_2) {
const float sina = ImSin(start);
for (size_t i = 0; i < dots; i++) {
const float xx = ImMax(sina * (i * (thickness * nextItemKoeff)), 0.f);
ImColor c = color_alpha(ImColor::HSV(out_h + i * ((1.f / dots) * 2.f), out_s, out_v), 1.f);
window->DrawList->AddCircleFilled(ImVec2(centre.x - hsize + xx, centre.y), thickness, c, 8);
}
} else {
for (size_t i = 0; i < dots; i++) {
const float sina = ImSin(ImMax(start - (IM_PI / dots) * i, PI_DIV_2));
const float xx = ImMax(1.f * (i * (thickness * nextItemKoeff)), 0.f);
const float th = sina * thickness;
ImColor c = color_alpha(ImColor::HSV(out_h + i * ((1.f / dots) * 2.f), out_s, out_v), 1.f);
window->DrawList->AddCircleFilled(ImVec2(centre.x - hsize + xx, centre.y), th, c, 8);
}
}
}
//const float sina = ImSin( ImFmod((start + (IM_PI - i * offset)), PI_DIV_2));
inline void SpinnerDotsToBar(const char *label, float radius, float thickness, float offset_k, const ImColor &color = white, float speed = 2.8f, size_t dots = 5)
{
SPINNER_HEADER(pos, size, centre, num_segments);
const float nextItemKoeff = 3.5f;
const float heightSpeed = 0.8f;
const float hsize = dots * (thickness * nextItemKoeff) / 2.f - (thickness * nextItemKoeff) * 0.5f;
const float start = (float)ImGui::GetTime() * speed;
const float offset = PI_DIV(dots);
const float hradius = (radius);
float out_h, out_s, out_v;
ImGui::ColorConvertRGBtoHSV(color.Value.x, color.Value.y, color.Value.z, out_h, out_s, out_v);
for (size_t i = 0; i < dots; i++)
{
const float sina = ImSin((start + (IM_PI - i * offset)) * heightSpeed);
const float sinb = ImSin((start + (IM_PI + IM_PI * offset_k - i * offset)) * heightSpeed);
const float y = ImMin(centre.y + sina * hradius, centre.y);
const float y2 = ImMin(sinb, 0.f) * (hradius * offset_k);
const float y3 = (y + y2);
const float deltay = ImAbs(y - centre.y);
ImColor c = color_alpha(ImColor::HSV(out_h + i * ((1.f / dots) * 2.f), out_s, out_v), 1.f);
ImVec2 p1(centre.x - hsize + i * (thickness * nextItemKoeff), y3);
ImVec2 p2(centre.x - hsize + i * (thickness * nextItemKoeff), y3 + 2 * deltay);
window->DrawList->AddCircleFilled(p1, thickness, c, 8);
window->DrawList->AddCircleFilled(p2, thickness, c, 8);
window->DrawList->AddLine(p1, p2, c, thickness * 2.f);
}
}
inline void SpinnerWaveDots(const char *label, float radius, float thickness, const ImColor &color = white, float speed = 2.8f, int lt = 8)
{
SPINNER_HEADER(pos, size, centre, num_segments);
const float nextItemKoeff = 2.5f;
const float dots = (size.x / (thickness * nextItemKoeff));
const float offset = PI_DIV(dots);
const float start = (float)ImGui::GetTime() * speed;
float out_h, out_s, out_v;
ImGui::ColorConvertRGBtoHSV(color.Value.x, color.Value.y, color.Value.z, out_h, out_s, out_v);
for (size_t i = 0; i < dots; i++)
{
float a = start + (IM_PI - i * offset);
float y = centre.y + ImSin(a) * (size.y / 2.f);
ImColor c = ImColor::HSV(out_h + i * (1.f / dots * 2.f), out_s, out_v);
window->DrawList->AddCircleFilled(ImVec2(centre.x - (size.x / 2.f) + i * thickness * nextItemKoeff, y), thickness, color_alpha(c, 1.f), lt);
}
}
inline void SpinnerFadeDots(const char *label, float radius, float thickness, const ImColor &color = white, float speed = 2.8f, int lt = 8, int mode = 0)
{
SPINNER_HEADER(pos, size, centre, num_segments);
const float start = (float)ImGui::GetTime() * speed;
const float nextItemKoeff = 2.5f;
const float dots = (size.x / (thickness * nextItemKoeff));
const float heightSpeed = 0.8f;
for (size_t i = 0; i < dots; i++)
{
float a = mode
? damped_spring(1, 10.f, 1.0f, ImSin(ImFmod(start + (IM_PI - i * (IM_PI / dots)), PI_2)))
: ImSin(start + (IM_PI - i * (IM_PI / dots)) * heightSpeed);
window->DrawList->AddCircleFilled(ImVec2(centre.x - (size.x / 2.f) + i * thickness * nextItemKoeff, centre.y), thickness, color_alpha(color, ImMax(0.1f, a)), lt);
}
}
inline void SpinnerThreeDots(const char *label, float radius, float thickness, const ImColor &color = white, float speed = 2.8f, int lt = 8)
{
SPINNER_HEADER(pos, size, centre, num_segments);
const float start = ImFmod((float)ImGui::GetTime() * speed, PI_2);
const float nextItemKoeff = 2.5f;
const float offset = size.x / 4.f;
float ab = start;
int msize = 2;
if (start < IM_PI) { ab = 0; msize = 1; }
for (size_t i = 0; i < msize; i++)
{
float a = ab + i * IM_PI - PI_DIV_2;
window->DrawList->AddCircleFilled(ImVec2(centre.x - offset + ImSin(a) * offset, centre.y + ImCos(a) * offset), thickness, color_alpha(color, 1.f), lt);
}
float ba = start; msize = 2;
if (start > IM_PI && start < PI_2) { ba = 0; msize = 1; }
for (size_t i = 0; i < msize; i++)
{
float a = -ba + i * IM_PI + PI_DIV_2;
window->DrawList->AddCircleFilled(ImVec2(centre.x + offset + ImSin(a) * offset, centre.y + ImCos(a) * offset), thickness, color_alpha(color, 1.f), lt);
}
}
inline void SpinnerFiveDots(const char *label, float radius, float thickness, const ImColor &color = 0xffffffff, float speed = 2.8f, int lt = 8)
{
SPINNER_HEADER(pos, size, centre, num_segments);
const float start = ImFmod((float)ImGui::GetTime() * speed, PI_2 * 2);
const float nextItemKoeff = 2.5f;
const float offset = size.x / 4.f;
float ab = 0;
int msize = 1;
if (start < IM_PI) { ab = start; msize = 2; }
for (size_t i = 0; i < msize; i++)
{
float a = -ab + i * IM_PI - PI_DIV_2;
window->DrawList->AddCircleFilled(ImVec2(centre.x - offset + ImSin(a) * offset, centre.y + ImCos(a) * offset), thickness, color_alpha(color, 1.f), lt);
}
float ba = 0; msize = 1;
if (start > IM_PI && start < PI_2) { ba = start; msize = 2; }
for (size_t i = 0; i < msize; i++)
{
float a = -ba + i * IM_PI;
window->DrawList->AddCircleFilled(ImVec2(centre.x + ImSin(a) * offset, centre.y + offset + ImCos(a) * offset), thickness, color_alpha(color, 1.f), lt);
}
float bc = 0; msize = 1;
if (start > PI_2 && start < IM_PI * 3) { bc = start; msize = 2; }
for (size_t i = 0; i < msize; i++)
{
float a = -bc + i * IM_PI - IM_PI;
window->DrawList->AddCircleFilled(ImVec2(centre.x + ImSin(a) * offset, centre.y - offset + ImCos(a) * offset), thickness, color_alpha(color, 1.f), lt);
}
float bd = 0; msize = 1;
if (start > IM_PI * 3 && start < IM_PI * 4) { bd = start; msize = 2; }
for (size_t i = 0; i < msize; i++)
{
float a = -bd + i * IM_PI + PI_DIV_2;
window->DrawList->AddCircleFilled(ImVec2(centre.x + offset + ImSin(a) * offset, centre.y + ImCos(a) * offset), thickness, color_alpha(color, 1.f), lt);
}
}
inline void Spinner4Caleidospcope(const char *label, float radius, float thickness, const ImColor &color = 0xffffffff, float speed = 2.8f, int lt = 8)
{
SPINNER_HEADER(pos, size, centre, num_segments);
const float start = ImFmod((float)ImGui::GetTime() * speed, PI_2);
const float nextItemKoeff = 2.5f;
const float offset = size.x / 4.f;
float ab = start;
int msize = 2;
float out_h, out_s, out_v;
ImGui::ColorConvertRGBtoHSV(color.Value.x, color.Value.y, color.Value.z, out_h, out_s, out_v);
for (size_t i = 0; i < msize; i++)
{
float a = ab - i * IM_PI;
ImColor c = color_alpha(ImColor::HSV(out_h + (0.1f * i), out_s, out_v, 0.7f), 1.f);
window->DrawList->AddCircleFilled(ImVec2(centre.x - offset + ImSin(a) * offset, centre.y + ImCos(a) * offset), thickness, c, lt);
}
for (size_t i = 0; i < msize; i++)
{
float a = ab + i * IM_PI + PI_DIV_2;
ImColor c = color_alpha(ImColor::HSV(out_h + 0.2f + (0.1f * i), out_s, out_v, 0.7f), 1.f);
window->DrawList->AddCircleFilled(ImVec2(centre.x + ImSin(a) * offset, centre.y - offset + ImCos(a) * offset), thickness, c, lt);
}
float ba = start; msize = 2;
for (size_t i = 0; i < msize; i++)
{
float a = -ba + i * IM_PI + PI_DIV_2;
ImColor c = color_alpha(ImColor::HSV(out_h + 0.4f + (0.1f * i), out_s, out_v, 0.7f), 1.f);
window->DrawList->AddCircleFilled(ImVec2(centre.x + offset + ImSin(a) * offset, centre.y + ImCos(a) * offset), thickness, c, lt);
}
for (size_t i = 0; i < msize; i++)
{
float a = ab - i * IM_PI + PI_DIV_4;
ImColor c = color_alpha(ImColor::HSV(out_h + 0.6f + (0.1f * i), out_s, out_v, 0.7f), 1.f);
window->DrawList->AddCircleFilled(ImVec2(centre.x + ImSin(a) * offset, centre.y + offset + ImCos(a) * offset), thickness, c, lt);
}
}
inline void SpinnerMultiFadeDots(const char *label, float radius, float thickness, const ImColor &color = white, float speed = 2.8f, int lt = 8)
{
SPINNER_HEADER(pos, size, centre, num_segments);
const float start = (float)ImGui::GetTime() * speed;
const float nextItemKoeff = 2.5f;
const float dots = (size.x / (thickness * nextItemKoeff));
const float heightSpeed = 0.8f;
for (size_t j = 0; j < dots; j++)
{
for (size_t i = 0; i < dots; i++)
{
float a = start - (IM_PI - i * j * PI_DIV(dots));
window->DrawList->AddCircleFilled(ImVec2(centre.x - (size.x / 2.f) + i * thickness * nextItemKoeff, centre.y - (size.y / 2.f) + j * thickness * nextItemKoeff), thickness, color_alpha(color, ImMax(0.1f, ImSin(a * heightSpeed))), lt);
}
}
}
inline void SpinnerScaleDots(const char *label, float radius, float thickness, const ImColor &color = white, float speed = 2.8f, int lt = 8)
{
SPINNER_HEADER(pos, size, centre, num_segments);
const float nextItemKoeff = 2.5f;
const float heightSpeed = 0.8f;
const float dots = (size.x / (thickness * nextItemKoeff));
const float start = (float)ImGui::GetTime() * speed;
for (size_t i = 0; i < dots; i++)
{
const float a = start + (IM_PI - i * PI_DIV(dots));
const float th = thickness * ImSin(a * heightSpeed);
window->DrawList->AddCircleFilled(ImVec2(centre.x - (size.x / 2.f) + i * thickness * nextItemKoeff, centre.y), thickness, color_alpha(color, 0.1f), lt);
window->DrawList->AddCircleFilled(ImVec2(centre.x - (size.x / 2.f) + i * thickness * nextItemKoeff, centre.y), th, color_alpha(color, 1.f), lt);
}
}
inline void SpinnerSquareSpins(const char *label, float radius, float thickness, const ImColor &color = white, float speed = 2.8f)
{
SPINNER_HEADER(pos, size, centre, num_segments);
const float nextItemKoeff = 2.5f;
const float heightSpeed = 0.8f;
const float dots = (size.x / (thickness * nextItemKoeff));
const float start = (float)ImGui::GetTime() * speed;
for (size_t i = 0; i < dots; i++)
{
const float a = ImFmod(start + i * ((PI_DIV_2 * 0.7f) / dots), PI_DIV_2);
const float th = thickness * (ImCos(a * heightSpeed) * 2.f);
ImVec2 pmin = ImVec2(centre.x - (size.x / 2.f) + i * thickness * nextItemKoeff - thickness, centre.y - thickness);
ImVec2 pmax = ImVec2(centre.x - (size.x / 2.f) + i * thickness * nextItemKoeff + thickness, centre.y + thickness);
window->DrawList->AddRect(pmin, pmax, color_alpha(color, 1.f), 0.f);
ImVec2 lmin = ImVec2(centre.x - (size.x / 2.f) + i * thickness * nextItemKoeff - thickness, centre.y - th + thickness);
ImVec2 lmax = ImVec2(centre.x - (size.x / 2.f) + i * thickness * nextItemKoeff + thickness - 1, centre.y - th + thickness);
window->DrawList->AddLine(lmin, lmax, color_alpha(color, 1.f), 1.f);
}
}
inline void SpinnerMovingDots(const char *label, float radius, float thickness, const ImColor &color = white, float speed = 2.8f, size_t dots = 3)
{
SPINNER_HEADER(pos, size, centre, num_segments);
const float nextItemKoeff = 2.5f;
const float heightKoeff = 2.f;
const float heightSpeed = 0.8f;
const float start = ImFmod((float)ImGui::GetTime() * speed, size.x);
float offset = 0;
for (size_t i = 0; i < dots; i++)
{
float th = thickness;
offset = ImFmod(start + i * (size.x / dots), size.x);
if (offset < thickness) { th = offset; }
if (offset > size.x - thickness) { th = size.x - offset; }
window->DrawList->AddCircleFilled(ImVec2(pos.x + offset - thickness, centre.y), th, color_alpha(color, 1.f), 8);
}
}
inline void SpinnerRotateDots(const char *label, float radius, float thickness, const ImColor &color = white, float speed = 2.8f, int dots = 2, int mode = 0)
{
SPINNER_HEADER(pos, size, centre, num_segments);
ImGuiStorage* storage = window->DC.StateStorage;
const ImGuiID velocityId = window->GetID("##velocity");
const ImGuiID vtimeId = window->GetID("##velocitytime");
float velocity = storage->GetFloat(velocityId, 0.f);
float vtime = storage->GetFloat(vtimeId, 0.f);
float dtime = ImFmod((float)vtime, IM_PI);
float start = (vtime += velocity);
if (dtime > 0.f && dtime < PI_DIV_2) { velocity += 0.001f * speed; }
else if (dtime > IM_PI * 0.9f && dtime < IM_PI) { velocity -= 0.01f * speed; }
if (velocity > 0.1f) velocity = 0.1f;