Game Development Community

How to make a OnDoubleClick (SOLVED)

by Azmodeus · in Torque Game Builder · 10/03/2006 (6:56 pm) · 4 replies

Is there an easy way to make a function "onDoubleClick"

We have onMouseDown but if you click it 2 times it still sends the %clicks = 1 message first before the 2nd one. What is interesting, is if you cick it 3 times it will send 3 messages where %clicks=1,2 and 3.

I havnt found anything yet in the reference that would allow you to set a delay or similar before it sends the first onMouseDown to avoid clicks where the just just clicked once.

I can make OnClick pretty easy. Just add a hook in OnMouseDown where %clicks=1 and set a flag, then call onClick in OnMouseUp if it is the same object etc.

Problem is that for OnDoubleClick where %clicks=2 it still sends onClick first when it was really a double click.

Suggestions?

#1
10/03/2006 (7:36 pm)
This seems to work for me. Play with your delay as you need to.

function pChair::onMouseDown(%this, %modifier, %worldPosition, %clicks)
{
   %this.isMouseDown = true;
}
function pChair::onMouseUp(%this, %modifier, %worldPosition, %clicks)
{
   if (%this.isMouseDown)
   {
      if (%clicks ==1)
      {
         %this.lastSchedule = %this.schedule(500,"onClick", %modifier, %worldPosition, %clicks);                
      }
      else
      {
         cancel(%this.lastSchedule);
      }
      
      if (%clicks == 2)
      {
         %this.onClick(%modifier, %worldPosition, %clicks);
      }
   }
   %this.isMouseDown = false;
}

function pChair::onClick(%this, %modifier, %worldPosition, %clicks)
{
   if (%clicks == 1)
      echo ("Clicked a chair");
   else
      echo ("Sit down in a chair");
   
}
#2
10/03/2006 (7:38 pm)
This all depends. In most applications, a single click selects an object or does some other kind of passive command. A second click within a certain ammount of time will activate an additional or less passive command.

Still, you'd have to use some schedules to track the clicks.

Psudo code Example:


button.maxDoubleClickTime = 400 (milliseconds)
button.singleClick = false


button.onClick() {

  // Single Click Processing
  if (this.singleClick == false) {
    schedule(button.singleClickSchedule, this.maxDoubleClickTime);
    this.singleClick = true;

  // Double Click Processing -- we only get here if there's a 2nd click before the single click schedule is called
  } else { 
    // do double click action here
    this.singleClick= false // this will cancle any pending single click actions
  }
}


// Single Click Helper Method
button.singleClickSchedule() {
  
  // Only do the action if it's still a single click
  if (this.singleClick== true) {
    
    // do single click action here...
    
    this.singleClick= false // set back to false so we can accept single click actions again

  }
}

Hope that answers your question.

Edit: Guess you figured it out by yourself. :)
#3
10/03/2006 (7:43 pm)
Yep, thanks though. :)

Now on to the next hurdle. Its sometimes neverending isnt it?

:)
#4
10/03/2006 (10:05 pm)
I always thought that's what the %mouseClicks parameter was for.

Yeah... I just tested it. The fourth value from the onMouseUp callback is the number of clicks that built up to the one sent in. So, the callbacks look like this:


onMouseUp is called with 1 click reported


onMouseUp is called with 1 click reported

onMouseUp is called with 2 clicks reported

Works for more than two clicks too.
Nice alternative solutions though! That's how you know you're making games and not waiting for someone to do it for you ;)