From a10335ed478da31985b06a0a9819206dfbab351f Mon Sep 17 00:00:00 2001 From: Sebastian Date: Fri, 1 Nov 2024 14:08:27 +0100 Subject: [PATCH] polar snapping now very goood! --- src/datatypes.h | 31 ++++++++++ src/edge.h | 18 +++--- src/graph-manager.h | 142 ++++++++++++++++++++++++------------------- src/main.cpp | 43 +++++++------ src/node.h | 5 +- src/signal-manager.h | 10 +-- src/signal.h | 10 ++- src/state-manager.h | 28 ++++----- 8 files changed, 171 insertions(+), 116 deletions(-) diff --git a/src/datatypes.h b/src/datatypes.h index 4ad058c..5008d51 100644 --- a/src/datatypes.h +++ b/src/datatypes.h @@ -25,6 +25,27 @@ enum Distribution {GEOMETRIC, ROUNDROBIN, RANDOM}; +// Types + +class GraphItem; +class Node; +class Edge; +class Signal; + +#define RANGE_FILTER(L) std::ranges::views::filter(L) + +typedef std::shared_ptr SPTR_GraphItem; +typedef std::shared_ptr SPTR_Node; +typedef std::shared_ptr SPTR_Edge; + +typedef std::shared_ptr SPTR_Signal; + +typedef std::vector VEC_SPTR_GraphItem; +typedef std::vector VEC_SPTR_Node; +typedef std::vector VEC_SPTR_Edge; +typedef std::vector VEC_SPTR_Signal; + + // 2D Vectors template @@ -155,6 +176,16 @@ Vec2D operator*(const Vec2D left, T scalar){ return Vec2D(left.x * scalar, left.y * scalar); }; +template +Vec2D cartesianToPolar(T x, T y) { + return Vec2D{sqrt(x * x + y * y), atan(y / x)}; // r,theta +} + +template +Vec2D polarToCartesian(T r, T q) { + return Vec2D{r * cos(q), r * sin(q)}; +} + // App Settings struct AppSettings{ int mode = 0; diff --git a/src/edge.h b/src/edge.h index 91926a8..c85dfb3 100644 --- a/src/edge.h +++ b/src/edge.h @@ -1,24 +1,21 @@ #pragma once +#include "datatypes.h" #include "graph-item.h" -#include - -class Node; class Edge : public GraphItem{ public: Edge() {} - Edge(std::shared_ptr start, std::shared_ptr end) : start(start), end(end) {} + Edge(SPTR_Node start, SPTR_Node end) : start(start), end(end) {} - std::shared_ptr start; - std::shared_ptr end; + SPTR_Node start; + SPTR_Node end; bool pass_forward = true; bool pass_backward = true; bool active = false; - bool remove = false; bool config = false; bool split = false; @@ -27,11 +24,16 @@ class Edge : public GraphItem{ virtual bool GActive() {return active;} - std::shared_ptr getNextNode(std::shared_ptr origin){ + SPTR_Node getNextNode(SPTR_Node origin){ return start == origin ? end : start; } void draw(Vec2D mouse_pos, Camera2D cam) { } + + void remove(){ + delete this; + } + }; diff --git a/src/graph-manager.h b/src/graph-manager.h index fee8692..935dd56 100644 --- a/src/graph-manager.h +++ b/src/graph-manager.h @@ -1,14 +1,18 @@ #pragma once -#include "button.h" + #include "datatypes.h" +#include "raylib.h" +#include "raymath.h" + +#include "button.h" #include "graph-item.h" #include "midi-node.h" #include "node.h" #include "edge.h" -#include "raymath.h" + #include -#include +#include #include #include #include @@ -22,24 +26,26 @@ class GraphManager{ GraphManager() {} - std::vector> nodes; - std::vector> edges; + VEC_SPTR_Node nodes; + VEC_SPTR_Edge edges; - std::map, int> counter; + VEC_SPTR_Edge remove_edges; - std::shared_ptr getRandomNode(){ - std::vector> o; + std::map counter; + + SPTR_Node getRandomNode(){ + VEC_SPTR_GraphItem o; std::ranges::sample(nodes, std::back_inserter(o), 1, std::mt19937{std::random_device{}()}); return std::dynamic_pointer_cast(o.front()); } - std::shared_ptr getRandomEdge(){ - std::vector> o; + SPTR_Edge getRandomEdge(){ + VEC_SPTR_GraphItem o; std::ranges::sample(edges, std::back_inserter(o), 1, std::mt19937{std::random_device{}()}); return std::dynamic_pointer_cast(o.front()); } - void copyNodes(std::vector> nodes, Vec2D position){ + void copyNodes(VEC_SPTR_Node nodes, Vec2D position){ for(auto n: nodes){ auto nc = std::make_shared(*n); nc->position = position + nc->drag_offset; @@ -57,7 +63,7 @@ class GraphManager{ } } - std::shared_ptr copyNode(std::shared_ptr n, Vec2D position){ + SPTR_Node copyNode(SPTR_Node n, Vec2D position){ auto nc = std::make_shared(*n); nc->setPosition(position); nodes.push_back(nc); @@ -70,17 +76,8 @@ class GraphManager{ nodes.push_back(nc); return nc; } - - /*std::shared_ptr addNode(float x, float y){*/ - /* return addNode(Vec2D(x,y));*/ - /*}*/ - /**/ - /*std::shared_ptr addNode(Vec2D position){*/ - /* nodes.emplace_back(std::make_shared(position));*/ - /* return nodes.back();*/ - /*}*/ - - std::shared_ptr addMidiNode(Vec2D position){ + + SPTR_Node addMidiNode(Vec2D position){ nodes.emplace_back(std::make_shared(position)); return nodes.back(); } @@ -95,12 +92,12 @@ class GraphManager{ counter.clear(); } - std::shared_ptr next(std::shared_ptr current, std::shared_ptr last){ - std::vector> possible_connections = current->connections | std::ranges::views::filter([current](std::shared_ptr c){ + SPTR_Node next(SPTR_Node current, SPTR_Node last){ + VEC_SPTR_Edge possible_connections = current->connections | RANGE_FILTER([current](SPTR_Edge c){ return (c->start == current && c->pass_forward) || (c->end == current && c->pass_backward); - }) | std::ranges::to>>(); + }) | std::ranges::to(); - if(possible_connections.empty()) return std::shared_ptr(nullptr); + if(possible_connections.empty()) return SPTR_Node(nullptr); int return_path = 0; float angle = -1; @@ -108,7 +105,7 @@ class GraphManager{ Vec2D coming_from = (current->position - last->position).norm(); int i = 0; for(auto n : possible_connections){ - std::shared_ptr next_node = n->getNextNode(current); + SPTR_Node next_node = n->getNextNode(current); float angle_v1_v2 = coming_from.norm().dot((next_node->position - current->position).norm()); if(angle_v1_v2 > angle) { return_path = i; @@ -131,11 +128,11 @@ class GraphManager{ return_path = rand() % possible_connections.size(); } - possible_connections.at(return_path)->active = true; + //possible_connections.at(return_path)->active = true; return possible_connections.at(return_path)->getNextNode(current); } - void addConnection(std::shared_ptr from, std::shared_ptr to){ + void addConnection(SPTR_Node from, SPTR_Node to){ edges.emplace_back(std::make_shared()); edges.back()->start = from; edges.back()->end = to; @@ -143,8 +140,25 @@ class GraphManager{ to->connections.push_back(edges.back()); } + void splitEdge(SPTR_Edge edge){ + + } + + void removeEdge(SPTR_Edge edge){ + remove_edges.push_back(edge); + } + + void removeEdges(){ + for(auto edge : remove_edges){ + std::erase(edge->start->connections, edge); + std::erase(edge->end->connections, edge); + std::erase(edges, edge); + } + } + void drawEdges(Vec2D mouse_pos, Camera2D cam){ // std::erase_if(connections, [](Connection c){ return c.remove == true;}); + if(!remove_edges.empty()) removeEdges(); for(auto& c : edges){ Vec2D v1_position = c->start->position - ((c->start->position - c->end->position)).norm() * (c->start->radius + c->connector_offset); Vec2D v2_position = c->end->position - ((c->end->position - c->start->position)).norm() * (c->end->radius + c->connector_offset); @@ -178,56 +192,62 @@ class GraphManager{ 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->start->dragging && !c->end->dragging){ + // 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 block_point1 = (v1_position - v2_position).norm().orth()*4.0f + from_to_handle; - Vec2D 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 block_point1 = (v1_position - v2_position).norm().orth()*4.0f + to_from_handle; - Vec2D 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(!c->pass_forward){ + Vec2D block_point1 = (v1_position - v2_position).norm().orth()*4.0f + from_to_handle; + Vec2D 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 block_point1 = (v1_position - v2_position).norm().orth()*4.0f + to_from_handle; + Vec2D 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; + if(ViButton(mouse_pos, flip_handle.x, flip_handle.y, 8.0f, (Color){232, 50, 32, opacity} )) removeEdge(c); + if(ViButton(mouse_pos, flip_handle_2.x, flip_handle_2.y, 8.0f, (Color){232, 125, 62, opacity} )) splitEdge(c); + } } } void drawNodes(Vec2D mouse_pos, Camera2D camera){ - auto neighbors = nodes | std::ranges::views::filter([](std::shared_ptr n){ return !n->dragging && !n->selected;}) | std::ranges::to>>(); + auto neighbors = nodes | RANGE_FILTER([](SPTR_Node n){ return !n->dragging && !n->selected;}) | std::ranges::to(); float min_d = 480; for(auto n : nodes){ if(n->dragging){ - std::sort(neighbors.begin(), neighbors.end(), [n](std::shared_ptr na, std::shared_ptr nb){ return na->position.dist(n->position) < nb->position.dist(n->position);}); + std::sort(neighbors.begin(), neighbors.end(), [n, mouse_pos](SPTR_Node na, SPTR_Node nb){ return na->position.dist(mouse_pos) < nb->position.dist(mouse_pos);}); if(neighbors.size() > 1){ - min_d = neighbors.front()->position.dist(n->position); - n->neighbor = neighbors.at(1); - int i = 0; + min_d = neighbors.front()->position.dist(mouse_pos); + n->neighbor = neighbors.front(); + DrawCircleLinesV(n->neighbor->position, 20.0f, VS_COLOR_PINK); for(auto nb : neighbors){ - float d = nb->position.dist(n->position); + float d = nb->position.dist(mouse_pos); + float dn = nb->position.dist(n->position); 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); + std::string frac = MIDI::Fractions.at((int)Clamp(round(dn/60) - 1, 0, 31)); + Vector2 text_length = MeasureTextEx(GetFontDefault(), std::format("{:.1f}",dn).c_str(), 10.0f, 2.0f); Vector2 text_length_frac = MeasureTextEx(GetFontDefault(), frac.c_str(), 10.0f, 2.0f); - Vec2D middle_handle = nb->position + (n->position - nb->position) * ((d - text_length.x)*0.5f)/d; - Vec2D middle_handle_frac = nb->position + (n->position - nb->position) * ((d - text_length_frac.x)*0.5f)/d; + Vec2D middle_handle = nb->position + (n->position - nb->position) * ((dn - text_length.x)*0.5f)/dn; + Vec2D middle_handle_frac = nb->position + (n->position - nb->position) * ((dn - text_length_frac.x)*0.5f)/dn; Vec2D text_position = middle_handle + (nb->position - n->position).norm().orth() * 12.0f; Vec2D text_position_frac = middle_handle_frac - (nb->position - n->position).norm().orth() * 4.0f; float rotation = Vector2Angle((Vector2){1,0}, (n->position - nb->position)) * RAD2DEG; - 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); + DrawTextPro(GetFontDefault(), std::format("{:.1f}",dn).c_str(), text_position, (Vector2){0,0}, rotation, 10.0f, 2.0f, VS_COLOR_PINK); + if(nb == neighbors.front() && IsKeyDown(KEY_LEFT_SHIFT)) DrawTextPro(GetFontDefault(), frac.c_str(), text_position_frac, (Vector2){0,0}, rotation, 10.0f, 2.0f, VS_COLOR_PINK); + Vec2D n_position = n->position - ((n->position - nb->position)).norm() * 20.0f; + Vec2D nb_position = nb->position - ((nb->position - n->position)).norm() * 20.0f; + DrawLineV(n_position, nb_position, VS_COLOR_PINK); } + } } } diff --git a/src/main.cpp b/src/main.cpp index 974cbe1..9c9961a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -38,8 +38,7 @@ int main(){ srand(time(NULL)); - - InitWindow(1920, 1080, "VISEQ"); + InitWindow(1920, 1080, "VISEQ - The Visual Sequencer"); int current_monitor = GetCurrentMonitor(); int monitor_width = GetMonitorWidth(current_monitor); @@ -71,10 +70,10 @@ int main(){ camera.zoom = 1.0f; for(int i = 0; i < 4; ++i){ - app.gm.addMidiNode(Vec2D{(float)i * app.settings.grid*4 + 5 * app.settings.grid, 5 * app.settings.grid*4}); + app.gm.addMidiNode(Vec2D{(float)i * app.settings.grid*4 + 7 * app.settings.grid, 2 * app.settings.grid*4}); } for(int i = 3; i >= 0; --i){ - app.gm.addMidiNode(Vec2D{(float)i * app.settings.grid*4 + 5 * app.settings.grid, 6 * app.settings.grid*4}); + app.gm.addMidiNode(Vec2D{(float)i * app.settings.grid*4 + 7 * app.settings.grid, 3 * app.settings.grid*4}); } for(int i = 0; i < 8; ++i){ @@ -92,7 +91,7 @@ int main(){ Vec2D select_start; Rectangle selection; - std::vector> copied_nodes; + VEC_SPTR_Node copied_nodes; while(!WindowShouldClose()){ @@ -117,11 +116,11 @@ int main(){ Vec2D mouse_delta = {GetMouseDelta().x, GetMouseDelta().y}; Vec2D delta = mouse_delta * (-1.0f/camera.zoom); - auto dragging_nodes = app.gm.nodes | std::ranges::views::filter([](std::shared_ptr n) { return n->dragging;}); - auto connecting_nodes = app.gm.nodes | std::ranges::views::filter([](std::shared_ptr n) { return n->connecting;}); - auto selected_nodes = app.gm.nodes | std::ranges::views::filter([](std::shared_ptr n) { return n->selected && !n->dragging;}) | std::ranges::to>>(); - auto hovering_nodes = app.gm.nodes | std::ranges::views::filter([&mouse_pos](std::shared_ptr n) { return CheckCollisionPointCircle(mouse_pos, n->position, n->radius);}); - auto hovering_connectors = app.gm.nodes | std::ranges::views::filter([&mouse_pos](std::shared_ptr n) { return CheckCollisionPointCircle(mouse_pos, (n->position - ((n->position - mouse_pos)).norm() * (n->radius + 14.0f)), 8.0f);}); + auto dragging_nodes = app.gm.nodes | RANGE_FILTER([](SPTR_Node n) { return n->dragging;}); + auto connecting_nodes = app.gm.nodes | RANGE_FILTER([](SPTR_Node n) { return n->connecting;}); + auto selected_nodes = app.gm.nodes | RANGE_FILTER([](SPTR_Node n) { return n->selected && !n->dragging;}) | std::ranges::to(); + auto hovering_nodes = app.gm.nodes | RANGE_FILTER([&mouse_pos](SPTR_Node n) { return CheckCollisionPointCircle(mouse_pos, n->position, n->radius);}); + auto hovering_connectors = app.gm.nodes | RANGE_FILTER([&mouse_pos](SPTR_Node n) { return CheckCollisionPointCircle(mouse_pos, (n->position - ((n->position - mouse_pos)).norm() * (n->radius + 14.0f)), 8.0f);}); for(auto &n : app.gm.nodes ){ if(hovering_nodes.empty()) n->hovering = false; @@ -206,23 +205,29 @@ int main(){ case InteractionState::DRAG_NODE: { if(!dragging_nodes.empty()){ + SPTR_Node dragNode = dragging_nodes.front(); + for(auto &n : selected_nodes){ - if(!n->picked_up) n->drag_offset = n->position - dragging_nodes.front()->position; + if(!n->picked_up) n->drag_offset = n->position - dragNode->position; n->picked_up = true; } if(IsKeyDown(KEY_LEFT_CONTROL)) { - dragging_nodes.front()->position = (Vec2D){round((mouse_pos.x+dragging_nodes.front()->drag_offset.x)/app.settings.grid)*app.settings.grid, round((mouse_pos.y+dragging_nodes.front()->drag_offset.y)/app.settings.grid)*app.settings.grid}; + dragNode->position.x = round((mouse_pos.x + dragNode->drag_offset.x) / app.settings.grid) * app.settings.grid; + dragNode->position.y = round((mouse_pos.y + dragNode->drag_offset.y) / app.settings.grid) * app.settings.grid; } else if(IsKeyDown(KEY_LEFT_SHIFT)){ - float steps = round(dragging_nodes.front()->neighbor->position.dist(mouse_pos) / app.settings.grid) * app.settings.grid; - Vec2D polar = dragging_nodes.front()->neighbor->position - (dragging_nodes.front()->neighbor->position - (mouse_pos + dragging_nodes.front()->drag_offset)).norm() * steps; - dragging_nodes.front()->position = polar; + float steps = round((dragNode->neighbor->position.dist(mouse_pos)) / app.settings.grid) * app.settings.grid; + float pol_r = steps; + float pol_theta = Vector2Angle(Vec2D{1,0},(mouse_pos - dragNode->neighbor->position)); + + Vec2D polar = polarToCartesian(pol_r, pol_theta); + dragNode->position = dragNode->neighbor->position + polar; } else { - dragging_nodes.front()->position = mouse_pos + dragging_nodes.front()->drag_offset; + dragNode->position = mouse_pos + dragNode->drag_offset; } for(auto &n : selected_nodes){ - n->position = dragging_nodes.front()->position + n->drag_offset; + n->position = dragNode->position + n->drag_offset; } if(mouse_released) { @@ -269,7 +274,7 @@ int main(){ } else if(!hovering_connectors.empty()){ app.gm.addConnection(connecting_nodes.front(), hovering_connectors.front()); } else { - std::shared_ptr new_node = app.gm.addMidiNode(mouse_pos); + SPTR_Node new_node = app.gm.addMidiNode(mouse_pos); app.gm.addConnection(connecting_nodes.front(), new_node); } } @@ -280,7 +285,7 @@ int main(){ } case InteractionState::DELETE_SELECTED_NODES: { - app.removeNodes(selected_nodes | std::ranges::to>>()); + app.removeNodes(selected_nodes | std::ranges::to()); break; } case InteractionState::COPY_SELECTED_NODES: diff --git a/src/node.h b/src/node.h index 6952e7f..8474a44 100644 --- a/src/node.h +++ b/src/node.h @@ -1,6 +1,5 @@ #pragma once -#include #include #include "edge.h" @@ -17,9 +16,9 @@ class Node : public Movable, public GraphItem{ NodeType nodeType; - std::shared_ptr neighbor; + SPTR_Node neighbor; - std::vector> connections; + VEC_SPTR_Edge connections; Distribution distributionMode = Distribution::GEOMETRIC; diff --git a/src/signal-manager.h b/src/signal-manager.h index aa2ada5..6566ef6 100644 --- a/src/signal-manager.h +++ b/src/signal-manager.h @@ -21,7 +21,7 @@ class SignalManager{ std::mutex *mutex; GraphManager *gm; - std::vector> signals; + VEC_SPTR_Signal signals; bool loop = true; bool play = false; @@ -57,9 +57,9 @@ class SignalManager{ loop = false; } - void addSignalAtNode(std::shared_ptr node){ + void addSignalAtNode(SPTR_Node node){ Color c = {(unsigned char)(20 + (rand() % 200)), 100, (unsigned char)(20 + (int)(rand() % 200)), 200}; - signals.emplace_back(std::make_shared(node->position, 8.0f, 0, c)); // position + 1 or else it does not render. why? no idea! + signals.emplace_back(std::make_shared(node->position + Vec2D{0.5f, 0.5f}, 8.0f, 0, c)); // position + 1 or else it does not render. why? no idea! signals.back()->next = node; signals.back()->last = node; signals.back()->start = node; @@ -80,10 +80,10 @@ class SignalManager{ s->direction = (s->next->position - s->position).norm(); s->update(diff.count()); if(s->position.dist(s->next->position) < 0.6f) { - std::shared_ptr next_node = gm->next(s->next, s->last); + SPTR_Node next_node = gm->next(s->next, s->last); s->next->trigger(); - if(next_node != s->next && next_node != std::shared_ptr(nullptr) && s->next->getNodeType() == NodeType::MIDINODE) { + if(next_node != s->next && next_node != SPTR_Node(nullptr) && s->next->getNodeType() == NodeType::MIDINODE) { auto midinode = std::dynamic_pointer_cast(s->next); for(auto &m : midinode->midi_outs){ if(m.second.enabled) { diff --git a/src/signal.h b/src/signal.h index 29397f3..b72b799 100644 --- a/src/signal.h +++ b/src/signal.h @@ -1,11 +1,9 @@ #pragma once -#include #include #include "datatypes.h" #include "moveable.h" -#include "node.h" class Signal : public Movable{ public: @@ -13,11 +11,11 @@ class Signal : public Movable{ Signal(); Signal(Vec2D position, float r, float _speed, Color color) : Movable(position, Vec2D(0,0), _speed), radius(r), color(color) {} - std::shared_ptr next; - std::shared_ptr last; - std::shared_ptr edge; + SPTR_Node next; + SPTR_Node last; + SPTR_Edge edge; - std::shared_ptr start; + SPTR_Node start; bool at_target = false; diff --git a/src/state-manager.h b/src/state-manager.h index 416af1c..b9adef9 100644 --- a/src/state-manager.h +++ b/src/state-manager.h @@ -32,7 +32,7 @@ class AppState{ AppUI ui; void updateMidiPorts(){ - for(auto&n : gm.nodes | std::ranges::views::filter([](std::shared_ptr n){return n->getNodeType() == NodeType::MIDINODE;})){ + for(auto&n : gm.nodes | RANGE_FILTER([](SPTR_Node n){return n->getNodeType() == NodeType::MIDINODE;})){ auto nc = std::dynamic_pointer_cast(n); for(auto& mo : sm.midi.midi_out_ports){ if(!nc->midi_outs.contains(mo.number) && mo.enabled){ @@ -45,16 +45,16 @@ class AppState{ } } - void copyNodesAndConnections(std::vector> nodes, Vec2D position){ + void copyNodesAndConnections(VEC_SPTR_Node nodes, Vec2D position){ - std::vector> copy_connections; + VEC_SPTR_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 e){ return std::ranges::contains(nodes, e->start) && std::ranges::contains(nodes, e->end);}); + std::ranges::copy_if(n->connections.begin(), n->connections.end(), std::back_inserter(copy_connections), [nodes](SPTR_Edge e){ return std::ranges::contains(nodes, e->start) && std::ranges::contains(nodes, e->end);}); } for(auto n : nodes){ - std::shared_ptr old_node = n; - std::shared_ptr new_node = gm.copyNode(n, position + n->drag_offset); + SPTR_Node old_node = n; + SPTR_Node new_node = gm.copyNode(n, position + n->drag_offset); new_node->copied = false; new_node->selected = true; @@ -67,26 +67,26 @@ class AppState{ gm.edges.insert(gm.edges.end(), copy_connections.cbegin(), copy_connections.cend()); } - void removeNodes(std::vector> nodes){ + void removeNodes(VEC_SPTR_Node nodes){ for(auto n : gm.nodes){ n->highlighted = false; } - auto erase_connections = gm.edges | std::ranges::views::filter([nodes](std::shared_ptr c){ return std::ranges::contains(nodes, c->start) || std::ranges::contains(nodes, c->end);}); + auto erase_connections = gm.edges | RANGE_FILTER([nodes](SPTR_Edge c){ return std::ranges::contains(nodes, c->start) || std::ranges::contains(nodes, c->end);}); - std::vector> adjacent_nodes; + VEC_SPTR_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 reroute_signals = sm.signals | std::ranges::views::filter([nodes](std::shared_ptr s){return std::ranges::contains(nodes, s->next);}); + auto reroute_signals = sm.signals | RANGE_FILTER([nodes](SPTR_Signal s){return std::ranges::contains(nodes, s->next);}); for(auto s : reroute_signals){ float dist = 10000000; - std::shared_ptr next_node; + SPTR_Node next_node; for(auto n : adjacent_nodes){ if(n->position.dist(s->position) < dist) { dist = n->position.dist(s->position); @@ -97,16 +97,16 @@ class AppState{ s->last = next_node; } - auto rebase_signals = sm.signals | std::ranges::views::filter([nodes](std::shared_ptr s){return std::ranges::contains(nodes, s->start);}); + auto rebase_signals = sm.signals | RANGE_FILTER([nodes](SPTR_Signal s){return std::ranges::contains(nodes, s->start);}); for(auto s : rebase_signals){ s->start = s->next; } for(auto n : nodes){ - std::erase_if(gm.edges, [n](std::shared_ptr c){ return c->start == n || c->end == n;}); + std::erase_if(gm.edges, [n](SPTR_Edge c){ return c->start == n || c->end == n;}); } - std::erase_if(gm.nodes, [nodes](std::shared_ptr n){ return std::ranges::contains(nodes, n);}); + std::erase_if(gm.nodes, [nodes](SPTR_Node n){ return std::ranges::contains(nodes, n);}); } void draw(Vec2D mouse_pos, Camera2D camera){