node removal invalidates references, switch to different solution

This commit is contained in:
Sebastian
2024-10-25 17:13:30 +02:00
parent 7be8710138
commit 658bc118ef
3 changed files with 28 additions and 1 deletions
+23 -1
View File
@@ -97,7 +97,7 @@ int main(){
double drag_timer = GetTime(); 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; InteractionState istate = HOVER_CANVAS;
Vec2D<float> select_start; Vec2D<float> select_start;
@@ -142,13 +142,18 @@ int main(){
if(!hovering_connectors.empty() && istate == InteractionState::HOVER_CANVAS) istate = InteractionState::HOVER_CONNECTOR; if(!hovering_connectors.empty() && istate == InteractionState::HOVER_CANVAS) istate = InteractionState::HOVER_CONNECTOR;
if(!connecting_nodes.empty()) istate = InteractionState::CONNECT_NODE; if(!connecting_nodes.empty()) istate = InteractionState::CONNECT_NODE;
if(!selected_nodes.empty() && IsKeyPressed(KEY_DELETE)) istate = InteractionState::DELETE_SELECTED_NODES;
switch(istate){ switch(istate){
case InteractionState::HOVER_CANVAS: case InteractionState::HOVER_CANVAS:
{
if(mouse_right_down) { if(mouse_right_down) {
camera.target = Vector2Add(camera.target, delta); camera.target = Vector2Add(camera.target, delta);
} }
break; break;
}
case InteractionState::DRAW_SELECTION: case InteractionState::DRAW_SELECTION:
{
if(mouse_clicked){ if(mouse_clicked){
select_start = mouse_pos; select_start = mouse_pos;
} }
@@ -179,7 +184,9 @@ int main(){
selection = (Rectangle){0,0,0,0}; selection = (Rectangle){0,0,0,0};
} }
break; break;
}
case InteractionState::HOVER_NODE: case InteractionState::HOVER_NODE:
{
if(mouse_clicked) { if(mouse_clicked) {
if(IsKeyDown(KEY_LEFT_SHIFT)) { if(IsKeyDown(KEY_LEFT_SHIFT)) {
hovering_nodes.front().selected = true; hovering_nodes.front().selected = true;
@@ -190,7 +197,9 @@ int main(){
} }
if(mouse_right_clicked) hovering_nodes.front().config = true; if(mouse_right_clicked) hovering_nodes.front().config = true;
break; break;
}
case InteractionState::DRAG_NODE: case InteractionState::DRAG_NODE:
{
if(IsKeyDown(KEY_LEFT_CONTROL)) { if(IsKeyDown(KEY_LEFT_CONTROL)) {
dragging_nodes.front().position = (Vec2D<float>){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}; dragging_nodes.front().position = (Vec2D<float>){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 { } else {
@@ -210,10 +219,14 @@ int main(){
} }
} }
break; break;
}
case InteractionState::HOVER_CONNECTOR: case InteractionState::HOVER_CONNECTOR:
{
if(mouse_clicked) hovering_connectors.front().connecting = true; if(mouse_clicked) hovering_connectors.front().connecting = true;
break; break;
}
case InteractionState::CONNECT_NODE: case InteractionState::CONNECT_NODE:
{
Vec2D<float> connector_position = connecting_nodes.front().position - ((connecting_nodes.front().position - mouse_pos)).norm() * (connecting_nodes.front().radius + 14.0f); Vec2D<float> 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){ if(connecting_nodes.front().connecting && mouse_down){
@@ -234,6 +247,15 @@ int main(){
connecting_nodes.front().connecting = false; connecting_nodes.front().connecting = false;
} }
break; break;
}
case InteractionState::DELETE_SELECTED_NODES:
{
for(auto &n : app.nm.nodes){
n.erase = true;
}
app.nm.removeNodes();
break;
}
}; };
float wheel = GetMouseWheelMove(); float wheel = GetMouseWheelMove();
+4
View File
@@ -20,6 +20,10 @@ class NodeManager{
nodes.emplace_back(position, 15.0f); nodes.emplace_back(position, 15.0f);
} }
void removeNodes(){
std::erase_if(nodes, [](Node n){ return n.erase;});
}
void drawNodes(Vec2D<float> *mouse_pos, Camera2D *camera){ void drawNodes(Vec2D<float> *mouse_pos, Camera2D *camera){
for(auto& n : nodes){ for(auto& n : nodes){
n.draw(mouse_pos, camera); n.draw(mouse_pos, camera);
+1
View File
@@ -32,6 +32,7 @@ class Node : public Movable{
bool connecting = false; bool connecting = false;
bool config = false; bool config = false;
bool selected = false; bool selected = false;
bool erase = false;
int midi_out = -1; int midi_out = -1;