Files
viseq/src/connection-manager.h
T

74 lines
1.8 KiB
C++
Raw Normal View History

#pragma once
#include "connection.h"
#include "node-manager.h"
2024-10-20 23:42:38 +02:00
#include "raylib.h"
#include <map>
2024-10-16 02:26:02 +02:00
#include <vector>
class ConnectionManager{
public:
ConnectionManager(NodeManager *nm) : nm(nm) {}
NodeManager *nm;
std::vector<Connection> connections;
std::map<int, int> counter;
void reset(){
counter.clear();
}
int next(int current, int last){
std::vector<Connection*> paths;
std::vector<int> node_ids;
for(auto& p : connections){
2024-10-20 23:42:38 +02:00
if((p.v1 == current && p.v1_v2) || (p.v2 == current && p.v2_v1)) {
p.active = false;
paths.push_back(&p);
node_ids.push_back( p.v1 == current ? p.v2 : p.v1 );
}
}
if(paths.size() == 2) {
int next = node_ids[0] == last ? node_ids[1] : node_ids[0];
if(paths[0]->v1 == next || paths[0]->v2 == next) paths[0]->active = true;
if(paths[1]->v1 == next || paths[1]->v2 == next) paths[1]->active = true;
return next;
}
if(paths.empty()) return -1;
int return_path = 0;
2024-10-16 02:26:02 +02:00
if(nm->nodes.at(current).mode == Distribution::ROUNDROBIN){
2024-10-16 02:26:02 +02:00
if(counter.find(current) == counter.end()) {
counter[current] = 0;
} else {
counter[current] = (counter[current] + 1) % paths.size();
return_path = counter[current];
}
}
if(nm->nodes.at(current).mode == Distribution::RANDOM){
2024-10-16 02:26:02 +02:00
return_path = rand() % paths.size();
}
2024-10-16 02:26:02 +02:00
paths.at(return_path)->active = true;
return paths.at(return_path)->v1 == current ? paths.at(return_path)->v2 : paths.at(return_path)->v1;
}
void addConnection(int from, int to){
connections.emplace_back(&nm->nodes, from, to);
}
void drawConnections(Vec2D<float> *mouse_pos, Camera2D *cam){
2024-10-16 02:26:02 +02:00
std::erase_if(connections, [](Connection c){ return c.remove == true;});
for(auto& c : connections){
2024-10-20 23:42:38 +02:00
c.draw(mouse_pos, cam);
}
}
};