Button Click Detector - Second Life Wiki

Button Click Detector

From Second Life Wiki

Second Life Wiki > Button Click Detector
Jump to: navigation, search
// 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.");
    }
}