OnTouchSelectTexture

From Second Life Wiki
Revision as of 19:29, 6 January 2009 by Nargus Asturias (talk | contribs) (New page: '''IMPORTANT NOTE:''' This menus control REQUIRED Nargus Dialog Control script and Nargus Menus Control script. You MUST have all three scripts ...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

IMPORTANT NOTE: This menus control REQUIRED Nargus Dialog Control script and Nargus Menus Control script. You MUST have all three scripts in the same prim for it to work.

Read Me

  1. These scripts were written by Tiyuk Quellmalz, with much help from Nargus Asturias' scripts from the Second Life Wiki: Dialog Menus Control
  2. The license is Public Domain because Nargus Asturias did not specify a license, and I both used and modified his code. If you extend this code, please credit both Nargus Asturias and Tiyuk Quellmalz for the work they have done thus far.
  3. Functionality: Drop this set of scripts (make sure you get all three) into a prim, along with one or more textures. All sides of the prim will have their texture changed to the name of the texture you select from the menu that appears when you touch the object.
  4. Bugs: Sometimes, just after an inventory refresh, the menu won't appear when you touch it. Wait a few seconds and try again.
  5. To-Do: Allow modifying the texture on only specific sides of the object. I don't really have a use case for this yet, so it's not implemented.

Scripts

OnTouchSelectTexture.lsl <lsl> // READ ME: // Put "NargusBase" and "NargusDMC" along with this script // in a prim and touch. //Credits go to Nargus Asturias for the core menu code. //Texture selection code added by Tiyuk Quellmalz. //This code is in the Public Domain.

// Dialog constants integer lnkDialog = 14001; integer lnkDialogNotify = 14004; integer lnkDialogResponse = 14002; integer lnkDialogTimeOut = 14003;

integer lnkDialogReshow = 14011; integer lnkDialogCancel = 14012;

integer lnkMenuClear = 15001; integer lnkMenuAdd = 15002; integer lnkMenuShow = 15003;

string seperator = "||"; integer dialogTimeOut = 0;

// ********** DIALOG FUNCTIONS ********** string packDialogMessage(string message, list buttons, list returns){

   string packed_message = message + seperator + (string)dialogTimeOut;

   integer i;
   integer count = llGetListLength(buttons);
   for(i=0; i<count; i++) packed_message += seperator + llList2String(buttons, i) + seperator + llList2String(returns, i);

   return packed_message;

}

dialogReshow(){llMessageLinked(LINK_THIS, lnkDialogReshow, "", NULL_KEY);} dialogCancel(){

   llMessageLinked(LINK_THIS, lnkDialogCancel, "", NULL_KEY);
   llSleep(1);

}

dialogNotify(key id, string message){

   list rows;

   llMessageLinked(LINK_THIS, lnkDialogNotify,
       message + seperator + (string)dialogTimeOut + seperator,
       id);

} // ********** END DIALOG FUNCTIONS **********


default{

   state_entry(){
       integer numtexs = llGetInventoryNumber(INVENTORY_TEXTURE);
       integer i;
       integer first_list = TRUE;
       integer depth = 0;
       list texture_names;
       
       llMessageLinked(LINK_THIS, lnkMenuClear, "", NULL_KEY);
       
       for(i = 0; i < numtexs; i++)
       {
           string curr = llGetSubString(llGetInventoryName(INVENTORY_TEXTURE, i), 0, 23);
           integer len = llGetListLength(texture_names);
           integer op_a = (first_list == FALSE && len == 10);
           integer op_b = (first_list == TRUE && len == 11);
           integer op_c = i == (numtexs - 1);
           if( op_a || op_b || op_c)
           {
               integer back_button;
               integer next_button;
               back_button = FALSE;
               next_button = FALSE;
               
               if(op_c && !(op_a || op_b))
                   texture_names += [ curr ];
               if(first_list == FALSE)
               {
                   texture_names = llListInsertList(texture_names, [ "Back" ], 0);
                   back_button = TRUE;
               }
               else
               {
                   first_list = FALSE;
               }
               
               if(i != (numtexs - 1)) //There are more menus to make
               {
                   texture_names = llListInsertList(texture_names, [ "Next" ], 2);
                   next_button = TRUE;
               }
                   
               list call_names = texture_names; 
               
               if(back_button)
               {
                   call_names = llListReplaceList(call_names, [ "MENU_Menu" + ((string) (depth - 1)) ], 0, 0);
               }
               
               if(next_button)
               {
                   call_names = llListReplaceList(call_names, [ "MENU_Menu" + ((string) (depth + 1)) ], 2, 2);   
               }
           
                   
               llMessageLinked(LINK_THIS, lnkMenuAdd, packDialogMessage(
                   "[ Group " + (string) depth + " ]\n" +
                   "Select Texture",
                   texture_names,
                   call_names
               ), "Menu" + (string) depth);
               texture_names = [ curr ];
               depth++;
           }
           else
           {
               texture_names += [ curr ];
           }
       }

//       llSetText("Touch me to show menu", <1,1,1>, 1);
       llSay(0, "Initialization complete.");
   }

   link_message(integer sender_num, integer num, string str, key id){
       if(num == lnkDialogTimeOut){
           dialogNotify(llGetOwner(), "Menu time-out. Please try again.");
           state default;
       }else if(num == lnkDialogResponse){
           if(llGetSubString(str, 0, 8) != "MENU_Menu")
           {
               llSetTexture(str, ALL_SIDES);               
           }
       }
   }

   touch_start(integer num_detected){
       llMessageLinked(LINK_THIS, lnkMenuShow, "", llDetectedOwner(0));
   }
   
   on_rez(integer i)
   {
       llResetScript();   
   }
   
   changed(integer change)
   {
       if(change == CHANGED_INVENTORY)
       {
           llSay(0, "Inventory change detected; re-initializing...");
           llResetScript();    
       }   
   }

} </lsl>