node removal invalidates references, switch to different solution
This commit is contained in:
+23
-1
@@ -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<float> 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<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 {
|
||||
@@ -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<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){
|
||||
@@ -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();
|
||||
|
||||
@@ -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<float> *mouse_pos, Camera2D *camera){
|
||||
for(auto& n : nodes){
|
||||
n.draw(mouse_pos, camera);
|
||||
|
||||
@@ -32,6 +32,7 @@ class Node : public Movable{
|
||||
bool connecting = false;
|
||||
bool config = false;
|
||||
bool selected = false;
|
||||
bool erase = false;
|
||||
|
||||
int midi_out = -1;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user