forked from hugeproblem/nged
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
197 lines (175 loc) · 5.82 KB
/
main.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
#include <nged/nged.h>
#include <nged/nged_imgui.h>
#include <nged/style.h>
#include <nged/entry/entry.h>
#include <spdlog/spdlog.h>
#ifdef _WIN32
#include <spdlog/sinks/wincolor_sink.h>
#include <spdlog/sinks/msvc_sink.h>
#else
#include <spdlog/sinks/ansicolor_sink.h>
#endif
#include <nlohmann/json.hpp>
#include <imgui.h>
#include <misc/cpp/imgui_stdlib.h>
#include <misc/cpp/imgui_stdlib.cpp>
#include <chrono>
class DummyNode : public nged::Node
{
int numInput = 1;
int numOutput = 1;
nged::Canvas::ImagePtr icon = nullptr;
public:
DummyNode(
int numInput,
int numOutput,
nged::Graph* parent,
std::string const& type,
std::string const& name)
: nged::Node(parent, type, name), numInput(numInput), numOutput(numOutput)
{
constexpr int res = 64;
uint8_t pixels[res * res * 4] = {0};
for (int y = 0; y < 64; y++)
for (int x = 0; x < 64; x++) {
const int index = (y * res + x) * 4;
pixels[index + 0] = x * 4; // 红
pixels[index + 1] = y * 4; // 绿
pixels[index + 2] = rand() & 0xff; // 蓝
float d = gmath::distance(nged::Vec2(x, y), nged::Vec2(res / 2, res / 2)); // 到中心的距离
pixels[index + 3] =
uint8_t(gmath::clamp((res / 2 - 1 - d) / 4.f, 0.f, 1.f) * 255.f); // 透明
}
icon = nged::Canvas::createImage(pixels, res, res);
}
nged::sint numMaxInputs() const override { return numInput; }
nged::sint numOutputs() const override { return numOutput; }
bool acceptInput(nged::sint port, nged::Node const* srcNode, nged::sint srcPort) const override
{
// 为了测试完整性检查,“picky”节点只接受来自另一个“picky”节点的链接
if (srcNode->type() == "picky" && type() == "picky")
return false;
return true;
}
void draw(nged::Canvas* canvas, nged::GraphItemState state) const override
{
auto left = nged::Vec2(aabb().min.x, pos().y);
canvas->drawImage(icon, left - nged::Vec2(40, 16), left - nged::Vec2(8, -16));
nged::Node::draw(canvas, state);
}
};
class SubGraphNode : public DummyNode
{
nged::GraphPtr subgraph_;
public:
SubGraphNode(nged::Graph* parent) : DummyNode(1, 1, parent, "subgraph", "subgraph")
{
subgraph_ = std::make_shared<nged::Graph>(parent->docRoot(), parent, "subgraph");
}
virtual nged::Graph* asGraph() override { return subgraph_.get(); }
virtual nged::Graph const* asGraph() const override { return subgraph_.get(); }
virtual bool serialize(nged::Json& json) const override
{
return DummyNode::serialize(json) && subgraph_->serialize(json);
}
virtual bool deserialize(nged::Json const& json) override
{
return DummyNode::deserialize(json) && subgraph_->deserialize(json);
}
};
struct DummyNodeDef
{
std::string type;
int numinput, numoutput;
};
static DummyNodeDef defs[] = {
{"exec", 4, 1},
{"null", 1, 1},
{"merge", -1, 1},
{"split", 1, 2},
{"picky", 3, 2},
{"out", 1, 0},
{"in", 0, 1}};
class MyNodeFactory : public nged::NodeFactory
{
nged::GraphPtr createRootGraph(nged::NodeGraphDoc* root) const override
{
return std::make_shared<nged::Graph>(root, nullptr, "root");
}
nged::NodePtr createNode(nged::Graph* parent, std::string_view type) const override
{
std::string typestr(type);
if (type == "subgraph")
return std::make_shared<SubGraphNode>(parent);
for (auto const& d : defs)
if (d.type == type)
return std::make_shared<DummyNode>(d.numinput, d.numoutput, parent, typestr, typestr);
return std::make_shared<DummyNode>(4, 1, parent, typestr, typestr);
}
void listNodeTypes(
nged::Graph* graph,
void* context,
void (*ret)(
void* context,
nged::StringView category,
nged::StringView type,
nged::StringView name)) const override
{
ret(context, "subgraph", "subgraph", "subgraph");
for (auto const& d : defs)
ret(context, "demo", d.type, d.type);
}
};
class DemoApp : public nged::App
{
nged::EditorPtr editor = nullptr;
void init()
{
#ifdef _WIN32
spdlog::set_default_logger(std::make_shared<spdlog::logger>(
"", std::make_shared<spdlog::sinks::wincolor_stdout_sink_mt>()));
spdlog::default_logger()->sinks().emplace_back(
std::make_shared<spdlog::sinks::msvc_sink_mt>());
#else
spdlog::set_default_logger(std::make_shared<spdlog::logger>(
"", std::make_shared<spdlog::sinks::ansicolor_stdout_sink_mt>()));
#endif
spdlog::set_level(spdlog::level::trace);
ImGui::GetIO().IniFilename = nullptr; // 禁用窗口大小/位置/布局存储
App::init();
editor = nged::newImGuiNodeGraphEditor();
editor->setResponser(std::make_shared<nged::DefaultImGuiResponser>());
editor->setItemFactory(nged::addImGuiItems(nged::defaultGraphItemFactory()));
editor->setViewFactory(nged::defaultViewFactory());
editor->setNodeFactory(std::make_shared<MyNodeFactory>());
editor->initCommands();
nged::addImGuiInteractions();
nged::ImGuiResource::reloadFonts();
auto doc = editor->createNewDocAndDefaultViews();
auto root = doc->root();
doc->root()->createNode("in");
}
char const* title() { return "Demo"; }
bool agreeToQuit() { return editor->agreeToQuit(); }
void update()
{
static auto prev = std::chrono::system_clock::now();
auto now = std::chrono::system_clock::now();
auto dt = std::chrono::duration_cast<std::chrono::milliseconds>(now - prev);
ImGui::PushFont(nged::ImGuiResource::instance().sansSerifFont);
editor->update(dt.count() / 1000.f);
// ImGui::Begin("测试");
// ImGui::Text("你好世界!");
// ImGui::End();
editor->draw();
ImGui::PopFont();
prev = now;
}
void quit() {}
}; // Demo App
int main()
{
nged::startApp(new DemoApp());
return 0;
}
//