diff --git a/src/button.h b/src/button.h index dd8ee23..f7aed10 100644 --- a/src/button.h +++ b/src/button.h @@ -1,10 +1,11 @@ #pragma once +#include "datatypes.h" #include "raylib.h" #include "raymath.h" -inline bool ViButton(float x, float y, float radius, Color color){ - Vector2 mp = GetMousePosition(); +inline bool ViButton(Vec2D *mouse_pos, float x, float y, float radius, Color color){ + Vector2 mp = {mouse_pos->x, mouse_pos->y}; //GetMousePosition(); TODO: Refactor my own vectors to raylib vectors DrawCircle(x,y,radius, color); return (Vector2Distance(mp, (Vector2){x,y}) < radius && IsMouseButtonPressed(MOUSE_BUTTON_LEFT)); } diff --git a/src/connection-manager.h b/src/connection-manager.h index 5b4ceb0..d8afcf9 100644 --- a/src/connection-manager.h +++ b/src/connection-manager.h @@ -1,6 +1,7 @@ #pragma once #include "connection.h" +#include "raylib.h" #include #include @@ -19,7 +20,7 @@ class ConnectionManager{ std::vector nodes; for(auto& p : connections){ - if(((p.v1 == current && p.v1_v2) || (p.v2 == current && p.v2_v1))) { + if((p.v1 == current && p.v1_v2) || (p.v2 == current && p.v2_v1)) { p.active = false; paths.push_back(&p); nodes.push_back( p.v1 == current ? p.v2 : p.v1 ); @@ -52,10 +53,10 @@ class ConnectionManager{ return paths.at(return_path)->v1 == current ? paths.at(return_path)->v2 : paths.at(return_path)->v1; } - void draw(){ + void draw(Vec2D *mouse_pos, Camera2D *cam){ std::erase_if(connections, [](Connection c){ return c.remove == true;}); for(auto& c : connections){ - c.draw(); + c.draw(mouse_pos, cam); } } }; diff --git a/src/connection.h b/src/connection.h index a6a2a2c..5655063 100644 --- a/src/connection.h +++ b/src/connection.h @@ -2,6 +2,7 @@ #include "button.h" #include "node.h" +#include "raylib.h" class Connection{ @@ -24,28 +25,32 @@ class Connection{ v2 = vt; } - void draw(){ - Vec2D v1_position = v1->position - ((v1->position - v2->position)).norm() * (v1->radius + 10.0f); - Vec2D v2_position = v2->position - ((v2->position - v1->position)).norm() * (v2->radius + 10.0f); + void draw(Vec2D *mouse_pos, Camera2D *cam){ + Vec2D v1_position = v1->position - ((v1->position - v2->position)).norm() * (v1->radius + 14.0f); + Vec2D v2_position = v2->position - ((v2->position - v1->position)).norm() * (v2->radius + 14.0f); Color c = {229, 181, 103, 255}; if(active) c = {108, 153, 187, 255}; DrawLine(v1_position.x, v1_position.y, v2_position.x, v2_position.y, c); + DrawCircle(v1_position.x, v1_position.y, 2.0f, c); + DrawCircle(v2_position.x, v2_position.y, 2.0f, c); Vec2D from_to_handle = v1->position - ((v1->position - v2->position)).norm() * (v1->radius + 35); Vec2D to_from_handle = v2->position - ((v2->position - v1->position)).norm() * (v2->radius + 35); Vec2D middle_handle = v2->position - ((v2->position - v1->position))*0.5f; Vec2D config_handle = v2->position - ((v2->position - v1->position))*0.66f; - Vec2D flip_handle = (v1_position - v2_position).norm().orth()*20.0f; +// Vec2D flip_handle = (v1_position - v2_position).norm().orth()*20.0f; - if(ViButton(from_to_handle.x, from_to_handle.y, 8.0f, v1_v2 ? (Color){232, 125, 62, 255} : (Color){232, 50, 32, 255} )) v1_v2 = !v1_v2; - if(ViButton(to_from_handle.x, to_from_handle.y, 8.0f, v2_v1 ? (Color){232, 125, 62, 255} : (Color){232, 50, 32, 255} )) v2_v1 = !v2_v1; + unsigned char opacity = 150.0f - Clamp(middle_handle.dist(mouse_pos), 0.0f, 150.0f); + unsigned char opacity_from = 250.0f - Clamp(from_to_handle.dist(mouse_pos), 0.0f, 120.0f); + unsigned char opacity_to = 250.0f - Clamp(to_from_handle.dist(mouse_pos), 0.0f, 120.0f); - if(ViButton(middle_handle.x, middle_handle.y, 8.0f, (Color){232, 50, 32, 255} )) remove = true; - - if(ViButton(config_handle.x, config_handle.y, 8.0f, (Color){158, 134, 200, 255})) config = !config; + if(ViButton(mouse_pos, from_to_handle.x, from_to_handle.y, 8.0f, v1_v2 ? (Color){120, 210, 115, opacity_from} : (Color){232, 50, 32, opacity_from} )) v1_v2 = !v1_v2; + if(ViButton(mouse_pos, to_from_handle.x, to_from_handle.y, 8.0f, v2_v1 ? (Color){120, 210, 115, opacity_to} : (Color){232, 50, 32, opacity_to} )) v2_v1 = !v2_v1; + if(ViButton(mouse_pos, middle_handle.x, middle_handle.y, 8.0f, (Color){232, 50, 32, opacity} )) remove = true; + if(ViButton(mouse_pos, config_handle.x, config_handle.y, 8.0f, (Color){158, 134, 200, opacity})) config = !config; - if(ViButton(config_handle.x + flip_handle.x, config_handle.y + flip_handle.y, 8.0f, (Color){158, 0, 200, 255})) flip(); + //if(ViButton(mouse_pos, config_handle.x + flip_handle.x, config_handle.y + flip_handle.y, 8.0f, (Color){158, 0, 200, opacity})) flip(); } }; diff --git a/src/datatypes.h b/src/datatypes.h index 340dc25..1f0b34b 100644 --- a/src/datatypes.h +++ b/src/datatypes.h @@ -139,3 +139,24 @@ struct MidiOut{ int number; }; +namespace MIDI{ + inline std::vector Notes = { + "C-1", "C#-1", "D-1", "D#-1", "E-1", "F-1", "F#-1", "G-1", + "G#-1", "A-1", "A#-1", "B-1", "C0", "C#0", "D0", "D#0", + "E0", "F0", "F#0", "G0", "G#0", "A0", "A#0", "B0", + "C1", "C#1", "D1", "D#1", "E1", "F1", "F#1", "G1", + "G#1", "A1", "A#1", "B1", "C2", "C#2", "D2", "D#2", + "E2", "F2", "F#2", "G2", "G#2", "A2", "A#2", "B2", + "C3", "C#3", "D3", "D#3", "E3", "F3", "F#3", "G3", + "G#3", "A3", "A#3", "B3", "C4", "C#4", "D4", "D#4", + "E4", "F4", "F#4", "G4", "G#4", "A4", "A#4", "B4", + "C5", "C#5", "D5", "D#5", "E5", "F5", "F#5", "G5", + "G#5", "A5", "A#5", "B5", "C6", "C#6", "D6", "D#6", + "E6", "F6", "F#6", "G6", "G#6", "A6", "A#6", "B6", + "C7", "C#7", "D7", "D#7", "E7", "F7", "F#7", "G7", + "G#7", "A7", "A#7", "B7", "C8", "C#8", "D8", "D#8", + "E8", "F8", "F#8", "G8", "G#8", "A8", "A#8", "B8", + "C9", "C#9", "D9", "D#9", "E9", "F9", "F#9", "G9", + "G#9", "A9", "A#9", "B9", "C10" + }; +} diff --git a/src/main.cpp b/src/main.cpp index c52bfff..eba62c5 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -86,7 +86,6 @@ int main(){ for(int i = 0; i < 10; i++){ nodes.emplace(nodes.end(), Node((float)(rand() % app.width*0.5 + app.width*0.25), (float)(rand() % app.height*0.5 + app.height*0.25), 15.0f)); - std::cout << nodes.back().position.x << " " << nodes.back().position.y << std::endl; } for(int i = 0; i < 1; i++){ @@ -97,6 +96,9 @@ int main(){ Vec2D drag_offset; + Vec2D cam_drag_offset; + Vec2D cam_mouse_drag_offset; + Node* connecting_from = nullptr; double drag_timer = GetTime(); @@ -105,11 +107,30 @@ int main(){ Vector2 mp = GetScreenToWorld2D(GetMousePosition(), camera); Vec2D mouse_pos = {(float)mp.x, (float)mp.y}; - + Vec2D cam_pos = {camera.offset.x, camera.offset.y}; + bool mouse_down = IsMouseButtonDown(MOUSE_BUTTON_LEFT); bool mouse_clicked = IsMouseButtonPressed(MOUSE_BUTTON_LEFT); bool mouse_released = IsMouseButtonReleased(MOUSE_BUTTON_LEFT); + if (IsMouseButtonDown(MOUSE_BUTTON_RIGHT)) + { + Vector2 delta = GetMouseDelta(); + delta = Vector2Scale(delta, -1.0f/camera.zoom); + camera.target = Vector2Add(camera.target, delta); + } + + 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(mouse_clicked){ drag_start = mouse_pos; } @@ -122,6 +143,13 @@ int main(){ bool connecting = false; bool dragging = false; + for(int x = -5000; x < 5000; x+=50){ + for(int y = -5000; y < 5000; y+=50){ +// DrawLine(-5000, i, 5000, i, (Color){80,80,80,100}); + DrawPixel(x, y, (Color){120,120,120,200}); + } + } + 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(); @@ -171,7 +199,13 @@ int main(){ drag_timer = GetTime(); } if(mouse_down && n.picked_up){ - if(GetTime() - drag_timer > 0.1) n.position = mouse_pos + drag_offset; + if(GetTime() - drag_timer > 0.1) { + if(IsKeyDown(KEY_LEFT_CONTROL)) { + n.position = (Vec2D){round((mouse_pos.x+drag_offset.x)/50.0f)*50.0f, round((mouse_pos.y+drag_offset.y)/50.0f)*50.0f}; + } else { + n.position = mouse_pos + drag_offset; + } + } } if(mouse_released && n.picked_up){ @@ -183,10 +217,10 @@ int main(){ } } - n.draw(&mouse_pos); + n.draw(&mouse_pos, &camera); } - cm.draw(); + cm.draw(&mouse_pos, &camera); for(auto& s : signals){ s.direction = (s.next->position - s.position).norm(); @@ -201,7 +235,7 @@ int main(){ s.next = n; } } - s.draw(&mouse_pos); + s.draw(&mouse_pos, &camera); } midi.update(); EndMode2D(); @@ -209,61 +243,94 @@ int main(){ // Main Config Window GuiWindowBox((Rectangle){0,0,280,(float)app.height}, "config"); { - GuiLabel((Rectangle){20, 60, 240, 20}, "MIDI OUT"); + GuiLabel((Rectangle){20, 55, 240, 20}, "MIDI OUT"); int i = 0; for(auto& mo : midi.midi_out_ports){ GuiCheckBox((Rectangle){20, 80 + i*20.0f, 20, 20}, mo.port.port_name.c_str(), &mo.enabled); i++; } - if(GuiButton((Rectangle){20, 60, 240, 20}, "ADD SIGNAL")){ - signals.emplace_back(Signal(nodes.front().position.x + 1, nodes.front().position.y + 1, 5.0f, 2.0f)); // position + 1 or else it does not render. why? no idea! - signals.back().next = &nodes[0]; - signals.back().last = &nodes[0]; + if(GuiButton((Rectangle){20, 100 + i*20.0f, 240, 20}, "ENABLE FOR ALL")) { + for(auto &midis : midi.midi_out_ports){ + if(midis.enabled){ + for(auto &n : nodes){ + n.midi_outs[midis.number].enabled = true; + } + } + } + } + + GuiLabel((Rectangle){20, 135 + i*20.0f, 240, 20}, "SIGNALS"); + if(GuiButton((Rectangle){20, 160 + i*20.0f, 240, 20}, "ADD RANDOM SIGNAL")){ + int ni = rand() % nodes.size(); + signals.emplace_back(Signal(nodes[ni].position.x + 1, nodes[ni].position.y + 1, 5.0f, 2.0f)); // position + 1 or else it does not render. why? no idea! + signals.back().next = &nodes[ni]; + signals.back().last = &nodes[ni]; + } + + GuiLabel((Rectangle){20, 185 + i*20.0f, 240, 20}, "NODES"); + if(GuiButton((Rectangle){20, 215 + i*20.0f, 240, 20}, "ADD RANDOM NODE")){ + nodes.emplace_back(Node((float)(rand() % app.width*0.5 + app.width*0.25), (float)(rand() % app.height*0.5 + app.height*0.25), 15.0f)); } } + // Per Node Config Windows for(auto& n : nodes){ - if(n.config){ - n.config = !GuiWindowBox((Rectangle){n.position.x + 10, n.position.y + 10,240,400}, "config"); - int option = n.mode; - GuiComboBox((Rectangle){ n.position.x + 20, n.position.y + 40, 220, 20 }, "Round Robin;Random", &option); - n.mode = (Distribution)option; + Vector2 np = GetWorldToScreen2D((Vector2){n.position.x, n.position.y}, camera); - int y_midi = 60; - GuiLabel((Rectangle){n.position.x + 20, n.position.y + y_midi, 220, 20}, "MIDI OUT"); - for(auto& mo : midi.midi_out_ports){ - if(!n.midi_outs.contains(mo.number) && mo.enabled){ - n.midi_outs[mo.number] = {mo.number, mo.port.port_name, false}; - } + for(auto& mo : midi.midi_out_ports){ + if(!n.midi_outs.contains(mo.number) && mo.enabled){ + n.midi_outs[mo.number] = {mo.number, mo.port.port_name, false}; } + if(n.midi_outs.contains(mo.number) && !mo.enabled){ + n.midi_outs.erase(mo.number); + } + } + + if(n.config){ + n.config = !GuiWindowBox((Rectangle){np.x + 10, np.y + 10, 240, 440}, "config"); + + int option = n.mode; + GuiToggleGroup((Rectangle){ np.x + 20, np.y + 40, 110, 20 }, "Round Robin;Random", &option); + n.mode = (Distribution)option; + + GuiLabel((Rectangle){np.x + 20, np.y + 65, 220, 20}, "SIGNALS"); + if(GuiButton((Rectangle){np.x + 20, np.y + 90, 220, 20}, "ADD SIGNAL")){ + signals.emplace_back(Signal(n.position.x + 1, n.position.y + 1, 5.0f, 2.0f)); // position + 1 or else it does not render. why? no idea! + signals.back().next = &n; + signals.back().last = &n; + } + + int y_midi = 120; + GuiLabel((Rectangle){np.x + 20, np.y + y_midi, 220, 20}, "MIDI OUT"); + int i = 0; for(auto& mo : n.midi_outs){ - GuiCheckBox((Rectangle){n.position.x + 20, n.position.y + y_midi + 20 + i*20.0f, 20, 20}, mo.second.name.c_str(), &mo.second.enabled); + GuiCheckBox((Rectangle){np.x + 20, np.y + y_midi + 20 + i*20.0f, 20, 20}, mo.second.name.c_str(), &mo.second.enabled); i++; } - int y_channel = y_midi + 20 + i*20; - GuiLabel((Rectangle){n.position.x + 20, n.position.y + y_channel, 220, 20}, "CHANNEL"); - GuiToggleGroup((Rectangle){n.position.x + 20, n.position.y + y_channel + 30, 25, 20}, "1;2;3;4;5;6;7;8\n9;10;11;12;13;14;15;16", &n.midi_channel); + int y_channel = y_midi + 30 + i*20; + GuiLabel((Rectangle){np.x + 20, np.y + y_channel, 220, 20}, "CHANNEL"); + GuiToggleGroup((Rectangle){np.x + 20, np.y + y_channel + 30, 25, 20}, "1;2;3;4;5;6;7;8\n9;10;11;12;13;14;15;16", &n.midi_channel); - GuiLabel((Rectangle){n.position.x + 20, n.position.y + y_channel + 90, 100, 20}, "NOTE"); + GuiLabel((Rectangle){np.x + 20, np.y + y_channel + 90, 100, 20}, "NOTE"); float v = n.midi_note; - GuiLabel((Rectangle){n.position.x + 210, n.position.y + y_channel + 90, 20, 20}, midi.midiNotes[(int)round(v)].c_str()); - GuiLabel((Rectangle){n.position.x + 160, n.position.y + y_channel + 90, 40, 20}, std::format("{}",n.midi_note).c_str()); - GuiSlider((Rectangle){n.position.x + 20, n.position.y + y_channel + 120, 220, 20}, "", "", &v, 0, 127); + GuiLabel((Rectangle){np.x + 210, np.y + y_channel + 90, 20, 20}, midi.midiNotes[(int)round(v)].c_str()); + GuiLabel((Rectangle){np.x + 160, np.y + y_channel + 90, 40, 20}, std::format("{}",n.midi_note).c_str()); + GuiSlider((Rectangle){np.x + 20, np.y + y_channel + 120, 220, 20}, "", "", &v, 0, 127); n.midi_note = round(v); - GuiLabel((Rectangle){n.position.x + 20, n.position.y + y_channel + 150, 100, 20}, "VELOCITY"); + GuiLabel((Rectangle){np.x + 20, np.y + y_channel + 150, 100, 20}, "VELOCITY"); v = n.midi_velocity; - GuiLabel((Rectangle){n.position.x + 210, n.position.y + y_channel + 150, 20, 20}, std::format("{}",n.midi_velocity).c_str()); - GuiSlider((Rectangle){n.position.x + 20, n.position.y + y_channel + 180, 220, 20}, "", "", &v, 0, 127); + GuiLabel((Rectangle){np.x + 210, np.y + y_channel + 150, 20, 20}, std::format("{}",n.midi_velocity).c_str()); + GuiSlider((Rectangle){np.x + 20, np.y + y_channel + 180, 220, 20}, "", "", &v, 0, 127); n.midi_velocity = round(v); - GuiLabel((Rectangle){n.position.x + 20, n.position.y + y_channel + 210, 100, 20}, "GATE"); + GuiLabel((Rectangle){np.x + 20, np.y + y_channel + 210, 100, 20}, "GATE"); v = n.midi_gate; - GuiLabel((Rectangle){n.position.x + 210, n.position.y + y_channel + 210, 20, 20}, std::format("{}",(float)n.midi_gate/1000.0f).c_str()); - GuiSlider((Rectangle){n.position.x + 20, n.position.y + y_channel + 240, 220, 20}, "", "", &v, 0, 5000); + GuiLabel((Rectangle){np.x + 210, np.y + y_channel + 210, 20, 20}, std::format("{}",(float)n.midi_gate/1000.0f).c_str()); + GuiSlider((Rectangle){np.x + 20, np.y + y_channel + 240, 220, 20}, "", "", &v, 0, 5000); n.midi_gate = round(v); //if(GuiValueBox((Rectangle){n.position.x + 100, n.position.y + y_channel + 40, 140, 20}, "VELOCITY", &n.midi_velocity, 0, 127, n.gui_midi_velocity_edit)) n.gui_midi_velocity_edit = !n.gui_midi_velocity_edit; diff --git a/src/midi.h b/src/midi.h index 7b3a903..287e70c 100644 --- a/src/midi.h +++ b/src/midi.h @@ -67,6 +67,13 @@ class Midi { midi_outs[out_port->number] = out; } } + + void close_midi_out(MidiOutPort *out_port){ + if(midi_outs.contains(out_port->number)){ + midi_outs[out_port->number]->close_port(); + midi_outs.erase(out_port->number); + } + } void note(int midi_out, int channel, int note, int velocity, int gate){ if(!midi_outs.empty()){ @@ -89,6 +96,8 @@ class Midi { for(auto& p : midi_out_ports){ if(p.enabled) { open_midi_out(&p); + } else if(!p.enabled) { + close_midi_out(&p); } } } diff --git a/src/moveable.h b/src/moveable.h index 54b53a3..74d9470 100644 --- a/src/moveable.h +++ b/src/moveable.h @@ -1,6 +1,7 @@ #pragma once #include "datatypes.h" +#include "raylib.h" class Movable{ @@ -17,6 +18,6 @@ class Movable{ Vec2D direction; virtual void update() {}; - virtual void draw(Vec2D *mouse) {}; + virtual void draw(Vec2D *mouse, Camera2D *cam) {}; }; diff --git a/src/node.h b/src/node.h index 8e42297..d38c4d4 100644 --- a/src/node.h +++ b/src/node.h @@ -5,6 +5,7 @@ #include "datatypes.h" #include "moveable.h" +#include "raymath.h" #define R 14.0f @@ -46,20 +47,23 @@ class Node : public Movable{ Distribution mode = Distribution::ROUNDROBIN; - void draw(Vec2D *mouse) { + void draw(Vec2D *mouse, Camera2D *cam) { if(!hover_move) DrawCircle(position.x, position.y, radius, (Color){180, 210, 115, 255}); if(hover_move) DrawCircle(position.x, position.y, radius, (Color){108, 153, 187, 255}); - + int offset = MeasureText(MIDI::Notes[midi_note].c_str(), 10); + DrawText(MIDI::Notes[midi_note].c_str(), position.x-offset/2.0f, position.y-5, 10, WHITE ); + if(mode == Distribution::RANDOM) DrawCircle(position.x, position.y, radius/2.0f, (Color){229, 181, 103, 255}); Vec2D connector = position - ((position - *mouse)).norm() * (radius + R); + float opacity = 150.0f - Clamp(position.dist(mouse), 0.0f, 150.0f); if(hover_connect){ DrawCircleLines(position.x, position.y, radius + R, (Color){108, 153, 187, 255}); DrawCircle(connector.x, connector.y, 8.0f, (Color){108, 153, 187, 255}); } else { - DrawCircleLines(position.x, position.y, radius + R, (Color){108, 153, 187, 50}); - DrawCircle(connector.x, connector.y, 8.0f, (Color){108, 153, 187, 50}); + DrawCircleLines(position.x, position.y, radius + R, (Color){108, 153, 187, (unsigned char)opacity}); + DrawCircle(connector.x, connector.y, 8.0f, (Color){108, 153, 187, (unsigned char)opacity}); } } }; diff --git a/src/signal.h b/src/signal.h index 8c6b209..ffd0fe8 100644 --- a/src/signal.h +++ b/src/signal.h @@ -23,7 +23,7 @@ class Signal : public Movable{ float radius; - void draw(Vec2D *mouse) { + void draw(Vec2D *mouse, Camera2D *cam) { position += direction.norm() * speed;