From 658bc118ef2ca09d54b5d836ab74652147960ddc Mon Sep 17 00:00:00 2001 From: Sebastian Date: Fri, 25 Oct 2024 17:13:30 +0200 Subject: [PATCH] node removal invalidates references, switch to different solution --- src/main.cpp | 24 +++++++++++++++++++++++- src/node-manager.h | 4 ++++ src/node.h | 1 + 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/main.cpp b/src/main.cpp index 08452ed..8eece87 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -97,7 +97,7 @@ int main(){ double drag_timer = GetTime(); - enum InteractionState {HOVER_CANVAS, HOVER_NODE, HOVER_CONNECTOR, DRAG_NODE, CONNECT_NODE, DRAW_SELECTION}; + enum InteractionState {HOVER_CANVAS, HOVER_NODE, HOVER_CONNECTOR, DRAG_NODE, CONNECT_NODE, DRAW_SELECTION, DELETE_SELECTED_NODES}; InteractionState istate = HOVER_CANVAS; Vec2D select_start; @@ -142,13 +142,18 @@ int main(){ if(!hovering_connectors.empty() && istate == InteractionState::HOVER_CANVAS) istate = InteractionState::HOVER_CONNECTOR; if(!connecting_nodes.empty()) istate = InteractionState::CONNECT_NODE; + if(!selected_nodes.empty() && IsKeyPressed(KEY_DELETE)) istate = InteractionState::DELETE_SELECTED_NODES; + switch(istate){ case InteractionState::HOVER_CANVAS: + { if(mouse_right_down) { camera.target = Vector2Add(camera.target, delta); } break; + } case InteractionState::DRAW_SELECTION: + { if(mouse_clicked){ select_start = mouse_pos; } @@ -179,7 +184,9 @@ int main(){ selection = (Rectangle){0,0,0,0}; } break; + } case InteractionState::HOVER_NODE: + { if(mouse_clicked) { if(IsKeyDown(KEY_LEFT_SHIFT)) { hovering_nodes.front().selected = true; @@ -190,7 +197,9 @@ int main(){ } if(mouse_right_clicked) hovering_nodes.front().config = true; break; + } case InteractionState::DRAG_NODE: + { 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}; } else { @@ -210,10 +219,14 @@ int main(){ } } break; + } case InteractionState::HOVER_CONNECTOR: + { if(mouse_clicked) hovering_connectors.front().connecting = true; break; + } case InteractionState::CONNECT_NODE: + { Vec2D connector_position = connecting_nodes.front().position - ((connecting_nodes.front().position - mouse_pos)).norm() * (connecting_nodes.front().radius + 14.0f); if(connecting_nodes.front().connecting && mouse_down){ @@ -234,6 +247,15 @@ int main(){ connecting_nodes.front().connecting = false; } break; + } + case InteractionState::DELETE_SELECTED_NODES: + { + for(auto &n : app.nm.nodes){ + n.erase = true; + } + app.nm.removeNodes(); + break; + } }; float wheel = GetMouseWheelMove(); diff --git a/src/node-manager.h b/src/node-manager.h index db99f4b..c77aae7 100644 --- a/src/node-manager.h +++ b/src/node-manager.h @@ -20,6 +20,10 @@ class NodeManager{ nodes.emplace_back(position, 15.0f); } + void removeNodes(){ + std::erase_if(nodes, [](Node n){ return n.erase;}); + } + void drawNodes(Vec2D *mouse_pos, Camera2D *camera){ for(auto& n : nodes){ n.draw(mouse_pos, camera); diff --git a/src/node.h b/src/node.h index d5d3935..8235fea 100644 --- a/src/node.h +++ b/src/node.h @@ -32,6 +32,7 @@ class Node : public Movable{ bool connecting = false; bool config = false; bool selected = false; + bool erase = false; int midi_out = -1;