From 300001d38513482ead8b5f6200a96e7c076a1219 Mon Sep 17 00:00:00 2001 From: Sebastian Date: Wed, 13 Nov 2024 15:35:39 +0100 Subject: [PATCH] Global snap to grid. --- src/main.cpp | 33 ++++++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 11db124..4bce921 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -25,8 +25,6 @@ #include #include #include -#include -#include #include using json = nlohmann::json; @@ -170,6 +168,7 @@ int main(){ VEC_SPTR_Node copied_nodes; + Vec2Df modified_mouse; while(!WindowShouldClose()){ @@ -193,7 +192,35 @@ int main(){ Vec2Df mouse_delta = {GetMouseDelta().x, GetMouseDelta().y}; Vec2Df delta = mouse_delta * (-1.0f/camera.zoom); - + + if(IsKeyDown(KEY_LEFT_CONTROL)) { + modified_mouse.x = round(mouse_pos.x / app.settings.grid) * app.settings.grid; + modified_mouse.y = round(mouse_pos.y / app.settings.grid) * app.settings.grid; + } else if(IsKeyDown(KEY_LEFT_SHIFT)){ + float steps = round((dragNode->neighbors.front()->position.dist(mouse_pos)) / app.settings.grid) * app.settings.grid; + if(dragNode->neighbors.size() > 1){ + Vec2Df dn1 = dragNode->neighbors[0]->position; + Vec2Df dn2 = dragNode->neighbors[1]->position; + Vec2Df v1_orth = dn1.center(dn2); + + if(dn1.x < dn2.x) std::swap(dn1,dn2); + + float dist = dn1.dist(dn2); + float dist_orth = std::sqrt(std::pow(steps,2) - std::pow(dist/2,2)); + + if(mouse_pos.y < v1_orth.y) dist_orth *= -1; + + Vec2Df v1_orth_t = v1_orth + (dn1-dn2).norm().orth() * dist_orth; + DrawLineV(dn1, dn2, VS_COLOR_RED); + DrawLineV(v1_orth, v1_orth_t, VS_COLOR_RED); + DrawCircleV(v1_orth, 3.0f, VS_COLOR_RED); + dragNode->position = v1_orth_t; + } else if(!dragNode->neighbors.empty()){ + float pol_r = steps; + float pol_theta = Vector2Angle(Vec2Df{1,0},(mouse_pos - dragNode->neighbors.front()->position)); + Vec2Df polar = VISEQ::VMATH::polarToCartesian(pol_r, pol_theta); + dragNode->position = dragNode->neighbors.front()->position + polar; + } // 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->picked_up;}) | std::ranges::to();*/