I’ve been playing around in Processing to create a synth which is controlled by linking nodes together. These nodes are basically circles, with information displayed within them.
One thing I ran into right away which, in my opinion, should be a Processing core class is a lack of a Draggable class. With the help of some googling, I eventually cooked up my own, which performs quickly no matter the framerate.
This code makes use of Vectors, so it should be a very quick bit of code. I’ve had no problems using it.
Vector nodes;
-
boolean drag_locked;
-
void setup()
-
{
-
size(700, 500, P2D);
-
smooth();
-
nodes = new Vector();
-
drag_locked = false;
-
nodes.add(new Node(20, 20,20));
-
nodes.add(new Node(50, 50,20));
-
frameRate(30);
-
}
-
-
void draw()
-
{
-
smooth();
-
background(20);
-
for(int i=0;i<nodes .size();i++) {
-
Node temp = (Node)nodes.get(i);
-
temp.draw();
-
}
-
}
-
class Node
-
{
-
float x,y,mo_x,mo_y;
-
int radius;
-
boolean locked;
-
Node(float x, float y, int radius){
-
this.x = x;
-
this.y = y;
-
this.radius = radius;
-
}
-
void draw(){
-
fill(0,255,0);
-
ellipse(x, y, radius*2, radius*2);
-
if(over() && keyPressed){
-
nodes.remove(nodes.indexOf(this));
-
}
-
if(mousePressed && over() && !drag_locked){
-
mo_x = x-mouseX;
-
mo_y = y-mouseY;
-
locked = true;
-
drag_locked = true;
-
}
-
if(!mousePressed){
-
locked = false;
-
drag_locked = false;
-
}
-
if(locked){
-
x = mouseX+mo_x;
-
y = mouseY+mo_y;
-
}
-
}
-
boolean over(){
-
if(dist(x, y, mouseX, mouseY) < radius){
-
return true;
-
}
-
return false;
-
}
-
-
}
This will set up two nodes on your screen which are draggable, complete with offsetting and so on to make them behave perfectly. Hope this helps!