-
Notifications
You must be signed in to change notification settings - Fork 545
/
imgui_node_editor.cpp
5855 lines (4743 loc) · 173 KB
/
imgui_node_editor.cpp
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
//------------------------------------------------------------------------------
// VERSION 0.9.1
//
// LICENSE
// This software is dual-licensed to the public domain and under the following
// license: you are granted a perpetual, irrevocable license to copy, modify,
// publish, and distribute this file as you see fit.
//
// CREDITS
// Written by Michal Cichon
//------------------------------------------------------------------------------
# include "imgui_node_editor_internal.h"
# include <cstdio> // snprintf
# include <string>
# include <fstream>
# include <bitset>
# include <climits>
# include <algorithm>
# include <sstream>
# include <streambuf>
# include <type_traits>
// https://stackoverflow.com/a/8597498
# define DECLARE_HAS_NESTED(Name, Member) \
\
template<class T> \
struct has_nested_ ## Name \
{ \
typedef char yes; \
typedef yes(&no)[2]; \
\
template<class U> static yes test(decltype(U::Member)*); \
template<class U> static no test(...); \
\
static bool const value = sizeof(test<T>(0)) == sizeof(yes); \
};
namespace ax {
namespace NodeEditor {
namespace Detail {
# if !defined(IMGUI_VERSION_NUM) || (IMGUI_VERSION_NUM < 18822)
# define DECLARE_KEY_TESTER(Key) \
DECLARE_HAS_NESTED(Key, Key) \
struct KeyTester_ ## Key \
{ \
template <typename T> \
static int Get(typename std::enable_if<has_nested_ ## Key<ImGuiKey_>::value, T>::type*) \
{ \
return ImGui::GetKeyIndex(T::Key); \
} \
\
template <typename T> \
static int Get(typename std::enable_if<!has_nested_ ## Key<ImGuiKey_>::value, T>::type*) \
{ \
return -1; \
} \
}
DECLARE_KEY_TESTER(ImGuiKey_F);
DECLARE_KEY_TESTER(ImGuiKey_D);
static inline int GetKeyIndexForF()
{
return KeyTester_ImGuiKey_F::Get<ImGuiKey_>(nullptr);
}
static inline int GetKeyIndexForD()
{
return KeyTester_ImGuiKey_D::Get<ImGuiKey_>(nullptr);
}
# else
static inline ImGuiKey GetKeyIndexForF()
{
return ImGuiKey_F;
}
static inline ImGuiKey GetKeyIndexForD()
{
return ImGuiKey_D;
}
# endif
} // namespace Detail
} // namespace NodeEditor
} // namespace ax
//------------------------------------------------------------------------------
namespace ed = ax::NodeEditor::Detail;
//------------------------------------------------------------------------------
static const int c_BackgroundChannelCount = 1;
static const int c_LinkChannelCount = 4;
static const int c_UserLayersCount = 5;
static const int c_UserLayerChannelStart = 0;
static const int c_BackgroundChannelStart = c_UserLayerChannelStart + c_UserLayersCount;
static const int c_LinkStartChannel = c_BackgroundChannelStart + c_BackgroundChannelCount;
static const int c_NodeStartChannel = c_LinkStartChannel + c_LinkChannelCount;
static const int c_BackgroundChannel_SelectionRect = c_BackgroundChannelStart + 0;
static const int c_UserChannel_Content = c_UserLayerChannelStart + 1;
static const int c_UserChannel_Grid = c_UserLayerChannelStart + 2;
static const int c_UserChannel_HintsBackground = c_UserLayerChannelStart + 3;
static const int c_UserChannel_Hints = c_UserLayerChannelStart + 4;
static const int c_LinkChannel_Selection = c_LinkStartChannel + 0;
static const int c_LinkChannel_Links = c_LinkStartChannel + 1;
static const int c_LinkChannel_Flow = c_LinkStartChannel + 2;
static const int c_LinkChannel_NewLink = c_LinkStartChannel + 3;
static const int c_ChannelsPerNode = 5;
static const int c_NodeBaseChannel = 0;
static const int c_NodeBackgroundChannel = 1;
static const int c_NodeUserBackgroundChannel = 2;
static const int c_NodePinChannel = 3;
static const int c_NodeContentChannel = 4;
static const float c_GroupSelectThickness = 6.0f; // canvas pixels
static const float c_LinkSelectThickness = 5.0f; // canvas pixels
static const float c_NavigationZoomMargin = 0.1f; // percentage of visible bounds
static const float c_MouseZoomDuration = 0.15f; // seconds
static const float c_SelectionFadeOutDuration = 0.15f; // seconds
static const auto c_MaxMoveOverEdgeSpeed = 10.0f;
static const auto c_MaxMoveOverEdgeDistance = 300.0f;
#if IMGUI_VERSION_NUM > 18101
static const auto c_AllRoundCornersFlags = ImDrawFlags_RoundCornersAll;
#else
static const auto c_AllRoundCornersFlags = 15;
#endif
//------------------------------------------------------------------------------
# if defined(_DEBUG) && defined(_WIN32)
extern "C" __declspec(dllimport) void __stdcall OutputDebugStringA(const char* string);
static void LogV(const char* fmt, va_list args)
{
const int buffer_size = 1024;
static char buffer[1024];
vsnprintf(buffer, buffer_size - 1, fmt, args);
buffer[buffer_size - 1] = 0;
ImGui::LogText("\nNode Editor: %s", buffer);
OutputDebugStringA("NodeEditor: ");
OutputDebugStringA(buffer);
OutputDebugStringA("\n");
}
# endif
void ed::Log(const char* fmt, ...)
{
# if defined(_DEBUG) && defined(_WIN32)
va_list args;
va_start(args, fmt);
LogV(fmt, args);
va_end(args);
# endif
}
//------------------------------------------------------------------------------
static bool IsGroup(const ed::Node* node)
{
if (node && node->m_Type == ed::NodeType::Group)
return true;
else
return false;
}
//------------------------------------------------------------------------------
static void ImDrawListSplitter_Grow(ImDrawList* draw_list, ImDrawListSplitter* splitter, int channels_count)
{
IM_ASSERT(splitter != nullptr);
IM_ASSERT(splitter->_Count <= channels_count);
if (splitter->_Count == 1)
{
splitter->Split(draw_list, channels_count);
return;
}
int old_channels_count = splitter->_Channels.Size;
if (old_channels_count < channels_count)
{
splitter->_Channels.reserve(channels_count);
splitter->_Channels.resize(channels_count);
}
int old_used_channels_count = splitter->_Count;
splitter->_Count = channels_count;
for (int i = old_used_channels_count; i < channels_count; i++)
{
if (i >= old_channels_count)
{
IM_PLACEMENT_NEW(&splitter->_Channels[i]) ImDrawChannel();
}
else
{
splitter->_Channels[i]._CmdBuffer.resize(0);
splitter->_Channels[i]._IdxBuffer.resize(0);
}
}
}
static void ImDrawList_ChannelsGrow(ImDrawList* draw_list, int channels_count)
{
ImDrawListSplitter_Grow(draw_list, &draw_list->_Splitter, channels_count);
}
static void ImDrawListSplitter_SwapChannels(ImDrawListSplitter* splitter, int left, int right)
{
IM_ASSERT(left < splitter->_Count && right < splitter->_Count);
if (left == right)
return;
auto currentChannel = splitter->_Current;
auto* leftCmdBuffer = &splitter->_Channels[left]._CmdBuffer;
auto* leftIdxBuffer = &splitter->_Channels[left]._IdxBuffer;
auto* rightCmdBuffer = &splitter->_Channels[right]._CmdBuffer;
auto* rightIdxBuffer = &splitter->_Channels[right]._IdxBuffer;
leftCmdBuffer->swap(*rightCmdBuffer);
leftIdxBuffer->swap(*rightIdxBuffer);
if (currentChannel == left)
splitter->_Current = right;
else if (currentChannel == right)
splitter->_Current = left;
}
static void ImDrawList_SwapChannels(ImDrawList* drawList, int left, int right)
{
ImDrawListSplitter_SwapChannels(&drawList->_Splitter, left, right);
}
static void ImDrawList_SwapSplitter(ImDrawList* drawList, ImDrawListSplitter& splitter)
{
auto& currentSplitter = drawList->_Splitter;
std::swap(currentSplitter._Current, splitter._Current);
std::swap(currentSplitter._Count, splitter._Count);
currentSplitter._Channels.swap(splitter._Channels);
}
//static void ImDrawList_TransformChannel_Inner(ImVector<ImDrawVert>& vtxBuffer, const ImVector<ImDrawIdx>& idxBuffer, const ImVector<ImDrawCmd>& cmdBuffer, const ImVec2& preOffset, const ImVec2& scale, const ImVec2& postOffset)
//{
// auto idxRead = idxBuffer.Data;
//
// int indexOffset = 0;
// for (auto& cmd : cmdBuffer)
// {
// auto idxCount = cmd.ElemCount;
//
// if (idxCount == 0) continue;
//
// auto minIndex = idxRead[indexOffset];
// auto maxIndex = idxRead[indexOffset];
//
// for (auto i = 1u; i < idxCount; ++i)
// {
// auto idx = idxRead[indexOffset + i];
// minIndex = std::min(minIndex, idx);
// maxIndex = ImMax(maxIndex, idx);
// }
//
// for (auto vtx = vtxBuffer.Data + minIndex, vtxEnd = vtxBuffer.Data + maxIndex + 1; vtx < vtxEnd; ++vtx)
// {
// vtx->pos.x = (vtx->pos.x + preOffset.x) * scale.x + postOffset.x;
// vtx->pos.y = (vtx->pos.y + preOffset.y) * scale.y + postOffset.y;
// }
//
// indexOffset += idxCount;
// }
//}
//static void ImDrawList_TransformChannels(ImDrawList* drawList, int begin, int end, const ImVec2& preOffset, const ImVec2& scale, const ImVec2& postOffset)
//{
// int lastCurrentChannel = drawList->_ChannelsCurrent;
// if (lastCurrentChannel != 0)
// drawList->ChannelsSetCurrent(0);
//
// auto& vtxBuffer = drawList->VtxBuffer;
//
// if (begin == 0 && begin != end)
// {
// ImDrawList_TransformChannel_Inner(vtxBuffer, drawList->IdxBuffer, drawList->CmdBuffer, preOffset, scale, postOffset);
// ++begin;
// }
//
// for (int channelIndex = begin; channelIndex < end; ++channelIndex)
// {
// auto& channel = drawList->_Channels[channelIndex];
// ImDrawList_TransformChannel_Inner(vtxBuffer, channel.IdxBuffer, channel.CmdBuffer, preOffset, scale, postOffset);
// }
//
// if (lastCurrentChannel != 0)
// drawList->ChannelsSetCurrent(lastCurrentChannel);
//}
//static void ImDrawList_ClampClipRects_Inner(ImVector<ImDrawCmd>& cmdBuffer, const ImVec4& clipRect, const ImVec2& offset)
//{
// for (auto& cmd : cmdBuffer)
// {
// cmd.ClipRect.x = ImMax(cmd.ClipRect.x + offset.x, clipRect.x);
// cmd.ClipRect.y = ImMax(cmd.ClipRect.y + offset.y, clipRect.y);
// cmd.ClipRect.z = std::min(cmd.ClipRect.z + offset.x, clipRect.z);
// cmd.ClipRect.w = std::min(cmd.ClipRect.w + offset.y, clipRect.w);
// }
//}
//static void ImDrawList_TranslateAndClampClipRects(ImDrawList* drawList, int begin, int end, const ImVec2& offset)
//{
// int lastCurrentChannel = drawList->_ChannelsCurrent;
// if (lastCurrentChannel != 0)
// drawList->ChannelsSetCurrent(0);
//
// auto clipRect = drawList->_ClipRectStack.back();
//
// if (begin == 0 && begin != end)
// {
// ImDrawList_ClampClipRects_Inner(drawList->CmdBuffer, clipRect, offset);
// ++begin;
// }
//
// for (int channelIndex = begin; channelIndex < end; ++channelIndex)
// {
// auto& channel = drawList->_Channels[channelIndex];
// ImDrawList_ClampClipRects_Inner(channel.CmdBuffer, clipRect, offset);
// }
//
// if (lastCurrentChannel != 0)
// drawList->ChannelsSetCurrent(lastCurrentChannel);
//}
static void ImDrawList_PathBezierOffset(ImDrawList* drawList, float offset, const ImVec2& p0, const ImVec2& p1, const ImVec2& p2, const ImVec2& p3)
{
using namespace ed;
auto acceptPoint = [drawList, offset](const ImCubicBezierSubdivideSample& r)
{
drawList->PathLineTo(r.Point + ImNormalized(ImVec2(-r.Tangent.y, r.Tangent.x)) * offset);
};
ImCubicBezierSubdivide(acceptPoint, p0, p1, p2, p3);
}
/*
static void ImDrawList_PolyFillScanFlood(ImDrawList *draw, std::vector<ImVec2>* poly, ImColor color, int gap = 1, float strokeWidth = 1.0f)
{
std::vector<ImVec2> scanHits;
ImVec2 min, max; // polygon min/max points
auto io = ImGui::GetIO();
float y;
bool isMinMaxDone = false;
unsigned int polysize = poly->size();
// find the orthagonal bounding box
// probably can put this as a predefined
if (!isMinMaxDone)
{
min.x = min.y = FLT_MAX;
max.x = max.y = FLT_MIN;
for (auto p : *poly)
{
if (p.x < min.x) min.x = p.x;
if (p.y < min.y) min.y = p.y;
if (p.x > max.x) max.x = p.x;
if (p.y > max.y) max.y = p.y;
}
isMinMaxDone = true;
}
// Bounds check
if ((max.x < 0) || (min.x > io.DisplaySize.x) || (max.y < 0) || (min.y > io.DisplaySize.y)) return;
// Vertically clip
if (min.y < 0) min.y = 0;
if (max.y > io.DisplaySize.y) max.y = io.DisplaySize.y;
// so we know we start on the outside of the object we step out by 1.
min.x -= 1;
max.x += 1;
// Initialise our starting conditions
y = min.y;
// Go through each scan line iteratively, jumping by 'gap' pixels each time
while (y < max.y)
{
scanHits.clear();
{
int jump = 1;
ImVec2 fp = poly->at(0);
for (size_t i = 0; i < polysize - 1; i++)
{
ImVec2 pa = poly->at(i);
ImVec2 pb = poly->at(i + 1);
// jump double/dud points
if (pa.x == pb.x && pa.y == pb.y) continue;
// if we encounter our hull/poly start point, then we've now created the
// closed
// hull, jump the next segment and reset the first-point
if ((!jump) && (fp.x == pb.x) && (fp.y == pb.y))
{
if (i < polysize - 2)
{
fp = poly->at(i + 2);
jump = 1;
i++;
}
}
else
{
jump = 0;
}
// test to see if this segment makes the scan-cut.
if ((pa.y > pb.y && y < pa.y && y > pb.y) || (pa.y < pb.y && y > pa.y && y < pb.y))
{
ImVec2 intersect;
intersect.y = y;
if (pa.x == pb.x)
{
intersect.x = pa.x;
}
else
{
intersect.x = (pb.x - pa.x) / (pb.y - pa.y) * (y - pa.y) + pa.x;
}
scanHits.push_back(intersect);
}
}
// Sort the scan hits by X, so we have a proper left->right ordering
sort(scanHits.begin(), scanHits.end(), [](ImVec2 const &a, ImVec2 const &b) { return a.x < b.x; });
// generate the line segments.
{
int i = 0;
int l = scanHits.size() - 1; // we need pairs of points, this prevents segfault.
for (i = 0; i < l; i += 2)
{
draw->AddLine(scanHits[i], scanHits[i + 1], color, strokeWidth);
}
}
}
y += gap;
} // for each scan line
scanHits.clear();
}
*/
static void ImDrawList_AddBezierWithArrows(ImDrawList* drawList, const ImCubicBezierPoints& curve, float thickness,
float startArrowSize, float startArrowWidth, float endArrowSize, float endArrowWidth,
bool fill, ImU32 color, float strokeThickness, const ImVec2* startDirHint = nullptr, const ImVec2* endDirHint = nullptr)
{
using namespace ax;
if ((color >> 24) == 0)
return;
const auto half_thickness = thickness * 0.5f;
if (fill)
{
drawList->AddBezierCubic(curve.P0, curve.P1, curve.P2, curve.P3, color, thickness);
if (startArrowSize > 0.0f)
{
const auto start_dir = ImNormalized(startDirHint ? *startDirHint : ImCubicBezierTangent(curve.P0, curve.P1, curve.P2, curve.P3, 0.0f));
const auto start_n = ImVec2(-start_dir.y, start_dir.x);
const auto half_width = startArrowWidth * 0.5f;
const auto tip = curve.P0 - start_dir * startArrowSize;
drawList->PathLineTo(curve.P0 - start_n * ImMax(half_width, half_thickness));
drawList->PathLineTo(curve.P0 + start_n * ImMax(half_width, half_thickness));
drawList->PathLineTo(tip);
drawList->PathFillConvex(color);
}
if (endArrowSize > 0.0f)
{
const auto end_dir = ImNormalized(endDirHint ? -*endDirHint : ImCubicBezierTangent(curve.P0, curve.P1, curve.P2, curve.P3, 1.0f));
const auto end_n = ImVec2( -end_dir.y, end_dir.x);
const auto half_width = endArrowWidth * 0.5f;
const auto tip = curve.P3 + end_dir * endArrowSize;
drawList->PathLineTo(curve.P3 + end_n * ImMax(half_width, half_thickness));
drawList->PathLineTo(curve.P3 - end_n * ImMax(half_width, half_thickness));
drawList->PathLineTo(tip);
drawList->PathFillConvex(color);
}
}
else
{
if (startArrowSize > 0.0f)
{
const auto start_dir = ImNormalized(ImCubicBezierTangent(curve.P0, curve.P1, curve.P2, curve.P3, 0.0f));
const auto start_n = ImVec2(-start_dir.y, start_dir.x);
const auto half_width = startArrowWidth * 0.5f;
const auto tip = curve.P0 - start_dir * startArrowSize;
if (half_width > half_thickness)
drawList->PathLineTo(curve.P0 - start_n * half_width);
drawList->PathLineTo(tip);
if (half_width > half_thickness)
drawList->PathLineTo(curve.P0 + start_n * half_width);
}
ImDrawList_PathBezierOffset(drawList, half_thickness, curve.P0, curve.P1, curve.P2, curve.P3);
if (endArrowSize > 0.0f)
{
const auto end_dir = ImNormalized(ImCubicBezierTangent(curve.P0, curve.P1, curve.P2, curve.P3, 1.0f));
const auto end_n = ImVec2( -end_dir.y, end_dir.x);
const auto half_width = endArrowWidth * 0.5f;
const auto tip = curve.P3 + end_dir * endArrowSize;
if (half_width > half_thickness)
drawList->PathLineTo(curve.P3 + end_n * half_width);
drawList->PathLineTo(tip);
if (half_width > half_thickness)
drawList->PathLineTo(curve.P3 - end_n * half_width);
}
ImDrawList_PathBezierOffset(drawList, half_thickness, curve.P3, curve.P2, curve.P1, curve.P0);
drawList->PathStroke(color, true, strokeThickness);
}
}
//------------------------------------------------------------------------------
//
// Pin
//
//------------------------------------------------------------------------------
void ed::Pin::Draw(ImDrawList* drawList, DrawFlags flags)
{
if (flags & Hovered)
{
drawList->ChannelsSetCurrent(m_Node->m_Channel + c_NodePinChannel);
drawList->AddRectFilled(m_Bounds.Min, m_Bounds.Max,
m_Color, m_Rounding, m_Corners);
if (m_BorderWidth > 0.0f)
{
FringeScaleScope fringe(1.0f);
drawList->AddRect(m_Bounds.Min, m_Bounds.Max,
m_BorderColor, m_Rounding, m_Corners, m_BorderWidth);
}
if (!Editor->IsSelected(m_Node))
m_Node->Draw(drawList, flags);
}
}
ImVec2 ed::Pin::GetClosestPoint(const ImVec2& p) const
{
auto pivot = m_Pivot;
auto extent = m_Radius + m_ArrowSize;
if (m_SnapLinkToDir && extent > 0.0f)
{
pivot.Min += m_Dir * extent;
pivot.Max += m_Dir * extent;
extent = 0;
}
return ImRect_ClosestPoint(pivot, p, true, extent);
}
ImLine ed::Pin::GetClosestLine(const Pin* pin) const
{
auto pivotA = m_Pivot;
auto pivotB = pin->m_Pivot;
auto extentA = m_Radius + m_ArrowSize;
auto extentB = pin->m_Radius + pin->m_ArrowSize;
if (m_SnapLinkToDir && extentA > 0.0f)
{
pivotA.Min += m_Dir * extentA;
pivotA.Max += m_Dir * extentA;
extentA = 0;
}
if (pin->m_SnapLinkToDir && extentB > 0.0f)
{
pivotB.Min += pin->m_Dir * extentB;
pivotB.Max += pin->m_Dir * extentB;
extentB = 0;
}
return ImRect_ClosestLine(pivotA, pivotB, extentA, extentB);
}
//------------------------------------------------------------------------------
//
// Node
//
//------------------------------------------------------------------------------
bool ed::Node::AcceptDrag()
{
m_DragStart = m_Bounds.Min;
return true;
}
void ed::Node::UpdateDrag(const ImVec2& offset)
{
auto size = m_Bounds.GetSize();
m_Bounds.Min = ImFloor(m_DragStart + offset);
m_Bounds.Max = m_Bounds.Min + size;
}
bool ed::Node::EndDrag()
{
return m_Bounds.Min != m_DragStart;
}
void ed::Node::Draw(ImDrawList* drawList, DrawFlags flags)
{
if (flags == Detail::Object::None)
{
drawList->ChannelsSetCurrent(m_Channel + c_NodeBackgroundChannel);
drawList->AddRectFilled(
m_Bounds.Min,
m_Bounds.Max,
m_Color, m_Rounding);
if (IsGroup(this))
{
drawList->AddRectFilled(
m_GroupBounds.Min,
m_GroupBounds.Max,
m_GroupColor, m_GroupRounding);
if (m_GroupBorderWidth > 0.0f)
{
FringeScaleScope fringe(1.0f);
drawList->AddRect(
m_GroupBounds.Min,
m_GroupBounds.Max,
m_GroupBorderColor, m_GroupRounding, c_AllRoundCornersFlags, m_GroupBorderWidth);
}
}
# if 0
// #debug: highlight group regions
auto drawRect = [drawList](const ImRect& rect, ImU32 color)
{
if (ImRect_IsEmpty(rect)) return;
drawList->AddRectFilled(rect.Min, rect.Max, color);
};
drawRect(GetRegionBounds(NodeRegion::Top), IM_COL32(255, 0, 0, 64));
drawRect(GetRegionBounds(NodeRegion::Bottom), IM_COL32(255, 0, 0, 64));
drawRect(GetRegionBounds(NodeRegion::Left), IM_COL32(0, 255, 0, 64));
drawRect(GetRegionBounds(NodeRegion::Right), IM_COL32(0, 255, 0, 64));
drawRect(GetRegionBounds(NodeRegion::TopLeft), IM_COL32(255, 0, 255, 64));
drawRect(GetRegionBounds(NodeRegion::TopRight), IM_COL32(255, 0, 255, 64));
drawRect(GetRegionBounds(NodeRegion::BottomLeft), IM_COL32(255, 0, 255, 64));
drawRect(GetRegionBounds(NodeRegion::BottomRight), IM_COL32(255, 0, 255, 64));
drawRect(GetRegionBounds(NodeRegion::Center), IM_COL32(0, 0, 255, 64));
drawRect(GetRegionBounds(NodeRegion::Header), IM_COL32(0, 255, 255, 64));
# endif
DrawBorder(drawList, m_BorderColor, m_BorderWidth);
}
else if (flags & Selected)
{
const auto borderColor = Editor->GetColor(StyleColor_SelNodeBorder);
const auto& editorStyle = Editor->GetStyle();
drawList->ChannelsSetCurrent(m_Channel + c_NodeBaseChannel);
DrawBorder(drawList, borderColor, editorStyle.SelectedNodeBorderWidth, editorStyle.SelectedNodeBorderOffset);
}
else if (!IsGroup(this) && (flags & Hovered))
{
const auto borderColor = Editor->GetColor(StyleColor_HovNodeBorder);
const auto& editorStyle = Editor->GetStyle();
drawList->ChannelsSetCurrent(m_Channel + c_NodeBaseChannel);
DrawBorder(drawList, borderColor, editorStyle.HoveredNodeBorderWidth, editorStyle.HoverNodeBorderOffset);
}
}
void ed::Node::DrawBorder(ImDrawList* drawList, ImU32 color, float thickness, float offset)
{
if (thickness > 0.0f)
{
const ImVec2 extraOffset = ImVec2(offset, offset);
drawList->AddRect(m_Bounds.Min - extraOffset, m_Bounds.Max + extraOffset,
color, ImMax(0.0f, m_Rounding + offset), c_AllRoundCornersFlags, thickness);
}
}
void ed::Node::GetGroupedNodes(std::vector<Node*>& result, bool append)
{
if (!append)
result.resize(0);
if (!IsGroup(this))
return;
const auto firstNodeIndex = result.size();
Editor->FindNodesInRect(m_GroupBounds, result, true, false);
for (auto index = firstNodeIndex; index < result.size(); ++index)
result[index]->GetGroupedNodes(result, true);
}
ImRect ed::Node::GetRegionBounds(NodeRegion region) const
{
if (m_Type == NodeType::Node)
{
if (region == NodeRegion::Header)
return m_Bounds;
}
else if (m_Type == NodeType::Group)
{
const float activeAreaMinimumSize = ImMax(ImMax(
Editor->GetView().InvScale * c_GroupSelectThickness,
m_GroupBorderWidth), c_GroupSelectThickness);
const float minimumSize = activeAreaMinimumSize * 5;
auto bounds = m_Bounds;
if (bounds.GetWidth() < minimumSize)
bounds.Expand(ImVec2(minimumSize - bounds.GetWidth(), 0.0f));
if (bounds.GetHeight() < minimumSize)
bounds.Expand(ImVec2(0.0f, minimumSize - bounds.GetHeight()));
if (region == NodeRegion::Top)
{
bounds.Max.y = bounds.Min.y + activeAreaMinimumSize;
bounds.Min.x += activeAreaMinimumSize;
bounds.Max.x -= activeAreaMinimumSize;
return bounds;
}
else if (region == NodeRegion::Bottom)
{
bounds.Min.y = bounds.Max.y - activeAreaMinimumSize;
bounds.Min.x += activeAreaMinimumSize;
bounds.Max.x -= activeAreaMinimumSize;
return bounds;
}
else if (region == NodeRegion::Left)
{
bounds.Max.x = bounds.Min.x + activeAreaMinimumSize;
bounds.Min.y += activeAreaMinimumSize;
bounds.Max.y -= activeAreaMinimumSize;
return bounds;
}
else if (region == NodeRegion::Right)
{
bounds.Min.x = bounds.Max.x - activeAreaMinimumSize;
bounds.Min.y += activeAreaMinimumSize;
bounds.Max.y -= activeAreaMinimumSize;
return bounds;
}
else if (region == NodeRegion::TopLeft)
{
bounds.Max.x = bounds.Min.x + activeAreaMinimumSize * 2;
bounds.Max.y = bounds.Min.y + activeAreaMinimumSize * 2;
return bounds;
}
else if (region == NodeRegion::TopRight)
{
bounds.Min.x = bounds.Max.x - activeAreaMinimumSize * 2;
bounds.Max.y = bounds.Min.y + activeAreaMinimumSize * 2;
return bounds;
}
else if (region == NodeRegion::BottomRight)
{
bounds.Min.x = bounds.Max.x - activeAreaMinimumSize * 2;
bounds.Min.y = bounds.Max.y - activeAreaMinimumSize * 2;
return bounds;
}
else if (region == NodeRegion::BottomLeft)
{
bounds.Max.x = bounds.Min.x + activeAreaMinimumSize * 2;
bounds.Min.y = bounds.Max.y - activeAreaMinimumSize * 2;
return bounds;
}
else if (region == NodeRegion::Header)
{
bounds.Min.x += activeAreaMinimumSize;
bounds.Max.x -= activeAreaMinimumSize;
bounds.Min.y += activeAreaMinimumSize;
bounds.Max.y = ImMax(bounds.Min.y + activeAreaMinimumSize, m_GroupBounds.Min.y);
return bounds;
}
else if (region == NodeRegion::Center)
{
bounds.Max.x -= activeAreaMinimumSize;
bounds.Min.y = ImMax(bounds.Min.y + activeAreaMinimumSize, m_GroupBounds.Min.y);
bounds.Min.x += activeAreaMinimumSize;
bounds.Max.y -= activeAreaMinimumSize;
return bounds;
}
}
return ImRect();
}
ed::NodeRegion ed::Node::GetRegion(const ImVec2& point) const
{
if (m_Type == NodeType::Node)
{
if (m_Bounds.Contains(point))
return NodeRegion::Header;
else
return NodeRegion::None;
}
else if (m_Type == NodeType::Group)
{
static const NodeRegion c_Regions[] =
{
// Corners first, they may overlap other regions.
NodeRegion::TopLeft,
NodeRegion::TopRight,
NodeRegion::BottomLeft,
NodeRegion::BottomRight,
NodeRegion::Header,
NodeRegion::Top,
NodeRegion::Bottom,
NodeRegion::Left,
NodeRegion::Right,
NodeRegion::Center
};
for (auto region : c_Regions)
{
auto bounds = GetRegionBounds(region);
if (bounds.Contains(point))
return region;
}
}
return NodeRegion::None;
}
//------------------------------------------------------------------------------
//
// Link
//
//------------------------------------------------------------------------------
void ed::Link::Draw(ImDrawList* drawList, DrawFlags flags)
{
if (flags == None)
{
drawList->ChannelsSetCurrent(c_LinkChannel_Links);
Draw(drawList, m_Color, 0.0f);
}
else if (flags & Selected)
{
const auto borderColor = Editor->GetColor(StyleColor_SelLinkBorder);
drawList->ChannelsSetCurrent(c_LinkChannel_Selection);
Draw(drawList, borderColor, 4.5f);
}
else if (flags & Hovered)
{
const auto borderColor = Editor->GetColor(StyleColor_HovLinkBorder);
drawList->ChannelsSetCurrent(c_LinkChannel_Selection);
Draw(drawList, borderColor, 2.0f);
}
else if (flags & Highlighted)
{
drawList->ChannelsSetCurrent(c_LinkChannel_Selection);
Draw(drawList, m_HighlightColor, 3.5f);
}
}
void ed::Link::Draw(ImDrawList* drawList, ImU32 color, float extraThickness) const
{
if (!m_IsLive)
return;
const auto curve = GetCurve();
ImDrawList_AddBezierWithArrows(drawList, curve, m_Thickness + extraThickness,
m_StartPin && m_StartPin->m_ArrowSize > 0.0f ? m_StartPin->m_ArrowSize + extraThickness : 0.0f,
m_StartPin && m_StartPin->m_ArrowWidth > 0.0f ? m_StartPin->m_ArrowWidth + extraThickness : 0.0f,
m_EndPin && m_EndPin->m_ArrowSize > 0.0f ? m_EndPin->m_ArrowSize + extraThickness : 0.0f,
m_EndPin && m_EndPin->m_ArrowWidth > 0.0f ? m_EndPin->m_ArrowWidth + extraThickness : 0.0f,
true, color, 1.0f,
m_StartPin && m_StartPin->m_SnapLinkToDir ? &m_StartPin->m_Dir : nullptr,
m_EndPin && m_EndPin->m_SnapLinkToDir ? &m_EndPin->m_Dir : nullptr);
}
void ed::Link::UpdateEndpoints()
{
const auto line = m_StartPin->GetClosestLine(m_EndPin);
m_Start = line.A;
m_End = line.B;
}
ImCubicBezierPoints ed::Link::GetCurve() const
{
auto easeLinkStrength = [](const ImVec2& a, const ImVec2& b, float strength)
{
const auto distanceX = b.x - a.x;
const auto distanceY = b.y - a.y;
const auto distance = ImSqrt(distanceX * distanceX + distanceY * distanceY);
const auto halfDistance = distance * 0.5f;
if (halfDistance < strength)
strength = strength * ImSin(IM_PI * 0.5f * halfDistance / strength);
return strength;
};
const auto startStrength = easeLinkStrength(m_Start, m_End, m_StartPin->m_Strength);
const auto endStrength = easeLinkStrength(m_Start, m_End, m_EndPin->m_Strength);
const auto cp0 = m_Start + m_StartPin->m_Dir * startStrength;
const auto cp1 = m_End + m_EndPin->m_Dir * endStrength;
ImCubicBezierPoints result;
result.P0 = m_Start;
result.P1 = cp0;
result.P2 = cp1;
result.P3 = m_End;
return result;
}
bool ed::Link::TestHit(const ImVec2& point, float extraThickness) const
{
if (!m_IsLive)
return false;
auto bounds = GetBounds();
if (extraThickness > 0.0f)
bounds.Expand(extraThickness);
if (!bounds.Contains(point))
return false;
const auto bezier = GetCurve();
const auto result = ImProjectOnCubicBezier(point, bezier.P0, bezier.P1, bezier.P2, bezier.P3, 50);
return result.Distance <= m_Thickness + extraThickness;
}
bool ed::Link::TestHit(const ImRect& rect, bool allowIntersect) const
{
if (!m_IsLive)
return false;
const auto bounds = GetBounds();
if (rect.Contains(bounds))
return true;
if (!allowIntersect || !rect.Overlaps(bounds))
return false;
const auto bezier = GetCurve();
const auto p0 = rect.GetTL();
const auto p1 = rect.GetTR();
const auto p2 = rect.GetBR();