Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

discussion: mousehelddown - get more from touches

Goapinoa Primeau
Addict
Join date: 29 Jun 2006
Posts: 58
12-04-2006 10:52
CODE
//Enhanced Touch Events 

// Using this simple script you can define something to happen when the prim is touched (as normal),
// Also something to happen when the mouse is held down on the prim for a period of time. : )




//global declarations

integer touchcount= 0; //set count zero
integer max = 20; //how many touches will fire before we fire mousehelddown.


mousehelddown()
{
//stuff to do when the mouse is held down on the prim
llSay(0,"someones held the mouse down on me!");
}

default
{
state_entry()
{

}

touch_start(integer total_number)
{
// you can use this event just as normal, if you wish
}
touch(integer num)
{
touchcount++; //increment touchcount
if(touchcount>max) //check if touchcount is more than our max
{
touchcount=0; //reset the count
mousehelddown(); //run the mousehelddown code, see above
}
}
touch_end(integer num_detected)
{
touchcount=0; //reset counter
}

}
Nada Epoch
The Librarian
Join date: 4 Nov 2002
Posts: 1,423
Original Thread
12-05-2006 08:22
/15/fd/152727/1.html
_____________________
i've got nothing. ;)
Nynthan Folsom
Registered User
Join date: 29 Aug 2006
Posts: 70
12-06-2006 06:30
I would prolly go with a timer instead.

CODE
float holdTime=3.0;

doMouseDown() {
llSay(0, "Down");
}

doHold() {
llSay(0, "Held");
}

doMouseUp {
llSay(0, "Up");
}

default {
touch_start() {
llSetTimerEvent(holdTime);
doMouseDown();
}
timer() {
llSetTimerEvent(0);
doHold();
}
touch_end() {
llSetTimerEvent(0);
doMouseUp();
}
}
Baron Hauptmann
Just Designs / Scripter
Join date: 29 Oct 2005
Posts: 358
12-06-2006 06:40
From: Nynthan Folsom
I would prolly go with a timer instead.




Why would you rather use a timer? It seems like in a script, the timer is more likely to already be in use, and the touch event is less likely to be used already.

Is there some reason the timer would be preferable?

Thanks,
Baron H.
Joannah Cramer
Registered User
Join date: 12 Apr 2006
Posts: 1,539
12-06-2006 07:59
From: Baron Hauptmann
Is there some reason the timer would be preferable?

The frequency of touch() events appear directly linked to sim performance, which means it can sometimes take your script few times longer to notice the 'mouse held' event in one sim, compared to another, if you rely on counting number of touch() events fired.

Timer is indeed typically used for other things already, so i'd rather go with:
CODE

integer held_time;
integer held_length = 5; // how many secs the mouse button must be down to register

default [

touch_start( ... ) {

held_time = llGetUnixTime() + held_length;
}

touch( ... ) {

if( held_time == -1 ) return;
if( llGetUnixTime() < held_time ) return;

held_time = -1; // mark the held event as triggered
... // and do whatever it's suppposed to do, here
}
}
Goapinoa Primeau
Addict
Join date: 29 Jun 2006
Posts: 58
doubleclickable prims too : )
12-06-2006 08:28
Nice one Joannah - I've not used that function before, must read wiki more!!

That timer suggest got me thinkin : ) and i cooked up a doubleclickable prim script.
If theres a way to do this without using timer, it would be superb : )


//code

integer clicked;

doubleclickstuff()
{
llSay(0,"doubleclick";);
}

singleclickstuff()
{
llSay(0,"singleclick";);
}

default
{
state_entry()
{

}
touch_start(integer total_number)
{
if(clicked)
{
llSetTimerEvent(0.0);
clicked=FALSE;
doubleclickstuff();
return;
}
if(!clicked)
{
clicked=TRUE;
llSetTimerEvent(1.5);
}
}
timer()
{
llSetTimerEvent(0.0);
singleclickstuff();
clicked=FALSE;
}
}
Joannah Cramer
Registered User
Join date: 12 Apr 2006
Posts: 1,539
12-06-2006 10:30
From: Goapinoa Primeau
If theres a way to do this without using timer, it would be superb : )

Similar to previous one:
CODE

integer click_last;

default {

touch_end( integer Contacts ) {

integer click_time = llGetUnixTime();
if( click_time == click_last ) {
// multiple clicks within 1 sec, fast enough to count as double click
llOwnerSay( "double click" );
// put some throttle until next double click can be recognized. here it's 1 sec
click_last = click_time + 1;
}
else if( click_time < click_last ) { return; } // throttle is still active
else { click_last = click_time; }
}
}

edit: note this doesn't fork between single and double clicks, if there's single-click behaviour attached to either touch_start or touch_end, it'll be triggered on each click as usual.