From e6f370ba4f900eaff4bacbf52e1260c8930105e7 Mon Sep 17 00:00:00 2001 From: Sebastian Date: Wed, 13 Nov 2024 14:39:46 +0100 Subject: [PATCH] Connectables working now. On to more refactoring! --- src/connectable.h | 6 +----- src/connector.h | 5 ++++- src/graph-manager.h | 5 +++++ src/main.cpp | 3 ++- src/node.h | 13 ++++++++++--- 5 files changed, 22 insertions(+), 10 deletions(-) diff --git a/src/connectable.h b/src/connectable.h index 7389158..1a885cf 100644 --- a/src/connectable.h +++ b/src/connectable.h @@ -1,6 +1,5 @@ #pragma once -#include "datatypes.h" #include "draggable.h" #include @@ -8,9 +7,7 @@ namespace VISEQ::BASE { class Connectable : public Draggable{ public: Connectable() {} - /*Connectable() {VISEQ::BASE::Connectable::connectables.emplace_back(this);}*/ Connectable(Vec2Df position) : Draggable(position) {} - /*virtual ~Connectable() { connectables.erase(std::remove(connectables.begin(), connectables.end(), std::shared_ptr(this)), connectables.end()); }*/ virtual ~Connectable() {} inline static std::vector> connectables; @@ -23,7 +20,6 @@ namespace VISEQ::BASE { virtual bool hovering() override { Vec2Df mp = {GetMousePosition().x, GetMousePosition().y}; - std::cout << "hovering" << std::endl; return CheckCollisionPointCircle(mp, position, 5); } @@ -42,7 +38,6 @@ namespace VISEQ::BASE { Draggable::putDown(); } - virtual void connect() { Vec2Df mp = {GetMousePosition().x, GetMousePosition().y}; @@ -50,6 +45,7 @@ namespace VISEQ::BASE { for(auto c : connectables){ if(c.get() != this && c->hovering()) { partner = c; + c->partner.reset(this); break; } } diff --git a/src/connector.h b/src/connector.h index a9688e6..d22a9a9 100644 --- a/src/connector.h +++ b/src/connector.h @@ -13,10 +13,13 @@ namespace VISEQ::GRAPH { float radius = 5.0f; float orbit = 14.0f; + Vec2Df center_position; void setPosition(Vec2Df pos) override { Vec2Df mp = {GetMousePosition().x, GetMousePosition().y}; - position = pos - ((pos - mp)).norm() * (radius + orbit); + center_position = pos; + if(!partner) position = center_position - ((center_position - mp)).norm() * (radius + orbit); + if(partner) position = center_position - ((center_position - partner->position)).norm() * (radius + orbit); } void draw() override { diff --git a/src/graph-manager.h b/src/graph-manager.h index b7abb92..a7dd299 100644 --- a/src/graph-manager.h +++ b/src/graph-manager.h @@ -56,6 +56,11 @@ namespace VISEQ::GRAPH { std::ranges::sample(edges, std::back_inserter(o), 1, std::mt19937{std::random_device{}()}); return std::dynamic_pointer_cast(o.front()); } + + void addNode(SPTR_Node node){ + nodes.insert(nodes.end(), node); + nodes.back()->init(); + } void copyNodes(VEC_SPTR_Node _nodes, Vec2Df position){ for(auto n: _nodes){ diff --git a/src/main.cpp b/src/main.cpp index fc36e20..11db124 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -101,7 +101,8 @@ int main(){ for(auto n : graph.at("graphmanager").at("nodes")){ std::shared_ptr mn = n; - app.gm.nodes.push_back(mn); + app.gm.addNode(mn); + //app.gm.nodes.push_back(mn); std::cout << std::format("adding node {}", mn->id) << std::endl; } diff --git a/src/node.h b/src/node.h index 908e214..4aa136c 100644 --- a/src/node.h +++ b/src/node.h @@ -18,14 +18,18 @@ namespace VISEQ::GRAPH::NODES { class Node : public BASE::Draggable, public GraphItem{ public: - Node() {addConnector();} - Node(float x, float y) : Draggable(Vec2Df(x, y)) {addConnector();} - Node(Vec2Df p) : Draggable(p) {addConnector();} + Node() {} + Node(float x, float y) : Draggable(Vec2Df(x, y)) {} + Node(Vec2Df p) : Draggable(p) {} void addConnector() { connectors.emplace_back(std::make_shared(position, radius + 5.0f)); connectors.back()->init(); } + + void init() { + addConnector(); + } NodeType nodeType; @@ -55,10 +59,13 @@ namespace VISEQ::GRAPH::NODES { void update() override{ if(pickedUp()) drag(); if(released()) putDown(); + int free_connectors = 0; for(auto c : connectors){ + if(!c->partner) free_connectors++; c->setPosition(position); c->update(); } + if(free_connectors == 0) addConnector(); } void draw(Vec2Df mp, Camera2D cam) override {}