conversion to smart pointers done but still buggy
This commit is contained in:
+47
-56
@@ -3,11 +3,13 @@
|
||||
#include "edge.h"
|
||||
#include "datatypes.h"
|
||||
#include "graph-manager.h"
|
||||
#include "midi-node.h"
|
||||
#include "signal-manager.h"
|
||||
#include "ui.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <iterator>
|
||||
#include <memory>
|
||||
#include <ranges>
|
||||
#include <vector>
|
||||
|
||||
@@ -30,100 +32,89 @@ class AppState{
|
||||
AppUI ui;
|
||||
|
||||
void updateMidiPorts(){
|
||||
for(auto&n : gm.nodes | std::views::values){
|
||||
for(auto&n : gm.nodes | std::ranges::views::filter([](std::shared_ptr<Node> n){return n->getNodeType() == NodeType::MIDINODE;})){
|
||||
auto nc = std::dynamic_pointer_cast<MidiNode>(n);
|
||||
for(auto& mo : sm.midi.midi_out_ports){
|
||||
if(!n.midi_outs.contains(mo.number) && mo.enabled){
|
||||
n.midi_outs[mo.number] = {mo.number, mo.port.port_name, false};
|
||||
if(!nc->midi_outs.contains(mo.number) && mo.enabled){
|
||||
nc->midi_outs[mo.number] = {mo.number, mo.port.port_name, false};
|
||||
}
|
||||
if(n.midi_outs.contains(mo.number) && !mo.enabled){
|
||||
n.midi_outs.erase(mo.number);
|
||||
if(nc->midi_outs.contains(mo.number) && !mo.enabled){
|
||||
nc->midi_outs.erase(mo.number);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void copyNodesAndConnections(Vec2D<float> position){
|
||||
auto copy_nodes_ids = gm.nodes | std::ranges::views::values | std::ranges::views::filter([](Node n){return n.copied;}) | std::views::transform([](Node n){return n.id;}) | std::ranges::to<std::vector<int>>();
|
||||
void copyNodesAndConnections(std::vector<std::shared_ptr<Node>> nodes, Vec2D<float> position){
|
||||
|
||||
std::vector<Edge> copy_connections;
|
||||
std::ranges::copy_if(gm.connections, std::back_inserter(copy_connections), [copy_nodes_ids](Edge c){return std::ranges::contains(copy_nodes_ids, c.node_id_start) && std::ranges::contains(copy_nodes_ids, c.node_id_end);});
|
||||
std::vector<std::shared_ptr<Edge>> copy_connections;
|
||||
for(auto n : nodes){
|
||||
std::ranges::copy_if(n->connections.begin(), n->connections.end(), std::back_inserter(copy_connections), [nodes](std::shared_ptr<Edge> e){ return std::ranges::contains(nodes, e->start) && std::ranges::contains(nodes, e->end);});
|
||||
}
|
||||
|
||||
for(auto &n : copy_nodes_ids){
|
||||
int old_id = n;
|
||||
int new_id = gm.copyNode(n, position + gm.nodes[n].drag_offset);
|
||||
gm.nodes[new_id].copied = false;
|
||||
gm.nodes[new_id].selected = true;
|
||||
for(auto n : nodes){
|
||||
std::shared_ptr<Node> old_node = n;
|
||||
std::shared_ptr<Node> new_node = gm.copyNode(n, position + n->drag_offset);
|
||||
new_node->copied = false;
|
||||
new_node->selected = true;
|
||||
|
||||
for(auto &c : copy_connections){
|
||||
if(c.node_id_start == old_id) c.node_id_start = new_id;
|
||||
if(c.node_id_end == old_id) c.node_id_end = new_id;
|
||||
if(c->start == old_node) c->start = new_node;
|
||||
if(c->end == old_node) c->end = new_node;
|
||||
}
|
||||
}
|
||||
|
||||
gm.connections.insert(gm.connections.end(), copy_connections.cbegin(), copy_connections.cend());
|
||||
gm.edges.insert(gm.edges.end(), copy_connections.cbegin(), copy_connections.cend());
|
||||
}
|
||||
|
||||
void removeNodes(){
|
||||
void removeNodes(std::vector<std::shared_ptr<Node>> nodes){
|
||||
|
||||
for(auto &n : gm.nodes){
|
||||
n.second.highlighted = false;
|
||||
for(auto n : gm.nodes){
|
||||
n->highlighted = false;
|
||||
}
|
||||
|
||||
auto erase_nodes = gm.nodes | std::ranges::views::values | std::ranges::views::filter([](Node n){return n.erase;});
|
||||
auto erase_connections = gm.edges | std::ranges::views::filter([nodes](std::shared_ptr<Edge> c){ return std::ranges::contains(nodes, c->start) || std::ranges::contains(nodes, c->end);});
|
||||
|
||||
std::vector<int> erase_nodes_ids;
|
||||
for(auto &e : erase_nodes){
|
||||
erase_nodes_ids.push_back(e.id);
|
||||
std::vector<std::shared_ptr<Node>> adjacent_nodes;
|
||||
|
||||
for(auto c : erase_connections){
|
||||
if(!c->start->selected) adjacent_nodes.push_back(c->start);
|
||||
if(!c->end->selected) adjacent_nodes.push_back(c->end);
|
||||
}
|
||||
|
||||
auto erase_connections = gm.connections | std::ranges::views::filter([erase_nodes_ids](Edge c){ return std::ranges::contains(erase_nodes_ids, c.node_id_start) || std::ranges::contains(erase_nodes_ids, c.node_id_end);});
|
||||
auto reroute_signals = sm.signals | std::ranges::views::filter([nodes](std::shared_ptr<Signal> s){return std::ranges::contains(nodes, s->next);});
|
||||
|
||||
std::vector<int> adjacent_nodes_ids;
|
||||
|
||||
for(auto &c : erase_connections){
|
||||
if(!gm.nodes.at(c.node_id_start).selected) adjacent_nodes_ids.push_back(c.node_id_start);
|
||||
if(!gm.nodes.at(c.node_id_end).selected) adjacent_nodes_ids.push_back(c.node_id_end);
|
||||
}
|
||||
|
||||
auto reroute_signals = sm.signals | std::ranges::views::filter([erase_nodes_ids](Signal s){
|
||||
return std::ranges::contains(erase_nodes_ids, s.next);
|
||||
});
|
||||
|
||||
for(auto &s : reroute_signals){
|
||||
for(auto s : reroute_signals){
|
||||
float dist = 10000000;
|
||||
int next_id = -1;
|
||||
for(int i : adjacent_nodes_ids){
|
||||
if(gm.nodes.at(i).position.dist(&s.position) < dist) {
|
||||
dist = gm.nodes.at(i).position.dist(&s.position);
|
||||
next_id = i;
|
||||
std::shared_ptr<Node> next_node;
|
||||
for(auto n : adjacent_nodes){
|
||||
if(n->position.dist(s->position) < dist) {
|
||||
dist = n->position.dist(s->position);
|
||||
next_node = n;
|
||||
}
|
||||
}
|
||||
s.next = next_id;
|
||||
s.last = next_id;
|
||||
s->next = next_node;
|
||||
s->last = next_node;
|
||||
}
|
||||
|
||||
auto rebase_signals = sm.signals | std::ranges::views::filter([erase_nodes_ids](Signal s){
|
||||
return std::ranges::contains(erase_nodes_ids, s.start);
|
||||
});
|
||||
auto rebase_signals = sm.signals | std::ranges::views::filter([nodes](std::shared_ptr<Signal> s){return std::ranges::contains(nodes, s->start);});
|
||||
|
||||
for(auto &s : rebase_signals){
|
||||
s.start = s.next;
|
||||
for(auto s : rebase_signals){
|
||||
s->start = s->next;
|
||||
}
|
||||
|
||||
for(auto &n : erase_nodes){
|
||||
std::erase_if(gm.connections, [n](Edge c){ return c.node_id_start == n.id || c.node_id_end == n.id;});
|
||||
for(auto n : nodes){
|
||||
std::erase_if(gm.edges, [n](std::shared_ptr<Edge> c){ return c->start == n || c->end == n;});
|
||||
}
|
||||
std::erase_if(gm.nodes, [](std::pair<int, Node> n){ return n.second.erase;});
|
||||
gm.keys.clear();
|
||||
std::ranges::copy(gm.nodes | std::ranges::views::keys, std::back_inserter(gm.keys));
|
||||
std::erase_if(gm.nodes, [nodes](std::shared_ptr<Node> n){ return std::ranges::contains(nodes, n);});
|
||||
}
|
||||
|
||||
void draw(Vec2D<float> *mouse_pos, Camera2D *camera){
|
||||
void draw(Vec2D<float> mouse_pos, Camera2D camera){
|
||||
mutex.lock();
|
||||
updateMidiPorts();
|
||||
mutex.unlock();
|
||||
gm.drawNodes(mouse_pos, camera);
|
||||
gm.drawConnections(mouse_pos, camera);
|
||||
gm.drawEdges(mouse_pos, camera);
|
||||
sm.drawSignals(mouse_pos, camera);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user