TerraForge3D  2.3.1
3D Terrain And Landscape Generator
NodeEditor.h
1#pragma once
2#include "json/json.hpp"
3#include "imgui-node-editor/imgui_node_editor.h"
4
5#include <vector>
6#include <string>
7#include <iostream>
8#include <unordered_map>
9#include <iterator>
10#include <functional>
11#include <mutex>
12
13#define MAKE_IMGUI_ID(x) ("##" + std::to_string(x)).c_str()
14#define MAKE_IMGUI_LABEL(x, y) (y + std::string("##") + std::to_string(x)).c_str()
15
16namespace ImGuiNodeEditor = ax::NodeEditor;
17
18enum NodeEditorPinType
19{
20 Output = 0,
21 Input,
22 PinTypeCount
23};
24
26{
27 float value;
28};
29
31{
32 float x;
33 float y;
34 float z;
35
36 float texX;
37 float texY;
38
39 float minX;
40 float minY;
41 float minZ;
42
43 float maxX;
44 float maxY;
45 float maxZ;
46
47 void *userData1;
48 void *userData2;
49 void *userData3;
50
52 NodeInputParam(float *pos, float *texCoord, float *minPos, float *maxPos);
53};
54
55
56int GenerateUID();
57void SeUIDSeed(int seed);
58
59class NodeEditorNode;
60class NodeEditorPin;
61
63{
64 std::string saveFile;
65 std::function<void(void)> updateFunc;
66 std::function<void(void)> makeNodeFunc;
67 std::function<NodeEditorNode*(nlohmann::json)> insNodeFunc;
68
69 NodeEditorConfig(std::string saveFile = "NodeEditor.terr3d");
70};
71
73{
74public:
75 int id;
76 ImGuiNodeEditor::LinkId _id;
77 NodeEditorPin *from;
78 NodeEditorPin *to;
79 ImVec4 color = ImVec4(1, 1, 1, 1);
80 float thickness = 1.0f;
81
82 nlohmann::json Save();
83 void Load(nlohmann::json data);
84
85 NodeEditorLink(int id = GenerateUID());
86};
87
89{
90public:
91 int id;
92 ImGuiNodeEditor::PinId _id;
93 NodeEditorLink *link;
94 NodeEditorPin *other;
95 NodeEditorNode *parent;
96 NodeEditorPinType type;
97 std::mutex mutex;
98 ImU32 color = ImColor(94, 95, 191);
99 uint32_t userData = 0;
100
101 virtual nlohmann::json Save();
102 virtual void Load(nlohmann::json data);
103 virtual void Begin();
104 virtual void End();
105 bool ValidateLink(NodeEditorLink *link);
106 void Link(NodeEditorLink *link);
107 bool IsLinked();
108 void Unlink();
109 void Render();
110 NodeOutput Evaluate(NodeInputParam input);
111
112
113 NodeEditorPin(NodeEditorPinType type = NodeEditorPinType::Input, int id = GenerateUID());
115};
116
118{
119public:
120 ImVec2 nodePosition;
121 bool reqNodePosLoad;
122 int id;
123 ImGuiNodeEditor::NodeId _id;
124 std::vector<NodeEditorPin *> outputPins;
125 std::vector<NodeEditorPin *> inputPins;
126 char userData[128];
127 std::string name;
128 ImU32 headerColor = ImColor(59, 29, 209);
129 std::mutex m;
130
131 virtual NodeOutput Evaluate(NodeInputParam input, NodeEditorPin *pin) = 0;
132 virtual std::vector<NodeEditorPin *> GetPins();
133 virtual bool OnLink(NodeEditorPin *pin, NodeEditorLink *link);
134 virtual void OnDelete();
135
136 nlohmann::json SaveInternal();
137 void LoadInternal(nlohmann::json data);
138 virtual void Load(nlohmann::json data) = 0;
139 virtual nlohmann::json Save() = 0;
140 virtual void OnRender() = 0;
141
142 void Render();
143 void Setup();
144 void DrawHeader(std::string text);
145
146 NodeEditorNode(int id = GenerateUID());
148};
149
150
152{
153public:
154 ImGuiNodeEditor::EditorContext *context;
155 std::string name = "Node Editor";
156 NodeEditorConfig config;
157 std::unordered_map<uintptr_t, NodeEditorLink *> links;
158 std::unordered_map<uintptr_t, NodeEditorNode *> nodes;
159 std::unordered_map<uintptr_t, NodeEditorPin *> pins;
160 NodeEditorNode *outputNode = nullptr;
161 std::mutex mutex;
162
163 nlohmann::json Save();
164 void Load(nlohmann::json data);
165 void Render();
166 void AddNode(NodeEditorNode *node);
167 void DeleteNode(NodeEditorNode *node);
168 void DeleteLink(NodeEditorLink *link);
169 void Reset();
170 void SetOutputNode(NodeEditorNode *node);
171
173 ~NodeEditor();
174
175private:
176 ImGuiNodeEditor::NodeId lastNodeId;
177};
Definition: AddNode.h:7
a class to store JSON values
Definition: json.hpp:17860
basic_json<> json
default JSON class
Definition: json.hpp:3411