Difference between revisions of "Button Click Detector"

From Second Life Wiki
Jump to navigation Jump to search
m (Minor language change for ToS compliance.)
m (some minor readability improvements)
Line 7: Line 7:
integer horizontal = 4;
integer horizontal = 4;
integer vertical = 3;
integer vertical = 3;
list buttons = [ "brown", "red", "orange", "yellow",
 
"blue", "cyan", "lblue", "gray",
list buttons = [
"green", "puke", "purple", "pink" ];
        "brown", "red", "orange", "yellow",
        "blue", "cyan", "lblue", "gray",
        "green", "puke", "purple", "pink"];


default
default
Line 15: Line 17:
     state_entry()
     state_entry()
     {
     {
         llSay(0, "Hello, Avatar!");
        // PUBLIC_CHANNEL has the integer value 0
         llSay(PUBLIC_CHANNEL, "Hello, Avatar!");
     }
     }
   
 
     touch_start(integer total_number)
     touch_start(integer num_detected)
     {
     {
         vector v = llDetectedTouchUV(0);
         vector touchUV = llDetectedTouchUV(0);
       
 
         if( v == TOUCH_INVALID_TEXCOORD ) {
         if( touchUV == TOUCH_INVALID_TEXCOORD )
             llSay(0, "I don't know what you just did.");
        {
             llSay(PUBLIC_CHANNEL, "I don't know what you just did.");
             return;
             return;
         }
         }


         float x = v.x;
         touchUV.y = 1.0 - touchUV.y;
        float y = 1.0-v.y;
 
       
         integer rowno = (integer)(touchUV.y / (1.0/(float)vertical));
        integer idx;
         integer colno = (integer)(touchUV.x / (1.0/(float)horizontal));
        integer rowno;
         integer index = rowno*horizontal + colno;
         integer colno;
       
        rowno = (integer)(y / (1.0/(float)vertical));
         colno = (integer)(x / (1.0/(float)horizontal));
          
        idx = rowno*horizontal + colno;


         if( idx > llGetListLength(buttons) ) {
         if( llGetListLength(buttons) < index )
             llSay(0, "Great, you stumped me. Bugger off.");
        {
             llSay(PUBLIC_CHANNEL, "Great, you stumped me. Bugger off.");
             return;
             return;
         }      
         }
       
 
         llSay(0, "Click at " + (string)x + ", " + (string)y);      
         llSay(PUBLIC_CHANNEL, "Click at <" + (string)touchUV.x + ", " + (string)touchUV.y + ">");
         llSay(0, "That is, row " + (string)rowno + ", col " + (string)colno);
         llSay(PUBLIC_CHANNEL, "That is row " + (string)rowno + " and col " + (string)colno);
         llSay(0, "That is, you clicked near the " + llList2String(buttons,idx) + " button.");
         llSay(PUBLIC_CHANNEL, "That is, you clicked near the " + llList2String(buttons, index) + " button.");
     }
     }
}
}
</lsl>
</lsl>

Revision as of 13:24, 7 October 2012

<lsl> // This script assumes a grid of 4 columns, 3 rows of buttons as listed in the 'buttons' list below. // But you can change it as you like, just change horizontal to the # of columns and vertical to the # of rows, // and of course the buttons list must be updated as well. Other than that the code (and more importantly, the MATH) // should not need updating.

integer horizontal = 4; integer vertical = 3;

list buttons = [

       "brown", "red", "orange", "yellow",
       "blue", "cyan", "lblue", "gray",
       "green", "puke", "purple", "pink"];

default {

   state_entry()
   {
       // PUBLIC_CHANNEL has the integer value 0
       llSay(PUBLIC_CHANNEL, "Hello, Avatar!");
   }
   touch_start(integer num_detected)
   {
       vector touchUV = llDetectedTouchUV(0);
       if( touchUV == TOUCH_INVALID_TEXCOORD )
       {
           llSay(PUBLIC_CHANNEL, "I don't know what you just did.");
           return;
       }
       touchUV.y = 1.0 - touchUV.y;
       integer rowno = (integer)(touchUV.y / (1.0/(float)vertical));
       integer colno = (integer)(touchUV.x / (1.0/(float)horizontal));
       integer index = rowno*horizontal + colno;
       if( llGetListLength(buttons) < index )
       {
           llSay(PUBLIC_CHANNEL, "Great, you stumped me. Bugger off.");
           return;
       }
       llSay(PUBLIC_CHANNEL, "Click at <" + (string)touchUV.x + ", " + (string)touchUV.y + ">");
       llSay(PUBLIC_CHANNEL, "That is row " + (string)rowno + " and col " + (string)colno);
       llSay(PUBLIC_CHANNEL, "That is, you clicked near the " + llList2String(buttons, index) + " button.");
   }

} </lsl>