SatDump 2.0.0-alpha-76a915210
Loading...
Searching...
No Matches
flowgraph.h
1#pragma once
2
3#include <vector>
4#include <string>
5#include "nlohmann/json.hpp"
6
8#include "logger.h"
9#include "imgui/imgui.h"
10#include "imgui/imgui_stdlib.h"
11#include "core/style.h"
12
13namespace satdump
14{
15 // TODOREWORK proper sub-namespace?
16
17 class NodeInternal
18 {
19 public:
20 const std::string title;
21
23 {
24 std::string name;
25 std::shared_ptr<void> ptr = nullptr;
26 };
27
28 std::vector<InOutConfig> inputs;
29 std::vector<InOutConfig> outputs;
30 bool has_run;
31
32 public:
33 std::function<void(InOutConfig, bool)> add_io_callback;
34
35 void addInputDynamic(InOutConfig cfg)
36 {
37 inputs.push_back(cfg);
38 add_io_callback(cfg, false);
39 }
40
41 public:
42 void reset()
43 {
44 has_run = false;
45 for (auto &i : inputs)
46 i.ptr.reset();
47 for (auto &o : outputs)
48 o.ptr.reset();
49 }
50
51 bool can_run()
52 {
53 bool v = true;
54 for (auto &i : inputs)
55 if (!i.ptr)
56 v = false;
57 return v;
58 }
59
60 virtual void process() = 0;
61 virtual void render() = 0;
62 virtual nlohmann::json to_json() = 0;
63 virtual void from_json(nlohmann::json j) = 0;
64
65 public:
66 NodeInternal(std::string title) : title(title) {}
67 virtual ~NodeInternal() {}
68 };
69
70 class Flowgraph
71 {
72 public:
73 std::map<std::string, std::function<std::shared_ptr<NodeInternal>()>> node_internal_registry;
74
75 public:
76 class Node
77 {
78 friend class Flowgraph;
79
80 private:
81 const int id;
82 const std::string title;
83 const std::string internal_id;
84 const std::shared_ptr<NodeInternal> internal;
85
86 bool pos_was_set = false;
87 float pos_x = 0;
88 float pos_y = 0;
89
90 struct InOut
91 {
92 int id;
93 std::string name;
94 bool is_out;
95
96 NLOHMANN_DEFINE_TYPE_INTRUSIVE(InOut, id, name, is_out);
97 };
98
99 std::vector<InOut> node_io;
100
101 public:
102 Node(Flowgraph *f, std::string id, std::shared_ptr<NodeInternal> i)
103 : id(f->getNewNodeID()), internal_id(id), title(i->title), internal(i)
104 {
105 for (auto &io : internal->inputs)
106 node_io.push_back({f->getNewNodeIOID(&node_io), io.name, false});
107 for (auto &io : internal->outputs)
108 node_io.push_back({f->getNewNodeIOID(&node_io), io.name, true});
109 internal->add_io_callback = [this, f](NodeInternal::InOutConfig io, bool out)
110 {
111 node_io.push_back({f->getNewNodeIOID(&node_io), io.name, out});
112 };
113 }
114
115 Node(Flowgraph *f, nlohmann::json j, std::shared_ptr<NodeInternal> i)
116 : id(j["id"]), internal_id(j["int_id"]), title(i->title), node_io(j["io"].get<std::vector<InOut>>()), internal(i)
117 {
118 internal->from_json(j["int_cfg"]);
119 internal->add_io_callback = [this, f](NodeInternal::InOutConfig io, bool out)
120 {
121 node_io.push_back({f->getNewNodeIOID(&node_io), io.name, out});
122 };
123
124 pos_x = j.contains("pos_x") ? j["pos_x"].get<float>() : 0;
125 pos_y = j.contains("pos_y") ? j["pos_y"].get<float>() : 0;
126 }
127
128 nlohmann::json getJSON()
129 {
130 nlohmann::json j;
131 j["id"] = id;
132 j["io"] = node_io;
133 j["int_id"] = internal_id;
134 j["int_cfg"] = internal->to_json();
135 j["pos_x"] = pos_x;
136 j["pos_y"] = pos_y;
137 return j;
138 }
139 };
140
141 struct Link
142 {
143 int id;
144 int start;
145 int end;
146
147 NLOHMANN_DEFINE_TYPE_INTRUSIVE(Link, id, start, end);
148 };
149
150 private:
151 std::vector<std::shared_ptr<Node>> nodes;
152 std::vector<Link> links;
153
154 int getNewNodeID();
155 int getNewNodeIOID(std::vector<Node::InOut> *ptr = nullptr);
156 int getNewLinkID();
157
158 public:
159 Flowgraph();
160 ~Flowgraph();
161
162 std::shared_ptr<Node> addNode(std::string id, std::shared_ptr<NodeInternal> i)
163 {
164 auto ptr = std::make_shared<Node>(this, id, i);
165 nodes.push_back(ptr);
166 return ptr;
167 }
168
169 void render();
170
171 nlohmann::json getJSON()
172 {
173 nlohmann::json j;
174 for (auto &n : nodes)
175 j["nodes"][n->id] = n->getJSON();
176 j["links"] = links;
177 return j;
178 }
179
180 void setJSON(nlohmann::json j)
181 {
182 nodes.clear();
183 links.clear();
184
185 for (auto &n : j["nodes"].items())
186 {
187 auto i = node_internal_registry[n.value()["int_id"]]();
188 nodes.push_back(std::make_shared<Node>(this, n.value(), i));
189 }
190 links = j["links"];
191 }
192
193 public:
194 void run();
195 };
196}
Definition flowgraph.h:23