forked from Nelarius/imnodes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimnodes.cpp
executable file
·2204 lines (1885 loc) · 61.9 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
863
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
};
// 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;
}
static const int invalid_index = -1;
private:
int m_index;
};
// [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, OptionalIndex::invalid_index);
free_list.push_back(i);
}
}
// set all values to false
memset(in_use.Data, 0, sizeof(bool) * in_use.size());
}
inline T& find_or_create_new(int id)
{
OptionalIndex index =
id_map.GetInt(ImGuiID(id), OptionalIndex::invalid_index);
if (!index.has_value())
{
if (free_list.size() > 0)
{
index = free_list.back();
free_list.pop_back();
}
else
{
index = pool.size();
pool.push_back(T());
in_use.push_back(true);
}
id_map.SetInt(id, index.value());
}
in_use[index.value()] = true;
return pool[index.value()];
}
// 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;
}
};
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<ImRect> attribute_rects;
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(), attribute_rects(), pin_indices(), draggable(true)
{
}
};
struct PinData
{
int id;
int node_idx;
int attribute_idx;
AttributeType type;
PinShape shape;
struct
{
ImU32 background, hovered;
} color_style;
PinData()
: node_idx(), attribute_idx(), type(AttributeType_None), color_style()
{
}
PinData(int nidx, int aindx, AttributeType t)
: node_idx(nidx), attribute_idx(aindx), type(t), color_style()
{
}
};
struct LinkData
{
int id;
int start_attribute_id, end_attribute_id;
struct
{
ImU32 base, hovered, selected;
} color_style;
LinkData() : id(), start_attribute_id(), end_attribute_id(), color_style()
{
}
};
struct LinkPredicate
{
bool operator()(const LinkData& lhs, const LinkData& rhs) const
{
// Do a unique compare by sorting the attribute ids.
// This catches duplicate links, whether they are in the
// same direction or not.
int lhs_start = lhs.start_attribute_id;
int lhs_end = lhs.end_attribute_id;
int rhs_start = rhs.start_attribute_id;
int rhs_end = rhs.end_attribute_id;
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;
}
};
// This represents a link which the user starts to dragging from a pin, i.e. it
// is not yet complete.
struct PartialLink
{
PartialLink()
: m_start_attribute_id(), m_end_attribute_id(), m_start_engaged(false),
m_end_engaged(false)
{
}
// Observers
inline bool has_start_attribute() const { return m_start_engaged; }
inline bool is_complete() const { return m_end_engaged; }
inline int start_attribute_id() const
{
assert(m_start_engaged);
return m_start_attribute_id;
}
inline int end_attribute_id() const
{
assert(m_start_engaged && m_end_engaged);
return m_end_attribute_id;
}
// Modifiers
void start_link(const int id)
{
// Don't start a link twice!
assert(!m_start_engaged);
m_start_attribute_id = id;
m_start_engaged = true;
}
void finish_link(const int id)
{
// Don't finish a link before starting it!
assert(m_start_engaged);
assert(!m_end_engaged);
m_end_attribute_id = id;
m_end_engaged = true;
}
void reset()
{
m_start_engaged = false;
m_end_engaged = false;
}
private:
int m_start_attribute_id, m_end_attribute_id;
bool m_start_engaged, m_end_engaged;
};
struct BezierCurve
{
// the curve control points
ImVec2 p0, p1, p2, p3;
};
struct LinkBezierData
{
BezierCurve bezier;
int num_segments;
};
enum BoxSelectorState
{
BoxSelectorState_Started,
BoxSelectorState_Dragging,
BoxSelectorState_None
};
struct BoxSelector
{
BoxSelectorState state;
ImRect box_rect;
BoxSelector() : state(BoxSelectorState_None), box_rect() {}
};
enum NodeInteractionState
{
NodeInteractionState_MouseDown,
NodeInteractionState_None
};
struct NodeInteraction
{
NodeInteractionState state;
// TODO: doesn't look like this struct is really needed
NodeInteraction() : state(NodeInteractionState_None) {}
};
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;
Style style;
ImVector<ColorStyleElement> color_modifier_stack;
ImVector<StyleElement> style_modifier_stack;
ImGuiTextBuffer text_buffer;
// TODO: these are just used to indicate the current node, attribute, pin
// being created by the user. In the long run, it may be much more efficient
// just to stick an optional reference here, so we don't need to do an
// editor->pool->index operation everytime we need to do something small
// with the node or pin.
// Indexes into the node pool array
OptionalIndex current_node_idx;
// This field indexes into the current node data's attribute array
OptionalIndex current_attribute_idx;
// Indexes into the pin pool array
OptionalIndex current_pin_idx;
struct
{
OptionalIndex node_idx;
OptionalIndex attribute_idx;
} active_item;
OptionalIndex hovered_node_idx;
OptionalIndex hovered_link_idx;
OptionalIndex hovered_pin_idx;
bool link_dropped;
} 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);
}
// Divides the bezier curve into n segments. Evaluates the distance to each
// segment. Chooses the segment with the smallest distance, and repeats the
// algorithm on that segment, for the given number of iterations.
inline float get_closest_point_on_cubic_bezier(
const int num_iterations,
const int num_segments,
const ImVec2& pos,
const BezierCurve& bezier)
{
assert(num_iterations > 0 && num_segments > 0);
float tstart = 0.0f;
float tend = 1.0f;
float tbest = 0.5f;
float best_distance = FLT_MAX;
for (int i = 0; i < num_iterations; ++i)
{
// split the current t-range to segments
const float dt = (tend - tstart) / num_segments;
for (int s = 0; s < num_segments; ++s)
{
const float tmid = tstart + dt * (float(s) + 0.5f);
const ImVec2 bt = eval_bezier(tmid, bezier);
const ImVec2 dv = bt - pos;
float cur_distance = ImLengthSqr(dv);
if (cur_distance < best_distance)
{
best_distance = cur_distance;
tbest = tmid;
}
}
// shrink the current t-range to the best segment
tstart = tbest - 0.5f * dt;
tend = tbest + 0.5f * dt;
}
return tbest;
}
inline float get_distance_to_cubic_bezier(
const ImVec2& pos,
const BezierCurve& bezier)
{
const int segments = 5;
const float length = ImSqrt(ImLengthSqr(bezier.p3 - bezier.p2)) +
ImSqrt(ImLengthSqr(bezier.p2 - bezier.p1)) +
ImSqrt(ImLengthSqr(bezier.p1 - bezier.p0));
const float iterations_per_length = 0.01f;
const int iterations =
int(ImClamp(length * iterations_per_length, 2.0f, 8.f));
const float t =
get_closest_point_on_cubic_bezier(iterations, segments, pos, bezier);
const ImVec2 point_on_curve = eval_bezier(t, 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));
ImRect rect(min, max);
rect.Add(bezier.p1);
rect.Add(bezier.p2);
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);
}
// function arguments assed by value, since we mutate them
const ImVec2 delta = end - start;
const float link_length = ImSqrt(ImLengthSqr(delta));
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(int(link_length * line_segments_per_length), 1);
return link_data;
}
inline bool is_mouse_hovering_near_link(const BezierCurve& bezier)
{
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);
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_nodes;
ImVector<int> selected_links;
PartialLink link_dragged;
BoxSelector box_selector;
NodeInteraction node_interaction;
EditorContext()
: nodes(), pins(), links(), panning(0.f, 0.f), link_dragged(),
box_selector()
{
}
};
namespace
{
// [SECTION] ui state logic
ImVec2 get_screen_space_pin_coordinates(
const ImRect& node_rect,
const ImRect& attr_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 * (attr_rect.Min.y + attr_rect.Max.y));
}
ImVec2 get_screen_space_pin_coordinates(
const EditorContext& editor,
const int pin_id)
{
const OptionalIndex pin_idx =
editor.pins.id_map.GetInt(pin_id, OptionalIndex::invalid_index);
const PinData& pin = editor.pins.pool[pin_idx.value()];
const NodeData& node = editor.nodes.pool[pin.node_idx];
return get_screen_space_pin_coordinates(
node.rect, node.attribute_rects[pin.attribute_idx], 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 box_selector_begin(BoxSelector& box_selector)
{
box_selector.state = BoxSelectorState_Started;
}
bool box_selector_active(const BoxSelector& box_selector)
{
return box_selector.state != BoxSelectorState_None;
}
void box_selector_process(
const BoxSelector& box_selector,
EditorContext& editor)
{
ImRect box_rect = box_selector.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);
}
// Clearing selected nodes list
editor.selected_nodes.clear();
// Test for overlap against node rectangles
for (int i = 0; i < editor.nodes.pool.size(); i++)
{
if (editor.nodes.in_use[i])
{
const ImRect& node_rect = editor.nodes.pool[i].rect;
if (box_rect.Overlaps(node_rect))
{
editor.selected_nodes.push_back(i);
}
}
}
// Clearing selected links list
editor.selected_links.clear();
// Test for overlap against links
for (int i = 0; i < editor.links.pool.size(); ++i)
{
if (editor.links.in_use[i])
{
const LinkData& link = editor.links.pool[i];
const ImVec2 start = get_screen_space_pin_coordinates(
editor, link.start_attribute_id);
const ImVec2 end =
get_screen_space_pin_coordinates(editor, link.end_attribute_id);
const PinData& pin_start =
editor.pins.find_or_create_new(link.start_attribute_id);
// Test
if (rectangle_overlaps_link(box_rect, start, end, pin_start.type))
{
editor.selected_links.push_back(i);
}
}
}
}
void box_selector_update(
BoxSelector& box_selector,
EditorContext& editor_ctx,
const ImGuiIO& io)
{
const ImU32 box_selector_color = g.style.colors[ColorStyle_BoxSelector];
const ImU32 box_selector_outline =
g.style.colors[ColorStyle_BoxSelectorOutline];
switch (box_selector.state)
{
case BoxSelectorState_Started:
box_selector.box_rect.Min = io.MousePos;
box_selector.state = BoxSelectorState_Dragging;
// fallthrough to next case to get the rest of the state &
// render
case BoxSelectorState_Dragging:
{
box_selector.box_rect.Max = io.MousePos;
g.canvas_draw_list->AddRectFilled(
box_selector.box_rect.Min,
box_selector.box_rect.Max,
box_selector_color);
g.canvas_draw_list->AddRect(
box_selector.box_rect.Min,
box_selector.box_rect.Max,
box_selector_outline);
box_selector_process(box_selector, editor_ctx);
if (ImGui::IsMouseReleased(0))
{
box_selector.state = BoxSelectorState_None;
}
}
break;
default:
assert(!"Unreachable code!");
break;
}
}
void node_interaction_begin(EditorContext& editor, const int node_idx)
{
NodeInteraction& node_interaction = editor.node_interaction;
node_interaction.state = NodeInteractionState_MouseDown;
// 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_nodes.contains(node_idx))
{
editor.selected_nodes.clear();
editor.selected_nodes.push_back(node_idx);
}
}
void node_interaction_update(EditorContext& editor)
{
NodeInteraction& node_interaction = editor.node_interaction;
switch (node_interaction.state)
{
case NodeInteractionState_MouseDown:
{
if (ImGui::IsMouseDragging(0) &&
(!editor.link_dragged.has_start_attribute()))
{
for (int i = 0; i < editor.selected_nodes.size(); ++i)
{
const int idx = editor.selected_nodes[i];
NodeData& node = editor.nodes.pool[idx];
if (node.draggable)
{
node.origin += ImGui::GetIO().MouseDelta;
}
}
}
if (ImGui::IsMouseReleased(0))
{
node_interaction.state = NodeInteractionState_None;
}
}
break;
case NodeInteractionState_None:
break;
default:
assert(!"Unreachable code!");
break;
}
}
// [SECTION] render helpers
inline ImVec2 screen_space_to_grid_space(const ImVec2& v)
{
const EditorContext& editor = editor_context_get();
return v - g.canvas_origin_screen_space - editor.panning;
}
inline ImVec2 grid_space_to_editor_space(const ImVec2& v)
{
const EditorContext& editor = editor_context_get();
return v + editor.panning;
}
inline ImVec2 editor_space_to_screen_space(const ImVec2& v)
{
return g.canvas_origin_screen_space + v;
}
inline ImRect get_item_rect()
{
return ImRect(ImGui::GetItemRectMin(), ImGui::GetItemRectMax());
}
inline ImVec2 get_node_title_bar_origin(const NodeData& node)
{
return node.origin + node.layout_style.padding;
}
inline ImVec2 get_node_content_origin(const NodeData& node)
{
const ImVec2 title_bar_height = ImVec2(
0.f,
node.title_bar_content_rect.GetHeight() +
2.0f * node.layout_style.padding.y);
return node.origin + title_bar_height + node.layout_style.padding;
}
void draw_grid(EditorContext& editor, const ImVec2& canvas_size)
{
const ImVec2 offset = editor.panning;
for (float x = fmodf(offset.x, g.style.grid_spacing); x < canvas_size.x;
x += g.style.grid_spacing)
{
g.canvas_draw_list->AddLine(
editor_space_to_screen_space(ImVec2(x, 0.0f)),
editor_space_to_screen_space(ImVec2(x, canvas_size.y)),
g.style.colors[ColorStyle_GridLine]);
}
for (float y = fmodf(offset.y, g.style.grid_spacing); y < canvas_size.y;
y += g.style.grid_spacing)
{
g.canvas_draw_list->AddLine(
editor_space_to_screen_space(ImVec2(0.0f, y)),
editor_space_to_screen_space(ImVec2(canvas_size.x, y)),