forked from Nelarius/imnodes
-
Notifications
You must be signed in to change notification settings - Fork 2
/
imnodes_internal.h
482 lines (393 loc) · 11.9 KB
/
imnodes_internal.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
#pragma once
#include <imgui.h>
#define IMGUI_DEFINE_MATH_OPERATORS
#include <imgui_internal.h>
#include <assert.h>
#include <limits.h>
// the structure of this file:
//
// [SECTION] internal enums
// [SECTION] internal data structures
// [SECTION] global and editor context structs
// [SECTION] object pool implementation
struct ImNodesContext;
extern ImNodesContext* GImNodes;
// [SECTION] internal enums
typedef int ImNodesScope;
typedef int ImNodesAttributeType;
typedef int ImNodesUIState;
typedef int ImNodesClickInteractionType;
typedef int ImNodesLinkCreationType;
enum ImNodesScope_
{
ImNodesScope_None = 1,
ImNodesScope_Editor = 1 << 1,
ImNodesScope_Node = 1 << 2,
ImNodesScope_Attribute = 1 << 3
};
enum ImNodesAttributeType_
{
ImNodesAttributeType_None,
ImNodesAttributeType_Input,
ImNodesAttributeType_Output
};
enum ImNodesUIState_
{
ImNodesUIState_None = 0,
ImNodesUIState_LinkStarted = 1 << 0,
ImNodesUIState_LinkDropped = 1 << 1,
ImNodesUIState_LinkCreated = 1 << 2
};
enum ImNodesClickInteractionType_
{
ImNodesClickInteractionType_Node,
ImNodesClickInteractionType_Link,
ImNodesClickInteractionType_LinkCreation,
ImNodesClickInteractionType_Panning,
ImNodesClickInteractionType_BoxSelection,
ImNodesClickInteractionType_MiniMapPanning,
ImNodesClickInteractionType_MiniMapZooming,
ImNodesClickInteractionType_MiniMapSnapping,
ImNodesClickInteractionType_ImGuiItem,
ImNodesClickInteractionType_None
};
enum ImNodesLinkCreationType_
{
ImNodesLinkCreationType_Standard,
ImNodesLinkCreationType_FromDetach
};
// Callback type used to specify special behavior when hovering a node in the minimap
typedef void (*ImNodesMiniMapNodeHoveringCallback)(int, void*);
// [SECTION] internal data structures
// The object T must have the following interface:
//
// struct T
// {
// T();
//
// int id;
// };
template<typename T>
struct ImObjectPool
{
ImVector<T> Pool;
ImVector<bool> InUse;
ImVector<int> FreeList;
ImGuiStorage IdMap;
ImObjectPool() : Pool(), InUse(), FreeList(), IdMap() {}
};
// Emulates std::optional<int> using the sentinel value `INVALID_INDEX`.
struct ImOptionalIndex
{
ImOptionalIndex() : _Index(INVALID_INDEX) {}
ImOptionalIndex(const int value) : _Index(value) {}
// Observers
inline bool HasValue() const { return _Index != INVALID_INDEX; }
inline int Value() const
{
assert(HasValue());
return _Index;
}
// Modifiers
inline ImOptionalIndex& operator=(const int value)
{
_Index = value;
return *this;
}
inline void Reset() { _Index = INVALID_INDEX; }
inline bool operator==(const ImOptionalIndex& rhs) const { return _Index == rhs._Index; }
inline bool operator==(const int rhs) const { return _Index == rhs; }
inline bool operator!=(const ImOptionalIndex& rhs) const { return _Index != rhs._Index; }
inline bool operator!=(const int rhs) const { return _Index != rhs; }
static const int INVALID_INDEX = -1;
private:
int _Index;
};
struct ImNodeData
{
int Id;
ImVec2 Origin; // The node origin is in editor space
ImRect TitleBarContentRect;
ImRect Rect;
struct
{
ImU32 Background, BackgroundHovered, BackgroundSelected, Outline, Titlebar, TitlebarHovered,
TitlebarSelected;
} ColorStyle;
struct
{
float CornerRounding;
ImVec2 Padding;
float BorderThickness;
} LayoutStyle;
ImVector<int> PinIndices;
bool Draggable;
ImNodeData(const int node_id)
: Id(node_id), Origin(100.0f, 100.0f), TitleBarContentRect(),
Rect(ImVec2(0.0f, 0.0f), ImVec2(0.0f, 0.0f)), ColorStyle(), LayoutStyle(), PinIndices(),
Draggable(true)
{
}
~ImNodeData() { Id = INT_MIN; }
};
struct ImPinData
{
int Id;
int ParentNodeIdx;
ImRect AttributeRect;
ImNodesAttributeType Type;
ImNodesPinShape Shape;
ImVec2 Pos; // screen-space coordinates
int Flags;
struct
{
ImU32 Background, Hovered;
} ColorStyle;
ImPinData(const int pin_id)
: Id(pin_id), ParentNodeIdx(), AttributeRect(), Type(ImNodesAttributeType_None),
Shape(ImNodesPinShape_CircleFilled), Pos(), Flags(ImNodesAttributeFlags_None),
ColorStyle()
{
}
};
struct ImLinkData
{
int Id;
int StartPinIdx, EndPinIdx;
struct
{
ImU32 Base, Hovered, Selected;
} ColorStyle;
ImLinkData(const int link_id) : Id(link_id), StartPinIdx(), EndPinIdx(), ColorStyle() {}
};
struct ImClickInteractionState
{
ImNodesClickInteractionType Type;
struct
{
int StartPinIdx;
ImOptionalIndex EndPinIdx;
ImNodesLinkCreationType Type;
} LinkCreation;
struct
{
ImRect Rect;
} BoxSelector;
ImClickInteractionState() : Type(ImNodesClickInteractionType_None) {}
};
struct ImNodesColElement
{
ImU32 Color;
ImNodesCol Item;
ImNodesColElement(const ImU32 c, const ImNodesCol s) : Color(c), Item(s) {}
};
struct ImNodesStyleVarElement
{
ImNodesStyleVar Item;
float Value;
ImNodesStyleVarElement(const float value, const ImNodesStyleVar variable)
: Item(variable), Value(value)
{
}
};
// [SECTION] global and editor context structs
struct ImNodesEditorContext
{
ImObjectPool<ImNodeData> Nodes;
ImObjectPool<ImPinData> Pins;
ImObjectPool<ImLinkData> Links;
ImVector<int> NodeDepthOrder;
// ui related fields
ImVec2 Panning;
ImVector<int> SelectedNodeIndices;
ImVector<int> SelectedLinkIndices;
ImClickInteractionState ClickInteraction;
ImNodesEditorContext()
: Nodes(), Pins(), Links(), Panning(0.f, 0.f), SelectedNodeIndices(), SelectedLinkIndices(),
ClickInteraction()
{
}
};
struct ImNodesContext
{
ImNodesEditorContext* DefaultEditorCtx;
ImNodesEditorContext* EditorCtx;
// Canvas draw list and helper state
ImDrawList* CanvasDrawList;
ImGuiStorage NodeIdxToSubmissionIdx;
ImVector<int> NodeIdxSubmissionOrder;
ImVector<int> NodeIndicesOverlappingWithMouse;
ImVector<int> OccludedPinIndices;
// Canvas extents
ImVec2 CanvasOriginScreenSpace;
ImRect CanvasRectScreenSpace;
// MiniMap state
ImRect MiniMapRectScreenSpace;
ImVec2 MiniMapRectSnappingOffset;
float MiniMapZoom;
ImNodesMiniMapNodeHoveringCallback MiniMapNodeHoveringCallback;
void* MiniMapNodeHoveringCallbackUserData;
// Debug helpers
ImNodesScope CurrentScope;
// Configuration state
ImNodesIO Io;
ImNodesStyle Style;
ImVector<ImNodesColElement> ColorModifierStack;
ImVector<ImNodesStyleVarElement> StyleModifierStack;
ImGuiTextBuffer TextBuffer;
int CurrentAttributeFlags;
ImVector<int> AttributeFlagStack;
// UI element state
int CurrentNodeIdx;
int CurrentPinIdx;
int CurrentAttributeId;
ImOptionalIndex HoveredNodeIdx;
ImOptionalIndex HoveredLinkIdx;
ImOptionalIndex HoveredPinIdx;
ImOptionalIndex DeletedLinkIdx;
ImOptionalIndex SnapLinkIdx;
// Event helper state
// TODO: this should be a part of a state machine, and not a member of the global struct.
// Unclear what parts of the code this relates to.
int ImNodesUIState;
int ActiveAttributeId;
bool ActiveAttribute;
// ImGui::IO cache
ImVec2 MousePos;
bool LeftMouseClicked;
bool LeftMouseReleased;
bool AltMouseClicked;
bool LeftMouseDragging;
bool AltMouseDragging;
float AltMouseScrollDelta;
};
namespace ImNodes
{
static inline ImNodesEditorContext& EditorContextGet()
{
// No editor context was set! Did you forget to call ImNodes::CreateContext()?
assert(GImNodes->EditorCtx != NULL);
return *GImNodes->EditorCtx;
}
// [SECTION] ObjectPool implementation
template<typename T>
static inline int ObjectPoolFind(const ImObjectPool<T>& objects, const int id)
{
const int index = objects.IdMap.GetInt(static_cast<ImGuiID>(id), -1);
return index;
}
template<typename T>
static inline void ObjectPoolUpdate(ImObjectPool<T>& objects)
{
objects.FreeList.clear();
for (int i = 0; i < objects.InUse.size(); ++i)
{
if (!objects.InUse[i])
{
objects.IdMap.SetInt(objects.Pool[i].Id, -1);
objects.FreeList.push_back(i);
(objects.Pool.Data + i)->~T();
}
}
}
template<>
inline void ObjectPoolUpdate(ImObjectPool<ImNodeData>& nodes)
{
nodes.FreeList.clear();
for (int i = 0; i < nodes.InUse.size(); ++i)
{
if (nodes.InUse[i])
{
nodes.Pool[i].PinIndices.clear();
}
else
{
const int previous_id = nodes.Pool[i].Id;
const int previous_idx = nodes.IdMap.GetInt(previous_id, -1);
if (previous_idx != -1)
{
assert(previous_idx == i);
// Remove node idx form depth stack the first time we detect that this idx slot is
// unused
ImVector<int>& depth_stack = EditorContextGet().NodeDepthOrder;
const int* const elem = depth_stack.find(i);
assert(elem != depth_stack.end());
depth_stack.erase(elem);
}
nodes.IdMap.SetInt(previous_id, -1);
nodes.FreeList.push_back(i);
(nodes.Pool.Data + i)->~ImNodeData();
}
}
}
template<typename T>
static inline void ObjectPoolReset(ImObjectPool<T>& objects)
{
if (!objects.InUse.empty())
{
memset(objects.InUse.Data, 0, objects.InUse.size_in_bytes());
}
}
template<typename T>
static inline int ObjectPoolFindOrCreateIndex(ImObjectPool<T>& objects, const int id)
{
int index = objects.IdMap.GetInt(static_cast<ImGuiID>(id), -1);
// Construct new object
if (index == -1)
{
if (objects.FreeList.empty())
{
index = objects.Pool.size();
IM_ASSERT(objects.Pool.size() == objects.InUse.size());
const int new_size = objects.Pool.size() + 1;
objects.Pool.resize(new_size);
objects.InUse.resize(new_size);
}
else
{
index = objects.FreeList.back();
objects.FreeList.pop_back();
}
IM_PLACEMENT_NEW(objects.Pool.Data + index) T(id);
objects.IdMap.SetInt(static_cast<ImGuiID>(id), index);
}
// Flag it as used
objects.InUse[index] = true;
return index;
}
template<>
inline int ObjectPoolFindOrCreateIndex(ImObjectPool<ImNodeData>& nodes, const int node_id)
{
int node_idx = nodes.IdMap.GetInt(static_cast<ImGuiID>(node_id), -1);
// Construct new node
if (node_idx == -1)
{
if (nodes.FreeList.empty())
{
node_idx = nodes.Pool.size();
IM_ASSERT(nodes.Pool.size() == nodes.InUse.size());
const int new_size = nodes.Pool.size() + 1;
nodes.Pool.resize(new_size);
nodes.InUse.resize(new_size);
}
else
{
node_idx = nodes.FreeList.back();
nodes.FreeList.pop_back();
}
IM_PLACEMENT_NEW(nodes.Pool.Data + node_idx) ImNodeData(node_id);
nodes.IdMap.SetInt(static_cast<ImGuiID>(node_id), node_idx);
ImNodesEditorContext& editor = EditorContextGet();
editor.NodeDepthOrder.push_back(node_idx);
}
// Flag node as used
nodes.InUse[node_idx] = true;
return node_idx;
}
template<typename T>
static inline T& ObjectPoolFindOrCreateObject(ImObjectPool<T>& objects, const int id)
{
const int index = ObjectPoolFindOrCreateIndex(objects, id);
return objects.Pool[index];
}
} // namespace ImNodes