From 79c3de9d973f1dc858f16f55a329a72551b96789 Mon Sep 17 00:00:00 2001 From: Sebastian Date: Thu, 24 Oct 2024 00:39:39 +0200 Subject: [PATCH] connecting rewritten with vector view, mousewheel + ctrl changes notes --- Makefile | 6 +-- src/main.cpp | 136 ++++++++++++++++++++++++++++++++------------------- 2 files changed, 90 insertions(+), 52 deletions(-) diff --git a/Makefile b/Makefile index f64a096..958deb2 100644 --- a/Makefile +++ b/Makefile @@ -1,8 +1,8 @@ CXX = g++ -CXXFLAGS = -std=c++20 -Wall -g -fno-omit-frame-pointer -gdwarf-4 -Og -Wnarrowing -CXXINC = -I/usr/lib/gcc/x86_64-pc-linux-gnu/13.3.0/include -Iinclude +CXXFLAGS = -std=c++20 -Wall -g -fno-omit-frame-pointer -gdwarf-4 -Og -Wnarrowing -DLIBREMIDI_ALSA=1 +CXXINC = -Iinclude -LDLIBS = -lraylib +LDLIBS = -lraylib -lasound -pthread LDFLAGS = -Llib -L/usr/lib -L/usr/local/lib hdr_files = ${wildcard src/*.h} diff --git a/src/main.cpp b/src/main.cpp index 171f5fa..a79ed2f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -19,6 +19,7 @@ #include "datatypes.h" #include "signal-manager.h" #include +#include #include #include #include @@ -100,14 +101,14 @@ int main(){ camera.zoom = 1.0f; SetTargetFPS(app.fps); - - ToggleFullscreen(); + + nodes.reserve(1000); for(int i = 0; i < 4; ++i){ - nodes.emplace(nodes.end(), Node((float)i * app.grid + 10 * app.grid, 5 * app.grid, 15.0f)); + nodes.emplace_back((float)i * app.grid + 10 * app.grid, 5 * app.grid, 15.0f); } for(int i = 3; i >= 0; --i){ - nodes.emplace(nodes.end(), Node((float)i * app.grid + 10 * app.grid, 6 * app.grid, 15.0f)); + nodes.emplace_back((float)i * app.grid + 10 * app.grid, 6 * app.grid, 15.0f); } for(int i = 0; i < 8; ++i){ @@ -130,6 +131,8 @@ int main(){ Vec2D select_start; Rectangle selection; + ToggleFullscreen(); + while(!WindowShouldClose()){ //app.grid = round(((app.fps / (app.bpm / 60.0f)) * app.speed) / 4.0f); // from quarter notes to 16th notes = div by 4 @@ -143,6 +146,16 @@ int main(){ bool mouse_clicked = IsMouseButtonPressed(MOUSE_BUTTON_LEFT); bool mouse_released = IsMouseButtonReleased(MOUSE_BUTTON_LEFT); + for(auto &n : nodes){ + n.hovering = false; + n.hover_connect = false; + } + + auto dragging_nodes = nodes | std::ranges::views::filter([](Node n) { return n.dragging;}); + auto connecting_nodes = nodes | std::ranges::views::filter([](Node n) { return n.connecting;}); + auto hovering_nodes = nodes | std::ranges::views::filter([&mouse_pos](Node n) { return CheckCollisionPointCircle(mouse_pos, n.position, n.radius);}); + auto hovering_connectors = nodes | std::ranges::views::filter([&mouse_pos](Node n) { return CheckCollisionPointCircle(mouse_pos, (n.position - ((n.position - mouse_pos)).norm() * (n.radius + 14.0f)), 8.0f);}); + if(IsMouseButtonDown(MOUSE_BUTTON_RIGHT)){ Vector2 delta = GetMouseDelta(); delta = Vector2Scale(delta, -1.0f/camera.zoom); @@ -151,12 +164,16 @@ int main(){ float wheel = GetMouseWheelMove(); if(wheel != 0){ - Vector2 mouseWorldPos = GetScreenToWorld2D(GetMousePosition(), camera); - camera.offset = GetMousePosition(); - camera.target = mouseWorldPos; - float scaleFactor = 1.0f + (0.25f*fabsf(wheel)); - if (wheel < 0) scaleFactor = 1.0f/scaleFactor; - camera.zoom = Clamp(camera.zoom*scaleFactor, 0.125f, 64.0f); + if(!hovering_nodes.empty() && IsKeyDown(KEY_LEFT_CONTROL)){ + hovering_nodes.front().midi_note = (hovering_nodes.front().midi_note + 1) % midi.midiNotes.size(); + } else { + Vector2 mouseWorldPos = GetScreenToWorld2D(GetMousePosition(), camera); + camera.offset = GetMousePosition(); + camera.target = mouseWorldPos; + float scaleFactor = 1.0f + (0.25f*fabsf(wheel)); + if (wheel < 0) scaleFactor = 1.0f/scaleFactor; + camera.zoom = Clamp(camera.zoom*scaleFactor, 0.125f, 64.0f); + } } if(mouse_clicked){ @@ -176,15 +193,18 @@ int main(){ } } - for(auto &n : nodes){ - n.hovering = false; + + for(auto &h : hovering_connectors){ + h.hover_connect = true; } + if(dragging_nodes.empty() && connecting_nodes.empty()) { - auto dragging_nodes = nodes | std::ranges::views::filter([](Node n) { return n.dragging;}); - - if(dragging_nodes.empty()) { - auto hovering_nodes = nodes | std::ranges::views::filter([&mouse_pos](Node n) { return CheckCollisionPointCircle(mouse_pos, n.position, n.radius);}); - + if(!hovering_connectors.empty()){ + if(mouse_clicked){ + hovering_connectors.front().connecting = true; + } + } + if(hovering_nodes.empty() || selection.width > 0){ if(mouse_clicked){ select_start = mouse_pos; @@ -216,7 +236,9 @@ int main(){ } selection = (Rectangle){0,0,0,0}; } - } else { + } + + if(!hovering_nodes.empty()){ hovering_nodes.front().hovering = true; if(selection.width == 0.0f){ if(mouse_clicked){ @@ -232,7 +254,7 @@ int main(){ } } - } else { + } else if(!dragging_nodes.empty()){ if(mouse_down){ if(IsKeyDown(KEY_LEFT_CONTROL)) { @@ -260,42 +282,58 @@ int main(){ } } - } + } else if(!connecting_nodes.empty() || connecting){ - for(auto& n : nodes){ - Vec2D connector_position = n.position - ((n.position - mouse_pos)).norm() * (n.radius + 14.0f); - float dist_connector = (connector_position - mouse_pos).length(); + Vec2D connector_position = connecting_nodes.front().position - ((connecting_nodes.front().position - mouse_pos)).norm() * (connecting_nodes.front().radius + 14.0f); - float dist = (n.position - mouse_pos).length(); - - // connect nodes - if( (dist <= n.radius + 25.0f && dist > n.radius) || n.connecting){ - n.hover_connect = true; - } else { - n.hover_connect = false; - } - - if(dist_connector <= 8.0f && mouse_clicked && !connecting) { - connecting_from = &n; - n.connecting = true; - connecting = true; - } - - if(n.connecting && mouse_down){ + if(connecting_nodes.front().connecting && mouse_down){ DrawLine(connector_position.x, connector_position.y, mouse_pos.x, mouse_pos.y, (Color){229, 181, 103, 255}); } - if(mouse_released && n.connecting) { - n.connecting = false; - //connecting_from = nullptr; - } - - if(mouse_released && (n.hover_connect || n.dragging)) { - if(&n != connecting_from && connecting_from != nullptr){ - cm.addConnection(connecting_from, &n); - connecting_from = nullptr; + if(mouse_released) { + if(!hovering_nodes.empty()){ + cm.addConnection(&connecting_nodes.front(), &hovering_nodes.front()); + } else if(!hovering_connectors.empty()){ + cm.addConnection(&connecting_nodes.front(), &hovering_connectors.front()); } + connecting_nodes.front().connecting = false; } + } + + for(auto& n : nodes){ + /*Vec2D connector_position = n.position - ((n.position - mouse_pos)).norm() * (n.radius + 14.0f);*/ + /*float dist_connector = (connector_position - mouse_pos).length();*/ + /**/ + /*float dist = (n.position - mouse_pos).length();*/ + /**/ + /*// connect nodes*/ + /*if( (dist <= n.radius + 25.0f && dist > n.radius) || n.connecting){*/ + /* n.hover_connect = true;*/ + /*} else {*/ + /* n.hover_connect = false;*/ + /*}*/ + /**/ + /*if(dist_connector <= 8.0f && mouse_clicked && !connecting) {*/ + /* connecting_from = &n;*/ + /* n.connecting = true;*/ + /* connecting = true;*/ + /*}*/ + /**/ + /*if(n.connecting && mouse_down){*/ + /* DrawLine(connector_position.x, connector_position.y, mouse_pos.x, mouse_pos.y, (Color){229, 181, 103, 255});*/ + /*}*/ + /**/ + /*if(mouse_released && n.connecting) {*/ + /* n.connecting = false;*/ + /* //connecting_from = nullptr;*/ + /*}*/ + /**/ + /*if(mouse_released && (n.hover_connect || n.dragging)) {*/ + /* if(&n != connecting_from && connecting_from != nullptr){*/ + /* cm.addConnection(connecting_from, &n);*/ + /* connecting_from = nullptr;*/ + /* }*/ + /*}*/ n.draw(&mouse_pos, &camera); } @@ -354,7 +392,7 @@ int main(){ int s_nodes = s_signal + 60 + signal_configs*20; GuiLabel((Rectangle){20, (float)s_nodes + 30, 240, 20}, "NODES"); if(GuiButton((Rectangle){20, (float)s_nodes + 60, 240, 20}, "ADD RANDOM NODE")){ - nodes.emplace_back(Node(200.0f, 200.0f, 15.0f)); + nodes.emplace_back(200.0f, 200.0f, 15.0f); }