Major refactoring: State, mouse and more...

This commit is contained in:
Sebastian
2024-11-14 00:55:09 +01:00
parent 300001d385
commit 5d3b72d4ef
15 changed files with 243 additions and 207 deletions
+81 -118
View File
@@ -5,7 +5,8 @@
* 2024 Sebastian Kujas
*
*/
#include "globals.h"
#include "datatypes.h"
#include "graph-manager.h"
#include "node.h"
#include "vmath.h"
@@ -15,7 +16,7 @@
#include <raylib.h>
#include <raymath.h>
#include "state-manager.h"
#include "app-manager.h"
#include "datatypes.h"
#include "midi-node.h"
@@ -29,10 +30,13 @@
using json = nlohmann::json;
VISEQ::APP::AppState app;
VISEQ::APP::AppManager app;
std::shared_ptr<VISEQ::APP::State> state = VISEQ::APP::State::get();
Vec2Df mouse_pos;
Vec2Df drag_start;
//Vec2Df global_mouse;
Camera2D camera = { {0} };
@@ -59,19 +63,19 @@ int main(){
app.ui.init();
app.settings.fps = 60.0f;
app.settings.bpm = 120.0f;
app.settings.grid = 60.0f; // 120px for a 16th note
state->settings.fps = 60.0f;
state->settings.bpm = 120.0f;
state->settings.grid = 60.0f; // 120px for a 16th note
app.settings.speed = (app.settings.grid * 16.0f) * (app.settings.bpm / 120.f);
state->settings.speed = (state->settings.grid * 16.0f) * (state->settings.bpm / 120.f);
app.settings.width = monitor_width - 300;
app.settings.height = monitor_height - 300;
state->settings.width = monitor_width - 300;
state->settings.height = monitor_height - 300;
SetTargetFPS(app.settings.fps);
SetTargetFPS(state->settings.fps);
camera.target = (Vector2){ app.settings.width/2.0f, app.settings.height/2.0f };
camera.offset = (Vector2){ app.settings.width/2.0f, app.settings.height/2.0f };
camera.target = (Vector2){ state->settings.width/2.0f, state->settings.height/2.0f };
camera.offset = (Vector2){ state->settings.width/2.0f, state->settings.height/2.0f };
camera.rotation = 0.0f;
camera.zoom = 1.0f;
@@ -79,7 +83,7 @@ int main(){
std::ifstream f("state.json");
json graph = json::parse(f);
app.settings = graph.at("settings");
state->settings = graph.at("settings");
int highest_id = 0;
std::shared_ptr<Node> hid;
@@ -139,10 +143,10 @@ int main(){
} else {
for(int i = 0; i < 4; ++i){
app.gm.addMidiNode(Vec2Df{(float)i * app.settings.grid*4 + 7 * app.settings.grid, 2 * app.settings.grid*4});
app.gm.addMidiNode(Vec2Df{(float)i * state->settings.grid*4 + 7 * state->settings.grid, 2 * state->settings.grid*4});
}
for(int i = 3; i >= 0; --i){
app.gm.addMidiNode(Vec2Df{(float)i * app.settings.grid*4 + 7 * app.settings.grid, 3 * app.settings.grid*4});
app.gm.addMidiNode(Vec2Df{(float)i * state->settings.grid*4 + 7 * state->settings.grid, 3 * state->settings.grid*4});
}
for(int i = 0; i < 8; ++i){
@@ -176,7 +180,7 @@ int main(){
ClearBackground((Color){46,46,46});
BeginMode2D(camera);
app.settings.speed = (app.settings.grid * 16.0f) * (app.settings.bpm / 120.f);
state->settings.speed = (state->settings.grid * 16.0f) * (state->settings.bpm / 120.f);
Vector2 mp = GetScreenToWorld2D(GetMousePosition(), camera);
Vec2Df mouse_pos = {(float)mp.x, (float)mp.y};
@@ -193,34 +197,38 @@ int main(){
Vec2Df mouse_delta = {GetMouseDelta().x, GetMouseDelta().y};
Vec2Df delta = mouse_delta * (-1.0f/camera.zoom);
state->raw_mouse = mouse_pos;
state->mod_mouse = mouse_pos;
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;
}
state->mod_mouse.x = round(mouse_pos.x / state->settings.grid) * state->settings.grid;
state->mod_mouse.y = round(mouse_pos.y / state->settings.grid) * state->settings.grid;
}
/*else if(IsKeyDown(KEY_LEFT_SHIFT)){*/
/* float steps = round((dragNode->neighbors.front()->position.dist(mouse_pos)) / state->settings.grid) * state->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<VEC_SPTR_Node>();*/
@@ -245,8 +253,8 @@ int main(){
/*if(!copied_nodes.empty() && IsKeyDown(KEY_LEFT_CONTROL) && IsKeyPressed(KEY_V)) istate = InteractionState::PASTE_SELECTED_NODES;*/
/**/
/*if(IsKeyPressed(KEY_TAB)) {*/
/* app.settings.mode = (app.settings.mode + 1) % 3;*/
/* app.gm.setNodeMode(app.settings.mode);*/
/* state->settings.mode = (state->settings.mode + 1) % 3;*/
/* app.gm.setNodeMode(state->settings.mode);*/
/*}*/
/*if(!selected_nodes.empty()){*/
@@ -381,10 +389,10 @@ int main(){
/* }*/
/**/
/* if(IsKeyDown(KEY_LEFT_CONTROL)) {*/
/* dragNode->position.x = round((mouse_pos.x + dragNode->drag_offset.x) / app.settings.grid) * app.settings.grid;*/
/* dragNode->position.y = round((mouse_pos.y + dragNode->drag_offset.y) / app.settings.grid) * app.settings.grid;*/
/* dragNode->position.x = round((mouse_pos.x + dragNode->drag_offset.x) / state->settings.grid) * state->settings.grid;*/
/* dragNode->position.y = round((mouse_pos.y + dragNode->drag_offset.y) / state->settings.grid) * state->settings.grid;*/
/* } else if(IsKeyDown(KEY_LEFT_SHIFT)){*/
/* float steps = round((dragNode->neighbors.front()->position.dist(mouse_pos)) / app.settings.grid) * app.settings.grid;*/
/* float steps = round((dragNode->neighbors.front()->position.dist(mouse_pos)) / state->settings.grid) * state->settings.grid;*/
/* if(dragNode->neighbors.size() > 1){*/
/* Vec2Df dn1 = dragNode->neighbors[0]->position;*/
/* Vec2Df dn2 = dragNode->neighbors[1]->position;*/
@@ -443,11 +451,11 @@ int main(){
/* Vec2Df target_position = mouse_pos;*/
/**/
/* if(IsKeyDown(KEY_LEFT_CONTROL)) {*/
/* target_position.x = round((mouse_pos.x) / app.settings.grid) * app.settings.grid;*/
/* target_position.y = round((mouse_pos.y) / app.settings.grid) * app.settings.grid;*/
/* target_position.x = round((mouse_pos.x) / state->settings.grid) * state->settings.grid;*/
/* target_position.y = round((mouse_pos.y) / state->settings.grid) * state->settings.grid;*/
/* } else if(IsKeyDown(KEY_LEFT_SHIFT)){*/
/* std::shared_ptr<Node> closest = app.gm.getClosestNode(mouse_pos);*/
/* float steps = round((closest->position.dist(mouse_pos)) / app.settings.grid) * app.settings.grid;*/
/* float steps = round((closest->position.dist(mouse_pos)) / state->settings.grid) * state->settings.grid;*/
/* float pol_r = steps;*/
/* float pol_theta = Vector2Angle(Vec2Df{1,0},(mouse_pos - closest->position));*/
/**/
@@ -506,74 +514,29 @@ int main(){
/* }*/
/*};*/
/*float wheel = GetMouseWheelMove();*/
/*if(wheel != 0){*/
/* if(!hovering_nodes.empty() && hovering_nodes.front()->getNodeType() == NodeType::MIDINODE) {*/
/* VEC_SPTR_Node modify_nodes;*/
/* std::copy_if(selected_nodes.begin(), selected_nodes.end(), std::back_inserter(modify_nodes), [](SPTR_Node n){ return n->getNodeType() == NodeType::MIDINODE;});*/
/* if(selected_nodes.empty()) std::copy_if(hovering_nodes.begin(), hovering_nodes.end(), std::back_inserter(modify_nodes), [modify_nodes](SPTR_Node n){ return n->getNodeType() == NodeType::MIDINODE && !std::ranges::contains(modify_nodes, n);});*/
/**/
/* for(auto n : modify_nodes){*/
/* std::shared_ptr<MidiNode> midiNode = std::dynamic_pointer_cast<MidiNode>(n);*/
/* if(IsKeyDown(KEY_LEFT_CONTROL) && !IsKeyDown(KEY_LEFT_ALT)){*/
/* switch(app.settings.mode){*/
/* case 0:{*/
/* midiNode->midi_note += wheel;*/
/* break;}*/
/* case 1:{*/
/* midiNode->midi_cc += wheel;*/
/* break;}*/
/* case 2:{*/
/* midiNode->midi_pitch_bend += wheel;*/
/* break;}*/
/* };*/
/* if(midiNode->midi_note < 0 ) midiNode->midi_note = VISEQ::MIDI::Notes.size()-1;*/
/* if(midiNode->midi_note >= VISEQ::MIDI::Notes.size() ) midiNode->midi_note = 0;*/
/**/
/* if(midiNode->midi_cc < 0 ) midiNode->midi_cc = 127;*/
/* if(midiNode->midi_cc > 127 ) midiNode->midi_cc = 0;*/
/**/
/* if(midiNode->midi_pitch_bend < 0 ) midiNode->midi_pitch_bend = 0;*/
/* if(midiNode->midi_pitch_bend > 16383 ) midiNode->midi_pitch_bend = 16383;*/
/**/
/* } else if(IsKeyDown(KEY_LEFT_SHIFT) && !IsKeyDown(KEY_LEFT_ALT)) {*/
/* midiNode->midi_velocity += wheel;*/
/* if(midiNode->midi_velocity < 0 ) midiNode->midi_velocity = 127;*/
/* if(midiNode->midi_velocity > 127 ) midiNode->midi_velocity = 0; */
/* } else if(IsKeyDown(KEY_LEFT_ALT)) {*/
/* switch(app.settings.mode){*/
/* case 0:{*/
/* float incr = (float)wheel * (IsKeyDown(KEY_LEFT_SHIFT) ? 100.0f : IsKeyDown(KEY_LEFT_CONTROL) ? 1000.0f : 10.0f);*/
/* midiNode->midi_gate += incr;*/
/* if(midiNode->midi_gate < 0 ) midiNode->midi_gate = 0;*/
/* break;}*/
/* case 1:{*/
/* midiNode->midi_cc_value += wheel;*/
/* if(midiNode->midi_cc_value < 0 ) midiNode->midi_cc_value = 0;*/
/* if(midiNode->midi_cc_value > 127 ) midiNode->midi_cc_value = 127;*/
/* break;}*/
/* case 2:{*/
/* float incr = (float)wheel * (IsKeyDown(KEY_LEFT_SHIFT) ? 100.0f : IsKeyDown(KEY_LEFT_CONTROL) ? 1000.0f : 10.0f);*/
/* midiNode->midi_pitch_bend += incr;*/
/* if(midiNode->midi_pitch_bend < 0 ) midiNode->midi_pitch_bend = 0;*/
/* if(midiNode->midi_pitch_bend > 16383 ) midiNode->midi_pitch_bend = 16383;*/
/* break;}*/
/* };*/
/* }*/
/* }*/
/* }*/
/* if(!IsKeyDown(KEY_LEFT_SHIFT) && !IsKeyDown(KEY_LEFT_CONTROL) && !IsKeyDown(KEY_LEFT_ALT)){*/
/* 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);*/
/* }*/
/*}*/
float wheel = GetMouseWheelMove();
if(wheel != 0){
if(!hovering_nodes.empty() && hovering_nodes.front()->getNodeType() == NodeType::MIDINODE) {
VEC_SPTR_Node modify_nodes;
std::copy_if(selected_nodes.begin(), selected_nodes.end(), std::back_inserter(modify_nodes), [](SPTR_Node n){ return n->getNodeType() == NodeType::MIDINODE;});
if(selected_nodes.empty()) std::copy_if(hovering_nodes.begin(), hovering_nodes.end(), std::back_inserter(modify_nodes), [modify_nodes](SPTR_Node n){ return n->getNodeType() == NodeType::MIDINODE && !std::ranges::contains(modify_nodes, n);});
for(int x = -app.settings.grid*100; x < app.settings.grid*100; x+=app.settings.grid){
for(int y = -app.settings.grid*100; y < app.settings.grid*100; y+=app.settings.grid){
for(auto n : modify_nodes){
std::shared_ptr<MidiNode> midiNode = std::dynamic_pointer_cast<MidiNode>(n);
}
}
if(!IsKeyDown(KEY_LEFT_SHIFT) && !IsKeyDown(KEY_LEFT_CONTROL) && !IsKeyDown(KEY_LEFT_ALT)){
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);
}
}
for(int x = -state->settings.grid*100; x < state->settings.grid*100; x+=state->settings.grid){
for(int y = -state->settings.grid*100; y < state->settings.grid*100; y+=state->settings.grid){
DrawPixel(x, y, (Color){130,130,130,200});
}
}