Files
viseq/src/connection-manager.h
T

67 lines
1.7 KiB
C++
Raw Normal View History

#pragma once
#include "connection.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() {}
std::vector<Connection> connections;
std::map<Node*, int> counter;
2024-10-16 02:26:02 +02:00
Node* next(Node* current, Node* last){
std::vector<Connection*> paths;
std::vector<Node*> nodes;
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);
nodes.push_back( p.v1 == current ? p.v2 : p.v1 );
}
}
if(paths.size() == 2) {
Node* next = nodes[0] == last ? nodes[1] : nodes[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 nullptr;
int return_path = 0;
2024-10-16 02:26:02 +02:00
if(current->mode == Distribution::ROUNDROBIN){
if(counter.find(current) == counter.end()) {
counter[current] = 0;
} else {
counter[current] = (counter[current] + 1) % paths.size();
return_path = counter[current];
}
}
if(current->mode == Distribution::RANDOM){
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(Node *from, Node *to){
connections.emplace(connections.end(), 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);
}
}
};