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 › Need help with this program
Page Index Toggle Pages: 1
Need help with this program (Read 456 times)
Need help with this program
Jul 9th, 2008, 9:09am
 
What I want to know is how I make it that the program only shows one handle, instead of thirteen. It would also help me understand the program better in general.

Hope someone can help...

Handle[] handles;
int num;

void setup()
{
 size(200, 200);
 num = height/15;
 handles = new Handle[num];
 int hsize = 10;
 for(int i=0; i<num; i++) {
   handles[i] = new Handle(width/2, 10+i*15, 50-hsize/2, 10, handles);
 }
}

void draw()
{
 background(153);
 
 for(int i=0; i<num; i++) {
   handles[i].update();
   handles[i].display();
 }
 
 fill(0);
 rect(0, 0, width/2, height);
}

void mouseReleased()
{
 for(int i=0; i<num; i++) {
   handles[i].release();
 }
}

class Handle
{
 int x, y;
 int boxx, boxy;
 int length;
 int size;
 boolean over;
 boolean press;
 boolean locked = false;
 boolean otherslocked = false;
 Handle[] others;
 
 Handle(int ix, int iy, int il, int is, Handle[] o)
 {
   x = ix;
   y = iy;
   length = il;
   size = is;
   boxx = x+length - size/2;
   boxy = y - size/2;
   others = o;
 }
 
 void update()
 {
   boxx = x+length;
   boxy = y - size/2;
   
   for(int i=0; i<others.length; i++) {
     if(others[i].locked == true) {
       otherslocked = true;
       break;
     } else {
       otherslocked = false;
     }  
   }
   
   if(otherslocked == false) {
     over();
     press();
   }
   
   if(press) {
     length = lock(mouseX-width/2-size/2, 0, width/2-size-1);
   }
 }
 
 void over()
 {
   if(overRect(boxx, boxy, size, size)) {
     over = true;
   } else {
     over = false;
   }
 }
 
 void press()
 {
   if(over && mousePressed || locked) {
     press = true;
     locked = true;
   } else {
     press = false;
   }
 }
 
 void release()
 {
   locked = false;
 }
 
 void display()
 {
   line(x, y, x+length, y);
   fill(255);
   stroke(0);
   rect(boxx, boxy, size, size);
   if(over || press) {
     line(boxx, boxy, boxx+size, boxy+size);
     line(boxx, boxy+size, boxx+size, boxy);
   }

 }
}

boolean overRect(int x, int y, int width, int height)
{
 if (mouseX >= x && mouseX <= x+width &&
     mouseY >= y && mouseY <= y+height) {
   return true;
 } else {
   return false;
 }
}

int lock(int val, int minv, int maxv)
{
 return  min(max(val, minv), maxv);
}
Re: Need help with this program
Reply #1 - Jul 9th, 2008, 9:51am
 
line 7, replace num = height/15; by num = 1;
Re: Need help with this program
Reply #2 - Jul 9th, 2008, 8:36pm
 
Thanks!

Would it be also possible to make it that if the handle were moved in a certain direction, that it would then perform a certain function. ie: control motor speed or direction
Page Index Toggle Pages: 1