Files
viseq/src/node.cpp
T

109 lines
2.9 KiB
C++
Raw Normal View History

2026-01-06 02:44:18 +01:00
#include "node.h"
#include <memory>
#include <raylib.h>
#include "connector.h"
#include "datatypes.h"
#include "state.h"
#include "vmath.h"
2026-01-12 00:16:36 +01:00
#include "midi-node.h"
#include "config-node.h"
2026-01-06 02:44:18 +01:00
namespace VISEQ::GRAPH::NODES {
void Node::setMode(int _mode) {mode = _mode;}
std::shared_ptr<Node> Node::getPtr(){
return shared_from_this();
}
bool Node::hovering() {
return CheckCollisionPointCircle(SCREEN_MOUSE, position, radius);
}
void Node::addConnector() {
connectors.emplace_back(std::make_shared<Connector>(getPtr(), position, radius + 5.0f));
}
void Node::removeConnector(std::weak_ptr<Connector> connector){
std::erase(connectors, connector.lock());
}
NodeType Node::getNodeType() const {return nodeType;}
void Node::deselect() {
selected = false;
}
GraphItemType Node::getType() {
return GraphItemType::NODE;
}
void Node::clearConnectors(){
connectors.clear();
update();
}
void Node::update() {
if(hovering()) state->pushEvent({getPtr(), APP::Interaction::HOVER});
if(hovering() && clicked()) state->pushEvent({getPtr(), APP::Interaction::PICKUP});
if(pickedUp()) state->pushEvent({getPtr(), APP::Interaction::DRAG});
if(picked_up && released()) state->pushEvent({getPtr(), APP::Interaction::PUTDOWN});
if(hovering() && MOUSE_RIGHT.clicked) {
selected = true;
config = true;
state->pushEvent({getPtr(), APP::Interaction::RIGHTCLICK});
}
int free_connectors = 0;
for(auto c : connectors){
if(c->partner.expired()) free_connectors++;
c->setPosition(position);
c->setOrbit(radius + 8.0f);
c->update();
}
if(free_connectors == 0) addConnector();
}
void Node::draw(Vec2Df mp, Camera2D cam) {}
void Node::draw() {
for(auto c : connectors){
c->draw();
}
}
2026-01-12 00:16:36 +01:00
void to_json(json& j, const Node& N){
if(N.getNodeType() == NodeType::MIDINODE){
j = dynamic_cast<const MidiNode&>(N);
}
if(N.getNodeType() == NodeType::CONFIGNODE){
j = dynamic_cast<const ConfigNode&>(N);
}
}
void from_json(const json& j, Node& N){
if(j.at("nodeType") == NodeType::MIDINODE){
N = j.template get<MidiNode>();
}
if(j.at("nodeType") == NodeType::CONFIGNODE){
N = j.template get<ConfigNode>();
}
}
void to_json(json& j, const SPTR_Node& N){
j = *N;
}
void from_json(const json& j, std::shared_ptr<Node>& N){
std::cout << "SPTR_Node" << std::endl;
if(N->getNodeType() == NodeType::MIDINODE){
N.reset(new MidiNode(j.get<MidiNode>()));
}
if(N->getNodeType() == NodeType::CONFIGNODE){
N.reset(new ConfigNode(j.get<ConfigNode>()));
}
}
2026-01-06 02:44:18 +01:00
}