User:Xen Lisle/Texture Slide

From Second Life Wiki
Jump to navigation Jump to search

Texture Slide

General information:

This is a proof of concept to show how to slide a texture across a prim face with mouse movement.

The script will display a grid texture of 25 squares. Clicking on a grid square will return that squares value.
Clicking and holding while moving the mouse, the texture will slide in the direction of the mouse movement.

Script

<lsl> //Touch and drag a texture across a prim face //Useful for interfaces and such //Example script gives you 25 buttons to click on //By Xen Lisle //Free to use /////////User Variables///////// integer gFace = 0; //prim face you want the script to work on vector gRepeats = <1.0,1.0,0.0>; //the UV repeats of the texture string gTexture = "69737f61-1fdd-6033-7ea3-a52e61a40137"; //your menu texture list gList = [<0.0,1.0,0.0>,<0.2,0.8,0.0>,"Button","A-0", //strided list to define button labels and values

                <0.2,1.0,0.0>,<0.4,0.8,0.0>,"Button","A-1", //vector one is top left button corner
                <0.4,1.0,0.0>,<0.6,0.8,0.0>,"Button","A-2", //vector two is bottom right button corner
                <0.6,1.0,0.0>,<0.8,0.8,0.0>,"Button","A-3", //next two elements hold data for the buttons
                <0.8,1.0,0.0>,<1.0,0.8,0.0>,"Button","A-4", //example LABEL,DATA
                                                            //assigning a label helps to filter different button actions                
                <0.0,0.8,0.0>,<0.2,0.6,0.0>,"Button","B-0", //not very helpful in our example but included anyway
                <0.2,0.8,0.0>,<0.4,0.6,0.0>,"Button","B-1", 
                <0.4,0.8,0.0>,<0.6,0.6,0.0>,"Button","B-2",
                <0.6,0.8,0.0>,<0.8,0.6,0.0>,"Button","B-3",
                <0.8,0.8,0.0>,<1.0,0.6,0.0>,"Button","B-4",
                
                <0.0,0.6,0.0>,<0.2,0.4,0.0>,"Button","C-0",
                <0.2,0.6,0.0>,<0.4,0.4,0.0>,"Button","C-1",
                <0.4,0.6,0.0>,<0.6,0.4,0.0>,"Button","C-2",
                <0.6,0.6,0.0>,<0.8,0.4,0.0>,"Button","C-3",
                <0.8,0.6,0.0>,<1.0,0.4,0.0>,"Button","C-4",
                
                <0.0,0.4,0.0>,<0.2,0.2,0.0>,"Button","D-0",
                <0.2,0.4,0.0>,<0.4,0.2,0.0>,"Button","D-1",
                <0.4,0.4,0.0>,<0.6,0.2,0.0>,"Button","D-2",
                <0.6,0.4,0.0>,<0.8,0.2,0.0>,"Button","D-3",
                <0.8,0.4,0.0>,<1.0,0.2,0.0>,"Button","D-4",
                
                <0.0,0.2,0.0>,<0.2,0.0,0.0>,"Button","E-0",
                <0.2,0.2,0.0>,<0.4,0.0,0.0>,"Button","E-1",
                <0.4,0.2,0.0>,<0.6,0.0,0.0>,"Button","E-2",
                <0.6,0.2,0.0>,<0.8,0.0,0.0>,"Button","E-3",
                <0.8,0.2,0.0>,<1.0,0.0,0.0>,"Button","E-4"
               ];

////////////////////////////////

vector gStartST; vector gEndST; vector gOffsetUV;

list FindUV(vector v, list lst) {

   integer i;
   vector cordA;
   vector cordB;
   integer lines = llGetListLength(lst) / 4;
   v -= <llFloor(v.x),llFloor(v.y),0.0>; //clamp values between 0 and 1
   for (i=0;i<lines;i++)
   {
       cordA = llList2Vector(lst,i * 4);
       cordB = llList2Vector(lst, i * 4 + 1);
       if (v.x > cordA.x && v.x < cordB.x && v.y < cordA.y && v.y > cordB.y)
       {
           return llList2List(lst,i * 4 + 2,i * 4 + 3);
       }
   }
   return [];

}

default {

   state_entry()
   {
       if (gTexture =="") gTexture ="69737f61-1fdd-6033-7ea3-a52e61a40137";
       llSetLinkPrimitiveParamsFast(0,[PRIM_TEXTURE,gFace,gTexture,gRepeats, ZERO_VECTOR, 0.]);
   }
   touch_start(integer total_number)
   {
       if (llDetectedTouchFace(0) == gFace)
       {
           gOffsetUV = llGetTextureOffset(gFace);
           gStartST = llDetectedTouchST(0);
           gStartST = <gStartST.x*gRepeats.x,gStartST.y*gRepeats.y,0.0>; //scale the touch vector to match our texture scale
       }
       
   }
   
   touch(integer total_number)
   {
       if (llDetectedTouchFace(0) == gFace)
       {
           gEndST = llDetectedTouchST(0);
           gEndST = <gEndST.x*gRepeats.x,gEndST.y*gRepeats.y,0.0>;//scale the touch vector to match our texture scale
           if (llVecDist(gStartST,gEndST) > 0.)
           {
                vector newUV = gOffsetUV + (gStartST - gEndST);
                newUV -= <llFloor(newUV.x),llFloor(newUV.y),0.0>; //clamp values between 0 and 1
                llSetLinkPrimitiveParamsFast(0,[PRIM_TEXTURE,gFace,gTexture,gRepeats, newUV, 0.]);//apply new texture offset
           }
       }
   }
   
   touch_end(integer total_number)
   {
       if (llDetectedTouchFace(0) == gFace)
       {
           if (llVecDist(gStartST,gEndST) > 0.); //compare mouse movement to see if texture was moved. maybe useful to do something here
           else //did not slide the texture. was a button press
           {
               list list_data = FindUV(llDetectedTouchUV(0),gList);
               //This next line is where we compare button labels
               if (llList2String(list_data,0) == "Button") llSay(0,llList2String(list_data,1));                                
           }
       }
   }

} </lsl>