-
Notifications
You must be signed in to change notification settings - Fork 578
/
Copy pathidtypes-example.cpp
212 lines (165 loc) · 4.95 KB
/
idtypes-example.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
# include <imgui.h>
# include <imgui_node_editor.h>
# include <application.h>
# include <vector>
# include <unordered_map>
#include "edconfig.h"
namespace ed = ax::NodeEditor;
struct Pin
{
};
struct Node
{
std::vector<Pin> InPins;
std::vector<Pin> OutPins;
};
bool CustomNodeId::operator<(const CustomNodeId &id) const
{
return name < id.name;
}
bool CustomNodeId::operator==(const CustomNodeId &id) const
{
return name == id.name;
}
std::string CustomNodeId::AsString() const
{
return name;
}
CustomNodeId CustomNodeId::FromString(const char *str, const char *end)
{
return CustomNodeId{ std::string(str, end) };
}
bool CustomNodeId::IsValid() const
{
return !name.empty();
}
const CustomNodeId CustomNodeId::Invalid = {};
bool CustomPinId::operator<(const CustomPinId &id) const
{
if (nodeName < id.nodeName) return true;
else if (nodeName > id.nodeName) return false;
if (int(direction) < int(id.direction)) return true;
else if (int(direction) > int(id.direction)) return false;
return pinIndex < id.pinIndex;
}
bool CustomPinId::operator==(const CustomPinId &id) const
{
return nodeName == id.nodeName && direction == id.direction && pinIndex == id.pinIndex;
}
std::string CustomPinId::AsString() const
{
return nodeName + ";" + std::to_string(int(direction)) + ";" + std::to_string(pinIndex);
}
CustomPinId CustomPinId::FromString(const char *str, const char *end)
{
std::string string(str, end);
auto sep1 = string.find(';');
if (sep1 == 0 || sep1 == std::string::npos) {
return Invalid;
}
auto sep2 = string.find(';', sep1 + 1);
if (sep2 == sep1 + 1 || sep2 == std::string::npos) {
return Invalid;
}
char *e;
auto nodeName = std::string(string.data(), sep1);
int dir = std::strtol(string.data() + sep1 + 1, &e, 10);
if (dir > 1) {
return Invalid;
}
int pin = std::strtol(string.data() + sep2 + 1, &e, 10);
return CustomPinId{ nodeName, CustomPinId::Direction(dir), pin };
}
bool CustomPinId::IsValid() const
{
return !nodeName.empty();
}
const CustomPinId CustomPinId::Invalid = {};
bool CustomLinkId::operator<(const CustomLinkId &id) const
{
if (in < id.in) return true;
if (in == id.in) return out < id.out;
return false;
}
bool CustomLinkId::operator==(const CustomLinkId &id) const
{
return in == id.in && out == id.out;
}
std::string CustomLinkId::AsString() const
{
return in.AsString() + "->" + out.AsString();
}
CustomLinkId CustomLinkId::FromString(const char *str, const char *end)
{
std::string string(str, end);
auto sep = string.find("->");
if (sep == 0 || sep == std::string::npos) {
return Invalid;
}
auto in = CustomPinId::FromString(str, str + sep);
auto out = CustomPinId::FromString(str + sep + 2, end);
return { in, out };
}
bool CustomLinkId::IsValid() const
{
return in.IsValid() && out.IsValid();
}
const CustomLinkId CustomLinkId::Invalid = {};
struct Example:
public Application
{
using Application::Application;
void OnStart() override
{
ed::Config config;
config.SettingsFile = "IdTypes.json";
m_Context = ed::CreateEditor(&config);
m_Nodes.insert({ "foo", Node{ { {}, {} }, { {} } } });
m_Nodes.insert({ "bar", Node{ { {} }, { {}, {}, {} } } });
}
void OnStop() override
{
ed::DestroyEditor(m_Context);
}
void OnFrame(float deltaTime) override
{
auto& io = ImGui::GetIO();
ImGui::Text("FPS: %.2f (%.2gms)", io.Framerate, io.Framerate ? 1000.0f / io.Framerate : 0.0f);
ImGui::Separator();
ed::SetCurrentEditor(m_Context);
ed::Begin("My Editor", ImVec2(0.0, 0.0f));
// Start drawing nodes.
for (auto &n: m_Nodes) {
ed::BeginNode(CustomNodeId{ n.first });
ImGui::TextUnformatted(n.first.c_str());
ImGui::BeginGroup();
for (int i = 0; i < n.second.InPins.size(); ++i) {
ed::BeginPin(CustomPinId{ n.first, CustomPinId::Direction::In, i }, ed::PinKind::Input);
ImGui::Text("-> In");
ed::EndPin();
}
ImGui::EndGroup();
ImGui::SameLine();
ImGui::BeginGroup();
for (int i = 0; i < n.second.OutPins.size(); ++i) {
ed::BeginPin(CustomPinId{ n.first, CustomPinId::Direction::Out, i }, ed::PinKind::Output);
ImGui::Text("Out ->");
ed::EndPin();
}
ImGui::EndGroup();
ed::EndNode();
}
ed::End();
ed::SetCurrentEditor(nullptr);
//ImGui::ShowMetricsWindow();
}
ed::EditorContext* m_Context = nullptr;
std::unordered_map<std::string, Node> m_Nodes;
};
int Main(int argc, char** argv)
{
Example exampe("IdTypes", argc, argv);
if (exampe.Create())
return exampe.Run();
return 0;
}