Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* INFINITE MENU-BASED ITEM GIVER V2.0
- Will display objects in a menu with numbered buttons and give the selected one.
- Created 2020-05-09 by Bugs Larnia
- v2.1 2020-05-11 BL - Added optional folder name
- */
- //USER SETTINGS
- integer giIncludeAllButton = FALSE; //Includes an [ ALL ] button that gives all contents in one folder
- integer giOwnerOnly = FALSE; //Sets access to owner only; overrides giGroupOnly
- integer giGroupOnly = FALSE; //Sets access to containing prim's group only
- string gsFolderName = ""; //Optional folder name for [ ALL ] button; if emtpy, containing object name is used
- //END USER SETTINGS
- /*======================================================*/
- list glButtonLabels;
- list glLabelDescriptions;
- list glInventory;
- integer giShowNavigation;
- integer giIndexStart;
- integer giIndexEnd;
- integer giPageItemCount;
- integer giEars;
- string gsLabelAll = "[ ALL ]";
- string gsLabelNext = "[ NEXT >> ]";
- string gsLabelBack = "[ << BACK ]";
- //#REGION LIST HANDLING
- //Create the inventory list
- CreateInventoryList(integer piInventoryType)
- {
- ClearLists();
- integer iLength = llGetInventoryNumber(piInventoryType);
- if (!iLength)
- {
- return;
- }
- giShowNavigation = (giIncludeAllButton && (iLength > 11)) || (iLength > 12);
- integer i;
- for (i = 0; i < iLength; ++i)
- {
- string sName = llGetInventoryName(piInventoryType, i);
- glInventory += sName;
- }
- RemoveThisScript();
- for (i = 0; i < iLength; ++i)
- {
- string sName = llList2String(glInventory, i);
- AddLabelAndDescription(i, sName);
- }
- InitNavigation();
- }
- RemoveThisScript()
- {
- integer iScriptIndex = llListFindList(glInventory, [llGetScriptName()]);
- if (~iScriptIndex)
- {
- glInventory = llDeleteSubList(glInventory, iScriptIndex, iScriptIndex);
- }
- }
- //Create the button labels and the descriptions
- AddLabelAndDescription(integer piIndex, string psName)
- {
- string sLabel = (string)(++piIndex);
- glButtonLabels += sLabel;
- glLabelDescriptions += (sLabel + " - " + llGetSubString(psName, 0, 50));
- }
- //Clear all lists
- ClearLists()
- {
- glButtonLabels = [];
- glLabelDescriptions = [];
- glInventory = [];
- }
- integer GetInventoryListCount()
- {
- return llGetListLength(glInventory);
- }
- string CreateFolderName()
- {
- if (gsFolderName != "")
- {
- return gsFolderName;
- }
- return llGetObjectName();
- }
- //#ENDREGION
- //#REGION MENU HANDLING
- ShowMenuPage(key pkId)
- {
- list lPageButtonLabels = llList2List(glButtonLabels, giIndexStart, giIndexEnd);
- list lPageDescriptions = llList2List(glLabelDescriptions, giIndexStart, giIndexEnd);
- //Set system buttons (Next, Back and All)
- if (giShowNavigation)
- {
- if (giIncludeAllButton)
- {
- lPageButtonLabels = [gsLabelBack, gsLabelAll, gsLabelNext] + lPageButtonLabels;
- }
- else
- {
- lPageButtonLabels = [gsLabelBack, gsLabelNext] + lPageButtonLabels;
- }
- }
- else if (giIncludeAllButton)
- {
- lPageButtonLabels = [gsLabelAll] + lPageButtonLabels;
- }
- ShowDialog(pkId, lPageButtonLabels, lPageDescriptions);
- }
- //#ENDREGION
- //#REGION MENU NAVIGATION HANDLING
- //Set the initial values
- InitNavigation()
- {
- giPageItemCount = 12;
- if (giIncludeAllButton)
- {
- --giPageItemCount;
- }
- if (giShowNavigation)
- {
- giPageItemCount -= 2;
- }
- giIndexStart = 0;
- UpdateEndIndex();
- }
- //Update the page to show
- UpdatePage(integer piForward)
- {
- integer iIndexStartOld = giIndexStart;
- if (piForward)
- {
- giIndexStart += giPageItemCount;
- }
- else
- {
- giIndexStart -= giPageItemCount;
- }
- integer iInventoryListCount = GetInventoryListCount();
- if (giIndexStart >= iInventoryListCount)
- {
- giIndexStart = 0;
- }
- else if (giIndexStart < 0)
- {
- if (iIndexStartOld > 0)
- {
- giIndexStart = 0;
- }
- else
- {
- giIndexStart = iInventoryListCount - giPageItemCount;
- }
- }
- UpdateEndIndex();
- }
- //#ENDREGION
- //Update the end index based on the start index and the calculated page length
- UpdateEndIndex()
- {
- giIndexEnd = giIndexStart + (giPageItemCount - 1);
- integer iInventoryListCount = GetInventoryListCount();
- if (giIndexEnd >= iInventoryListCount)
- {
- giIndexEnd = iInventoryListCount - 1;
- }
- }
- //#ENDREGION
- //#REGION PRE-RUN CHECKS
- integer HasPermissions(key pkId)
- {
- if (giOwnerOnly)
- {
- return (pkId == llGetOwner());
- }
- if (giGroupOnly)
- {
- return llSameGroup(pkId);
- }
- return TRUE;
- }
- integer HasInventory()
- {
- return (GetInventoryListCount() > 0);
- }
- //#ENDREGION
- //#REGION COMMUNICATION
- TellAgent(key pkId, string psMsg)
- {
- llRegionSayTo(pkId, 0, psMsg);
- }
- ShowDialog(key pkId, list plButtonLabels, list plButtonDescriptions)
- {
- key kOwner = llGetOwner();
- integer iChannel = (integer)llFrand(-99999999.0) - 1000000;
- giEars = llListen(iChannel, "", kOwner, "");
- llDialog(pkId, CreateMenuText(plButtonDescriptions), plButtonLabels, iChannel);
- llSetTimerEvent(45.0);
- }
- string CreateMenuText(list plButtonDescriptions)
- {
- return (
- "Please make your choice:\n\n" +
- llDumpList2String(plButtonDescriptions, "\n")
- );
- }
- StopListening()
- {
- llSetTimerEvent(0.0);
- llListenRemove(giEars);
- }
- //#ENDREGION
- default
- {
- state_entry()
- {
- CreateInventoryList(INVENTORY_ALL);
- }
- touch_start(integer total_number)
- {
- key kId = llDetectedKey(0);
- if (!HasPermissions(kId))
- {
- TellAgent(kId, "I'm sorry, but you do not have permissions to access this object.");
- return;
- }
- if (!HasInventory())
- {
- TellAgent(kId, "I'm sorry, but there seems to be no inventory to give.");
- return;
- }
- InitNavigation();
- ShowMenuPage(kId);
- }
- listen(integer piChannel, string psName, key pkId, string psMsg)
- {
- StopListening();
- if (psMsg == gsLabelAll)
- {
- string sFolderName = CreateFolderName();
- llGiveInventoryList(pkId, sFolderName, glInventory);
- TellAgent(pkId, "Look in your inventory for a folder named \"" + sFolderName + "\"");
- }
- else if (psMsg == gsLabelNext)
- {
- UpdatePage(TRUE);
- ShowMenuPage(pkId);
- }
- else if (psMsg == gsLabelBack)
- {
- UpdatePage(FALSE);
- ShowMenuPage(pkId);
- }
- else
- {
- integer iIndex = ((integer)psMsg - 1);
- string sName = llList2String(glInventory, iIndex);
- llGiveInventory(pkId, sName);
- TellAgent(pkId, "Look in your inventory for a item named \"" + sName + "\"");
- }
- }
- timer()
- {
- StopListening();
- }
- changed(integer piChange)
- {
- if (piChange & CHANGED_INVENTORY)
- {
- llResetScript();
- }
- }
- on_rez(integer piParam)
- {
- llResetScript();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement