Files
simple-ontology/build/application.windows32/source/nodes.pde
2019-05-06 03:24:40 +02:00

130 lines
3.1 KiB
Plaintext

int nodeCount;
int i_selectedNode = -1;
int padding = textSize/3*2; //Padding between text and surrounding box
int letTolerance = 10; //Widens the clickable areas of in/outlets a bit
void addNode(int x, int y, String title) {
nodes[nodeCount] = new Node(x, y, title);
nodeCount++;
}
void deselect() {
i_selectedNode = -1;
}
void deleteSelectedNode() { //Seperate from class for garbage collector reasons
if (i_selectedNode != -1) {
for (int i = 0; i < linkCount; i++) {
if (links[i].targetNode == i_selectedNode || links[i].parentNode == i_selectedNode) {
links[i].active = false;
}
}
nodes[i_selectedNode].deleted = true;
i_selectedNode = -1;
}
}
class Node {
String title;
int id;
int inlets = 2;
int outlets = 2;
int x, y;
int h = textSize+2*padding;
int w = textSize+2*padding;
PVector dragMouseStartPoint = new PVector(0, 0);
PVector outletCenter = new PVector(0, 0);
PVector inletCenter = new PVector(0, 0);
boolean deleted;
boolean drag;
boolean hover() {
if (mouseX > x-w/2 && mouseX < x+w/2 && mouseY > y-h/2 && mouseY < y+h/2) {
return true;
} else {
return false;
}
}
boolean hoverOutlet() {
if (mouseX > x-(textSize+padding)/2-letTolerance && mouseX < x+(textSize+padding)/2+letTolerance && mouseY > y-padding-textSize/2-(textSize+padding)/2-letTolerance && mouseY < y-padding-textSize/2) {
return true;
} else {
return false;
}
}
boolean hoverInlet() {
if (mouseX > x-(textSize+padding)/2-letTolerance && mouseX < x+(textSize+padding)/2+letTolerance && mouseY > y+padding+textSize/2 && mouseY < y+padding+textSize/2+(textSize+padding)/2+letTolerance) {
return true;
} else {
return false;
}
}
Node(int x_, int y_, String title_) {
id = nodeCount;
title = title_;
x = x_;
y = y_;
setVectors();
refreshSize();
}
void setVectors() {
outletCenter.x = x;
outletCenter.y = y-(padding+textSize/2)*1.3;
inletCenter.x = x;
inletCenter.y = y+(padding+textSize/2)*1.3;
}
/**
* Called when changing nodes title or when zooming via mouse-wheel
*/
void refreshSize() {
w = int(textWidth(title))+2*padding;
h = textSize+2*padding;
}
/**
* Update-function is really only necessary when dragging the node
*/
void update() {
if (drag) {
x -= dragMouseStartPoint.x - mouseX;
dragMouseStartPoint.x = mouseX;
y -= dragMouseStartPoint.y - mouseY;
dragMouseStartPoint.y = mouseY;
setVectors();
}
}
void display() {
/**
* Selected nodes border
*/
if (i_selectedNode == id) {
noStroke();
fill(150, 100);
rect(x, y, w+20, h+20, 4);
}
/**
* Inlet & Outlet
*/
stroke(255, darkMode? 255 : 0);
strokeWeight(1);
fill(255, 0, 0);
ellipse(x, y-padding-textSize/2, textSize+padding, textSize+padding);
fill(0);
ellipse(x, y+padding+textSize/2, textSize+padding, textSize+padding);
/**
* Box & Title
*/
stroke(127);
strokeWeight(2);
fill(50);
rect(x, y, w, h, 4);
fill(255);
text(title, x, y+textSize/3);
}
}