Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Discussion: Press & Hold trigger emulation

Soen Eber
Registered User
Join date: 3 Aug 2006
Posts: 428
04-01-2007 07:41
Function of this code is to wait until a user presses and holds down the mouse key for 2 seconds before triggering an action - in this case presenting a basic menu.

You would want to use it for those occasions where you want to administer an object (such as setting up an access list, running a configuration utility, perform window tinting, etc) with a mouse touch, but you want to filter out unintentional random clicks.

Currently the code restricts access to the objects owner (in two locations - touch_start and touch). It would make sense to encapsulate the access check to a separate function and add an additional test in the listen event as well for greater security. A 2nd expansion would add a configuration variable to switch between owner, group, and public access.

CODE

// set these to whatever values you wish
integer myChannel = 123;
float trigger_time = 2.0;
float idle_time = 10.0;
list menu = ["A", "B"];

// used for controlling command entry
float elapsed = 0.0;
integer listenHandle;

A()
{
llSay(0, "A Pressed");
}
B()
{
llSay(0, "B Pressed");
}
touch_hold()
{
elapsed = llGetAndResetTime();
llListenControl(listenHandle, TRUE); // ...enable
llSetTimerEvent(idle_time);
llDialog(llDetectedKey(0), "Menu Stub", menu, myChannel);
}
default
{
state_entry()
{
listenHandle = llListen(myChannel,"","","");
elapsed = 0.0;
}
on_rez(integer startup_param)
{
llResetScript();
}
touch_start(integer num_detected)
{
if (llDetectedKey(0) == llGetOwner()) {
elapsed = llGetAndResetTime();
}
}
touch(integer num_detected)
{
if (llDetectedKey(0) == llGetOwner()) {
elapsed = llGetTime();
if (elapsed > trigger_time) {
touch_hold();
}
}
}
timer()
{
llSetTimerEvent(0.0);
llListenControl(listenHandle, FALSE); // ...disable
}
listen(integer chan, string name, key id, string cmd)
{
if (llListFindList(menu, [cmd]) != -1) {
if (llToUpper(cmd) == "A") {
A();
}
else if (llToUpper(cmd) == "B") {
B();
}
}
}
}
Nada Epoch
The Librarian
Join date: 4 Nov 2002
Posts: 1,423
Original Thread
04-09-2007 04:58
/15/7d/174781/1.html
_____________________
i've got nothing. ;)
Soen Eber
Registered User
Join date: 3 Aug 2006
Posts: 428
04-18-2007 16:43
Version 1.1: allows access control - owner/group/public
Version 1.2: bugfix (= instead of == in access routine)

CODE

// Touch & Hold trigger - perform action when mouse pressed for [n] seconds
// Revison History
// 1.0 03/00/2007 Soen Eber Initial Development
// 1.1 04/18/2007 Soen Eber Added access control

// set these to whatever values you wish
integer myChannel = 123;
float trigger_time = 2.0;
float idle_time = 10.0;
list menu_root = ["A", "B"];
list menu = [];

// used for setting access
list menu_access = ["Owner","Group","Public"];
string allowed_access;

// used for controlling command entry
float elapsed = 0.0;
integer listenHandle;

A(key agent)
{
llSay(0, "A Pressed");
}
B(key agent)
{
llSay(0, "B Pressed");
}
integer allowed(key agent)
{
if (allowed_access == "Public") return TRUE;
if (allowed_access == "Group") {
if (llSameGroup(agent)) return TRUE;
if (agent == llGetOwner()) return TRUE;
}
if (allowed_access == "Owner" && (agent == llGetOwner())) return TRUE;
return FALSE;
}
touch_hold()
{
elapsed = llGetAndResetTime();
llListenControl(listenHandle, TRUE);
llSetTimerEvent(idle_time);
llDialog(llDetectedKey(0), "Menu Stub", menu, myChannel);
}
default
{
state_entry()
{
listenHandle = llListen(myChannel,"","","");
elapsed = 0.0;
allowed_access = "Owner";
}
on_rez(integer startup_param)
{
llResetScript();
}
touch_start(integer num_detected)
{
if (llDetectedKey(0) == llGetOwner()) {
menu = menu_root + ["Set Access..."];
}
else {
menu = menu_root;
}
elapsed = llGetAndResetTime();
}
touch(integer num_detected)
{
if (allowed(llDetectedKey(0))) {
elapsed = llGetTime();
if (elapsed > trigger_time) {
touch_hold();
}
}
}
touch_end(integer num_detected)
{
if (elapsed < trigger_time) {
if (allowed(llDetectedKey(0))) {
// Place normal touch code here - restricted to allowed type
llSay(0, "touched by someone allowed access");
}
else {
// Place normal "not allowed" touch code here
llSay(0, "touched by someone not allowed access");
}
// Place normal unrestricted touch code here
llSay(0, "touched - unrestricted access");
}
}
timer()
{
llSetTimerEvent(0.0);
llListenControl(listenHandle, FALSE);
}
listen(integer chan, string name, key agent, string cmd)
{
if (
(llListFindList(menu, [cmd]) != -1)
|| (llListFindList(menu_access, [cmd]) != -1)
) {
// Code restricted to Owner
if (agent == llGetOwner()) {
if (cmd == "Set Access...") {
llDialog(agent, "Set Access", menu_access, myChannel);
}
if (cmd == "Owner") {
allowed_access = "Owner";
}
if (cmd == "Group") {
allowed_access = "Group";
}
if (cmd == "Public") {
allowed_access = "Public";
}
}
// Code restricted to allowed access
if (allowed(agent)) {
if (cmd == "A") {
A(agent);
}
else if (cmd == "B") {
B(agent);
}
}
// Code restricted to no one
{
}
}
}
}