two direction connections with blockers working

This commit is contained in:
Sebastian
2024-10-15 02:59:26 +02:00
parent 313bf767c6
commit b74c63a115
5 changed files with 101 additions and 33 deletions
+10
View File
@@ -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));
}
+44
View File
@@ -0,0 +1,44 @@
#pragma once
#include "connection.h"
#include <map>
class ConnectionManager{
public:
ConnectionManager() {}
std::vector<Connection> connections;
std::map<Node*, int> counter;
Node* next(Node* current){
std::vector<Connection*> 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();
}
}
};
+19 -3
View File
@@ -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* from;
Node* to;
Node* v1;
Node* v2;
bool v1_v2 = true;
bool v2_v1 = true;
bool active = false;
void draw(){
Vec2D<float> v1_position = v1->position - ((v1->position - v2->position)).norm() * (v1->radius + 10.0f);
Vec2D<float> 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<float> from_to_handle = v1->position - ((v1->position - v2->position)).norm() * (v1->radius + 35);
Vec2D<float> 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;
}
};
+14 -16
View File
@@ -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<libremidi::output_port> midi_out_ports;
std::vector<Node> nodes;
std::vector<Signal> signals;
//std::map<Movable*, std::vector<Node*>> connections;
//std::vector<Connection> connections;
ConnectionManager cm;
Vec2D<float> mouse_pos;
Vec2D<float> 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];
}
@@ -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<float> from_position = from.first->position - ((from.first->position - to->position)).norm() * (from.first->radius + 10.0f);*/
/* Vec2D<float> 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));
}
}
+11 -11
View File
@@ -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<Node*> connections;
// std::vector<Node*> 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<float> *mouse) {
if(!hover_move) DrawCircle(position.x, position.y, radius, (Color){180, 210, 115, 255});
@@ -42,11 +42,11 @@ class Node : public Movable{
Vec2D<float> connector = position - ((position - *mouse)).norm() * (radius + R);
for(auto& to : connections){
Vec2D<float> from_position = position - ((position - to->position)).norm() * (radius + 10.0f);
Vec2D<float> 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<float> from_position = position - ((position - to->position)).norm() * (radius + 10.0f);*/
/* Vec2D<float> 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});