-
Notifications
You must be signed in to change notification settings - Fork 399
/
Copy pathimgui_user.inl
930 lines (756 loc) · 28.8 KB
/
imgui_user.inl
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
#include "imgui.h"
#include "imgui_internal.h"
static const float NODE_SLOT_RADIUS = 4.0f;
namespace ImGui
{
bool ToolbarButton(ImTextureID texture, const ImVec4& bg_color, const char* tooltip)
{
auto frame_padding = GetStyle().FramePadding;
PushStyleColor(ImGuiCol_FrameBg, ImVec4(0, 0, 0, 0));
PushStyleColor(ImGuiCol_Button, ImVec4(0, 0, 0, 0));
PushStyleColor(ImGuiCol_ButtonActive, ImVec4(0, 0, 0, 0));
PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(0, 0));
PushStyleVar(ImGuiStyleVar_WindowPadding, frame_padding);
PushStyleVar(ImGuiStyleVar_WindowRounding, 0);
bool ret = false;
SameLine();
ImVec4 tint_color = GetStyle().Colors[ImGuiCol_Text];
if (ImageButton(texture, ImVec2(24, 24), ImVec2(0, 0), ImVec2(1, 1), -1, bg_color, tint_color))
{
ret = true;
}
if (IsItemHovered())
{
SetTooltip("%s", tooltip);
}
PopStyleColor(3);
PopStyleVar(3);
return ret;
}
bool BeginToolbar(const char* str_id, ImVec2 screen_pos, ImVec2 size)
{
bool is_global = GImGui->CurrentWindowStack.Size == 1;
SetNextWindowPos(screen_pos);
ImVec2 frame_padding = GetStyle().FramePadding;
PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(0, 0));
PushStyleVar(ImGuiStyleVar_WindowPadding, frame_padding);
PushStyleVar(ImGuiStyleVar_WindowRounding, 0);
float padding = frame_padding.y * 2;
ImGuiWindowFlags flags = ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize |
ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoSavedSettings;
if (size.x == 0) size.x = GetContentRegionAvailWidth();
SetNextWindowSize(size);
bool ret = is_global ? Begin(str_id, nullptr, size, -1, flags) : BeginChild(str_id, size, false, flags);
PopStyleVar(3);
return ret;
}
void EndToolbar()
{
auto frame_padding = GetStyle().FramePadding;
PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(0, 0));
PushStyleVar(ImGuiStyleVar_WindowPadding, frame_padding);
PushStyleVar(ImGuiStyleVar_WindowRounding, 0);
ImVec2 pos = GetWindowPos();
ImVec2 size = GetWindowSize();
if (GImGui->CurrentWindowStack.Size == 2) End(); else EndChild();
PopStyleVar(3);
ImGuiWindow* win = GetCurrentWindowRead();
if(GImGui->CurrentWindowStack.Size > 1) SetCursorScreenPos(pos + ImVec2(0, size.y + GetStyle().FramePadding.y * 2));
}
void ResetActiveID()
{
SetActiveID(0);
}
int PlotHistogramEx(const char* label,
float (*values_getter)(void* data, int idx),
void* data,
int values_count,
int values_offset,
const char* overlay_text,
float scale_min,
float scale_max,
ImVec2 graph_size,
int selected_index)
{
ImGuiWindow* window = GetCurrentWindow();
if (window->SkipItems) return -1;
ImGuiContext& g = *GImGui;
const ImGuiStyle& style = g.Style;
const ImVec2 label_size = CalcTextSize(label, NULL, true);
if (graph_size.x == 0.0f) graph_size.x = CalcItemWidth() + (style.FramePadding.x * 2);
if (graph_size.y == 0.0f) graph_size.y = label_size.y + (style.FramePadding.y * 2);
const ImRect frame_bb(
window->DC.CursorPos, window->DC.CursorPos + ImVec2(graph_size.x, graph_size.y));
const ImRect inner_bb(frame_bb.Min + style.FramePadding, frame_bb.Max - style.FramePadding);
const ImRect total_bb(frame_bb.Min,
frame_bb.Max +
ImVec2(label_size.x > 0.0f ? style.ItemInnerSpacing.x + label_size.x : 0.0f, 0));
ItemSize(total_bb, style.FramePadding.y);
if (!ItemAdd(total_bb, NULL)) return -1;
// Determine scale from values if not specified
if (scale_min == FLT_MAX || scale_max == FLT_MAX)
{
float v_min = FLT_MAX;
float v_max = -FLT_MAX;
for (int i = 0; i < values_count; i++)
{
const float v = values_getter(data, i);
v_min = ImMin(v_min, v);
v_max = ImMax(v_max, v);
}
if (scale_min == FLT_MAX) scale_min = v_min;
if (scale_max == FLT_MAX) scale_max = v_max;
}
RenderFrame(
frame_bb.Min, frame_bb.Max, GetColorU32(ImGuiCol_FrameBg), true, style.FrameRounding);
int res_w = ImMin((int)graph_size.x, values_count);
// Tooltip on hover
int v_hovered = -1;
if (IsHovered(inner_bb, 0))
{
const float t = ImClamp(
(g.IO.MousePos.x - inner_bb.Min.x) / (inner_bb.Max.x - inner_bb.Min.x), 0.0f, 0.9999f);
const int v_idx = (int)(t * (values_count + 0));
IM_ASSERT(v_idx >= 0 && v_idx < values_count);
const float v0 = values_getter(data, (v_idx + values_offset) % values_count);
SetTooltip("%d: %8.4g", v_idx, v0);
v_hovered = v_idx;
}
const float t_step = 1.0f / (float)res_w;
float v0 = values_getter(data, (0 + values_offset) % values_count);
float t0 = 0.0f;
ImVec2 p0 = ImVec2(t0, 1.0f - ImSaturate((v0 - scale_min) / (scale_max - scale_min)));
const ImU32 col_base = GetColorU32(ImGuiCol_PlotHistogram);
const ImU32 col_hovered = GetColorU32(ImGuiCol_PlotHistogramHovered);
for (int n = 0; n < res_w; n++)
{
const float t1 = t0 + t_step;
const int v_idx = (int)(t0 * values_count + 0.5f);
IM_ASSERT(v_idx >= 0 && v_idx < values_count);
const float v1 = values_getter(data, (v_idx + values_offset + 1) % values_count);
const ImVec2 p1 = ImVec2(t1, 1.0f - ImSaturate((v1 - scale_min) / (scale_max - scale_min)));
window->DrawList->AddRectFilled(ImLerp(inner_bb.Min, inner_bb.Max, p0),
ImLerp(inner_bb.Min, inner_bb.Max, ImVec2(p1.x, 1.0f)) + ImVec2(-1, 0),
selected_index == v_idx ? col_hovered : col_base);
t0 = t1;
p0 = p1;
}
if (overlay_text)
{
RenderTextClipped(ImVec2(frame_bb.Min.x, frame_bb.Min.y + style.FramePadding.y),
frame_bb.Max,
overlay_text,
NULL,
NULL,
ImGuiAlign_Center);
}
RenderText(ImVec2(frame_bb.Max.x + style.ItemInnerSpacing.x, inner_bb.Min.y), label);
if (v_hovered >= 0 && IsMouseClicked(0))
{
return v_hovered;
}
return -1;
}
bool ColorPicker(float* col, bool alphabar)
{
// https://github.com/ocornut/imgui/issues/346
const float EDGE_SIZE = 200;
const ImVec2 SV_PICKER_SIZE = ImVec2(EDGE_SIZE, EDGE_SIZE);
const float SPACING = GetStyle().ItemInnerSpacing.x;
const float HUE_PICKER_WIDTH = 20.f;
const float CROSSHAIR_SIZE = 7.0f;
bool value_changed = false;
ImDrawList* draw_list = GetWindowDrawList();
ImVec2 picker_pos = GetCursorScreenPos();
float hue, saturation, value;
ColorConvertRGBtoHSV(col[0], col[1], col[2], hue, saturation, value);
ImColor colors[] = { ImColor(255, 0, 0),
ImColor(255, 255, 0),
ImColor(0, 255, 0),
ImColor(0, 255, 255),
ImColor(0, 0, 255),
ImColor(255, 0, 255),
ImColor(255, 0, 0) };
for (int i = 0; i < 6; ++i)
{
draw_list->AddRectFilledMultiColor(ImVec2(picker_pos.x + SV_PICKER_SIZE.x + SPACING,
picker_pos.y + i * (SV_PICKER_SIZE.y / 6)),
ImVec2(picker_pos.x + SV_PICKER_SIZE.x + SPACING + HUE_PICKER_WIDTH,
picker_pos.y + (i + 1) * (SV_PICKER_SIZE.y / 6)),
colors[i],
colors[i],
colors[i + 1],
colors[i + 1]);
}
draw_list->AddLine(ImVec2(picker_pos.x + SV_PICKER_SIZE.x + SPACING - 2,
picker_pos.y + hue * SV_PICKER_SIZE.y),
ImVec2(picker_pos.x + SV_PICKER_SIZE.x + SPACING + 2 + HUE_PICKER_WIDTH,
picker_pos.y + hue * SV_PICKER_SIZE.y),
ImColor(255, 255, 255));
if (alphabar)
{
float alpha = col[3];
draw_list->AddRectFilledMultiColor(
ImVec2(picker_pos.x + SV_PICKER_SIZE.x + 2 * SPACING + HUE_PICKER_WIDTH, picker_pos.y),
ImVec2(picker_pos.x + SV_PICKER_SIZE.x + 2 * SPACING + 2 * HUE_PICKER_WIDTH,
picker_pos.y + SV_PICKER_SIZE.y),
ImColor(0, 0, 0),
ImColor(0, 0, 0),
ImColor(255, 255, 255),
ImColor(255, 255, 255));
draw_list->AddLine(
ImVec2(picker_pos.x + SV_PICKER_SIZE.x + 2 * (SPACING - 2) + HUE_PICKER_WIDTH,
picker_pos.y + alpha * SV_PICKER_SIZE.y),
ImVec2(picker_pos.x + SV_PICKER_SIZE.x + 2 * (SPACING + 2) + 2 * HUE_PICKER_WIDTH,
picker_pos.y + alpha * SV_PICKER_SIZE.y),
ImColor(255.f - alpha, 255.f, 255.f));
}
ImVec4 cHueValue(1, 1, 1, 1);
ColorConvertHSVtoRGB(hue, 1, 1, cHueValue.x, cHueValue.y, cHueValue.z);
ImU32 oHueColor = ColorConvertFloat4ToU32(cHueValue);
draw_list->AddRectFilledMultiColor(ImVec2(picker_pos.x, picker_pos.y),
ImVec2(picker_pos.x + SV_PICKER_SIZE.x, picker_pos.y + SV_PICKER_SIZE.y),
0xffffFFFF,
oHueColor,
oHueColor,
0xffFFffFF);
draw_list->AddRectFilledMultiColor(ImVec2(picker_pos.x, picker_pos.y),
ImVec2(picker_pos.x + SV_PICKER_SIZE.x, picker_pos.y + SV_PICKER_SIZE.y),
0x00000000,
0x00000000,
0xff000000,
0xff000000);
float x = saturation * SV_PICKER_SIZE.x;
float y = (1 - value) * SV_PICKER_SIZE.y;
ImVec2 p(picker_pos.x + x, picker_pos.y + y);
draw_list->AddLine(
ImVec2(p.x - CROSSHAIR_SIZE, p.y), ImVec2(p.x - 2, p.y), ImColor(255, 255, 255));
draw_list->AddLine(
ImVec2(p.x + CROSSHAIR_SIZE, p.y), ImVec2(p.x + 2, p.y), ImColor(255, 255, 255));
draw_list->AddLine(
ImVec2(p.x, p.y + CROSSHAIR_SIZE), ImVec2(p.x, p.y + 2), ImColor(255, 255, 255));
draw_list->AddLine(
ImVec2(p.x, p.y - CROSSHAIR_SIZE), ImVec2(p.x, p.y - 2), ImColor(255, 255, 255));
InvisibleButton("saturation_value_selector", SV_PICKER_SIZE);
if (IsItemActive() && GetIO().MouseDown[0])
{
ImVec2 mouse_pos_in_canvas = ImVec2(
GetIO().MousePos.x - picker_pos.x, GetIO().MousePos.y - picker_pos.y);
if (mouse_pos_in_canvas.x < 0)
mouse_pos_in_canvas.x = 0;
else if (mouse_pos_in_canvas.x >= SV_PICKER_SIZE.x - 1)
mouse_pos_in_canvas.x = SV_PICKER_SIZE.x - 1;
if (mouse_pos_in_canvas.y < 0)
mouse_pos_in_canvas.y = 0;
else if (mouse_pos_in_canvas.y >= SV_PICKER_SIZE.y - 1)
mouse_pos_in_canvas.y = SV_PICKER_SIZE.y - 1;
value = 1 - (mouse_pos_in_canvas.y / (SV_PICKER_SIZE.y - 1));
saturation = mouse_pos_in_canvas.x / (SV_PICKER_SIZE.x - 1);
value_changed = true;
}
SetCursorScreenPos(ImVec2(picker_pos.x + SPACING + SV_PICKER_SIZE.x, picker_pos.y));
InvisibleButton("hue_selector", ImVec2(HUE_PICKER_WIDTH, SV_PICKER_SIZE.y));
if (GetIO().MouseDown[0] && (IsItemHovered() || IsItemActive()))
{
ImVec2 mouse_pos_in_canvas = ImVec2(
GetIO().MousePos.x - picker_pos.x, GetIO().MousePos.y - picker_pos.y);
if (mouse_pos_in_canvas.y < 0)
mouse_pos_in_canvas.y = 0;
else if (mouse_pos_in_canvas.y >= SV_PICKER_SIZE.y - 1)
mouse_pos_in_canvas.y = SV_PICKER_SIZE.y - 1;
hue = mouse_pos_in_canvas.y / (SV_PICKER_SIZE.y - 1);
value_changed = true;
}
if (alphabar)
{
SetCursorScreenPos(
ImVec2(picker_pos.x + SPACING * 2 + HUE_PICKER_WIDTH + SV_PICKER_SIZE.x, picker_pos.y));
InvisibleButton("alpha_selector", ImVec2(HUE_PICKER_WIDTH, SV_PICKER_SIZE.y));
if (GetIO().MouseDown[0] && (IsItemHovered() || IsItemActive()))
{
ImVec2 mouse_pos_in_canvas = ImVec2(
GetIO().MousePos.x - picker_pos.x, GetIO().MousePos.y - picker_pos.y);
if (mouse_pos_in_canvas.y < 0)
mouse_pos_in_canvas.y = 0;
else if (mouse_pos_in_canvas.y >= SV_PICKER_SIZE.y - 1)
mouse_pos_in_canvas.y = SV_PICKER_SIZE.y - 1;
float alpha = mouse_pos_in_canvas.y / (SV_PICKER_SIZE.y - 1);
col[3] = alpha;
value_changed = true;
}
}
ImColor color = ImColor::HSV(hue >= 1 ? hue - 10 * 1e-6f : hue,
saturation > 0 ? saturation : 10 * 1e-6f,
value > 0 ? value : 1e-6f);
col[0] = color.Value.x;
col[1] = color.Value.y;
col[2] = color.Value.z;
bool widget_used;
PushItemWidth((alphabar ? SPACING + HUE_PICKER_WIDTH : 0) + SV_PICKER_SIZE.x + SPACING +
HUE_PICKER_WIDTH - 2 * GetStyle().FramePadding.x);
widget_used = alphabar ? ColorEdit4("", col) : ColorEdit3("", col);
PopItemWidth();
float new_hue, new_sat, new_val;
ColorConvertRGBtoHSV(col[0], col[1], col[2], new_hue, new_sat, new_val);
if (new_hue <= 0 && hue > 0)
{
if (new_val <= 0 && value != new_val)
{
color = ImColor::HSV(hue, saturation, new_val <= 0 ? value * 0.5f : new_val);
col[0] = color.Value.x;
col[1] = color.Value.y;
col[2] = color.Value.z;
}
else if (new_sat <= 0)
{
color = ImColor::HSV(hue, new_sat <= 0 ? saturation * 0.5f : new_sat, new_val);
col[0] = color.Value.x;
col[1] = color.Value.y;
col[2] = color.Value.z;
}
}
return value_changed | widget_used;
}
bool ListBox(const char* label,
int* current_item,
int scroll_to_item,
bool (*items_getter)(void*, int, const char**),
void* data,
int items_count,
int height_in_items)
{
if (!ListBoxHeader(label, items_count, height_in_items)) return false;
// Assume all items have even height (= 1 line of text). If you need items of different or
// variable sizes you can create a custom version of ListBox() in your code without using the
// clipper.
bool value_changed = false;
if (scroll_to_item != -1)
{
SetScrollY(scroll_to_item * GetTextLineHeightWithSpacing());
}
ImGuiListClipper clipper(items_count, GetTextLineHeightWithSpacing());
for (int i = clipper.DisplayStart; i < clipper.DisplayEnd; i++)
{
const bool item_selected = (i == *current_item);
const char* item_text;
if (!items_getter(data, i, &item_text)) item_text = "*Unknown item*";
PushID(i);
if (Selectable(item_text, item_selected))
{
*current_item = i;
value_changed = true;
}
PopID();
}
clipper.End();
ListBoxFooter();
return value_changed;
}
void BringToFront()
{
ImGuiContext& g = *GImGui;
ImGuiWindow* window = GImGui->CurrentWindow;
if ((window->Flags & ImGuiWindowFlags_NoBringToFrontOnFocus) || g.Windows.back() == window)
{
return;
}
for (int i = 0; i < g.Windows.Size; i++)
{
if (g.Windows[i] == window)
{
g.Windows.erase(g.Windows.begin() + i);
break;
}
}
g.Windows.push_back(window);
}
static ImVec2 node_pos;
static ImGuiID last_node_id;
void BeginNode(ImGuiID id, ImVec2 screen_pos)
{
PushID(id);
last_node_id = id;
node_pos = screen_pos;
SetCursorScreenPos(screen_pos + GetStyle().WindowPadding);
PushItemWidth(150);
ImDrawList* draw_list = GetWindowDrawList();
draw_list->ChannelsSplit(2);
draw_list->ChannelsSetCurrent(1);
BeginGroup();
}
void EndNode(ImVec2& pos)
{
ImDrawList* draw_list = GetWindowDrawList();
SameLine();
float width = GetCursorScreenPos().x - node_pos.x;
EndGroup();
PopItemWidth();
float height = GetCursorScreenPos().y - node_pos.y;
ImVec2 size(width + GetStyle().WindowPadding.x, height + GetStyle().WindowPadding.y);
SetCursorScreenPos(node_pos);
SetNextWindowPos(node_pos);
SetNextWindowSize(size);
BeginChild((ImGuiID)last_node_id, size, false, ImGuiWindowFlags_NoInputs);
EndChild();
SetCursorScreenPos(node_pos);
InvisibleButton("bg", size);
if (IsItemActive() && IsMouseDragging(0))
{
pos += GetIO().MouseDelta;
}
draw_list->ChannelsSetCurrent(0);
draw_list->AddRectFilled(node_pos, node_pos + size, ImColor(230, 230, 230), 4.0f);
draw_list->AddRect(node_pos, node_pos + size, ImColor(150, 150, 150), 4.0f);
PopID();
draw_list->ChannelsMerge();
}
ImVec2 GetNodeInputPos(ImGuiID id, int input)
{
PushID(id);
ImGuiWindow* parent_win = GetCurrentWindow();
char title[256];
ImFormatString(title, IM_ARRAYSIZE(title), "%s.child_%08x", parent_win->Name, id);
ImGuiWindow* win = FindWindowByName(title);
if (!win)
{
PopID();
return ImVec2(0, 0);
}
ImVec2 pos = win->Pos;
pos.x -= NODE_SLOT_RADIUS;
ImGuiStyle& style = GetStyle();
pos.y += (GetTextLineHeight() + style.ItemSpacing.y) * input;
pos.y += style.WindowPadding.y + GetTextLineHeight() * 0.5f;
PopID();
return pos;
}
ImVec2 GetNodeOutputPos(ImGuiID id, int output)
{
PushID(id);
ImGuiWindow* parent_win = GetCurrentWindow();
char title[256];
ImFormatString(title, IM_ARRAYSIZE(title), "%s.child_%08x", parent_win->Name, id);
ImGuiWindow* win = FindWindowByName(title);
if (!win)
{
PopID();
return ImVec2(0, 0);
}
ImVec2 pos = win->Pos;
pos.x += win->Size.x + NODE_SLOT_RADIUS;
ImGuiStyle& style = GetStyle();
pos.y += (GetTextLineHeight() + style.ItemSpacing.y) * output;
pos.y += style.WindowPadding.y + GetTextLineHeight() * 0.5f;
PopID();
return pos;
}
bool NodePin(ImGuiID id, ImVec2 screen_pos)
{
ImDrawList* draw_list = GetWindowDrawList();
SetCursorScreenPos(screen_pos - ImVec2(NODE_SLOT_RADIUS, NODE_SLOT_RADIUS));
PushID(id);
InvisibleButton("", ImVec2(2 * NODE_SLOT_RADIUS, 2 * NODE_SLOT_RADIUS));
bool hovered = IsItemHovered();
PopID();
draw_list->AddCircleFilled(screen_pos,
NODE_SLOT_RADIUS,
hovered ? ImColor(0, 150, 0, 150) : ImColor(150, 150, 150, 150));
return hovered;
}
void NodeLink(ImVec2 from, ImVec2 to)
{
ImVec2 p1 = from;
ImVec2 t1 = ImVec2(+80.0f, 0.0f);
ImVec2 p2 = to;
ImVec2 t2 = ImVec2(+80.0f, 0.0f);
const int STEPS = 12;
ImDrawList* draw_list = GetWindowDrawList();
for (int step = 0; step <= STEPS; step++)
{
float t = (float)step / (float)STEPS;
float h1 = +2 * t * t * t - 3 * t * t + 1.0f;
float h2 = -2 * t * t * t + 3 * t * t;
float h3 = t * t * t - 2 * t * t + t;
float h4 = t * t * t - t * t;
draw_list->PathLineTo(ImVec2(h1 * p1.x + h2 * p2.x + h3 * t1.x + h4 * t2.x,
h1 * p1.y + h2 * p2.y + h3 * t1.y + h4 * t2.y));
}
draw_list->PathStroke(ImColor(200, 200, 100), false, 3.0f);
}
ImVec2 operator*(float f, const ImVec2& v)
{
return ImVec2(f * v.x, f * v.y);
}
CurveEditor BeginCurveEditor(const char* label)
{
CurveEditor editor;
editor.valid = false;
ImGuiWindow* window = GetCurrentWindow();
if (window->SkipItems) return editor;
ImGuiContext& g = *GImGui;
const ImGuiStyle& style = g.Style;
ImVec2 cursor_pos = GetCursorScreenPos();
const ImVec2 label_size = CalcTextSize(label, NULL, true);
ImVec2 graph_size;
graph_size.x = CalcItemWidth() + (style.FramePadding.x * 2);
graph_size.y = 100; // label_size.y + (style.FramePadding.y * 2);
const ImRect frame_bb(
window->DC.CursorPos, window->DC.CursorPos + ImVec2(graph_size.x, graph_size.y));
const ImRect inner_bb(frame_bb.Min + style.FramePadding, frame_bb.Max - style.FramePadding);
const ImRect total_bb(frame_bb.Min,
frame_bb.Max +
ImVec2(label_size.x > 0.0f ? style.ItemInnerSpacing.x + label_size.x : 0.0f, 0));
ItemSize(total_bb, style.FramePadding.y);
if (!ItemAdd(total_bb, NULL)) return editor;
editor.valid = true;
PushID(label);
RenderFrame(
frame_bb.Min, frame_bb.Max, GetColorU32(ImGuiCol_FrameBg), true, style.FrameRounding);
RenderText(ImVec2(frame_bb.Max.x + style.ItemInnerSpacing.x, inner_bb.Min.y), label);
editor.beg_pos = cursor_pos;
SetCursorScreenPos(cursor_pos);
editor.point_idx = -1;
return editor;
}
void EndCurveEditor(const CurveEditor& editor)
{
SetCursorScreenPos(editor.beg_pos);
InvisibleButton("bg", ImVec2(CalcItemWidth(), 100));
PopID();
}
bool CurvePoint(ImVec2* points, CurveEditor& editor)
{
ImGuiWindow* window = GetCurrentWindow();
ImGuiContext& g = *GImGui;
const ImGuiStyle& style = g.Style;
ImVec2 cursor_pos_backup = GetCursorScreenPos();
ImVec2 graph_size;
graph_size.x = CalcItemWidth() + (style.FramePadding.x * 2);
graph_size.y = 100; // label_size.y + (style.FramePadding.y * 2);
const ImRect frame_bb(window->DC.CursorPos, window->DC.CursorPos + graph_size);
const ImRect inner_bb(frame_bb.Min + style.FramePadding, frame_bb.Max - style.FramePadding);
const ImU32 col_base = GetColorU32(ImGuiCol_PlotLines);
const ImU32 col_hovered = GetColorU32(ImGuiCol_PlotLinesHovered);
ImVec2 left_tangent = points[0];
ImVec2 right_tangent = points[2];
ImVec2 p = points[1];
auto transform = [inner_bb](const ImVec2& p) -> ImVec2
{
return ImVec2(inner_bb.Min.x * (1 - p.x) + inner_bb.Max.x * p.x,
inner_bb.Min.y * p.y + inner_bb.Max.y * (1 - p.y));
};
ImVec2 pos = transform(p);
if (editor.point_idx >= 0)
{
window->DrawList->AddBezierCurve(pos,
transform(p + left_tangent),
transform(editor.prev_point + editor.prev_tangent),
transform(editor.prev_point),
col_base,
1.0f,
20);
}
editor.prev_point = p;
editor.prev_tangent = right_tangent;
static const float SIZE = 3;
SetCursorScreenPos(pos - ImVec2(SIZE, SIZE));
PushID(editor.point_idx);
++editor.point_idx;
InvisibleButton("", ImVec2(2 * NODE_SLOT_RADIUS, 2 * NODE_SLOT_RADIUS));
ImU32 col = IsItemHovered() ? col_hovered : col_base;
window->DrawList->AddLine(pos + ImVec2(-SIZE, 0), pos + ImVec2(0, SIZE), col);
window->DrawList->AddLine(pos + ImVec2(SIZE, 0), pos + ImVec2(0, SIZE), col);
window->DrawList->AddLine(pos + ImVec2(SIZE, 0), pos + ImVec2(0, -SIZE), col);
window->DrawList->AddLine(pos + ImVec2(-SIZE, 0), pos + ImVec2(0, -SIZE), col);
bool changed = false;
if (IsItemActive() && IsMouseDragging(0))
{
pos += GetIO().MouseDelta;
ImVec2 v;
v.x = (pos.x - inner_bb.Min.x) / (inner_bb.Max.x - inner_bb.Min.x);
v.y = (inner_bb.Max.y - pos.y) / (inner_bb.Max.y - inner_bb.Min.y);
v = ImClamp(v, ImVec2(0, 0), ImVec2(1, 1));
points[1] = v;
changed = true;
}
PopID();
SetCursorScreenPos(cursor_pos_backup);
return changed;
}
bool BeginResizablePopup(const char* str_id, const ImVec2& size_on_first_use)
{
if (GImGui->OpenPopupStack.Size <= GImGui->CurrentPopupStack.Size)
{
ClearSetNextWindowData();
return false;
}
ImGuiContext& g = *GImGui;
ImGuiWindow* window = g.CurrentWindow;
const ImGuiID id = window->GetID(str_id);
if (!IsPopupOpen(id))
{
ClearSetNextWindowData();
return false;
}
PushStyleVar(ImGuiStyleVar_WindowRounding, 0.0f);
ImGuiWindowFlags flags = ImGuiWindowFlags_ShowBorders | ImGuiWindowFlags_Popup | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoSavedSettings;
char name[32];
ImFormatString(name, 20, "##popup_%08x", id);
float alpha = 1.0f;
bool opened = Begin(name, NULL, size_on_first_use, alpha, flags);
if (!(window->Flags & ImGuiWindowFlags_ShowBorders))
g.CurrentWindow->Flags &= ~ImGuiWindowFlags_ShowBorders;
if (!opened)
EndPopup();
return opened;
}
void IntervalGraph(const unsigned long long* value_pairs,
int value_pairs_count,
unsigned long long scale_min,
unsigned long long scele_max)
{
ImGuiWindow* window = GetCurrentWindow();
if (window->SkipItems) return;
ImGuiContext& g = *GImGui;
const ImGuiStyle& style = g.Style;
ImVec2 graph_size(CalcItemWidth() + (style.FramePadding.x * 2), GetTextLineHeight());
const ImRect frame_bb(window->DC.CursorPos, window->DC.CursorPos + ImVec2(graph_size.x, graph_size.y));
const ImRect inner_bb(frame_bb.Min + style.FramePadding, frame_bb.Max - style.FramePadding);
const ImRect total_bb(frame_bb.Min, frame_bb.Max);
ItemSize(total_bb, style.FramePadding.y);
if (!ItemAdd(total_bb, NULL)) return;
double graph_length = double(scele_max - scale_min);
const ImU32 col_base = GetColorU32(ImGuiCol_PlotHistogram);
for (int i = 0; i < value_pairs_count; ++i)
{
ImVec2 tmp = frame_bb.Min + ImVec2(float((value_pairs[i * 2] - scale_min) / graph_length * graph_size.x), 0);
window->DrawList->AddRectFilled(
tmp, tmp + ImVec2(ImMax(1.0f, float(value_pairs[i * 2 + 1] / graph_length * graph_size.x)), graph_size.y), col_base);
}
}
bool FilterInput(const char* label, char* buf, size_t buf_size)
{
auto pos = GetCursorPos();
PushItemWidth(GetContentRegionAvail().x);
char tmp[32];
strcpy(tmp, "##");
strcat(tmp, label);
bool ret = InputText(tmp, buf, buf_size);
if (buf[0] == 0 && !IsItemActive())
{
pos.x += GetStyle().FramePadding.x;
SetCursorPos(pos);
AlignFirstTextHeightToWidgets();
TextColored(GetStyle().Colors[ImGuiCol_TextDisabled], "Filter");
}
PopItemWidth();
return ret;
}
void HSplitter(const char* str_id, ImVec2* size)
{
ImVec2 screen_pos = GetCursorScreenPos();
InvisibleButton(str_id, ImVec2(-1, 3));
ImVec2 end_pos = screen_pos + GetItemRectSize();
ImGuiWindow* win = GetCurrentWindow();
ImVec4* colors = GetStyle().Colors;
ImU32 color = GetColorU32(IsItemActive() || IsItemHovered() ? colors[ImGuiCol_ButtonActive] : colors[ImGuiCol_Button]);
win->DrawList->AddRectFilled(screen_pos, end_pos, color);
if (IsItemActive())
{
size->y = ImMax(1.0f, GetIO().MouseDelta.y + size->y);
}
}
static float s_max_timeline_value;
bool BeginTimeline(const char* str_id, float max_value)
{
s_max_timeline_value = max_value;
return BeginChild(str_id);
}
static const float TIMELINE_RADIUS = 6;
bool TimelineEvent(const char* str_id, float* values)
{
ImGuiWindow* win = GetCurrentWindow();
const ImU32 inactive_color = ColorConvertFloat4ToU32(GImGui->Style.Colors[ImGuiCol_Button]);
const ImU32 active_color = ColorConvertFloat4ToU32(GImGui->Style.Colors[ImGuiCol_ButtonHovered]);
const ImU32 line_color = ColorConvertFloat4ToU32(GImGui->Style.Colors[ImGuiCol_ColumnActive]);
bool changed = false;
ImVec2 cursor_pos = win->DC.CursorPos;
for (int i = 0; i < 2; ++i)
{
ImVec2 pos = cursor_pos;
pos.x += win->Size.x * values[i] / s_max_timeline_value + TIMELINE_RADIUS;
pos.y += TIMELINE_RADIUS;
SetCursorScreenPos(pos - ImVec2(TIMELINE_RADIUS, TIMELINE_RADIUS));
PushID(i);
InvisibleButton(str_id, ImVec2(2 * TIMELINE_RADIUS, 2 * TIMELINE_RADIUS));
if (IsItemActive() || IsItemHovered())
{
ImGui::SetTooltip("%f", values[i]);
ImVec2 a(pos.x, GetWindowContentRegionMin().y + win->Pos.y);
ImVec2 b(pos.x, GetWindowContentRegionMax().y + win->Pos.y);
win->DrawList->AddLine(a, b, line_color);
}
if (IsItemActive() && IsMouseDragging(0))
{
values[i] += GetIO().MouseDelta.x / win->Size.x * s_max_timeline_value;
changed = true;
}
PopID();
win->DrawList->AddCircleFilled(
pos, TIMELINE_RADIUS, IsItemActive() || IsItemHovered() ? active_color : inactive_color);
}
ImVec2 start = cursor_pos;
start.x += win->Size.x * values[0] / s_max_timeline_value + 2 * TIMELINE_RADIUS;
start.y += TIMELINE_RADIUS * 0.5f;
ImVec2 end = start + ImVec2(win->Size.x * (values[1] - values[0]) / s_max_timeline_value - 2 * TIMELINE_RADIUS,
TIMELINE_RADIUS);
PushID(-1);
SetCursorScreenPos(start);
InvisibleButton(str_id, end - start);
if (IsItemActive() && IsMouseDragging(0))
{
values[0] += GetIO().MouseDelta.x / win->Size.x * s_max_timeline_value;
values[1] += GetIO().MouseDelta.x / win->Size.x * s_max_timeline_value;
changed = true;
}
PopID();
SetCursorScreenPos(cursor_pos + ImVec2(0, GetTextLineHeightWithSpacing()));
win->DrawList->AddRectFilled(start, end, IsItemActive() || IsItemHovered() ? active_color : inactive_color);
if (values[0] > values[1])
{
float tmp = values[0];
values[0] = values[1];
values[1] = tmp;
}
if (values[1] > s_max_timeline_value) values[1] = s_max_timeline_value;
if (values[0] < 0) values[0] = 0;
return changed;
}
void EndTimeline()
{
ImGuiWindow* win = GetCurrentWindow();
ImU32 color = ColorConvertFloat4ToU32(GImGui->Style.Colors[ImGuiCol_Button]);
ImU32 line_color = ColorConvertFloat4ToU32(GImGui->Style.Colors[ImGuiCol_Border]);
ImU32 text_color = ColorConvertFloat4ToU32(GImGui->Style.Colors[ImGuiCol_Text]);
float rounding = GImGui->Style.ScrollbarRounding;
ImVec2 start(GetWindowContentRegionMin().x + win->Pos.x,
GetWindowContentRegionMax().y - GetTextLineHeightWithSpacing() + win->Pos.y);
ImVec2 end = GetWindowContentRegionMax() + win->Pos;
win->DrawList->AddRectFilled(start, end, color, rounding);
const int LINE_COUNT = 5;
const ImVec2 text_offset(0, GetTextLineHeightWithSpacing());
for (int i = 0; i < LINE_COUNT; ++i)
{
ImVec2 a = GetWindowContentRegionMin() + win->Pos + ImVec2(TIMELINE_RADIUS, 0);
a.x += i * GetWindowContentRegionWidth() / LINE_COUNT;
ImVec2 b = a;
b.y = start.y;
win->DrawList->AddLine(a, b, line_color);
char tmp[256];
ImFormatString(tmp, sizeof(tmp), "%.2f", i * s_max_timeline_value / LINE_COUNT);
win->DrawList->AddText(b, text_color, tmp);
}
EndChild();
}
} // namespace ImGui
#include "imgui_dock.inl"