Files
viseq/src/node.h
T

111 lines
2.6 KiB
C++
Raw Normal View History

2024-10-09 02:01:23 +02:00
#pragma once
#include <memory>
#include <raylib.h>
2024-10-09 02:01:23 +02:00
#include "connector.h"
2024-10-09 02:01:23 +02:00
#include "datatypes.h"
#include "draggable.h"
2024-10-30 14:55:24 +01:00
#include "graph-item.h"
#include "interaction-event.h"
#include "state.h"
#include "vmath.h"
2024-10-20 01:17:18 +02:00
2024-11-03 03:48:49 +01:00
namespace VISEQ::GRAPH::EDGES{
class Edge;
}
2024-10-09 02:01:23 +02:00
2024-11-03 03:48:49 +01:00
namespace VISEQ::GRAPH::NODES {
class Node : public BASE::Draggable, public GraphItem, public std::enable_shared_from_this<Node> {
2024-11-03 03:48:49 +01:00
public:
Node() {std::cout << "Node constructor" << std::endl;}
Node(float x, float y) : Draggable(Vec2Df(x, y)) {}
Node(Vec2Df p) : Draggable(p) {}
2024-11-03 03:48:49 +01:00
NodeType nodeType;
2024-11-06 02:28:37 +01:00
std::vector<std::shared_ptr<Node>> neighbors;
2024-11-03 03:48:49 +01:00
std::vector<std::shared_ptr<VISEQ::GRAPH::EDGES::Edge>> connections;
std::vector<std::shared_ptr<Connector>> connectors;
2024-11-03 03:48:49 +01:00
Distribution distributionMode = Distribution::GEOMETRIC;
2024-11-03 03:48:49 +01:00
float radius = 15.0f;
bool config = false;
bool triggered = false;
int mode = 0;
virtual void trigger() {}
virtual void setMode(int _mode) {mode = _mode;}
std::shared_ptr<Node> getPtr(){
return shared_from_this();
}
virtual bool hovering() override {
return CheckCollisionPointCircle(SCREEN_MOUSE, position, radius);
}
void addConnector() {
connectors.emplace_back(std::make_shared<Connector>(getPtr(), position, radius + 5.0f));
}
2024-12-03 01:25:49 +01:00
void removeConnector(std::weak_ptr<Connector> connector){
std::erase(connectors, connector.lock());
}
2024-11-03 03:48:49 +01:00
virtual NodeType getNodeType() const {return nodeType;}
void deselect() override {
selected = false;
}
GraphItemType getType() override{
return GraphItemType::NODE;
}
2024-12-05 23:39:01 +01:00
void clearConnectors(){
connectors.clear();
update();
}
void update() override{
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});
2024-12-05 23:39:01 +01:00
if(hovering() && MOUSE_RIGHT.clicked) {
selected = true;
config = true;
state->pushEvent({getPtr(), APP::Interaction::RIGHTCLICK});
}
int free_connectors = 0;
for(auto c : connectors){
2024-12-03 01:25:49 +01:00
if(c->partner.expired()) free_connectors++;
c->setPosition(position);
2024-11-11 02:11:54 +01:00
c->update();
}
if(free_connectors == 0) addConnector();
}
2024-11-12 01:42:05 +01:00
void draw(Vec2Df mp, Camera2D cam) override {}
void draw() override{
for(auto c : connectors){
c->draw();
}
}
2024-11-03 03:48:49 +01:00
};
2024-11-03 03:48:49 +01:00
typedef std::shared_ptr<VISEQ::GRAPH::NODES::Node> SPTR_Node;
2024-12-03 01:25:49 +01:00
typedef std::weak_ptr<VISEQ::GRAPH::NODES::Node> WPTR_Node;
2024-11-03 03:48:49 +01:00
typedef std::vector<SPTR_Node> VEC_SPTR_Node;
2024-12-03 01:25:49 +01:00
typedef std::vector<WPTR_Node> VEC_WPTR_Node;
2024-11-03 03:48:49 +01:00
}