-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathimgui_node_editor_internal.inl
More file actions
424 lines (316 loc) · 10.7 KB
/
imgui_node_editor_internal.inl
File metadata and controls
424 lines (316 loc) · 10.7 KB
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
//------------------------------------------------------------------------------
// 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
//------------------------------------------------------------------------------
# ifndef __IMGUI_NODE_EDITOR_INTERNAL_INL__
# define __IMGUI_NODE_EDITOR_INTERNAL_INL__
# pragma once
//------------------------------------------------------------------------------
# include "imgui_node_editor_internal.h"
//------------------------------------------------------------------------------
namespace ax {
namespace NodeEditor {
namespace Detail {
//------------------------------------------------------------------------------
//inline ImRect ToRect(const ax::rectf& rect)
//{
// return ImRect(
// to_imvec(rect.top_left()),
// to_imvec(rect.bottom_right())
// );
//}
//
//inline ImRect ToRect(const ax::rect& rect)
//{
// return ImRect(
// to_imvec(rect.top_left()),
// to_imvec(rect.bottom_right())
// );
//}
inline ImRect ImGui_GetItemRect()
{
return ImRect(ImGui::GetItemRectMin(), ImGui::GetItemRectMax());
}
inline bool operator==(const ImRect& lhs, const ImRect& rhs)
{
return lhs.Min == rhs.Min && lhs.Max == rhs.Max;
}
inline bool operator!=(const ImRect& lhs, const ImRect& rhs)
{
return !(lhs == rhs);
}
//------------------------------------------------------------------------------
# define CHECK(condition, message) if (condition) {} else { if (error) { if (!error->empty()) *error += '\n'; *error += string() + message; } return false; }
# define CHECK_TYPE(value, _type) CHECK(value.type() == _type, "Expected \"" + ToString(_type) + "\" but got \"" + ToString(value.type()) + "\".")
# define CHECK_KEY(value, key) CHECK(value.contains(key), "Required key \"" + key + "\" is missing.")
# define PARSE_IMPL(value, out, valueName) CHECK(Parse(value, out, error), "Failed to parse " + valueName + ".")
# define PARSE(value, out) CHECK(Parse(value, out, error), "Failed to parse value.")
# define CHECK_AND_PARSE(value, key, out) CHECK_KEY(value, key); PARSE_IMPL(value[key], out, "\"" + key + "\"")
# define CHECK_AND_PARSE_OPT(value, key, out) if (value.contains(key)) { CHECK_AND_PARSE(value, key, out); } else {}
inline bool Serialization::Parse(const string& str, ObjectId& result, string* error)
{
(void)error;
auto separator = str.find_first_of(':');
auto idStart = str.c_str() + ((separator != std::string::npos) ? separator + 1 : 0);
auto id = reinterpret_cast<void*>(strtoull(idStart, nullptr, 10));
if (str.compare(0, separator, "node") == 0)
result = ObjectId(NodeId(id));
else if (str.compare(0, separator, "link") == 0)
result = ObjectId(LinkId(id));
else if (str.compare(0, separator, "pin") == 0)
result = ObjectId(PinId(id));
else
// fallback to old format
result = ObjectId(NodeId(id)); //return ObjectId();
return true;
}
inline bool Serialization::Parse(const string& str, NodeId& result, string* error)
{
ObjectId objectId;
if (!Parse(str, objectId, error))
return false;
CHECK(objectId.IsNodeId(), "\"" + str + "\" is not a NodeId.");
result = objectId.AsNodeId();
return true;
}
inline bool Serialization::Parse(const string& str, json::value& result, string* error)
{
result = json::value::parse(str);
CHECK(result.is_discarded(), "Failed to parse json");
return true;
}
inline bool Serialization::Parse(const json::value& v, float& result, string* error)
{
CHECK_TYPE(v, json::type_t::number);
result = static_cast<float>(v.get<double>());
return true;
}
inline bool Serialization::Parse(const json::value& v, ImVec2& result, string* error)
{
CHECK_TYPE(v, json::type_t::object);
ImVec2 value;
CHECK_AND_PARSE(v, "x", value.x);
CHECK_AND_PARSE(v, "y", value.y);
result = value;
return true;
}
inline bool Serialization::Parse(const json::value& v, ImRect& result, string* error)
{
CHECK_TYPE(v, json::type_t::object);
ImRect value;
CHECK_AND_PARSE(v, "min", value.Min);
CHECK_AND_PARSE(v, "max", value.Max);
result = value;
return true;
}
inline bool Serialization::Parse(const json::value& v, NodeState& result, string* error)
{
CHECK_TYPE(v, json::type_t::object);
NodeState state;
CHECK_AND_PARSE (v, "location", state.m_Location);
CHECK_AND_PARSE_OPT(v, "group_size", state.m_GroupSize);
result = std::move(state);
return true;
}
inline bool Serialization::Parse(const json::value& v, NodesState& result, string* error)
{
CHECK_TYPE(v, json::type_t::object);
NodesState state;
PARSE(v, state.m_Nodes);
result = std::move(state);
return true;
}
inline bool Serialization::Parse(const json::value& v, SelectionState& result, string* error)
{
CHECK_TYPE(v, json::type_t::array);
SelectionState state;
PARSE(v, state.m_Selection);
result = std::move(state);
return true;
}
inline bool Serialization::Parse(const json::value& v, ViewState& result, string* error)
{
CHECK_TYPE(v, json::type_t::object);
ViewState state;
CHECK_AND_PARSE (v, "scroll", state.m_ViewScroll);
CHECK_AND_PARSE (v, "zoom", state.m_ViewZoom);
CHECK_AND_PARSE_OPT(v, "visible_rect", state.m_VisibleRect);
result = state;
return true;
}
inline bool Serialization::Parse(const json::value& v, EditorState& result, string* error)
{
CHECK_TYPE(v, json::type_t::object);
EditorState state;
CHECK_AND_PARSE(v, "nodes", state.m_NodesState);
CHECK_AND_PARSE(v, "selection", state.m_SelectionState);
CHECK_AND_PARSE(v, "view", state.m_ViewState);
result = state;
return true;
}
inline bool Serialization::Parse(const json::value& v, ObjectId& result, string* error)
{
CHECK_TYPE(v, json::type_t::string);
auto& str = v.get<std::string>();
return Parse(str, result, error);
}
template <typename T>
inline bool Serialization::Parse(const json::value& v, vector<T>& result, string* error)
{
CHECK_TYPE(v, json::type_t::array);
const auto& array = v.get<json::array>();
vector<T> paresed;
paresed.reserve(array.size());
for (auto& value : array)
{
T item;
PARSE(value, item);
paresed.emplace_back(std::move(item));
}
result = std::move(paresed);
return true;
}
template <typename K, typename V>
inline bool Serialization::Parse(const json::value& v, map<K, V>& result, string* error)
{
CHECK_TYPE(v, json::type_t::object);
const auto& object = v.get<json::object>();
map<K, V> paresed;
for (auto& entry : object)
{
K key;
PARSE(entry.first, key);
V value;
PARSE_IMPL(entry.second, value, entry.first);
paresed[std::move(key)] = std::move(value);
}
result = std::move(paresed);
return true;
}
inline string Serialization::ToString(const json::value& value)
{
return value.dump();
}
inline string Serialization::ToString(const json::type_t& type)
{
switch (type)
{
case json::type_t::null: return "null";
case json::type_t::object: return "object";
case json::type_t::array: return "array";
case json::type_t::string: return "string";
case json::type_t::boolean: return "boolean";
case json::type_t::number: return "number";
case json::type_t::discarded: return "discarded";
}
return "";
}
inline string Serialization::ToString(const ObjectId& objectId)
{
auto value = std::to_string(reinterpret_cast<uintptr_t>(objectId.AsPointer()));
switch (objectId.Type())
{
default:
case NodeEditor::Detail::ObjectType::None: return value;
case NodeEditor::Detail::ObjectType::Node: return "node:" + value;
case NodeEditor::Detail::ObjectType::Link: return "link:" + value;
case NodeEditor::Detail::ObjectType::Pin: return "pin:" + value;
}
return value;
}
inline string Serialization::ToString(const NodeId& nodeId)
{
return ToString(ObjectId(nodeId));
}
inline json::value Serialization::ToJson(const string& value)
{
json::value result;
if (!Parse(value, result))
return json::value(json::type_t::discarded);
return result;
}
inline json::value Serialization::ToJson(const ImVec2& value)
{
json::value result;
result["x"] = value.x;
result["y"] = value.y;
return result;
}
inline json::value Serialization::ToJson(const ImRect& value)
{
json::value result;
result["min"] = ToJson(value.Min);
result["max"] = ToJson(value.Max);
return result;
}
inline json::value Serialization::ToJson(const NodeState& value)
{
json::value result;
result["location"] = ToJson(value.m_Location);
if (value.m_GroupSize.x > 0 || value.m_GroupSize.y > 0)
result["group_size"] = ToJson(value.m_GroupSize);
return result;
}
inline json::value Serialization::ToJson(const NodesState& value)
{
return ToJson(value.m_Nodes);
}
inline json::value Serialization::ToJson(const SelectionState& value)
{
return ToJson(value.m_Selection);
}
inline json::value Serialization::ToJson(const ViewState& value)
{
json::value result;
result["scroll"] = ToJson(value.m_ViewScroll);
result["zoom"] = value.m_ViewZoom;
result["visible_rect"] = ToJson(value.m_VisibleRect);
return result;
}
inline json::value Serialization::ToJson(const EditorState& value)
{
json::value result;
result["nodes"] = ToJson(value.m_NodesState);
result["selection"] = ToJson(value.m_SelectionState);
result["view"] = ToJson(value.m_ViewState);
return result;
}
inline json::value Serialization::ToJson(const ObjectId& value)
{
return ToString(value);
}
template <typename T>
inline json::value Serialization::ToJson(const vector<T>& value)
{
json::array array;
array.reserve(value.size());
for (auto& v : value)
array.emplace_back(ToJson(v));
return array;
}
template <typename K, typename V>
inline json::value Serialization::ToJson(const map<K, V>& value)
{
json::object object;
for (auto& v : value)
object[ToString(v.first)] = ToJson(v.second);
return object;
}
# undef CHECK
# undef CHECK_TYPE
# undef CHECK_KEY
# undef PARSE_IMPL
# undef PARSE
# undef CHECK_AND_PARSE
# undef CHECK_AND_PARSE_OPT
//------------------------------------------------------------------------------
} // namespace Detail
} // namespace Editor
} // namespace ax
//------------------------------------------------------------------------------
# endif // __IMGUI_NODE_EDITOR_INTERNAL_INL__