diff --git a/src/datatypes.h b/src/datatypes.h index 797011e..96276f6 100644 --- a/src/datatypes.h +++ b/src/datatypes.h @@ -31,54 +31,23 @@ enum Distribution {GEOMETRIC, ROUNDROBIN, RANDOM}; // Types -class GraphItem; -class Node; -class Edge; -class Signal; +/*class GraphItem;*/ +/*class Node;*/ +/*class Edge;*/ +/*class Signal;*/ #define RANGE_FILTER(L) std::ranges::views::filter(L) -typedef std::shared_ptr SPTR_GraphItem; -typedef std::shared_ptr SPTR_Node; -typedef std::shared_ptr SPTR_Edge; - -typedef std::shared_ptr SPTR_Signal; - -typedef std::vector VEC_SPTR_GraphItem; -typedef std::vector VEC_SPTR_Node; -typedef std::vector VEC_SPTR_Edge; -typedef std::vector VEC_SPTR_Signal; - -using Vec2Df = VMath::Vec2D; - -namespace nlohmann { - template - struct adl_serializer> { - static void to_json(json& j, const std::shared_ptr& opt){ - if (opt) - { - j = *opt; - } - else - { - j = nullptr; - } - } - - static void from_json(const json& j, std::shared_ptr& opt){ - if (j.is_null()) - { - opt = nullptr; - } - else - { - opt.reset(new T(j.get())); - //opt = j.get(); - } - } - }; -} - +/*typedef std::shared_ptr SPTR_GraphItem;*/ +/*typedef std::shared_ptr SPTR_Node;*/ +/*typedef std::shared_ptr SPTR_Edge;*/ +/**/ +/*typedef std::shared_ptr SPTR_Signal;*/ +/**/ +/*typedef std::vector VEC_SPTR_GraphItem;*/ +/*typedef std::vector VEC_SPTR_Node;*/ +/*typedef std::vector VEC_SPTR_Edge;*/ +/*typedef std::vector VEC_SPTR_Signal;*/ // App Settings struct AppSettings{ diff --git a/src/edge.h b/src/edge.h index 75e5436..b294280 100644 --- a/src/edge.h +++ b/src/edge.h @@ -28,9 +28,11 @@ class Edge : public GraphItem{ return start == origin ? end : start; } - void draw(Vec2Df mouse_pos, Camera2D cam) { - - } + void draw(Vec2Df mouse_pos, Camera2D cam) {} }; -NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(Edge, id, start, end, pass_forward, pass_backward) + +/*typedef std::shared_ptr SPTR_Edge;*/ +/*typedef std::vector VEC_SPTR_Edge;*/ + +//NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(Edge, id, start, end, pass_forward, pass_backward) diff --git a/src/graph-item.h b/src/graph-item.h index d7c3768..98626b9 100644 --- a/src/graph-item.h +++ b/src/graph-item.h @@ -23,4 +23,8 @@ class GraphItem{ }; -NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(GraphItem, id, graphItemType, neighbors) + +/*typedef std::shared_ptr SPTR_GraphItem;*/ +/*typedef std::vector VEC_SPTR_GraphItem;*/ + +//NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(GraphItem, id, graphItemType, neighbors) diff --git a/src/graph-manager.h b/src/graph-manager.h index fa28233..3c5b128 100644 --- a/src/graph-manager.h +++ b/src/graph-manager.h @@ -8,8 +8,8 @@ #include "nlohmann/json.hpp" #include "button.h" -#include "midi-node.h" #include "node.h" +#include "midi-node.h" #include "edge.h" #include @@ -22,8 +22,21 @@ #include #include +#include "state-io.h" + using json = nlohmann::json; +typedef std::shared_ptr SPTR_GraphItem; +typedef std::shared_ptr SPTR_Node; +typedef std::shared_ptr SPTR_Edge; + +typedef std::shared_ptr SPTR_Signal; + +typedef std::vector VEC_SPTR_GraphItem; +typedef std::vector VEC_SPTR_Node; +typedef std::vector VEC_SPTR_Edge; +typedef std::vector VEC_SPTR_Signal; + class GraphManager{ public: @@ -280,14 +293,4 @@ class GraphManager{ }; -inline void to_json(json& j, const Node& N) { - std::vector conns; - std::ranges::transform(N.connections.begin(), N.connections.end(), std::back_inserter(conns), [](SPTR_Edge e){ return e->id;}); - j = json{{"neighbor", N.neighbor}, {"nodeType", N.nodeType}, {"distributionMode", N.distributionMode}, {"connections", conns}}; - -} -inline void from_json(const json& j, Node& N) { - -} - NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(GraphManager, nodes, edges) diff --git a/src/main.cpp b/src/main.cpp index cf04797..bc58ddb 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -83,7 +83,6 @@ int main(){ app.gm.addConnection(app.gm.nodes[i], app.gm.nodes[(i+1) % 8]); } - std::cout << std::setw(2) << json(app.gm) << std::endl; app.sm.addSignalAtNode(app.gm.nodes.front()); app.sm.startSignalThread(); @@ -96,6 +95,7 @@ int main(){ VEC_SPTR_Node copied_nodes; + while(!WindowShouldClose()){ BeginDrawing(); @@ -390,6 +390,8 @@ int main(){ EndDrawing(); } + std::cout << std::setw(4) << json(app.gm) << std::endl; + CloseWindow(); return 0; diff --git a/src/midi-node.h b/src/midi-node.h index 2108ad3..682261f 100644 --- a/src/midi-node.h +++ b/src/midi-node.h @@ -13,7 +13,7 @@ struct NodeMidiOut{ bool enabled; }; -NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(NodeMidiOut, i, name, enabled) +//NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(NodeMidiOut, i, name, enabled) class MidiNode : public Node{ @@ -169,4 +169,4 @@ class MidiNode : public Node{ } }; -NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(MidiNode, id, nodeType, send_note, send_cc, send_pc, send_pitch_bend, midi_outs, mode, midi_note_channel, midi_note, midi_velocity, midi_gate, midi_cc_channel, midi_cc, midi_cc_value, midi_pc_channel, midi_pc, midi_pc_value, midi_pitch_bend_channel, midi_pitch_bend) +//NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(MidiNode, id, nodeType, send_note, send_cc, send_pc, send_pitch_bend, midi_outs, mode, midi_note_channel, midi_note, midi_velocity, midi_gate, midi_cc_channel, midi_cc, midi_cc_value, midi_pc_channel, midi_pc, midi_pc_value, midi_pitch_bend_channel, midi_pitch_bend) diff --git a/src/moveable.h b/src/moveable.h index e846551..6efc10d 100644 --- a/src/moveable.h +++ b/src/moveable.h @@ -42,4 +42,4 @@ class Movable{ /*}*/ }; -NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(Movable, position, direction, speed) +//NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(Movable, position, direction, speed) diff --git a/src/node.h b/src/node.h index 61a82ec..6d8d9a2 100644 --- a/src/node.h +++ b/src/node.h @@ -1,5 +1,6 @@ #pragma once +#include #include #include "nlohmann/json.hpp" @@ -8,6 +9,11 @@ #include "graph-item.h" #include "moveable.h" +class Edge; + +typedef std::shared_ptr SPTR_Edge; +typedef std::vector VEC_SPTR_Edge; + class Node : public Movable, public GraphItem{ public: @@ -17,7 +23,7 @@ class Node : public Movable, public GraphItem{ NodeType nodeType; - SPTR_Node neighbor; + std::shared_ptr neighbor; VEC_SPTR_Edge connections; @@ -35,6 +41,10 @@ class Node : public Movable, public GraphItem{ void draw(Vec2Df mouse_pos, Camera2D cam) override {} }; +typedef std::shared_ptr SPTR_Node; +typedef std::vector VEC_SPTR_Node; + + /*inline void to_json(json& j, const std::shared_ptr& N){*/ /* j = json{{"id", N->id}, {"nodeType",N->nodeType}};*/ /*}*/ diff --git a/src/signal.h b/src/signal.h index e91565b..f487261 100644 --- a/src/signal.h +++ b/src/signal.h @@ -1,10 +1,12 @@ #pragma once #include - #include "datatypes.h" #include "moveable.h" +#include "node.h" +#include "edge.h" + class Signal : public Movable{ public: diff --git a/src/state-io.h b/src/state-io.h new file mode 100644 index 0000000..734373b --- /dev/null +++ b/src/state-io.h @@ -0,0 +1,97 @@ +#pragma once + +#include "midi-node.h" +#include "graph-item.h" +#include "node.h" +#include "edge.h" +#include "graph-manager.h" + +#include "datatypes.h" + +#include + +/*namespace nlohmann { */ +/* template */ +/* struct adl_serializer> {*/ +/* static void to_json(json& j, const std::shared_ptr& opt){*/ +/* if (opt)*/ +/* {*/ +/* j = *opt;*/ +/* }*/ +/* else*/ +/* {*/ +/* j = nullptr;*/ +/* }*/ +/* }*/ +/**/ +/* static void from_json(const json& j, std::shared_ptr& opt){*/ +/* if (j.is_null())*/ +/* {*/ +/* opt = nullptr;*/ +/* }*/ +/* else*/ +/* {*/ +/* opt.reset(new T(j.get()));*/ +/* //opt = j.get();*/ +/* }*/ +/* }*/ +/* };*/ +/*}*/ + + + +inline void to_json(json& j, const Edge& E){ + j = json{{"id",E.id}, {"start", E.start->id}, {"end",E.end->id}, {"pass_forward",E.pass_forward}, {"pass_backward", E.pass_backward}}; +} + +inline void to_json(json& j, const SPTR_Edge& E){ + j = *E; +} + +inline void to_json(json& j, const MidiNode& N){ + std::vector conns; + std::ranges::transform(N.connections.begin(), N.connections.end(), std::back_inserter(conns), [](SPTR_Edge e){ return e->id;}); + j = json{{"id", N.id}, {"position", N.position}, {"distributionMode", N.distributionMode}, + {"midi_note_channel",N.midi_note_channel}, + {"midi_velocity",N.midi_velocity}, + {"midi_gatel",N.midi_gate}, + {"midi_cc_channel",N.midi_cc_channel}, + {"midi_cc",N.midi_cc}, + {"midi_cc_value",N.midi_cc_value}, + {"midi_pc_channel",N.midi_pc_channel}, + {"midi_pc_value",N.midi_pc_value}, + {"midi_pitch_bend_channel",N.midi_pitch_bend_channel}, + {"midi_pitch_bend",N.midi_pitch_bend}, + {"connections",conns}}; +} + +inline void to_json(json& j, const std::shared_ptr& N){ + j = *N; +} + +inline void to_json(json& j, const GraphItem& G){ + j = json{{"id", G.id}, {"type", G.graphItemType}}; +} + +inline void to_json(json& j, const SPTR_GraphItem& G){ + j = *G; +} + +inline void to_json(json& j, const MidiOutPort MO){ + j = json{{"port_id", MO.number}, {"enabled", MO.enabled}}; +} + +inline void to_json(json& j, const NodeMidiOut MO){ + j = json{{"port_id", MO.i}, {"enabled", MO.enabled}, {"name", MO.name}}; +} + +inline void to_json(json& j, const Node& N){ + if(N.nodeType == NodeType::MIDINODE){ + j = dynamic_cast(N); + } +} + +inline void to_json(json& j, const std::shared_ptr& N){ + j = *N; +} + diff --git a/src/vmath.h b/src/vmath.h index 8dd9124..39d34a8 100644 --- a/src/vmath.h +++ b/src/vmath.h @@ -156,5 +156,6 @@ namespace VMath{ Vec2D polarToCartesian(T r, T q) { return Vec2D{r * cos(q), r * sin(q)}; } - } + +using Vec2Df = VMath::Vec2D;