Difference between revisions of "Button Click Detector"

From Second Life Wiki
Jump to navigation Jump to search
m (some minor readability improvements)
m (Replaced old <LSL> block with <source lang="lsl2">)
 
(One intermediate revision by one other user not shown)
Line 1: Line 1:
<lsl>
<source lang="lsl2">
// This script assumes a grid of 4 columns, 3 rows of buttons as listed in the 'buttons' list below.
// 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,
// But you can change it as you like, just change horizontal to the # of columns and vertical to the # of rows,
Line 7: Line 7:
integer horizontal = 4;
integer horizontal = 4;
integer vertical = 3;
integer vertical = 3;
 
list buttons = [ "brown", "red", "orange", "yellow",
list buttons = [
"blue", "cyan", "lblue", "gray",
        "brown", "red", "orange", "yellow",
"green", "puke", "purple", "pink" ];
        "blue", "cyan", "lblue", "gray",
        "green", "puke", "purple", "pink"];


default
default
{
{
    state_entry()
     touch_start(integer total_number)
    {
        // PUBLIC_CHANNEL has the integer value 0
        llSay(PUBLIC_CHANNEL, "Hello, Avatar!");
    }
 
     touch_start(integer num_detected)
     {
     {
         vector touchUV = llDetectedTouchUV(0);
         vector v = llDetectedTouchUV(0);
 
       
         if( touchUV == TOUCH_INVALID_TEXCOORD )
         if( v == 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;
         }
         }


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


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

Latest revision as of 00:10, 22 January 2015

// 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
{  
    touch_start(integer total_number)
    {
        vector v = llDetectedTouchUV(0);
        
        if( v == TOUCH_INVALID_TEXCOORD ) {
            llSay(0, "I don't know what you just did.");
            return;
        }

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

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