Jump to content

Dialog Choices from Numbered Buttons


Rolig Loon
 Share

You are about to reply to a thread that has been inactive for 4752 days.

Please take a moment to consider if this thread is worth bumping.

Recommended Posts

The 12-character limit on button labels in LSL dialogs can be frustrating for scripters. One way around it is to generate a numbered menu in the dialog text and then simply use the numbers as button labels. This script does just that, building on Void Singer's Multipage Dialog Pagination script, which displays 10 menu choices at a time in successive dialog pages. 

As written, this demo script reads a list of web site names and associated URLs from a notecard and then displays them as menu choices when the script is activated by touch.  Each line on the notecard starts with a site name, followed by a "=" sign and then a URL. The script could obviously be used for any application where you want users to choose an action from a numbered list of options.

 

//URL Chooser -- Rolig Loon -- February 2011

// Reads web site information from a notecard in the format   Site name = http://www.a_great_URL.com
// and presents result in a URL prompt.

// Multipage dialog function uDlgBtnLst by Void Singer ( http://community.secondlife.com/t5/LSL-Scripting-Library/Dynamic-Multi-page-Dialog-AKA-Pagination/m-p/708119#M6

list uDlgBtnLst( integer vIntPag )
{
    integer vIdxBeg = 10 * (~-vIntPag);          //-- 10 * (vIntPag - 1), enclose "~-X" in parens to avoid LSL bug
    integer vIdxMax = -~(~([] != gLstMnu) / 10); //-- (llGetListLength( gLstMnu ) - 1) / 10 + 1
    list vLstRtn =
      llListInsertList(
        llList2List( gLstMnu, vIdxBeg, vIdxBeg + 9 ), //-- grab 10 dialog buttons
        (list)("  <<---(" + (string)(vIntPag + (-(vIntPag > 1) | vIdxMax - vIntPag)) + ")"), //-- back button
        -1 ) + //-- inserts back button at index 9, pushing the last menu item to index 10
      (list)("  (" + (string)(-~((vIntPag < vIdxMax) * vIntPag)) + ")--->>"); //-- add fwd button at index 11
   
    return //-- fix the order to L2R/T2B
      llList2List( vLstRtn, -3, -1 ) + llList2List( vLstRtn, -6, -4 ) +
      llList2List( vLstRtn, -9, -7 ) + llList2List( vLstRtn, -12, -10 );
}

//--                       Anti-License Text                         --//*/
//     Contributed Freely to the Public Domain without limitation.     //*/
//   2009 (CC0) [ http://creativecommons.org/publicdomain/zero/1.0 ]   //*/
//  Void Singer [ https://wiki.secondlife.com/wiki/User:Void_Singer ]  //*/
//--

list gLstMnu;
integer gLine;
string gCard;
integer gDChan;
integer gDLisn;
key gQuery;
integer gCount;
list Sites;
list URLs;
list gDlabels;

default
{
    state_entry()
    {
        if (llGetInventoryNumber(INVENTORY_NOTECARD) > 0)
        {
            gCard = llGetInventoryName(INVENTORY_NOTECARD,0);
            gQuery =llGetNotecardLine(gCard,0);
            gCount = 1;
        }
        else
        {
            llOwnerSay("No URL notecard detected.");
        }
    }
    
    changed (integer change)
    {
        if (change & CHANGED_INVENTORY)
        {
            llResetScript();
        }
    }
    
    dataserver(key qID, string data)
    {
        if (qID == gQuery)
        {
            if (data != EOF)
            {
                integer idx = llSubStringIndex(data,"=");
                if (~idx)
                {
                    Sites += [llStringTrim(llGetSubString(data,0,idx-1),STRING_TRIM)];  //Site names from NC
                    URLs += [llStringTrim(llGetSubString(data,idx+1,-1),STRING_TRIM)];  //URLs from NC
                    gDlabels += [(string)gCount + ". " +llStringTrim(llGetSubString(data,0,idx-1),STRING_TRIM) + " \n"]; //Dialog text
                    gLstMnu += [(string)gCount]; // Button labels
                    ++gCount;
                }
                gQuery = llGetNotecardLine(gCard,++gLine);
            }
            else
            {
                llSay(0,"Initialized.");
                state running;
            }
        }
    }
}
                
state running
{
    changed (integer change)
    {
        if (change & CHANGED_INVENTORY)
        {
            llResetScript();
        }
    }
    
    touch_start(integer num)
    {
        llSetTimerEvent(10.0);
        gDChan = (integer)("0xF" + llGetSubString(llDetectedKey(0),0,6));
        gDLisn = llListen(gDChan,"","","");
        string temp = "";
        integer i;
        //Display the first 10 sites in dialog
        for (i=0;i<10;++i)
        {
            temp += llList2String(gDlabels,i);
        }
        llDialog(llDetectedKey(0),"Which site do you want to visit? \n"+ temp, uDlgBtnLst(1) ,gDChan);
    }
    
    listen (integer channel, string name, key id, string msg)
    {
        llSetTimerEvent(0.0);
        llListenRemove(gDLisn);
        // Has the user clicked either the ">>" or "<<" button?
        if (!llSubStringIndex( msg, "  " ))  //-- detects 2 (hidden) leading spaces
        {
            llSetTimerEvent(10.0);
            gDLisn = llListen(gDChan,"","","");
            string temp = "";
            integer menu =  (integer)llGetSubString( msg, -~llSubStringIndex( msg, "(" ), -1 );
            integer i;
            for (i=(10*(menu-1));i<(10*menu);++i)
            {
                temp += llList2String(gDlabels,i);
            }
            llDialog( id, "Which site do you want to visit? \n"+ temp, uDlgBtnLst(menu), gDChan );
        }
        else // If user has clicked a numbered button
        {
            integer Choice = (integer) msg -1;
            llLoadURL(id,llList2String(Sites,Choice),llList2String(URLs,Choice));
        }
    }
    
    timer()
    {
        llSetTimerEvent(0.0);
        llWhisper(0,"Timeout. Please close the dialog box on your screen.");
        llListenRemove(gDLisn);
    }      
}

 EDIT:  Repaired indexing error in the listen event.

 

Link to comment
Share on other sites

  • 2 weeks later...
You are about to reply to a thread that has been inactive for 4752 days.

Please take a moment to consider if this thread is worth bumping.

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
 Share

×
×
  • Create New...