diff --git a/src/app-manager.h b/src/app-manager.h index 25eca79..2538da5 100644 --- a/src/app-manager.h +++ b/src/app-manager.h @@ -62,30 +62,34 @@ namespace VISEQ::APP { std::ifstream f("state.json"); json graph = json::parse(f); + for(auto n : graph.at("gm").at("nodes")){ + gm->addNodeAndUpdate(n.template get>()); + } + int highest_id = 0; - std::shared_ptr hid; - for(auto n : graph.at("graphmanager").at("nodes")){ - std::shared_ptr mn = n; - if(mn->id > highest_id) { - highest_id = mn->id; + Node hid; + for(auto n : graph.at("gm").at("nodes")){ + MidiNode mn = n.template get(); + if(mn.id > highest_id) { + highest_id = mn.id; hid = mn; } } - for(auto e : graph.at("graphmanager").at("edges")){ + for(auto e : graph.at("gm").at("edges")){ if(e.at("id") > highest_id){ highest_id = e.at("id"); } } - for(auto n : graph.at("graphmanager").at("nodes")){ + for(auto n : graph.at("gm").at("nodes")){ std::shared_ptr mn = n; gm->addNode(mn); //app.gm->nodes.push_back(mn); std::cout << std::format("adding node {}", mn->id) << std::endl; } - for(auto e : graph.at("graphmanager").at("edges")){ + for(auto e : graph.at("gm").at("edges")){ int start_id = e.at("start").get(); int end_id = e.at("end").get(); @@ -103,7 +107,7 @@ namespace VISEQ::APP { } gm->lastEdge().lock()->newId(highest_id); - for(auto s : graph.at("signalmanager").at("signals")){ + for(auto s : graph.at("sm").at("signals")){ int start_id = s.at("start").get(); int next_id = s.at("next").get(); int last_id = s.at("last").get(); diff --git a/src/connector.h b/src/connector.h index ea762f7..51c0d37 100644 --- a/src/connector.h +++ b/src/connector.h @@ -8,9 +8,10 @@ #include "state.h" #include "vmath.h" #include +#include namespace VISEQ::GRAPH { - class Connector : public BASE::Connectable, public GraphItem{ + class Connector : public BASE::Connectable, public GraphItem, public std::enable_shared_from_this { public: Connector() {} @@ -31,8 +32,8 @@ namespace VISEQ::GRAPH { return GraphItemType::CONNECTOR; } - std::shared_ptr getPtr(){ - return std::static_pointer_cast(shared_from_this()); + std::shared_ptr getPtr(){ + return shared_from_this(); } virtual bool hovering() override { diff --git a/src/graph-item.h b/src/graph-item.h index affe564..e16c3e9 100644 --- a/src/graph-item.h +++ b/src/graph-item.h @@ -3,10 +3,9 @@ #include "raylib.h" #include "datatypes.h" #include "vmath.h" -#include namespace VISEQ::GRAPH { - class GraphItem : public std::enable_shared_from_this{ + class GraphItem { public: GraphItem() {newId(-1);} @@ -17,13 +16,8 @@ namespace VISEQ::GRAPH { GraphItemType gItemType; - int id = 0; - std::shared_ptr getptr(){ - return shared_from_this(); - } - void newId(int start){ static int _id; if(start > -1) _id = start; diff --git a/src/graph-manager.h b/src/graph-manager.h index 414801c..1cca0f7 100644 --- a/src/graph-manager.h +++ b/src/graph-manager.h @@ -73,13 +73,18 @@ namespace VISEQ::GRAPH { nodes.insert(nodes.end(), node); } + void addNodeAndUpdate(SPTR_Node node){ + addNode(node); + node->update(); + } + void addConnection(WPTR_Node from, WPTR_Node to){ if(from.lock() != to.lock()) { edges.emplace_back(std::make_shared()); edges.back()->start = from; edges.back()->end = to; -// edges.back()->start_conn = from.lock()->connectors.back(); // TODO: crashes on start -// edges.back()->end_conn = to.lock()->connectors.back(); + edges.back()->start_conn = from.lock()->connectors.back(); // TODO: crashes on start + edges.back()->end_conn = to.lock()->connectors.back(); from.lock()->connections.push_back(edges.back()); to.lock()->connections.push_back(edges.back()); } diff --git a/src/interaction-manager.h b/src/interaction-manager.h index 8106a18..306f8c1 100644 --- a/src/interaction-manager.h +++ b/src/interaction-manager.h @@ -16,14 +16,14 @@ #include namespace VISEQ::INTERACTION{ - class ScaleHandle : public BASE::Draggable, public GraphItem{ + class ScaleHandle : public BASE::Draggable, public GraphItem, public std::enable_shared_from_this{ public: ScaleHandle(){} float radius = 6; std::shared_ptr getPtr(){ - return std::static_pointer_cast(shared_from_this()); + return shared_from_this(); } virtual GRAPH::GraphItemType getType() override { diff --git a/src/midi-node.h b/src/midi-node.h index 33da7cf..3d78ca2 100644 --- a/src/midi-node.h +++ b/src/midi-node.h @@ -19,7 +19,7 @@ namespace VISEQ::GRAPH::NODES { bool enabled; }; - MidiNode() {} + MidiNode() : Node() {std::cout << "MidiNode Constructor std" << std::endl;} MidiNode(Vec2Df position) : Node(position) {} NodeType nodeType = NodeType::MIDINODE; @@ -63,6 +63,10 @@ namespace VISEQ::GRAPH::NODES { float time = 0; float last = 0; + std::shared_ptr getPtr(){ + return std::static_pointer_cast(shared_from_this()); + } + virtual NodeType getNodeType() const override {return nodeType;} void setMidiPort(int midi_port, bool _state){ diff --git a/src/node.h b/src/node.h index 3c53a6c..a599a97 100644 --- a/src/node.h +++ b/src/node.h @@ -17,7 +17,7 @@ namespace VISEQ::GRAPH::EDGES{ namespace VISEQ::GRAPH::NODES { - class Node : public BASE::Draggable, public GraphItem { + class Node : public BASE::Draggable, public GraphItem, public std::enable_shared_from_this { public: @@ -36,13 +36,17 @@ namespace VISEQ::GRAPH::NODES { float radius = 15.0f; bool config = false; + + std::shared_ptr getPtr(){ + return shared_from_this(); + } virtual bool hovering() override { return CheckCollisionPointCircle(SCREEN_MOUSE, position, radius); } void addConnector() { - connectors.emplace_back(std::make_shared(GraphItem::getptr(), position, radius + 5.0f)); + connectors.emplace_back(std::make_shared(getPtr(), position, radius + 5.0f)); } void removeConnector(std::weak_ptr connector){ @@ -63,10 +67,6 @@ namespace VISEQ::GRAPH::NODES { return GraphItemType::NODE; } - std::shared_ptr getPtr(){ - return std::static_pointer_cast(shared_from_this()); - } - void update() override{ if(hovering()) state->pushEvent({getPtr(), APP::Interaction::HOVER}); if(hovering() && clicked()) state->pushEvent({getPtr(), APP::Interaction::PICKUP}); diff --git a/src/state-io.h b/src/state-io.h index bb3b451..dc4a46b 100644 --- a/src/state-io.h +++ b/src/state-io.h @@ -20,35 +20,25 @@ typedef std::shared_ptr SPTR_GraphItem; typedef std::vector VEC_SPTR_GraphItem; -namespace nlohmann -{ -template -struct adl_serializer> -{ - static void to_json(json& j, const std::shared_ptr& opt) - { - if (opt) - { - j = *opt; - } - else - { - j = nullptr; - } - } +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())); - } - } -}; + static void from_json(const json& j, std::shared_ptr& opt) { + if (j.is_null()) { + opt = nullptr; + } else { + opt.reset(new T(j.get())); + } + } + }; } namespace VISEQ::GRAPH::EDGES {