Advertisement
Bugs_Larnia

LSL: Infinite Menu-Based Item Giver

Jan 13th, 2021
951
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* INFINITE MENU-BASED ITEM GIVER V2.0
  2. Will display objects in a menu with numbered buttons and give the selected one.
  3. Created 2020-05-09 by Bugs Larnia
  4. v2.1 2020-05-11 BL - Added optional folder name
  5. */
  6.  
  7. //USER SETTINGS
  8. integer giIncludeAllButton = FALSE; //Includes an [ ALL ] button that gives all contents in one folder
  9. integer giOwnerOnly = FALSE; //Sets access to owner only; overrides giGroupOnly
  10. integer giGroupOnly = FALSE; //Sets access to containing prim's group only
  11. string gsFolderName = ""; //Optional folder name for [ ALL ] button; if emtpy, containing object name is used
  12. //END USER SETTINGS
  13.  
  14. /*======================================================*/
  15. list glButtonLabels;
  16. list glLabelDescriptions;
  17. list glInventory;
  18.  
  19. integer giShowNavigation;
  20. integer giIndexStart;
  21. integer giIndexEnd;
  22. integer giPageItemCount;
  23. integer giEars;
  24.  
  25. string gsLabelAll = "[ ALL ]";
  26. string gsLabelNext = "[ NEXT >> ]";
  27. string gsLabelBack = "[ << BACK ]";
  28.  
  29. //#REGION LIST HANDLING
  30. //Create the inventory list
  31. CreateInventoryList(integer piInventoryType)
  32. {
  33.     ClearLists();
  34.    
  35.     integer iLength = llGetInventoryNumber(piInventoryType);    
  36.     if (!iLength)
  37.     {
  38.         return;
  39.     }
  40.     giShowNavigation = (giIncludeAllButton && (iLength > 11)) || (iLength > 12);
  41.    
  42.    
  43.    
  44.     integer i;
  45.     for (i = 0; i < iLength; ++i)
  46.     {
  47.         string sName = llGetInventoryName(piInventoryType, i);
  48.         glInventory += sName;        
  49.     }
  50.     RemoveThisScript();
  51.     for (i = 0; i < iLength; ++i)
  52.     {
  53.         string sName = llList2String(glInventory, i);
  54.         AddLabelAndDescription(i, sName);
  55.     }    
  56.     InitNavigation();
  57. }
  58.  
  59. RemoveThisScript()
  60. {
  61.     integer iScriptIndex = llListFindList(glInventory, [llGetScriptName()]);
  62.     if (~iScriptIndex)
  63.     {
  64.         glInventory = llDeleteSubList(glInventory, iScriptIndex, iScriptIndex);
  65.     }
  66. }
  67.  
  68. //Create the button labels and the descriptions
  69. AddLabelAndDescription(integer piIndex, string psName)
  70. {
  71.     string sLabel = (string)(++piIndex);
  72.     glButtonLabels += sLabel;
  73.     glLabelDescriptions += (sLabel + " - " + llGetSubString(psName, 0, 50));
  74. }
  75.  
  76. //Clear all lists
  77. ClearLists()
  78. {
  79.     glButtonLabels = [];
  80.     glLabelDescriptions = [];
  81.     glInventory = [];
  82. }
  83.  
  84. integer GetInventoryListCount()
  85. {
  86.     return llGetListLength(glInventory);
  87. }
  88.  
  89. string CreateFolderName()
  90. {
  91.     if (gsFolderName != "")
  92.     {
  93.         return gsFolderName;
  94.     }
  95.     return llGetObjectName();    
  96. }
  97. //#ENDREGION
  98.  
  99. //#REGION MENU HANDLING
  100. ShowMenuPage(key pkId)
  101. {
  102.     list lPageButtonLabels = llList2List(glButtonLabels, giIndexStart, giIndexEnd);
  103.     list lPageDescriptions = llList2List(glLabelDescriptions, giIndexStart, giIndexEnd);
  104.    
  105.     //Set system buttons (Next, Back and All)
  106.     if (giShowNavigation)
  107.     {
  108.         if (giIncludeAllButton)
  109.         {
  110.             lPageButtonLabels = [gsLabelBack, gsLabelAll, gsLabelNext] + lPageButtonLabels;
  111.         }
  112.         else
  113.         {
  114.             lPageButtonLabels  = [gsLabelBack, gsLabelNext] + lPageButtonLabels;
  115.         }
  116.     }
  117.     else if (giIncludeAllButton)
  118.     {
  119.         lPageButtonLabels = [gsLabelAll] + lPageButtonLabels;
  120.     }
  121.     ShowDialog(pkId, lPageButtonLabels, lPageDescriptions);
  122. }
  123.  
  124. //#ENDREGION
  125.  
  126. //#REGION MENU NAVIGATION HANDLING
  127. //Set the initial values
  128. InitNavigation()
  129. {
  130.     giPageItemCount = 12;
  131.     if (giIncludeAllButton)
  132.     {
  133.         --giPageItemCount;
  134.     }
  135.    
  136.     if (giShowNavigation)
  137.     {
  138.         giPageItemCount -= 2;
  139.     }
  140.     giIndexStart = 0;
  141.     UpdateEndIndex();
  142. }
  143.  
  144. //Update the page to show
  145. UpdatePage(integer piForward)
  146. {
  147.     integer iIndexStartOld = giIndexStart;
  148.     if (piForward)
  149.     {
  150.         giIndexStart += giPageItemCount;
  151.     }
  152.     else
  153.     {
  154.         giIndexStart -= giPageItemCount;
  155.     }
  156.    
  157.     integer iInventoryListCount = GetInventoryListCount();
  158.     if (giIndexStart >= iInventoryListCount)
  159.     {
  160.         giIndexStart = 0;
  161.     }
  162.     else if (giIndexStart < 0)
  163.     {
  164.         if (iIndexStartOld > 0)
  165.         {
  166.             giIndexStart = 0;
  167.         }
  168.         else
  169.         {
  170.             giIndexStart = iInventoryListCount - giPageItemCount;
  171.         }
  172.     }
  173.     UpdateEndIndex();
  174. }
  175. //#ENDREGION
  176.  
  177. //Update the end index based on the start index and the calculated page length
  178. UpdateEndIndex()
  179. {
  180.     giIndexEnd = giIndexStart + (giPageItemCount - 1);
  181.     integer iInventoryListCount = GetInventoryListCount();
  182.     if (giIndexEnd >= iInventoryListCount)
  183.     {
  184.         giIndexEnd = iInventoryListCount - 1;
  185.     }
  186. }
  187. //#ENDREGION
  188.  
  189. //#REGION PRE-RUN CHECKS
  190. integer HasPermissions(key pkId)
  191. {
  192.     if (giOwnerOnly)
  193.     {
  194.         return (pkId == llGetOwner());
  195.     }
  196.    
  197.     if (giGroupOnly)
  198.     {
  199.         return llSameGroup(pkId);
  200.     }
  201.    
  202.     return TRUE;
  203. }
  204.  
  205. integer HasInventory()
  206. {
  207.     return (GetInventoryListCount() > 0);
  208. }
  209. //#ENDREGION
  210.  
  211. //#REGION COMMUNICATION
  212. TellAgent(key pkId, string psMsg)
  213. {
  214.     llRegionSayTo(pkId, 0, psMsg);
  215. }
  216.  
  217. ShowDialog(key pkId, list plButtonLabels, list plButtonDescriptions)
  218. {
  219.     key kOwner = llGetOwner();
  220.     integer iChannel = (integer)llFrand(-99999999.0) - 1000000;
  221.     giEars = llListen(iChannel, "", kOwner, "");
  222.     llDialog(pkId, CreateMenuText(plButtonDescriptions), plButtonLabels, iChannel);
  223.     llSetTimerEvent(45.0);
  224. }
  225.  
  226. string CreateMenuText(list plButtonDescriptions)
  227. {
  228.     return (
  229.         "Please make your choice:\n\n" +
  230.         llDumpList2String(plButtonDescriptions, "\n")
  231.     );
  232. }
  233.  
  234. StopListening()
  235. {
  236.     llSetTimerEvent(0.0);
  237.     llListenRemove(giEars);
  238. }
  239. //#ENDREGION
  240.  
  241. default
  242. {
  243.     state_entry()
  244.     {
  245.         CreateInventoryList(INVENTORY_ALL);
  246.     }
  247.  
  248.     touch_start(integer total_number)
  249.     {
  250.         key kId = llDetectedKey(0);
  251.         if (!HasPermissions(kId))
  252.         {  
  253.             TellAgent(kId, "I'm sorry, but you do not have permissions to access this object.");
  254.             return;            
  255.         }
  256.         if (!HasInventory())
  257.         {
  258.             TellAgent(kId, "I'm sorry, but there seems to be no inventory to give.");
  259.             return;
  260.         }
  261.         InitNavigation();
  262.         ShowMenuPage(kId);
  263.     }
  264.    
  265.     listen(integer piChannel, string psName, key pkId, string psMsg)
  266.     {
  267.         StopListening();
  268.         if (psMsg == gsLabelAll)
  269.         {
  270.             string sFolderName = CreateFolderName();
  271.             llGiveInventoryList(pkId, sFolderName, glInventory);
  272.             TellAgent(pkId, "Look in your inventory for a folder named \"" + sFolderName + "\"");
  273.         }
  274.         else if (psMsg == gsLabelNext)
  275.         {
  276.             UpdatePage(TRUE);
  277.             ShowMenuPage(pkId);
  278.         }
  279.         else if (psMsg == gsLabelBack)
  280.         {
  281.             UpdatePage(FALSE);
  282.             ShowMenuPage(pkId);
  283.         }
  284.         else
  285.         {
  286.             integer iIndex = ((integer)psMsg - 1);
  287.             string sName = llList2String(glInventory, iIndex);
  288.             llGiveInventory(pkId, sName);
  289.             TellAgent(pkId, "Look in your inventory for a item named \"" + sName + "\"");
  290.         }
  291.     }
  292.    
  293.     timer()
  294.     {
  295.         StopListening();
  296.     }
  297.    
  298.     changed(integer piChange)
  299.     {
  300.         if (piChange & CHANGED_INVENTORY)
  301.         {
  302.             llResetScript();
  303.         }
  304.     }
  305.    
  306.     on_rez(integer piParam)
  307.     {
  308.         llResetScript();
  309.     }
  310. }
  311.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement