From b74c63a11509fbeb718c91f00614b8cbd44d21ee Mon Sep 17 00:00:00 2001 From: Sebastian Date: Tue, 15 Oct 2024 02:59:26 +0200 Subject: [PATCH] two direction connections with blockers working --- src/button.h | 10 +++++++++ src/connection-manager.h | 44 ++++++++++++++++++++++++++++++++++++++++ src/connection.h | 26 +++++++++++++++++++----- src/main.cpp | 32 ++++++++++++++--------------- src/node.h | 22 ++++++++++---------- 5 files changed, 101 insertions(+), 33 deletions(-) create mode 100644 src/button.h create mode 100644 src/connection-manager.h diff --git a/src/button.h b/src/button.h new file mode 100644 index 0000000..dd8ee23 --- /dev/null +++ b/src/button.h @@ -0,0 +1,10 @@ +#pragma once + +#include "raylib.h" +#include "raymath.h" + +inline bool ViButton(float x, float y, float radius, Color color){ + Vector2 mp = GetMousePosition(); + DrawCircle(x,y,radius, color); + return (Vector2Distance(mp, (Vector2){x,y}) < radius && IsMouseButtonPressed(MOUSE_BUTTON_LEFT)); +} diff --git a/src/connection-manager.h b/src/connection-manager.h new file mode 100644 index 0000000..f56d83a --- /dev/null +++ b/src/connection-manager.h @@ -0,0 +1,44 @@ +#pragma once + +#include "connection.h" +#include + +class ConnectionManager{ + + public: + + ConnectionManager() {} + + std::vector connections; + + std::map counter; + + Node* next(Node* current){ + std::vector paths; + + for(auto& p : connections){ + if((p.v1 == current && p.v1_v2) || (p.v2 == current && p.v2_v1)) { + p.active = false; + paths.push_back(&p); + } + } + + if(paths.empty()) return nullptr; + + int return_path = 0; + if(counter.find(current) == counter.end()) { + counter[current] = 0; + } else { + counter[current] = (counter[current] + 1) % paths.size(); + return_path = counter[current]; + } + paths[return_path]->active = true; + return paths[return_path]->v1 == current ? paths[return_path]->v2 : paths[return_path]->v1; + } + + void draw(){ + for(auto& c : connections){ + c.draw(); + } + } +}; diff --git a/src/connection.h b/src/connection.h index 4163783..c432586 100644 --- a/src/connection.h +++ b/src/connection.h @@ -1,17 +1,33 @@ #pragma once +#include "button.h" #include "node.h" class Connection{ public: Connection() {} - Connection(Node* from, Node* to) : from(from), to(to) {} + Connection(Node* v1, Node* v2) : v1(v1), v2(v2) {} + + Node* v1; + Node* v2; + + bool v1_v2 = true; + bool v2_v1 = true; + bool active = false; - Node* from; - Node* to; - void draw(){ - + Vec2D v1_position = v1->position - ((v1->position - v2->position)).norm() * (v1->radius + 10.0f); + Vec2D v2_position = v2->position - ((v2->position - v1->position)).norm() * (v2->radius + 10.0f); + + Color c = {229, 181, 103, 255}; + if(active) c = {108, 153, 187, 255}; + DrawLine(v1_position.x, v1_position.y, v2_position.x, v2_position.y, c); + + Vec2D from_to_handle = v1->position - ((v1->position - v2->position)).norm() * (v1->radius + 35); + Vec2D to_from_handle = v2->position - ((v2->position - v1->position)).norm() * (v2->radius + 35); + + if(ViButton(from_to_handle.x, from_to_handle.y, 8.0f, v1_v2 ? (Color){232, 125, 62, 255} : (Color){232, 50, 32, 255} )) v1_v2 = !v1_v2; + if(ViButton(to_from_handle.x, to_from_handle.y, 8.0f, v2_v1 ? (Color){232, 125, 62, 255} : (Color){232, 50, 32, 255} )) v2_v1 = !v2_v1; } }; diff --git a/src/main.cpp b/src/main.cpp index 3689048..e8d2112 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -34,6 +34,9 @@ #include "node.h" #include "signal.h" +#include "connection.h" + +#include "connection-manager.h" libremidi::observer obs; libremidi::midi_out midi_out; @@ -43,8 +46,8 @@ std::vector midi_out_ports; std::vector nodes; std::vector signals; - -//std::map> connections; +//std::vector connections; +ConnectionManager cm; Vec2D mouse_pos; Vec2D drag_start; @@ -99,7 +102,7 @@ int main(){ } for(int i = 0; i < 2; i++){ - signals.emplace(signals.end(), Signal(0, 0, 5.0f, 2.0f)); + signals.emplace(signals.end(), Signal(1280, 720, 5.0f, 2.0f)); signals[i].next = &nodes[0]; } @@ -108,7 +111,7 @@ int main(){ Vec2D drag_offset; Node* connecting_from; - + while(!WindowShouldClose()){ Vector2 mp = GetScreenToWorld2D(GetMousePosition(), camera); @@ -155,9 +158,9 @@ int main(){ } if(mouse_released && (n.hover_connect || n.hover_move)) { - if(&n != connecting_from){ - n.connections.push_back(connecting_from); - connecting_from->connections.push_back(&n); + if(&n != connecting_from && connecting_from != nullptr){ + cm.connections.emplace(cm.connections.end(), connecting_from, &n); + connecting_from = nullptr; } } @@ -182,19 +185,14 @@ int main(){ n.draw(&mouse_pos); } - /*for( auto from : connections){*/ - /* for( auto to : from.second){*/ - /* Vec2D from_position = from.first->position - ((from.first->position - to->position)).norm() * (from.first->radius + 10.0f);*/ - /* Vec2D to_position = to->position - ((to->position - from.first->position)).norm() * (to->radius + 10.0f);*/ - /* DrawLine(from_position.x, from_position.y, to_position.x, to_position.y, (Color){229, 181, 103, 255});*/ - /* }*/ - /*}*/ + cm.draw(); - for( auto& s : signals){ + for(auto& s : signals){ s.direction = s.next->position - s.position; if(s.position.dist(&s.next->position) < s.speed * 1.5) { - if(s.next->connections.size() > 0) { - s.next = s.next->get_next(); + Node* n = cm.next(s.next); + if(n != s.next && n != nullptr) { + s.next = n; midi_out.send_message(libremidi::channel_events::control_change(2, 10, 50)); } } diff --git a/src/node.h b/src/node.h index 6722b47..9b3c44d 100644 --- a/src/node.h +++ b/src/node.h @@ -22,19 +22,19 @@ class Node : public Movable{ bool hover_connect = false; bool connecting = false; - int next = 0; +// int next = 0; int midi_out = 0; int midi_note = 0; int midi_cc = 0; - std::vector connections; +// std::vector connections; - Node* get_next(){ - next = (next+1) % connections.size(); - return connections[next]; - } + /*Node* get_next(){*/ + /* next = (next+1) % connections.size();*/ + /* return connections[next];*/ + /*}*/ void draw(Vec2D *mouse) { if(!hover_move) DrawCircle(position.x, position.y, radius, (Color){180, 210, 115, 255}); @@ -42,11 +42,11 @@ class Node : public Movable{ Vec2D connector = position - ((position - *mouse)).norm() * (radius + R); - for(auto& to : connections){ - Vec2D from_position = position - ((position - to->position)).norm() * (radius + 10.0f); - Vec2D to_position = to->position - ((to->position - position)).norm() * (to->radius + 10.0f); - DrawLine(from_position.x, from_position.y, to_position.x, to_position.y, (Color){229, 181, 103, 255}); - } + /*for(auto& to : connections){*/ + /* Vec2D from_position = position - ((position - to->position)).norm() * (radius + 10.0f);*/ + /* Vec2D to_position = to->position - ((to->position - position)).norm() * (to->radius + 10.0f);*/ + /* DrawLine(from_position.x, from_position.y, to_position.x, to_position.y, (Color){229, 181, 103, 255});*/ + /*}*/ if(hover_connect){ DrawCircleLines(position.x, position.y, radius + R, (Color){108, 153, 187, 255});