Files
viseq/src/graph-manager.h
T

240 lines
9.8 KiB
C++
Raw Normal View History

2024-10-30 14:55:24 +01:00
#pragma once
#include "button.h"
2024-10-30 14:55:24 +01:00
#include "datatypes.h"
#include "graph-item.h"
#include "midi-node.h"
2024-10-30 14:55:24 +01:00
#include "node.h"
#include "edge.h"
#include "raymath.h"
#include <algorithm>
#include <iostream>
#include <iterator>
2024-10-30 14:55:24 +01:00
#include <map>
#include <memory>
2024-10-30 14:55:24 +01:00
#include <random>
#include <ranges>
#include <vector>
2024-10-30 14:55:24 +01:00
class GraphManager{
public:
GraphManager() {}
std::vector<std::shared_ptr<Node>> nodes;
std::vector<std::shared_ptr<Edge>> edges;
2024-10-30 14:55:24 +01:00
std::map<std::shared_ptr<Node>, int> counter;
2024-10-30 14:55:24 +01:00
std::shared_ptr<Node> getRandomNode(){
std::vector<std::shared_ptr<GraphItem>> o;
std::ranges::sample(nodes, std::back_inserter(o), 1, std::mt19937{std::random_device{}()});
return std::dynamic_pointer_cast<Node>(o.front());
}
std::shared_ptr<Edge> getRandomEdge(){
std::vector<std::shared_ptr<GraphItem>> o;
std::ranges::sample(edges, std::back_inserter(o), 1, std::mt19937{std::random_device{}()});
return std::dynamic_pointer_cast<Edge>(o.front());
2024-10-30 14:55:24 +01:00
}
void copyNodes(std::vector<std::shared_ptr<Node>> nodes, Vec2D<float> position){
for(auto n: nodes){
auto nc = std::make_shared<Node>(*n);
nc->position = position + nc->drag_offset;
nc->selected = true;
nodes.push_back(nc);
2024-10-30 14:55:24 +01:00
}
}
void copyNodes(std::vector<std::shared_ptr<MidiNode>> nodes, Vec2D<float> position){
for(auto n: nodes){
std::shared_ptr<MidiNode> nc = std::make_shared<MidiNode>(*n);
nc->position = position + nc->drag_offset;
nc->selected = true;
nodes.push_back(nc);
}
}
std::shared_ptr<Node> copyNode(std::shared_ptr<Node> n, Vec2D<float> position){
auto nc = std::make_shared<Node>(*n);
nc->setPosition(position);
nodes.push_back(nc);
return nc;
2024-10-30 14:55:24 +01:00
}
std::shared_ptr<MidiNode> copyNode(std::shared_ptr<MidiNode> n, Vec2D<float> position){
auto nc = std::make_shared<MidiNode>(*n);
nc->setPosition(position);
nodes.push_back(nc);
return nc;
2024-10-30 14:55:24 +01:00
}
/*std::shared_ptr<Node> addNode(float x, float y){*/
/* return addNode(Vec2D<float>(x,y));*/
/*}*/
/**/
/*std::shared_ptr<Node> addNode(Vec2D<float> position){*/
/* nodes.emplace_back(std::make_shared<Node>(position));*/
/* return nodes.back();*/
/*}*/
std::shared_ptr<Node> addMidiNode(Vec2D<float> position){
nodes.emplace_back(std::make_shared<MidiNode>(position));
return nodes.back();
2024-10-30 14:55:24 +01:00
}
void setNodeMode(int mode){
for(auto n: nodes){
n->SMode(mode);
}
2024-10-30 14:55:24 +01:00
}
void reset(){
counter.clear();
}
std::shared_ptr<Node> next(std::shared_ptr<Node> current, std::shared_ptr<Node> last){
std::vector<std::shared_ptr<Edge>> possible_connections = current->connections | std::ranges::views::filter([current](std::shared_ptr<Edge> c){
return (c->start == current && c->pass_forward) || (c->end == current && c->pass_backward);
}) | std::ranges::to<std::vector<std::shared_ptr<Edge>>>();
2024-10-30 14:55:24 +01:00
if(possible_connections.empty()) return std::shared_ptr<Node>(nullptr);
2024-10-30 14:55:24 +01:00
int return_path = 0;
float angle = -1;
if(current->distributionMode == Distribution::GEOMETRIC){
Vec2D<float> coming_from = (current->position - last->position).norm();
2024-10-30 14:55:24 +01:00
int i = 0;
for(auto n : possible_connections){
std::shared_ptr<Node> next_node = n->getNextNode(current);
float angle_v1_v2 = coming_from.norm().dot((next_node->position - current->position).norm());
2024-10-30 14:55:24 +01:00
if(angle_v1_v2 > angle) {
return_path = i;
angle = angle_v1_v2;
}
++i;
}
}
if(current->distributionMode == Distribution::ROUNDROBIN){
2024-10-30 14:55:24 +01:00
if(counter.find(current) == counter.end()) {
counter[current] = 0;
} else {
counter[current] = (counter[current] + 1) % possible_connections.size();
return_path = counter[current];
}
}
if(current->distributionMode == Distribution::RANDOM){
2024-10-30 14:55:24 +01:00
return_path = rand() % possible_connections.size();
}
possible_connections.at(return_path)->active = true;
return possible_connections.at(return_path)->getNextNode(current);
2024-10-30 14:55:24 +01:00
}
void addConnection(std::shared_ptr<Node> from, std::shared_ptr<Node> to){
edges.emplace_back(std::make_shared<Edge>());
edges.back()->start = from;
edges.back()->end = to;
from->connections.push_back(edges.back());
to->connections.push_back(edges.back());
2024-10-30 14:55:24 +01:00
}
void drawEdges(Vec2D<float> mouse_pos, Camera2D cam){
// std::erase_if(connections, [](Connection c){ return c.remove == true;});
for(auto& c : edges){
Vec2D<float> v1_position = c->start->position - ((c->start->position - c->end->position)).norm() * (c->start->radius + c->connector_offset);
Vec2D<float> v2_position = c->end->position - ((c->end->position - c->start->position)).norm() * (c->end->radius + c->connector_offset);
float v1_v2_distance = v1_position.dist(&v2_position);
float midi_gate_v1 = Clamp((4.f / 1000.f) * c->start->GGate(), 1.f, 4.f);
float midi_gate_v2 = Clamp((4.f / 1000.f) * c->end->GGate(), 1.f, 4.f);
// Line with small connector circles
Color col = {229, 181, 103, 255};
if(c->GActive()) col = {108, 153, 187, 255};
DrawLine(v1_position.x, v1_position.y, v2_position.x, v2_position.y, col);
if(midi_gate_v1 == 4.0f) col = VS_COLOR_RED;
DrawCircle(v1_position.x, v1_position.y, midi_gate_v1, col);
col = {229, 181, 103, 255};
if(midi_gate_v2 == 4.0f) col = VS_COLOR_RED;
DrawCircle(v2_position.x, v2_position.y, midi_gate_v2, col);
if(v1_v2_distance < 100) c->switches_offset = v1_v2_distance/2.3f;
Vec2D<float> from_to_handle = c->start->position - ((c->start->position - c->end->position)).norm() * (c->start->radius + c->switches_offset);
Vec2D<float> to_from_handle = c->end->position - ((c->end->position - c->start->position)).norm() * (c->end->radius + c->switches_offset);
Vec2D<float> middle_handle = c->end->position - ((c->end->position - c->start->position))*0.4f;
Vec2D<float> middle_handle_2 = c->end->position - ((c->end->position - c->start->position))*0.6f;
Vec2D<float> flip_handle = middle_handle + (v1_position - v2_position).norm().orth() * (v1_v2_distance < 120.0f ? Clamp((120.0f - v1_v2_distance)*0.2, 0.f, 20.f) : 1.0f);
Vec2D<float> flip_handle_2 = middle_handle_2 + (v1_position - v2_position).norm().orth() * (v1_v2_distance < 120.0f ? Clamp((120.0f - v1_v2_distance)*-0.2, -20.f, 0.f) : 1.0f);
// Dynamic opacity by distance to mouse
unsigned char opacity = 150.0f - Clamp(middle_handle.dist(mouse_pos), 0.0f, 150.0f);
unsigned char opacity_from = 250.0f - Clamp(from_to_handle.dist(mouse_pos), 0.0f, 220.0f);
unsigned char opacity_to = 250.0f - Clamp(to_from_handle.dist(mouse_pos), 0.0f, 220.0f);
// On/Off switches on both sides
if(ViButton(mouse_pos, from_to_handle.x, from_to_handle.y, 8.0f, c->pass_forward ? (Color){120, 210, 115, opacity_from} : (Color){232, 50, 32, opacity_from} )) c->pass_forward = !c->pass_forward;
if(ViButton(mouse_pos, to_from_handle.x, to_from_handle.y, 8.0f, c->pass_backward ? (Color){120, 210, 115, opacity_to} : (Color){232, 50, 32, opacity_to} )) c->pass_backward = !c->pass_backward;
if(!c->pass_forward){
Vec2D<float> block_point1 = (v1_position - v2_position).norm().orth()*4.0f + from_to_handle;
Vec2D<float> block_point2 = (v1_position - v2_position).norm().orth()*-4.0f + from_to_handle;
DrawLineEx(block_point1, block_point2, 2.0f, (Color){232,232,232,opacity_from});
}
if(!c->pass_backward){
Vec2D<float> block_point1 = (v1_position - v2_position).norm().orth()*4.0f + to_from_handle;
Vec2D<float> block_point2 = (v1_position - v2_position).norm().orth()*-4.0f + to_from_handle;
DrawLineEx(block_point1, block_point2, 2.0f, (Color){232,232,232,opacity_to});
}
if(ViButton(mouse_pos, flip_handle.x, flip_handle.y, 8.0f, (Color){232, 50, 32, opacity} )) c->remove = true;
if(ViButton(mouse_pos, flip_handle_2.x, flip_handle_2.y, 8.0f, (Color){232, 50, 32, opacity} )) c->split = true;
2024-10-30 14:55:24 +01:00
}
}
void drawNodes(Vec2D<float> mouse_pos, Camera2D camera){
auto neighbors = nodes | std::ranges::views::filter([](std::shared_ptr<Node> n){ return !n->dragging && !n->selected;}) | std::ranges::to<std::vector<std::shared_ptr<Node>>>();
2024-10-30 14:55:24 +01:00
float min_d = 480;
for(auto n : nodes){
if(n->dragging){
std::sort(neighbors.begin(), neighbors.end(), [n](std::shared_ptr<Node> na, std::shared_ptr<Node> nb){ return na->position.dist(n->position) < nb->position.dist(n->position);});
2024-10-30 14:55:24 +01:00
if(neighbors.size() > 1){
min_d = neighbors.front()->position.dist(n->position);
n->neighbor = neighbors.at(1);
2024-10-30 14:55:24 +01:00
int i = 0;
for(auto nb : neighbors){
float d = nb->position.dist(n->position);
2024-10-30 14:55:24 +01:00
if(d > 0 && d < min_d*1.5 && d < 960) {
std::string frac = MIDI::Fractions.at((int)Clamp(round(d/60) - 1, 0, 31));
Vector2 text_length = MeasureTextEx(GetFontDefault(), std::format("{:.1f}",d).c_str(), 10.0f, 2.0f);
Vector2 text_length_frac = MeasureTextEx(GetFontDefault(), frac.c_str(), 10.0f, 2.0f);
Vec2D<float> middle_handle = nb->position + (n->position - nb->position) * ((d - text_length.x)*0.5f)/d;
Vec2D<float> middle_handle_frac = nb->position + (n->position - nb->position) * ((d - text_length_frac.x)*0.5f)/d;
Vec2D<float> text_position = middle_handle + (nb->position - n->position).norm().orth() * 12.0f;
Vec2D<float> text_position_frac = middle_handle_frac - (nb->position - n->position).norm().orth() * 4.0f;
2024-10-30 14:55:24 +01:00
float rotation = Vector2Angle((Vector2){1,0}, (n->position - nb->position)) * RAD2DEG;
2024-10-30 14:55:24 +01:00
DrawTextPro(GetFontDefault(), std::format("{:.1f}",d).c_str(), text_position, (Vector2){0,0}, rotation, 10.0f, 2.0f, VS_COLOR_PINK);
if(i == 0 && IsKeyDown(KEY_LEFT_SHIFT)) DrawTextPro(GetFontDefault(), frac.c_str(), text_position_frac, (Vector2){0,0}, rotation, 10.0f, 2.0f, VS_COLOR_PINK);
DrawLineV(n->position, nb->position, VS_COLOR_PINK);
2024-10-30 14:55:24 +01:00
}
}
}
}
n->draw(mouse_pos, camera);
2024-10-30 14:55:24 +01:00
}
}
};