forked from Nelarius/imnodes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
imnodes.cpp
executable file
·2186 lines (1860 loc) · 61.7 KB
/
imnodes.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
// the structure of this file:
//
// [SECTION] internal data structures
// [SECTION] global struct
// [SECTION] editor context definition
// [SECTION] ui state logic
// [SECTION] render helpers
// [SECTION] API implementation
#include "imnodes.h"
#include "imgui.h"
#define IMGUI_DEFINE_MATH_OPERATORS
#include "imgui_internal.h"
// Check minimum ImGui version
#define MINIMUM_COMPATIBLE_IMGUI_VERSION 16401
#if IMGUI_VERSION_NUM < MINIMUM_COMPATIBLE_IMGUI_VERSION
#error \
"Minimum ImGui version requirement not met -- please use a newer version!"
#endif
#include <assert.h>
#include <math.h>
#include <new>
#include <stdint.h>
#include <string.h> // strlen, strncmp
#include <stdio.h> // for fwrite, ssprintf, sscanf
#include <stdlib.h>
namespace imnodes
{
namespace
{
bool initialized = false;
enum ScopeFlags
{
Scope_None = 1,
Scope_Editor = 1 << 1,
Scope_Node = 1 << 2,
Scope_Attribute = 1 << 3
};
enum Channels
{
Channels_NodeBackground = 0,
Channels_ImGui,
Channels_Count
};
enum AttributeType
{
AttributeType_None,
AttributeType_Input,
AttributeType_Output
};
// [SECTION] internal data structures
// The object T must have the following interface:
//
// struct T
// {
// T();
//
// int id;
// };
template<typename T>
struct ObjectPool
{
ImVector<T> pool;
ImVector<bool> in_use;
ImVector<int> free_list;
ImGuiStorage id_map;
ObjectPool() : pool(), in_use(), free_list(), id_map() {}
inline void update()
{
free_list.clear();
for (int i = 0; i < in_use.size(); ++i)
{
if (!in_use[i])
{
id_map.SetInt(pool[i].id, -1);
free_list.push_back(i);
}
}
// set all values to false
memset(in_use.Data, 0, sizeof(bool) * in_use.size());
}
inline int find_or_create_index_for(const int id)
{
int index = id_map.GetInt(static_cast<ImGuiID>(id), -1);
if (index == -1)
{
if (free_list.empty())
{
index = pool.size();
pool.push_back(T());
in_use.push_back(true);
}
else
{
index = free_list.back();
free_list.pop_back();
}
id_map.SetInt(static_cast<ImGuiID>(id), index);
}
in_use[index] = true;
return index;
}
inline T& find_or_create_new(const int id)
{
const int index = find_or_create_index_for(id);
return pool[index];
}
// Predicate must define operator()(const T& lhs, const T& operator) ->
// bool.
template<typename Predicate>
inline bool contains(const T& v, Predicate predicate) const
{
for (int i = 0; i < pool.size(); ++i)
{
if (!in_use[i])
{
continue;
}
if (predicate(v, pool[i]))
{
return true;
}
}
return false;
}
};
// Emulates std::optional<int> using the sentinel value `invalid_index`.
struct OptionalIndex
{
OptionalIndex() : m_index(invalid_index) {}
OptionalIndex(const int value) : m_index(value) {}
// Observers
inline bool has_value() const { return m_index != invalid_index; }
inline int value() const
{
assert(has_value());
return m_index;
}
// Modifiers
inline OptionalIndex& operator=(const int value)
{
m_index = value;
return *this;
}
inline void reset() { m_index = invalid_index; }
inline bool operator==(const OptionalIndex& rhs) const
{
return m_index == rhs.m_index;
}
inline bool operator==(const int rhs) const { return m_index == rhs; }
static const int invalid_index = -1;
private:
int m_index;
};
struct NodeData
{
int id;
ImVec2 origin; // The node origin is in editor space
ImRect title_bar_content_rect;
ImRect rect;
struct
{
ImU32 background, background_hovered, background_selected, outline,
titlebar, titlebar_hovered, titlebar_selected;
} color_style;
struct
{
float corner_rounding;
ImVec2 padding;
} layout_style;
ImVector<int> pin_indices;
bool draggable;
NodeData()
: id(0), origin(100.0f, 100.0f), title_bar_content_rect(),
rect(ImVec2(0.0f, 0.0f), ImVec2(0.0f, 0.0f)), color_style(),
layout_style(), pin_indices(), draggable(true)
{
}
};
struct PinData
{
int id;
int parent_node_idx;
ImRect attribute_rect;
AttributeType type;
PinShape shape;
struct
{
ImU32 background, hovered;
} color_style;
PinData()
: id(), parent_node_idx(), attribute_rect(), type(AttributeType_None),
shape(PinShape_CircleFilled), color_style()
{
}
};
struct LinkData
{
int id;
int start_pin_idx, end_pin_idx;
struct
{
ImU32 base, hovered, selected;
} color_style;
LinkData() : id(), start_pin_idx(), end_pin_idx(), color_style() {}
};
struct LinkPredicate
{
bool operator()(const LinkData& lhs, const LinkData& rhs) const
{
// Do a unique compare by sorting the pins' addresses.
// This catches duplicate links, whether they are in the
// same direction or not.
// Sorting by pin index should have the uniqueness guarantees as sorting
// by id -- each unique id will get one slot in the link pool array.
int lhs_start = lhs.start_pin_idx;
int lhs_end = lhs.end_pin_idx;
int rhs_start = rhs.start_pin_idx;
int rhs_end = rhs.end_pin_idx;
if (lhs_start > lhs_end)
{
ImSwap(lhs_start, lhs_end);
}
if (rhs_start > rhs_end)
{
ImSwap(rhs_start, rhs_end);
}
return lhs_start == rhs_start && lhs_end == rhs_end;
}
};
struct BezierCurve
{
// the curve control points
ImVec2 p0, p1, p2, p3;
};
struct LinkBezierData
{
BezierCurve bezier;
int num_segments;
};
enum ClickInteractionType
{
ClickInteractionType_Node,
ClickInteractionType_Link,
ClickInteractionType_LinkCreation,
ClickInteractionType_Panning,
ClickInteractionType_BoxSelection,
ClickInteractionType_None
};
struct ClickInteractionState
{
struct
{
int start_pin_idx;
int end_pin_idx;
} link_creation;
struct
{
ImRect rect;
} box_selector;
};
struct ColorStyleElement
{
ImU32 color;
ColorStyle item;
ColorStyleElement(const ImU32 c, const ColorStyle s) : color(c), item(s) {}
};
struct StyleElement
{
StyleVar item;
float value;
StyleElement(const float value, const StyleVar variable)
: item(variable), value(value)
{
}
};
// [SECTION] global struct
// this stores data which only lives for one frame
struct
{
EditorContext* default_editor_ctx;
EditorContext* editor_ctx;
ImDrawList* canvas_draw_list;
ImVec2 canvas_origin_screen_space;
ImRect canvas_rect_screen_space;
ScopeFlags current_scope;
IO io;
Style style;
ImVector<ColorStyleElement> color_modifier_stack;
ImVector<StyleElement> style_modifier_stack;
ImGuiTextBuffer text_buffer;
int current_node_idx;
int current_pin_idx;
OptionalIndex hovered_node_idx;
OptionalIndex hovered_link_idx;
OptionalIndex hovered_pin_idx;
OptionalIndex active_pin_idx;
OptionalIndex deleted_link_idx;
bool link_created;
} g;
EditorContext& editor_context_get()
{
assert(g.editor_ctx != NULL);
return *g.editor_ctx;
}
inline bool is_mouse_hovering_near_point(const ImVec2& point, float radius)
{
ImVec2 delta = ImGui::GetIO().MousePos - point;
return (delta.x * delta.x + delta.y * delta.y) < (radius * radius);
}
inline ImVec2 eval_bezier(float t, const BezierCurve& bezier)
{
// B(t) = (1-t)**3 p0 + 3(1 - t)**2 t P1 + 3(1-t)t**2 P2 + t**3 P3
return ImVec2(
(1 - t) * (1 - t) * (1 - t) * bezier.p0.x +
3 * (1 - t) * (1 - t) * t * bezier.p1.x +
3 * (1 - t) * t * t * bezier.p2.x + t * t * t * bezier.p3.x,
(1 - t) * (1 - t) * (1 - t) * bezier.p0.y +
3 * (1 - t) * (1 - t) * t * bezier.p1.y +
3 * (1 - t) * t * t * bezier.p2.y + t * t * t * bezier.p3.y);
}
// Calculates the closest point along each bezier curve segment.
ImVec2 get_closest_point_on_cubic_bezier(
const int num_segments,
const ImVec2& p,
const BezierCurve& bezier)
{
IM_ASSERT(num_segments > 0);
ImVec2 p_last = bezier.p0;
ImVec2 p_closest;
float p_closest_dist = FLT_MAX;
float t_step = 1.0f / (float)num_segments;
for (int i = 1; i <= num_segments; ++i)
{
ImVec2 p_current = eval_bezier(t_step * i, bezier);
ImVec2 p_line = ImLineClosestPoint(p_last, p_current, p);
float dist = ImLengthSqr(p - p_line);
if (dist < p_closest_dist)
{
p_closest = p_line;
p_closest_dist = dist;
}
p_last = p_current;
}
return p_closest;
}
inline float get_distance_to_cubic_bezier(
const ImVec2& pos,
const BezierCurve& bezier,
const int num_segments)
{
const ImVec2 point_on_curve =
get_closest_point_on_cubic_bezier(num_segments, pos, bezier);
const ImVec2 to_curve = point_on_curve - pos;
return ImSqrt(ImLengthSqr(to_curve));
}
inline ImRect get_containing_rect_for_bezier_curve(const BezierCurve& bezier)
{
const ImVec2 min = ImVec2(
ImMin(bezier.p0.x, bezier.p3.x), ImMin(bezier.p0.y, bezier.p3.y));
const ImVec2 max = ImVec2(
ImMax(bezier.p0.x, bezier.p3.x), ImMax(bezier.p0.y, bezier.p3.y));
const float hover_distance = g.style.link_hover_distance;
ImRect rect(min, max);
rect.Add(bezier.p1);
rect.Add(bezier.p2);
rect.Expand(ImVec2(hover_distance, hover_distance));
return rect;
}
inline LinkBezierData get_link_renderable(
ImVec2 start,
ImVec2 end,
const AttributeType start_type,
const float line_segments_per_length)
{
assert(
(start_type == AttributeType_Input) ||
(start_type == AttributeType_Output));
if (start_type == AttributeType_Input)
{
ImSwap(start, end);
}
const float link_length = ImSqrt(ImLengthSqr(end - start));
const ImVec2 offset = ImVec2(0.25f * link_length, 0.f);
LinkBezierData link_data;
link_data.bezier.p0 = start;
link_data.bezier.p1 = start + offset;
link_data.bezier.p2 = end - offset;
link_data.bezier.p3 = end;
link_data.num_segments =
ImMax(static_cast<int>(link_length * line_segments_per_length), 1);
return link_data;
}
inline bool is_mouse_hovering_near_link(
const BezierCurve& bezier,
const int num_segments)
{
const ImVec2 mouse_pos = ImGui::GetIO().MousePos;
// First, do a simple bounding box test against the box containing the link
// to see whether calculating the distance to the link is worth doing.
const ImRect link_rect = get_containing_rect_for_bezier_curve(bezier);
if (link_rect.Contains(mouse_pos))
{
const float distance =
get_distance_to_cubic_bezier(mouse_pos, bezier, num_segments);
if (distance < g.style.link_hover_distance)
{
return true;
}
}
return false;
}
inline float eval_implicit_line_eq(
const ImVec2& p1,
const ImVec2& p2,
const ImVec2& p)
{
return (p2.y - p1.y) * p.x + (p1.x - p2.x) * p.y +
(p2.x * p1.y - p1.x * p2.y);
}
inline int sign(float val) { return int(val > 0.0f) - int(val < 0.0f); }
inline bool rectangle_overlaps_line_segment(
const ImRect& rect,
const ImVec2& p1,
const ImVec2& p2)
{
if (rect.Contains(p1) && rect.Contains(p2))
{
return true;
}
bool line_intersects_square = false;
// First, test to see if the four corners are on different sides of the line
// going through p1 and p2.
{
const int corner_signs[4] = {
sign(eval_implicit_line_eq(p1, p2, rect.Min)),
sign(eval_implicit_line_eq(p1, p2, ImVec2(rect.Max.x, rect.Min.y))),
sign(eval_implicit_line_eq(p1, p2, ImVec2(rect.Min.x, rect.Max.y))),
sign(eval_implicit_line_eq(p1, p2, rect.Max))};
for (int i = 0, iprev = 3; i < 4; ++i, iprev = (iprev + 1) % 4)
{
const int s = corner_signs[i];
const int s_prev = corner_signs[iprev];
if (s == 0)
{
break;
}
// If the sign changes at any point, then the point is on another
// side of the line than the previous point, and we know that there
// is a possible intersection.
if (s != s_prev)
{
line_intersects_square = true;
break;
}
}
}
// See if the projections of the line segment and square overlap.
if (line_intersects_square)
{
ImRect proj_rect = rect;
if (proj_rect.Min.x > proj_rect.Max.x)
{
ImSwap(proj_rect.Min.x, proj_rect.Max.x);
}
if (proj_rect.Min.y > proj_rect.Max.y)
{
ImSwap(proj_rect.Min.y, proj_rect.Max.y);
}
if ((p1.x > proj_rect.Min.x && p1.x < proj_rect.Max.x) &&
(p1.y > proj_rect.Min.y && p1.y < proj_rect.Max.y))
{
return true;
}
if ((p2.x > proj_rect.Min.x && p2.x < proj_rect.Max.x) &&
(p2.y > proj_rect.Min.y && p2.y < proj_rect.Max.y))
{
return true;
}
}
return false;
}
inline bool rectangle_overlaps_bezier(
const ImRect& rectangle,
const LinkBezierData& link_data)
{
ImVec2 current = eval_bezier(0.f, link_data.bezier);
const float dt = 1.0f / link_data.num_segments;
for (int s = 0; s < link_data.num_segments; ++s)
{
ImVec2 next =
eval_bezier(static_cast<float>((s + 1) * dt), link_data.bezier);
if (rectangle_overlaps_line_segment(rectangle, current, next))
{
return true;
}
current = next;
}
return false;
}
inline bool rectangle_overlaps_link(
const ImRect& rectangle,
const ImVec2& start,
const ImVec2& end,
const AttributeType start_type)
{
// First level: simple rejection test via rectangle overlap:
ImRect lrect = ImRect(start, end);
if (lrect.Min.x > lrect.Max.x)
{
ImSwap(lrect.Min.x, lrect.Max.x);
}
if (lrect.Min.y > lrect.Max.y)
{
ImSwap(lrect.Min.y, lrect.Max.y);
}
if (rectangle.Overlaps(lrect))
{
// First, check if either one or both endpoinds are trivially contained
// in the rectangle
if (rectangle.Contains(start) || rectangle.Contains(end))
{
return true;
}
// Second level of refinement: do a more expensive test against the
// link
const LinkBezierData link_data = get_link_renderable(
start, end, start_type, g.style.link_line_segments_per_length);
return rectangle_overlaps_bezier(rectangle, link_data);
}
return false;
}
} // namespace
// [SECTION] editor context definition
struct EditorContext
{
ObjectPool<NodeData> nodes;
ObjectPool<PinData> pins;
ObjectPool<LinkData> links;
// ui related fields
ImVec2 panning;
ImVector<int> selected_node_indices;
ImVector<int> selected_link_indices;
ClickInteractionType click_interaction_type;
ClickInteractionState click_interaction_state;
EditorContext()
: nodes(), pins(), links(), panning(0.f, 0.f), selected_node_indices(),
selected_link_indices(),
click_interaction_type(ClickInteractionType_None),
click_interaction_state()
{
}
};
namespace
{
// [SECTION] ui state logic
ImVec2 get_screen_space_pin_coordinates(
const ImRect& node_rect,
const ImRect& attribute_rect,
const AttributeType type)
{
assert(type == AttributeType_Input || type == AttributeType_Output);
const float x = type == AttributeType_Input
? (node_rect.Min.x - g.style.pin_offset)
: (node_rect.Max.x + g.style.pin_offset);
return ImVec2(x, 0.5f * (attribute_rect.Min.y + attribute_rect.Max.y));
}
ImVec2 get_screen_space_pin_coordinates(
const EditorContext& editor,
const PinData& pin)
{
const ImRect& parent_node_rect =
editor.nodes.pool[pin.parent_node_idx].rect;
return get_screen_space_pin_coordinates(
parent_node_rect, pin.attribute_rect, pin.type);
}
// These functions are here, and not members of the BoxSelector struct, because
// implementing a C API in C++ is frustrating. EditorContext has a BoxSelector
// field, but the state changes depend on the editor. So, these are implemented
// as C-style free functions so that the code is not too much of a mish-mash of
// C functions and C++ method definitions.
void begin_node_selection(EditorContext& editor, const int node_idx)
{
// Don't start selecting a node if we are e.g. already creating and dragging
// a new link! New link creation can happen when the mouse is clicked over
// a node, but within the hover radius of a pin.
if (editor.click_interaction_type != ClickInteractionType_None)
{
return;
}
editor.click_interaction_type = ClickInteractionType_Node;
// If the node is not already contained in the selection, then we want only
// the interaction node to be selected, effective immediately.
//
// Otherwise, we want to allow for the possibility of multiple nodes to be
// moved at once.
if (!editor.selected_node_indices.contains(node_idx))
{
editor.selected_node_indices.clear();
editor.selected_link_indices.clear();
editor.selected_node_indices.push_back(node_idx);
}
}
void begin_link_interaction(EditorContext& editor, const int link_idx)
{
if (editor.click_interaction_type == ClickInteractionType_LinkCreation)
{
ClickInteractionState& state = editor.click_interaction_state;
const LinkData& link = editor.links.pool[link_idx];
state.link_creation.start_pin_idx =
g.hovered_pin_idx == link.start_pin_idx ? link.end_pin_idx
: link.start_pin_idx;
g.deleted_link_idx = link_idx;
}
else
{
editor.click_interaction_type = ClickInteractionType_Link;
}
// When a link is selected, clear all other selections, and insert the link
// as the sole selection.
editor.selected_node_indices.clear();
editor.selected_link_indices.clear();
editor.selected_link_indices.push_back(link_idx);
}
void begin_link_creation(EditorContext& editor, const int hovered_pin_idx)
{
editor.click_interaction_type = ClickInteractionType_LinkCreation;
editor.click_interaction_state.link_creation.start_pin_idx =
hovered_pin_idx;
}
void begin_canvas_interaction(EditorContext& editor)
{
const bool any_ui_element_hovered =
g.hovered_node_idx.has_value() || g.hovered_link_idx.has_value() ||
g.hovered_pin_idx.has_value() || ImGui::IsAnyItemHovered();
const bool mouse_not_in_canvas =
!(g.canvas_rect_screen_space.Contains(ImGui::GetMousePos()) &&
ImGui::IsWindowHovered());
if (any_ui_element_hovered || mouse_not_in_canvas)
{
return;
}
const bool left_mouse_clicked = ImGui::IsMouseClicked(0);
const bool middle_mouse_clicked = ImGui::IsMouseClicked(2);
const bool started_panning =
g.io.emulate_three_button_mouse.enabled
? (left_mouse_clicked && *g.io.emulate_three_button_mouse.modifier)
: middle_mouse_clicked;
editor.click_interaction_type = started_panning
? ClickInteractionType_Panning
: ClickInteractionType_BoxSelection;
if (editor.click_interaction_type == ClickInteractionType_BoxSelection)
{
editor.click_interaction_state.box_selector.rect.Min =
ImGui::GetIO().MousePos;
}
}
void box_selector_update_selection(EditorContext& editor, ImRect box_rect)
{
// Invert box selector coordinates as needed
if (box_rect.Min.x > box_rect.Max.x)
{
ImSwap(box_rect.Min.x, box_rect.Max.x);
}
if (box_rect.Min.y > box_rect.Max.y)
{
ImSwap(box_rect.Min.y, box_rect.Max.y);
}
// Update node selection
editor.selected_node_indices.clear();
// Test for overlap against node rectangles
for (int node_idx = 0; node_idx < editor.nodes.pool.size(); ++node_idx)
{
if (editor.nodes.in_use[node_idx])
{
NodeData& node = editor.nodes.pool[node_idx];
if (box_rect.Overlaps(node.rect))
{
editor.selected_node_indices.push_back(node_idx);
}
}
}
// Update link selection
editor.selected_link_indices.clear();
// Test for overlap against links
for (int link_idx = 0; link_idx < editor.links.pool.size(); ++link_idx)
{
if (editor.links.in_use[link_idx])
{
const LinkData& link = editor.links.pool[link_idx];
const PinData& pin_start = editor.pins.pool[link.start_pin_idx];
const PinData& pin_end = editor.pins.pool[link.end_pin_idx];
const ImRect& node_start_rect =
editor.nodes.pool[pin_start.parent_node_idx].rect;
const ImRect& node_end_rect =
editor.nodes.pool[pin_end.parent_node_idx].rect;
const ImVec2 start = get_screen_space_pin_coordinates(
node_start_rect, pin_start.attribute_rect, pin_start.type);
const ImVec2 end = get_screen_space_pin_coordinates(
node_end_rect, pin_end.attribute_rect, pin_end.type);
// Test
if (rectangle_overlaps_link(box_rect, start, end, pin_start.type))
{
editor.selected_link_indices.push_back(link_idx);
}
}
}
}
void translate_selected_nodes(EditorContext& editor)
{
if (ImGui::IsMouseDragging(0))
{
const ImGuiIO& io = ImGui::GetIO();
for (int i = 0; i < editor.selected_node_indices.size(); ++i)
{
const int node_idx = editor.selected_node_indices[i];
NodeData& node = editor.nodes.pool[node_idx];
if (node.draggable)
{
node.origin += io.MouseDelta;
}
}
}
}
bool finish_link_at_hovered_pin(
EditorContext& editor,
const OptionalIndex maybe_hovered_pin_idx)
{
if (!maybe_hovered_pin_idx.has_value())
{
return false;
}
if (maybe_hovered_pin_idx ==
editor.click_interaction_state.link_creation.start_pin_idx)
{
return false;
}
const int end_pin_idx = maybe_hovered_pin_idx.value();
LinkData test_link;
test_link.start_pin_idx =
editor.click_interaction_state.link_creation.start_pin_idx;
test_link.end_pin_idx = end_pin_idx;
if (editor.links.contains(test_link, LinkPredicate()))
{
return false;
}
editor.click_interaction_state.link_creation.end_pin_idx = end_pin_idx;
return true;
}
void click_interaction_update(EditorContext& editor)
{
const bool left_mouse_released = ImGui::IsMouseReleased(0);
switch (editor.click_interaction_type)
{
case ClickInteractionType_BoxSelection:
{
ImRect& box_rect = editor.click_interaction_state.box_selector.rect;
box_rect.Max = ImGui::GetIO().MousePos;
box_selector_update_selection(editor, box_rect);
const ImU32 box_selector_color = g.style.colors[ColorStyle_BoxSelector];
const ImU32 box_selector_outline =
g.style.colors[ColorStyle_BoxSelectorOutline];
g.canvas_draw_list->AddRectFilled(
box_rect.Min, box_rect.Max, box_selector_color);
g.canvas_draw_list->AddRect(
box_rect.Min, box_rect.Max, box_selector_outline);
if (left_mouse_released)
{
editor.click_interaction_type = ClickInteractionType_None;
}
}
break;
case ClickInteractionType_Node:
{
translate_selected_nodes(editor);
if (left_mouse_released)
{
editor.click_interaction_type = ClickInteractionType_None;
}
}
break;
case ClickInteractionType_Link:
{
if (left_mouse_released)
{
editor.click_interaction_type = ClickInteractionType_None;
}
}
break;
case ClickInteractionType_LinkCreation:
{
const PinData& pin = editor.pins.pool[editor.click_interaction_state
.link_creation.start_pin_idx];
const ImVec2 start_pos = get_screen_space_pin_coordinates(editor, pin);
// If we are within the hover radius of a receiving pin, snap the link
// endpoint to it
const ImVec2 end_pos =
g.hovered_pin_idx.has_value()
? get_screen_space_pin_coordinates(
editor, editor.pins.pool[g.hovered_pin_idx.value()])
: ImGui::GetIO().MousePos;
const LinkBezierData link_data = get_link_renderable(
start_pos,
end_pos,
pin.type,
g.style.link_line_segments_per_length);
g.canvas_draw_list->AddBezierCurve(
link_data.bezier.p0,
link_data.bezier.p1,
link_data.bezier.p2,
link_data.bezier.p3,
g.style.colors[ColorStyle_Link],
g.style.link_thickness,
link_data.num_segments);
if (left_mouse_released)
{
g.link_created =
finish_link_at_hovered_pin(editor, g.hovered_pin_idx);
editor.click_interaction_type = ClickInteractionType_None;
}
}
break;
case ClickInteractionType_Panning:
{
const bool dragging =
g.io.emulate_three_button_mouse.enabled
? (ImGui::IsMouseDragging(0, 0.f) &&
(*g.io.emulate_three_button_mouse.modifier))
: ImGui::IsMouseDragging(2, 0.f);
if (dragging)
{
editor.panning += ImGui::GetIO().MouseDelta;
}
else
{
editor.click_interaction_type = ClickInteractionType_None;
}
}
break;
case ClickInteractionType_None:
break;
default:
assert(!"Unreachable code!");
break;
}
}