From 2e103dc5618b2afea06497174cabca0486848442 Mon Sep 17 00:00:00 2001 From: Sebastian Date: Thu, 14 Nov 2024 01:37:16 +0100 Subject: [PATCH] global mouse moved to state and removed some GetMousePosition()s. --- src/connectable.h | 6 +----- src/connector.h | 9 ++++---- src/draggable.h | 3 +-- src/graph-item.h | 2 +- src/main.cpp | 24 ++++++++------------- src/midi-node.h | 55 ++++++++++++++++++++++++++--------------------- src/moveable.h | 2 +- src/node.h | 6 ++++-- src/state.h | 7 ++++-- src/ui.h | 4 ++-- 10 files changed, 58 insertions(+), 60 deletions(-) diff --git a/src/connectable.h b/src/connectable.h index e6c3a2b..50d6b57 100644 --- a/src/connectable.h +++ b/src/connectable.h @@ -19,12 +19,10 @@ namespace VISEQ::BASE { } virtual bool hovering() override { - Vec2Df mp = {GetMousePosition().x, GetMousePosition().y}; - return CheckCollisionPointCircle(mp, position, 5); + return CheckCollisionPointCircle(state->screen_mouse, position, 5); } virtual void drag() override { - Vec2Df mp = {GetMousePosition().x, GetMousePosition().y}; DrawLineV(position, state->mod_mouse, (Color){229, 181, 103, 255}); } @@ -39,8 +37,6 @@ namespace VISEQ::BASE { } virtual void connect() { - Vec2Df mp = {GetMousePosition().x, GetMousePosition().y}; - if(picked_up){ for(auto c : connectables){ if(c.get() != this && c->hovering()) { diff --git a/src/connector.h b/src/connector.h index d22a9a9..311da0b 100644 --- a/src/connector.h +++ b/src/connector.h @@ -8,6 +8,8 @@ namespace VISEQ::GRAPH { class Connector : public BASE::Connectable, public GraphItem{ public: + using GraphItem::state; + Connector() {} Connector(Vec2Df center_position, float orbit) : orbit(orbit) {setPosition(center_position);} @@ -16,16 +18,13 @@ namespace VISEQ::GRAPH { Vec2Df center_position; void setPosition(Vec2Df pos) override { - Vec2Df mp = {GetMousePosition().x, GetMousePosition().y}; center_position = pos; - if(!partner) position = center_position - ((center_position - mp)).norm() * (radius + orbit); + if(!partner) position = center_position - ((center_position - state->screen_mouse)).norm() * (radius + orbit); if(partner) position = center_position - ((center_position - partner->position)).norm() * (radius + orbit); } void draw() override { - Vec2Df mp = {GetMousePosition().x, GetMousePosition().y}; - - float opacity = 150.0f - Clamp(position.dist(mp), 0.0f, 150.0f); + float opacity = 150.0f - Clamp(position.dist(state->screen_mouse), 0.0f, 150.0f); if(partner){ DrawLineV(position, partner->position, VS_COLOR_RED); diff --git a/src/draggable.h b/src/draggable.h index 21a0175..e5ff1cd 100644 --- a/src/draggable.h +++ b/src/draggable.h @@ -17,10 +17,9 @@ namespace VISEQ::BASE { bool picked_up = false; virtual bool pickedUp() { - Vec2Df mp = {GetMousePosition().x, GetMousePosition().y}; if(clicked() && held()) { picked_up = true; - drag_offset = position - mp; + drag_offset = position - state->raw_mouse; } return picked_up; } diff --git a/src/graph-item.h b/src/graph-item.h index 033de38..9a5fa15 100644 --- a/src/graph-item.h +++ b/src/graph-item.h @@ -1,6 +1,6 @@ #pragma once -#include "globals.h" +#include "state.h" #include "raylib.h" #include "datatypes.h" #include diff --git a/src/main.cpp b/src/main.cpp index f107f3d..befc642 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -5,7 +5,7 @@ * 2024 Sebastian Kujas * */ -#include "globals.h" +#include "state.h" #include "datatypes.h" #include "graph-manager.h" #include "node.h" @@ -83,7 +83,7 @@ int main(){ std::ifstream f("state.json"); json graph = json::parse(f); - state->settings = graph.at("settings"); +// state->settings = graph.at("settings"); int highest_id = 0; std::shared_ptr hid; @@ -196,8 +196,9 @@ int main(){ Vec2Df mouse_delta = {GetMouseDelta().x, GetMouseDelta().y}; Vec2Df delta = mouse_delta * (-1.0f/camera.zoom); - - state->raw_mouse = mouse_pos; + + state->screen_mouse = mouse_pos; + state->raw_mouse = {GetMousePosition().x, GetMousePosition().y}; state->mod_mouse = mouse_pos; if(IsKeyDown(KEY_LEFT_CONTROL)) { @@ -513,22 +514,15 @@ int main(){ /* break;*/ /* }*/ /*};*/ + if(mouse_right_down) { + camera.target = Vector2Add(camera.target, delta); + } 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 = std::dynamic_pointer_cast(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; + camera.target = mouse_pos; 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); diff --git a/src/midi-node.h b/src/midi-node.h index 0ceabba..6fa487f 100644 --- a/src/midi-node.h +++ b/src/midi-node.h @@ -1,6 +1,7 @@ #pragma once #include "datatypes.h" +#include "graph-item.h" #include "node.h" #include "raymath.h" #include @@ -12,7 +13,8 @@ namespace VISEQ::GRAPH::NODES { class MidiNode : public Node{ public: - + using GraphItem::state; + struct NodeMidiOut{ int i; std::string name; @@ -65,8 +67,8 @@ namespace VISEQ::GRAPH::NODES { virtual NodeType getNodeType() const override {return nodeType;} - void setMidiPort(int midi_port, bool state){ - midi_outs[midi_port].enabled = state; + void setMidiPort(int midi_port, bool _state){ + midi_outs[midi_port].enabled = _state; } void trigger() override { @@ -83,49 +85,50 @@ namespace VISEQ::GRAPH::NODES { return midi_gate; } void interact(){ + float wheel = GetMouseWheelMove(); if(IsKeyDown(KEY_LEFT_CONTROL) && !IsKeyDown(KEY_LEFT_ALT)){ - switch(state->settings.mode){ + switch(GraphItem::state->node_mode){ case 0:{ - midiNode->midi_note += wheel; + midi_note += wheel; break;} case 1:{ - midiNode->midi_cc += wheel; + midi_cc += wheel; break;} case 2:{ - midiNode->midi_pitch_bend += wheel; + 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(midi_note < 0 ) midi_note = VISEQ::MIDI::Notes.size()-1; + if(midi_note >= VISEQ::MIDI::Notes.size() ) midi_note = 0; - if(midiNode->midi_cc < 0 ) midiNode->midi_cc = 127; - if(midiNode->midi_cc > 127 ) midiNode->midi_cc = 0; + if(midi_cc < 0 ) midi_cc = 127; + if(midi_cc > 127 ) 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; + if(midi_pitch_bend < 0 ) midi_pitch_bend = 0; + if(midi_pitch_bend > 16383 ) 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; + midi_velocity += wheel; + if(midi_velocity < 0 ) midi_velocity = 127; + if(midi_velocity > 127 ) midi_velocity = 0; } else if(IsKeyDown(KEY_LEFT_ALT)) { - switch(state->settings.mode){ + switch(GraphItem::state->node_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; + midi_gate += incr; + if(midi_gate < 0 ) 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; + midi_cc_value += wheel; + if(midi_cc_value < 0 ) midi_cc_value = 0; + if(midi_cc_value > 127 ) 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; + midi_pitch_bend += incr; + if(midi_pitch_bend < 0 ) midi_pitch_bend = 0; + if(midi_pitch_bend > 16383 ) midi_pitch_bend = 16383; break;} }; } @@ -133,6 +136,8 @@ namespace VISEQ::GRAPH::NODES { void draw(Vec2Df mouse, Camera2D cam) override { Node::draw(); + interact(); + last = time; time = GetTime(); if(time - start >= (float)midi_gate / 1000.0f){ diff --git a/src/moveable.h b/src/moveable.h index db5818d..36963a4 100644 --- a/src/moveable.h +++ b/src/moveable.h @@ -1,6 +1,6 @@ #pragma once -#include "globals.h" +#include "state.h" #include "vmath.h" #include diff --git a/src/node.h b/src/node.h index 4aa136c..bf46703 100644 --- a/src/node.h +++ b/src/node.h @@ -18,6 +18,8 @@ namespace VISEQ::GRAPH::NODES { class Node : public BASE::Draggable, public GraphItem{ public: + using GraphItem::state; + Node() {} Node(float x, float y) : Draggable(Vec2Df(x, y)) {} Node(Vec2Df p) : Draggable(p) {} @@ -52,11 +54,11 @@ namespace VISEQ::GRAPH::NODES { virtual void trigger() {}; virtual bool hovering() override { - Vec2Df mp = {GetMousePosition().x, GetMousePosition().y}; - return CheckCollisionPointCircle(mp, position, radius); + return CheckCollisionPointCircle(state->raw_mouse, position, radius); } void update() override{ + if(pickedUp()) drag(); if(released()) putDown(); int free_connectors = 0; diff --git a/src/state.h b/src/state.h index a50afb1..be55939 100644 --- a/src/state.h +++ b/src/state.h @@ -6,7 +6,6 @@ namespace VISEQ::APP{ struct AppSettings{ - int mode = 0; int width; int height; float grid; @@ -15,7 +14,7 @@ namespace VISEQ::APP{ float speed = 16.0f; int gui_selected_midi_out; }; - NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(AppSettings, mode, width, height, grid, bpm, fps, speed, gui_selected_midi_out) + NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(AppSettings, width, height, grid, bpm, fps, speed, gui_selected_midi_out) class State{ public: @@ -24,9 +23,13 @@ namespace VISEQ::APP{ void operator=(const State &) = delete; AppSettings settings; + Vec2Df raw_mouse; + Vec2Df screen_mouse; Vec2Df mod_mouse; + int node_mode = 0; + inline static std::shared_ptr instance; static std::shared_ptr get() { if(!instance){ diff --git a/src/ui.h b/src/ui.h index fe19f77..a2401cb 100644 --- a/src/ui.h +++ b/src/ui.h @@ -1,6 +1,6 @@ #pragma once -#include "globals.h" +#include "state.h" #include "midi-node.h" #include "raylib.h" #include "raygui/raygui.h" @@ -137,7 +137,7 @@ namespace VISEQ::APP::UI{ GuiCheckBox((Rectangle){np.x + 160, np.y + y_channel, 20, 20}, "PITCH", &mn->send_pitch_bend); int y_cc_note = y_channel + 120; - switch(state->settings.mode){ + switch(state->node_mode){ case 0:{ GuiLabel((Rectangle){np.x + 20, np.y + y_cc_note - 75, 220, 20}, "NOTE CHANNEL"); GuiToggleGroup((Rectangle){np.x + 20, np.y + y_cc_note - 50, 25, 20}, "1;2;3;4;5;6;7;8\n9;10;11;12;13;14;15;16", &mn->midi_note_channel);