json serialization beginnings, but still much to do

This commit is contained in:
Sebastian
2024-11-02 15:52:16 +01:00
parent 70d0a186a4
commit 522e9d456a
17 changed files with 25274 additions and 224 deletions
+28 -26
View File
@@ -6,28 +6,30 @@
*
*/
#include "midi-node.h"
#include <algorithm>
#include <iterator>
#include <memory>
#include <vector>
#define RAYGUI_IMPLEMENTATION
#include "datatypes.h"
#include <ranges>
#include <raylib.h>
#include <raymath.h>
#include "state-manager.h"
#include "datatypes.h"
#include "midi-node.h"
#include "nlohmann/json.hpp"
#include <cstdlib>
#include <stdio.h>
#include <cmath>
#include <ranges>
#include <algorithm>
#include <iterator>
#include <memory>
#include "state-manager.h"
using json = nlohmann::json;
AppState app;
Vec2D<float> mouse_pos;
Vec2D<float> drag_start;
Vec2Df mouse_pos;
Vec2Df drag_start;
Camera2D camera = { {0} };
@@ -71,25 +73,25 @@ int main(){
camera.zoom = 1.0f;
for(int i = 0; i < 4; ++i){
app.gm.addMidiNode(Vec2D<float>{(float)i * app.settings.grid*4 + 7 * app.settings.grid, 2 * app.settings.grid*4});
app.gm.addMidiNode(Vec2Df{(float)i * app.settings.grid*4 + 7 * app.settings.grid, 2 * app.settings.grid*4});
}
for(int i = 3; i >= 0; --i){
app.gm.addMidiNode(Vec2D<float>{(float)i * app.settings.grid*4 + 7 * app.settings.grid, 3 * app.settings.grid*4});
app.gm.addMidiNode(Vec2Df{(float)i * app.settings.grid*4 + 7 * app.settings.grid, 3 * app.settings.grid*4});
}
for(int i = 0; i < 8; ++i){
app.gm.addConnection(app.gm.nodes[i], app.gm.nodes[(i+1) % 8]);
}
std::cout << app.gm.nodes[0]->getNodeType() << std::endl;
std::cout << std::setw(2) << json(app.gm) << std::endl;
app.sm.addSignalAtNode(app.gm.nodes.front());
app.sm.addSignalAtNode(app.gm.nodes.front());
app.sm.startSignalThread();
enum InteractionState {HOVER_CANVAS, HOVER_NODE, HOVER_CONNECTOR, DRAG_NODE, CONNECT_NODE, DRAW_SELECTION, DELETE_SELECTED_NODES, COPY_SELECTED_NODES, PASTE_SELECTED_NODES};
InteractionState istate = HOVER_CANVAS;
Vec2D<float> select_start;
Vec2Df select_start;
Rectangle selection;
VEC_SPTR_Node copied_nodes;
@@ -103,8 +105,8 @@ int main(){
app.settings.speed = (app.settings.grid * 16.0f) * (app.settings.bpm / 120.f);
Vector2 mp = GetScreenToWorld2D(GetMousePosition(), camera);
Vec2D<float> mouse_pos = {(float)mp.x, (float)mp.y};
Vec2D<float> cam_pos = {camera.offset.x, camera.offset.y};
Vec2Df mouse_pos = {(float)mp.x, (float)mp.y};
Vec2Df cam_pos = {camera.offset.x, camera.offset.y};
bool mouse_down = IsMouseButtonDown(MOUSE_BUTTON_LEFT);
bool mouse_clicked = IsMouseButtonPressed(MOUSE_BUTTON_LEFT);
@@ -114,8 +116,8 @@ int main(){
bool mouse_right_clicked = IsMouseButtonPressed(MOUSE_BUTTON_RIGHT);
bool mouse_right_released = IsMouseButtonReleased(MOUSE_BUTTON_RIGHT);
Vec2D<float> mouse_delta = {GetMouseDelta().x, GetMouseDelta().y};
Vec2D<float> delta = mouse_delta * (-1.0f/camera.zoom);
Vec2Df mouse_delta = {GetMouseDelta().x, GetMouseDelta().y};
Vec2Df delta = mouse_delta * (-1.0f/camera.zoom);
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;});
@@ -163,8 +165,8 @@ int main(){
select_start = mouse_pos;
}
if(mouse_down){
Vec2D<float> select1 = {mouse_pos.x > select_start.x ? select_start.x : mouse_pos.x, mouse_pos.y > select_start.y ? select_start.y : mouse_pos.y};
Vec2D<float> select2 = {mouse_pos.x > select_start.x ? mouse_pos.x - select_start.x : select_start.x - mouse_pos.x, mouse_pos.y > select_start.y ? mouse_pos.y - select_start.y : select_start.y - mouse_pos.y};
Vec2Df select1 = {mouse_pos.x > select_start.x ? select_start.x : mouse_pos.x, mouse_pos.y > select_start.y ? select_start.y : mouse_pos.y};
Vec2Df select2 = {mouse_pos.x > select_start.x ? mouse_pos.x - select_start.x : select_start.x - mouse_pos.x, mouse_pos.y > select_start.y ? mouse_pos.y - select_start.y : select_start.y - mouse_pos.y};
DrawRectangleLines(select1.x, select1.y, select2.x, select2.y, (Color){150,150,150,150});
selection = (Rectangle){select1.x, select1.y, select2.x, select2.y};
@@ -219,9 +221,9 @@ int main(){
} else if(IsKeyDown(KEY_LEFT_SHIFT)){
float steps = round((dragNode->neighbor->position.dist(mouse_pos)) / app.settings.grid) * app.settings.grid;
float pol_r = steps;
float pol_theta = Vector2Angle(Vec2D<float>{1,0},(mouse_pos - dragNode->neighbor->position));
float pol_theta = Vector2Angle(Vec2Df{1,0},(mouse_pos - dragNode->neighbor->position));
Vec2D<float> polar = polarToCartesian(pol_r, pol_theta);
Vec2Df polar = VMath::polarToCartesian(pol_r, pol_theta);
dragNode->position = dragNode->neighbor->position + polar;
} else {
dragNode->position = mouse_pos + dragNode->drag_offset;
@@ -254,7 +256,7 @@ int main(){
}
case InteractionState::CONNECT_NODE:
{
Vec2D<float> connector_position = connecting_nodes.front()->position - ((connecting_nodes.front()->position - mouse_pos)).norm() * (connecting_nodes.front()->radius + 14.0f);
Vec2Df connector_position = connecting_nodes.front()->position - ((connecting_nodes.front()->position - mouse_pos)).norm() * (connecting_nodes.front()->radius + 14.0f);
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});
@@ -312,7 +314,7 @@ int main(){
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;});
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);});
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);