We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Click, Drag and Drop
Page Index Toggle Pages: 1
Click, Drag and Drop (Read 441 times)
Click, Drag and Drop
Oct 19th, 2006, 11:25pm
 
I'm trying to create a Processing program that when you click a button, it creates a shape that you can then drag and drop on the screen. So, if you were to say click the button five times, you would have five circles that you could then drag and drop.

Also, I would like it to be that when you drag the created shape to a spot, it will place itself in a grid. Much like the way desktop icons click into place and such.

I have the example code for dragging and dropping that is here: http://www.proce55ing.net/learning/examples/mousefunctions.html

I'm just at a loss as to how to implement the other two features. Any examples or tips would be greatly appreciated!
Re: Click, Drag and Drop
Reply #1 - Oct 20th, 2006, 10:19pm
 
I'm assuming you know how to put together classes and objects. Otherwise you had best brush up on these. Shiffman has some good tutorials on the subject if you're stuck.

I've put together some object dropping code for you. What you need to do is put the dragging feature in to it yourself. It's not beginner stuff but it's a foundation for some good code which has served me well.
Code:

// A Vector is a magic array
// It's a bit like a pez dispenser, allowing you to add and subtract.
// Doing this with normal arrays is far more complicated and slower,
// so it's best to dive in and learn this very useful thing.
Vector draggables;

void setup(){
size(400, 400);
smooth();
draggables = new Vector();
}

void draw(){
background(100);
for(int i = 0; i < draggables.size(); i++){
// The Vector only knows we have Objects in there, not what they are
// So we have to "cast" them (like a mould), ie: (datatype)variable
Drag d = (Drag)draggables.get(i);
d.draw();
}
}

// This adds to the Vector of draggable objects.
// But only when you are not over an existing object
// otherwise you add to the list when dragging.
void mousePressed(){
boolean mouseOver = false;
for(int i = 0; i < draggables.size(); i++){
Drag d = (Drag)draggables.get(i);
if(d.over()){
mouseOver = true;
}
}
if(!mouseOver){
draggables.add(new Drag(mouseX, mouseY, 50));
}
}

class Drag{
float x,y,big;
boolean locked;
Drag(float x, float y, float big){
this.x = x;
this.y = y;
this.big = big;
}
void draw(){
ellipse(x, y, big, big);

// The dragging stuff goes in here.
// use if(mousePressed) to turn locked on
// and the else of that to turn it off.

}
boolean over(){
if(dist(mouseX, mouseY, x, y) < big * 0.5){
return true;
}
return false;
}
}

As for the snapping values, I've put a function up at Seltar's Snippet Palace you can use for your dragging and dropping. That I've left for you to do as well.
Page Index Toggle Pages: 1