Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Discussion: The better double-click

Geuis Dassin
Filming Path creator
Join date: 3 May 2006
Posts: 565
01-14-2007 22:40
CODE

touch_start(integer total_number)
{
clickcount++;
if(clickcount == 1){
first_time = llGetUnixTime();
}
if(clickcount == 2 && (first_time == llGetUnixTime())){
//do your code here
llOwnerSay("dblclick");



clickcount = 0;
}
if(clickcount > 2 || first_time != llGetUnixTime()){
clickcount = 0;
first_time = 0;
}
}
Nada Epoch
The Librarian
Join date: 4 Nov 2002
Posts: 1,423
Original Thread
01-15-2007 23:14
/15/7b/160571/1.html
_____________________
i've got nothing. ;)
Domino Marama
Domino Designs
Join date: 22 Sep 2006
Posts: 1,126
01-16-2007 02:38
I can't see what cases this will catch that something simple like this won't.

CODE

integer first_time = 0;

default
{
touch_start(integer num_detected)
{
if ( first_time == llGetUnixTime() )
{
first_time = 0;
// double clicked code here
}
else
{
first_time = llGetUnixTime();
}
}
}


Both versions have two problems:

1) Multiple avatars clicking at the same time give a false double click
2) A valid double click close to the end of a second won't register, ie llGetUnixTime increases in the time between clicks.
Domino Marama
Domino Designs
Join date: 22 Sep 2006
Posts: 1,126
01-16-2007 03:00
From: Domino Marama
2) A valid double click close to the end of a second won't register, ie llGetUnixTime increases in the time between clicks.


Here's a fix for that one..

CODE

float check_time = 0.0;
float click_speed = 0.5;

default
{
touch_start(integer num_detected)
{
if ( check_time < llGetTime() )
{
check_time = llGetTime() + click_speed;
}
else
{
check_time = 0.0;
// double clicked code here
}
}
}


If the time should get reset (a point overly stated on the wiki I think), the above could give a false double click on the first click. If that would cause you a problem then use the longer code below.

CODE

float check_time = 0.0;
float click_speed = 0.5;
float test_time;

default
{
touch_start(integer num_detected)
{
test_time = llGetTime();
if ( check_time < test_time )
{
check_time = test_time + click_speed;
}
else
{
if ( ( check_time - click_speed ) > test_time )
{
check_time = test_time + click_speed;
}
else
{
check_time = 0.0;
// double clicked code here
}
}
}
}
Geuis Dassin
Filming Path creator
Join date: 3 May 2006
Posts: 565
01-17-2007 07:35
UNIX time doesn't get reset.

So, don't use llGetTime, use llGetUnixTime as I originally had it.
Ayrn Wake
Registered User
Join date: 7 Jan 2006
Posts: 39
02-12-2007 03:37
Wow, all these 'complicated' solutions to something that isn't very hard.
I'll provide my code as an alternative. It has a side effect (in that it resets script time), but so few things use it I don't see the harm. If you seriously want to use the code too, I suggest putting in a test to make sure its the same agent who's clicked, else 2 people clicking close together would trigger it. I've omitted that code because its fairly simple, and I want to keep the double click function as simple as I can.

CODE

default
{
touch_start(integer num_detected)
{
if (llGetAndResetTime() < 0.3)
{
// DOUBLE CLICK
}
}
}


Its not the best code, but to me its certainly more simple than the rest. Also, whilest script time occasionally resets itself anyways, dealing with such a short delay time... its not going to be often its triggered at the wrong time (though it may happen, I'm sure other tests could be implemented based on other time functions).

Bare in mind my script screws up if you try using script time (not timer events) elsewhere in the script, as it relies on resetting the time. But, it should be slightly faster than the above examples and doesn't require all the variables, so should also be more memory efficient.
Deanna Trollop
BZ Enterprises
Join date: 30 Jan 2006
Posts: 671
02-12-2007 14:48
One drawback to timerless double-click implimentations is that they can't be reliably used for single-clicks from an individual user as well.

CODE

key LastClicker = NULL_KEY;
float Timeout;

default
{
touch_start( integer Num )
{
if( LastClicker == NULL_KEY )
{
LastClicker = llDetectedKey( 0 );
Timeout = lGetTime()+ 0.5; // 0.5 = max double-click interval, change as desired
}
else if( llDetectedKey( 0 ) == LastClicker )
{
LastClicker = NULL_KEY;

if( lGetTime() <= Timeout )
{
// Double-Click
}
}
else
{
// Single-Click, only executed for a second clicker within Timeout window
}
}
}