Files
viseq/src/main.cpp
T

251 lines
6.1 KiB
C++
Raw Normal View History

2024-10-09 02:01:23 +02:00
/*
*
* VISEQ - Visual Sequencer
*
*
*/
/*Background: (46, 46, 46); #2e2e2e*/
/*Comments: (121, 121, 121); #797979*/
/*White: (214, 214, 214); #d6d6d6*/
/*Yellow: (229, 181, 103); #e5b567*/
/*Green: (180, 210, 115); #b4d273*/
/*Orange: (232, 125, 62); #e87d3e*/
/*Purple: (158, 134, 200); #9e86c8*/
/*Pink: (176, 82, 121); #b05279*/
/*Blue: (108, 153, 187); #6c99bb*/
2024-10-16 02:26:02 +02:00
#include "datatypes.h"
#include <iostream>
#include <raylib.h>
#include <raymath.h>
#define RAYGUI_IMPLEMENTATION
#include "raygui/raygui.h"
#include "raygui/dark/style_dark.h"
2024-10-09 02:01:23 +02:00
#include "libremidi/libremidi.hpp"
#include "libremidi/message.hpp"
#include "libremidi/observer_configuration.hpp"
#include <cstdlib>
#include <stdio.h>
#include <cmath>
#include "node.h"
#include "signal.h"
#include "connection-manager.h"
2024-10-09 02:01:23 +02:00
libremidi::observer obs;
libremidi::midi_out midi_out;
std::vector<libremidi::input_port> midi_in_ports;
std::vector<libremidi::output_port> midi_out_ports;
std::vector<Node> nodes;
std::vector<Signal> signals;
ConnectionManager cm;
2024-10-09 02:01:23 +02:00
Vec2D<float> mouse_pos;
Vec2D<float> drag_start;
bool mouse_clicked = false;
bool mouse_touched = false;
bool mouse_dragging = false;
2024-10-09 02:01:23 +02:00
enum States {IDLE, CONFIG};
struct AppState{
int width;
int height;
States state;
} app;
2024-10-09 02:01:23 +02:00
int main(){
srand(time(NULL));
InitWindow(2560, 1440, "VISEQ");
GuiLoadStyleDark();
int monitor = 0; //GetCurrentMonitor();
SetWindowMonitor(monitor);
app.width = GetMonitorWidth(monitor);
app.height = GetMonitorHeight(monitor);
Camera2D camera = { 0 };
camera.target = (Vector2){ app.width/2.0f, app.height/2.0f };
camera.offset = (Vector2){ app.width/2.0f, app.height/2.0f };
camera.rotation = 0.0f;
camera.zoom = 1.0f;
SetTargetFPS(120);
ToggleFullscreen();
2024-10-09 02:01:23 +02:00
for(const libremidi::input_port& port : obs.get_input_ports()){
midi_in_ports.push_back(port);
}
for(const libremidi::output_port& port : obs.get_output_ports()){
midi_out_ports.push_back(port);
}
for(int i = 0; i < 10; i++){
2024-10-16 02:26:02 +02:00
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;
2024-10-09 02:01:23 +02:00
}
2024-10-16 02:26:02 +02:00
for(int i = 0; i < 1; i++){
signals.emplace(signals.end(), 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[i].next = &nodes.front();
signals[i].last = &nodes.front();
2024-10-09 02:01:23 +02:00
}
midi_out.open_port(midi_out_ports[2]);
2024-10-13 15:07:19 +02:00
Vec2D<float> drag_offset;
2024-10-09 02:01:23 +02:00
2024-10-16 02:26:02 +02:00
Node* connecting_from = nullptr;
double drag_timer = GetTime();
while(!WindowShouldClose()){
2024-10-09 02:01:23 +02:00
Vector2 mp = GetScreenToWorld2D(GetMousePosition(), camera);
2024-10-10 02:59:27 +02:00
Vec2D<float> mouse_pos = {(float)mp.x, (float)mp.y};
2024-10-09 02:01:23 +02:00
bool mouse_down = IsMouseButtonDown(MOUSE_BUTTON_LEFT);
bool mouse_clicked = IsMouseButtonPressed(MOUSE_BUTTON_LEFT);
bool mouse_released = IsMouseButtonReleased(MOUSE_BUTTON_LEFT);
2024-10-10 02:59:27 +02:00
if(mouse_clicked){
2024-10-10 02:59:27 +02:00
drag_start = mouse_pos;
}
BeginDrawing();
2024-10-09 02:01:23 +02:00
ClearBackground((Color){46,46,46});
BeginMode2D(camera);
2024-10-16 02:26:02 +02:00
bool connecting = false;
bool dragging = false;
2024-10-13 15:07:19 +02:00
for(auto& n : nodes){
Vec2D<float> connector_position = n.position - ((n.position - mouse_pos)).norm() * (n.radius + 10.0f);
float dist_connector = (connector_position - mouse_pos).length();
float dist = (n.position - mouse_pos).length();
2024-10-13 15:07:19 +02:00
// connect nodes
if( (dist <= n.radius + 25.0f && dist > n.radius) || n.connecting){
n.hover_connect = true;
} else {
n.hover_connect = false;
}
2024-10-16 02:26:02 +02:00
if(dist_connector <= 6.0f && mouse_clicked && !connecting) {
connecting_from = &n;
n.connecting = true;
2024-10-16 02:26:02 +02:00
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;
2024-10-16 02:26:02 +02:00
//connecting_from = nullptr;
}
2024-10-13 15:07:19 +02:00
if(mouse_released && (n.hover_connect || n.hover_move)) {
if(&n != connecting_from && connecting_from != nullptr){
cm.connections.emplace(cm.connections.end(), connecting_from, &n);
connecting_from = nullptr;
2024-10-13 15:07:19 +02:00
}
}
2024-10-16 02:26:02 +02:00
if(!connecting){
// move node
if((n.position - mouse_pos).length() <= n.radius || n.picked_up){
n.hover_move = true;
} else {
n.hover_move = false;
}
2024-10-13 15:07:19 +02:00
2024-10-16 02:26:02 +02:00
if(mouse_clicked && n.hover_move && !dragging){
n.picked_up = true;
drag_offset = n.position - mouse_pos;
dragging = true;
drag_timer = GetTime();
}
if(mouse_down && n.picked_up){
if(GetTime() - drag_timer > 0.1) n.position = mouse_pos + drag_offset;
}
2024-10-16 02:26:02 +02:00
if(mouse_released && n.picked_up){
if(GetTime() - drag_timer < 0.1) n.config = true;
}
if(mouse_released) {
n.picked_up = false;
}
}
n.draw(&mouse_pos);
}
cm.draw();
for(auto& s : signals){
2024-10-16 02:26:02 +02:00
s.direction = (s.next->position - s.position).norm();
if(s.position.dist(&s.next->position) < s.speed * 1.5) {
2024-10-16 02:26:02 +02:00
Node* n = cm.next(s.next, s.last);
if(n != s.next && n != nullptr) {
2024-10-16 02:26:02 +02:00
s.last = s.next;
s.next = n;
2024-10-16 02:26:02 +02:00
std::cout << s.last << " " << s.next << std::endl;
2024-10-13 15:07:19 +02:00
midi_out.send_message(libremidi::channel_events::control_change(2, 10, 50));
}
}
s.draw(&mouse_pos);
}
EndMode2D();
2024-10-16 02:26:02 +02:00
GuiWindowBox((Rectangle){0,0,280,600}, "config");
if(GuiButton((Rectangle){20, 60, 240, 20}, "button")){
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];
}
for(auto& n : nodes){
if(n.config){
int option = n.mode;
GuiComboBox((Rectangle){ 20, 100, 240, 20 }, "Round Robin;Random", &option);
n.mode = (Distribution)option;
if(GuiButton((Rectangle){20, 140, 240, 20 }, "button")){
}
}
}
EndDrawing();
2024-10-09 02:01:23 +02:00
}
CloseWindow();
return 0;
2024-10-09 02:01:23 +02:00
}
2024-10-10 02:59:27 +02:00
2024-10-09 02:01:23 +02:00