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 & HelpPrograms › Buttons/Draggable Objects
Page Index Toggle Pages: 1
Buttons/Draggable Objects (Read 901 times)
Buttons/Draggable Objects
Sep 19th, 2005, 11:30pm
 
Hi,

Just wondering if anyone's written a basic button class, or something similar. I'm just writing a little program and want a few draggable buttons.

If not, what's the best way to write draggable objects. I've been thinking about it basically, but seems that there's lots o issues like tracking the currently draggable object etc... that I need to deal with correctly.

So, I'm sure this has come up before (but searching doesn't show me much) - any tips/techniques?
Re: Buttons/Draggable Objects
Reply #1 - Sep 20th, 2005, 2:37pm
 
I haven't actually got a 'draggable' button class but I do have a slider:

Code:

class HScroller
{
float min;
float max;
float def;
float x;
float y;
float xpos;
float w;
float scale;
float value;
boolean locked;

HScroller( float Min, float Max, float Default, float X, float Y, float W )
{
min = Min;
max = Max;
x = X;
y = Y;
w = W;

scale = ( max - min ) / w;
def = ( Default - min ) / scale;
xpos = def;
}

float update()
{
drag();
display();
readValue();

return value;
}

void drag()
{
if ( mousePressed && over() )
{
locked = true;
}
if ( !mousePressed )
{
locked = false;
}
if ( locked )
{
assignPos(mouseX-x);
}
}

void assignPos( float X )
{
xpos = X;

if ( X > w )
{
xpos = w;
}
if ( X < 0 )
{
xpos = 0;
}
}

void readValue()
{
value = xpos*scale + min;
}

void display()
{
noSmooth();
stroke(200,200,200);
fill(230,230,230);
strokeWeight(1);
line(x,y,x+w,y);
line(x+def, y-2, x+def, y+2);
rectMode(CENTER);
stroke(100,100,100);
rect(x+xpos, y, 5, 5);
smooth();
}

boolean over()
{
if( mouseX > x - 10 && mouseX < x + w + 10 && mouseY > y - 10 && mouseY < y + 10 )
{
return true;
}
else
{
return false;
}
}
}

Re: Buttons/Draggable Objects
Reply #2 - Sep 26th, 2005, 10:03pm
 
Cheers - that worked a treat!
Page Index Toggle Pages: 1