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 › delay an action
Page Index Toggle Pages: 1
delay an action (Read 572 times)
delay an action
Feb 6th, 2008, 11:08pm
 
what is the best way to delay an action by 60 seconds without freezing the whole app. I basically want to say something like

if you stay hovered over an object for 60 seconds then do something

Thanks
Re: delay an action
Reply #1 - Feb 7th, 2008, 12:57am
 
jher,

It's not necessary to freeze the whole app. Use a timer instead.
I came up with a rough sketch, hope this helps:

The timer starts as soon as the mouse moves over the button.
When the specified delaytime has passed, something happens.
Whenever you leave the button, the timer is reset.

Quote:


int starttime;
int delaytime = 5000; // delay in milliseconds

boolean hover = false;
boolean timer = false;

int s = 80; // rect size
int x = 100; // rext x position
int y = 100; // rect y position

void setup() {
 size(200,200);  
 background(0);
 rectMode(CENTER);
 fill(200);
}

void draw() {

 over(); // call over function to check mouse position

 // if mouse is hovering AND no timer is running yet...
 if (hover == true && timer == false) {
   starttime = millis(); // set startingtime
   timer = true; // start timer
 }  
 
 // check the timer
 if (hover == true && millis() - starttime >= delaytime ) {
   background(random(255)); // do something...
 }  
 else {
   background(0);  
 }

 rect(x, y, s, s); // draw rectangle
}

// check if mouse is over the rect
void over() {
 if (mouseX >= x-s/2 && mouseX <= x+s/2 && mouseY > y-s/2 && mouseY < y+s/2) {
   hover = true;
   fill(125); // rect fill color
 }  
 else {
   hover = false;
   timer = false; // stop timer
   fill(200); // rect fill color
 }
}


cheers.
Re: delay an action
Reply #2 - Feb 7th, 2008, 1:30am
 
thanks dek...

do you have any skills with rotating translate rectangles and keeping perpective right?

Im trying to zoom into these rects and the rects in the center of the screen look really good when they zoom up, but when the ones ones on the side of the screen zoom to the left or the right, and I cant seem to make them zoom up. I am using the z parameter of the translate method


below is my code

float SELECTED_WIDTH = 300, SELECTED_HEIGHT = 220;
float STANDARD_WIDTH = 215, STANDARD_HEIGHT = 157;
boolean timerSet = false;
Cell[] cellArray = new Cell[20];
int selectedIndex;


void setup()
{
 size(1280, 720, P3D);
 bezierDetail(50);
//  ortho(0, width, 0, height, -100, 100);
 float xPos = 0;
 float yPos = 0;
 selectedIndex = 0;
 //strokeCap(ROUND);
 stroke(255,255,255);

 
 for(int i=0; i < cellArray.length; i++)
 {

   
   if (xPos > (width - STANDARD_WIDTH))
   {
     xPos =0;
     yPos += STANDARD_HEIGHT+3;
   }
   xPos += STANDARD_WIDTH+3;
   cellArray[i] = new Cell( (int)xPos, (int)yPos);
 }
 cellArray[selectedIndex].isHovered = true;
}


void draw()
{
 background(0);
 translate(-10, 115);
 for(int i=0; i < cellArray.length; i++)
 {
   if(i != selectedIndex )cellArray[i].update();
 }
 cellArray[selectedIndex].update();
}


void keyPressed() {
 if(keyCode == 10) setSelection();
 if (key == CODED) {
   
   switch(keyCode)
   {
     case LEFT:
       changeSelection(-1);
     break;
     
     case RIGHT:
       changeSelection(1);
     break;
     
     case 10:
       setSelection();
     break;
     
     case DOWN:
       changeSelection(5);
     break;
     
     case UP:
       changeSelection(-5);
     break;
     
   }
   
 }
}

void changeSelection(int delta)
{
if(selectedIndex + delta >= 0 && selectedIndex + delta < cellArray.length)
{
  cellArray[selectedIndex].isHovered = false;
  cellArray[selectedIndex].isSelected = false;
  selectedIndex += delta;
  cellArray[selectedIndex].isHovered = true;
}
}

void setSelection()
{
cellArray[selectedIndex].isSelected = true;
}



class Cell
{
 float xPos, yPos;
 boolean isSelected = false, isHovered = false;
 float cellWidth, cellHeight;
 float rotationVal = 0.0;
 float zoomLevel = 0.0;
 int timeZoomed=-1;
 Cell(int x, int y)
 {
   xPos = x;
   yPos = y;
   cellWidth = STANDARD_WIDTH;
   cellHeight = STANDARD_HEIGHT;
 }
 void update()
 {
   
   if(isHovered)
   {
     zoom();
   }
   else
   {
     cellWidth = STANDARD_WIDTH;
     cellHeight = STANDARD_HEIGHT;
     unZoom();
   }
   if(isSelected) rotate();
   else unRotate();
   draw();
 }

 void rotate()
 {
   //ortho(-width/2, width/2, -height/2, height/2, -10, 10);
   rotationVal += .2;
   if(rotationVal > PI/2) fill(255);
   else fill(100);
   if(rotationVal > PI) rotationVal = PI;

 }
 void unRotate()
 {
   //ortho(-width/2, width/2, -height/2, height/2, -10, 10);
   rotationVal -= .2;
   if(rotationVal < PI/2) fill(100);
   else fill(255);
   if(rotationVal < 0) rotationVal = 0;

 }
 void zoom()
 {
   zoomLevel += 3;
   if(zoomLevel > 100)
   {
     zoomLevel = 100;
     if(!timerSet)
     {
       timeZoomed = millis();
       timerSet = true;
       println(timeZoomed);
     }
     else if(millis() - timeZoomed >= 5000)
    {
      this.isSelected = true;
      timerSet  =false;
    }
   }
   /*cellWidth += (SELECTED_WIDTH -  cellWidth)/5;
    if(cellWidth > SELECTED_WIDTH) cellWidth = SELECTED_WIDTH;
    cellHeight += (SELECTED_HEIGHT -  cellHeight)/5;
    if(cellHeight > SELECTED_HEIGHT) cellHeight = SELECTED_HEIGHT;
    */
 }
 void unZoom()
 {
   zoomLevel -= 6;
   if(zoomLevel < 0)
   {
     zoomLevel = 0;
   
   }
   /*cellWidth += (SELECTED_WIDTH -  cellWidth)/5;
    if(cellWidth > SELECTED_WIDTH) cellWidth = SELECTED_WIDTH;
    cellHeight += (SELECTED_HEIGHT -  cellHeight)/5;
    if(cellHeight > SELECTED_HEIGHT) cellHeight = SELECTED_HEIGHT;
    */
 }
 void draw()
 {
   pushMatrix();
   translate(xPos, yPos, zoomLevel);
   rotateY(rotationVal);
   strokeWeight(3);
   
   bezierRect(-cellWidth/2, -cellHeight/2, cellWidth, cellHeight, -10, -10);
   popMatrix();
 }
 
 void bezierRect(float x, float y, float w, float h, float xr, float yr) {
 float w2=w/2f, h2=h/2f, cx=x+w2, cy=y+h2;
 beginShape();
 vertex(cx,cy-h2);
 bezierVertex(cx+w2-xr, cy-h2, cx+w2, cy-h2+yr, cx+w2, cy);
 bezierVertex(cx+w2, cy+h2-yr, cx+w2-xr, cy+h2, cx, cy+h2);
 bezierVertex(cx-w2+xr, cy+h2, cx-w2, cy+h2-yr, cx-w2, cy);
 bezierVertex(cx-w2, cy-h2+yr, cx-w2+xr, cy-h2, cx, cy-h2);
 endShape();
}

}

Re: delay an action
Reply #3 - Feb 7th, 2008, 2:41pm
 
I'm no expert at 3D, so take this with a grain of salt.

I believe it's a matter of orthographic vs. perspective projection, whereas your "problem" lies with the latter. When using a one-, two- or three-point perspective, all points in the same axis will move together, the closer they come to the vanishing point, or vice versa, move apart, or in your case left and right, when moving away from it.
When using orthographic projection all points in the same axis will stay parallel to each other, no matter their distance in the space. Therefore they shouldn't move left or right when zooming in.

Have a look at the ortho vs. perspective sketch to find out more.
Re: delay an action
Reply #4 - Feb 7th, 2008, 3:22pm
 
dek...

I poked around at the ortho function and that did fix the zoom aspect of my problem, but then when I want to rotate that zoomed rectangle, the ortho function prevented the rotation from looking real.

Is there a clean way to flip between ortho and traditional perspective so that the rotation looks real as well as the zoom looking the way I want it. My friend anastasia says this is called foreshortening?



Re: delay an action
Reply #5 - Feb 7th, 2008, 3:31pm
 
I'm afraid that I just don't know my 3D good enough to answer that question in any satisfying way. Hope someone else might be able to answer that question.
Page Index Toggle Pages: 1