33 std::function<std::shared_ptr<NodeInternal>(
const Flowgraph *f)> func;
35 std::map<std::string, NodeInternalReg> node_internal_registry;
40 friend class Flowgraph;
46 const std::string title;
47 const std::string internal_id;
48 const std::shared_ptr<NodeInternal> internal;
50 bool pos_was_set =
false;
54 bool disabled =
false;
64 NLOHMANN_DEFINE_TYPE_INTRUSIVE(InOut,
id, name, is_out);
67 std::vector<InOut> node_io;
72 for (
auto &io : internal->blk->get_inputs())
73 node_io.push_back({_f->getNewNodeIOID(&node_io), io.name,
false, io.type});
74 for (
auto &io : internal->blk->get_outputs())
75 node_io.push_back({_f->getNewNodeIOID(&node_io), io.name,
true, io.type});
79 Node(Flowgraph *f, std::string
id, std::shared_ptr<NodeInternal> i) : _f(f), id(f->getNewNodeID()), internal_id(
id), title(i->blk->d_id), internal(i) { updateIO(); }
81 Node(Flowgraph *f, nlohmann::json j, std::shared_ptr<NodeInternal> i)
82 : id(j[
"id"]), internal_id(j[
"int_id"]), title(i->blk->d_id), node_io(j[
"io"].get<std::vector<InOut>>()), internal(i)
84 if (j.contains(
"int_cfg"))
85 internal->setP(j[
"int_cfg"]);
87 pos_x = j.contains(
"pos_x") ? j[
"pos_x"].get<
float>() : 0;
88 pos_y = j.contains(
"pos_y") ? j[
"pos_y"].get<
float>() : 0;
90 if (j.contains(
"disabled"))
91 disabled = j[
"disabled"];
94 nlohmann::json getJSON()
99 j[
"int_id"] = internal_id;
100 j[
"int_cfg"] = internal->getP();
103 j[
"disabled"] = disabled;
114 NLOHMANN_DEFINE_TYPE_INTRUSIVE(
Link,
id, start, end);
120 std::vector<std::shared_ptr<Node>> nodes;
121 std::vector<Link> links;
124 int getNewNodeIOID(std::vector<Node::InOut> *ptr =
nullptr);
127 void renderAddMenu(std::pair<const std::string, NodeInternalReg> &opt, std::vector<std::string> cats,
int pos);
130 bool debug_mode =
false;
136 std::map<std::string, double> variables;
138 std::shared_ptr<Node> addNode(std::string
id, std::shared_ptr<NodeInternal> i)
140 auto ptr = std::make_shared<Node>(
this,
id, i);
141 nodes.push_back(ptr);
147 nlohmann::json getJSON()
149 std::lock_guard<std::mutex> lg(flow_mtx);
151 for (
auto &n : nodes)
152 j[
"nodes"][n->id] = n->getJSON();
154 j[
"vars"] = variables;
158 void setJSON(nlohmann::json j)
160 std::lock_guard<std::mutex> lg(flow_mtx);
161 if (j.contains(
"vars"))
162 variables = j[
"vars"];
167 for (
auto &n : j[
"nodes"].items())
169 if (n.value().contains(
"int_id"))
171 if (node_internal_registry.count(n.value()[
"int_id"]))
175 auto i = node_internal_registry[n.value()[
"int_id"]].func(
this);
176 auto nn = std::make_shared<Node>(
this, n.value(), i);
179 catch (std::exception &e)
181 logger->error(
"Error adding node with ID : " + n.value()[
"int_id"].get<std::string>() +
", Error : %s", e.what());
186 logger->error(
"Could not find node with ID : " + n.value()[
"int_id"].get<std::string>());
191 logger->error(
"Node is missing int_id!");
196 std::vector<Link> tmp_links = j[
"links"];
198 for (
auto &link : tmp_links)
200 bool got_in =
false, got_ou =
false;
201 for (
auto &n : nodes)
203 for (
auto &io : n->node_io)
205 if (io.id == link.start)
207 if (io.id == link.end)
212 if (got_in && got_ou)
213 links.push_back(link);
217 for (
auto &n : nodes)
219 size_t ic = 0, oc = 0;
220 for (
auto &io : n->node_io)
224 if (n->internal->blk->get_outputs().size() > oc)
225 io.type = n->internal->blk->get_outputs()[oc].type;
230 if (n->internal->blk->get_inputs().size() > ic)
231 io.type = n->internal->blk->get_inputs()[ic++].type;
239 bool is_running =
false;