Files
viseq/src/app-manager.h
T

127 lines
3.0 KiB
C++
Raw Normal View History

2024-10-25 16:41:37 +02:00
#pragma once
#include "clipboard.h"
2024-10-25 16:41:37 +02:00
#include "datatypes.h"
2024-10-30 14:55:24 +01:00
#include "graph-manager.h"
#include "midi-node.h"
#include "mouse.h"
#include "node.h"
#include "raylib.h"
2024-10-25 16:41:37 +02:00
#include "signal-manager.h"
#include "state.h"
2024-10-25 16:41:37 +02:00
#include "ui.h"
2024-10-27 00:40:57 +02:00
#include <memory>
#include <ranges>
2024-10-25 16:41:37 +02:00
2024-11-03 03:48:49 +01:00
namespace VISEQ::APP {
2024-10-25 16:41:37 +02:00
2024-11-14 00:55:09 +01:00
class AppManager{
2024-11-03 03:48:49 +01:00
public:
AppManager() : sm(&gm, &mutex), mouse(&gm), ui(&sm, &gm) {}
2024-10-25 16:41:37 +02:00
2024-11-03 03:48:49 +01:00
std::mutex mutex;
2024-10-25 16:41:37 +02:00
2024-11-03 03:48:49 +01:00
VISEQ::GRAPH::GraphManager gm;
VISEQ::SIGNAL::SignalManager sm;
VISEQ::APP::Clipboard cb;
VISEQ::APP::Mouse mouse;
2024-10-25 16:41:37 +02:00
std::shared_ptr<APP::State> state = APP::State::get();
2024-11-03 03:48:49 +01:00
UI::AppUI ui;
2024-10-25 16:41:37 +02:00
2024-11-03 03:48:49 +01:00
void updateMidiPorts(){
for(auto&n : gm.nodes | RANGE_FILTER([](VISEQ::GRAPH::NODES::SPTR_Node n){return n->getNodeType() == NodeType::MIDINODE;})){
auto nc = std::dynamic_pointer_cast<MidiNode>(n);
for(auto& mo : sm.midi.midi_out_ports){
if(!nc->midi_outs.contains(mo.number) && mo.enabled){
nc->midi_outs[mo.number] = {mo.number, mo.port.port_name, false};
}
if(nc->midi_outs.contains(mo.number) && !mo.enabled){
nc->midi_outs.erase(mo.number);
}
2024-10-25 16:41:37 +02:00
}
}
}
2024-11-03 03:48:49 +01:00
void removeNodes(VEC_SPTR_Node nodes){
VEC_SPTR_Node adjacent_nodes = gm.getAdjacentNodes(nodes);
sm.rerouteSignal(nodes, adjacent_nodes);
sm.rebaseSignal(nodes);
gm.removeNodesAndEdges(nodes);
}
void doInteraction(){
while(event_queue.size() > 0){
auto e = event_queue.front();
std::cout << e.action << " " << e.item->z_index << std::endl;
if(e.item->getType() == GraphItemType::NODE || e.item->getType() == GraphItemType::CONNECTOR) {
switch(e.action){
case VISEQ::APP::Interaction::PICKUP:
{
e.item->pickUp();
break;
}
case VISEQ::APP::Interaction::DRAG:
{
e.item->drag();
break;
}
case VISEQ::APP::Interaction::PUTDOWN:
{
e.item->putDown();
break;
}
}
}
event_queue.pop();
}
}
void update(){
2024-11-03 03:48:49 +01:00
mutex.lock();
updateMidiPorts();
mutex.unlock();
doInteraction();
mouse.update();
if(KEYS_DOWN.ctrl && IsKeyPressed(KEY_C)){
VEC_SPTR_Node selected_nodes = gm.getSelection();
if(!selected_nodes.empty()) cb.pushNodes(selected_nodes);
}
if(KEYS_DOWN.ctrl && IsKeyPressed(KEY_V)){
VEC_SPTR_Node paste_nodes = cb.pullNodes();
if(!paste_nodes.empty()) gm.pasteNodesAndEdges(paste_nodes, MOD_MOUSE);
}
}
void draw(Vec2Df mouse_pos, Camera2D camera){
2024-11-03 03:48:49 +01:00
gm.drawNodes(mouse_pos, camera);
gm.drawEdges(mouse_pos, camera);
sm.drawSignals(mouse_pos, camera);
}
void drawUI(Vec2Df mouse_pos, Camera2D camera){
2024-11-03 03:48:49 +01:00
mutex.lock();
ui.draw(mouse_pos, camera);
mutex.unlock();
}
2024-11-03 03:48:49 +01:00
};
2024-11-14 00:55:09 +01:00
inline void to_json(json& j, const AppManager& A){
j = json{{"graphmanager", A.gm}, {"signalmanager", A.sm}};
2024-11-03 03:48:49 +01:00
}
2024-11-14 00:55:09 +01:00
inline void from_json(const json& j, AppManager& A){
2024-11-03 03:48:49 +01:00
j.at("graphmanager").get_to(A.gm); // = json{{"graphmanager", A.gm}, {"signalmanager", A.sm}};
}
}