Difference between revisions of "User:Xen Lisle/Texture Slide"

From Second Life Wiki
Jump to navigation Jump to search
m (Replaced old <LSL> block with <source lang="lsl2">)
 
(2 intermediate revisions by 2 users not shown)
Line 3: Line 3:
<div style="padding: 0.5em;">
<div style="padding: 0.5em;">
=Texture Slide=
=Texture Slide=
==General information:==
==General information:==
This is a proof of concept to show how to slide a texture across a prim face with mouse movement.<br><br>
This is a proof of concept to show how to slide a texture across a prim face with mouse movement.<br><br>
The script will display a grid texture of 25 squares. Clicking on a grid square will return that squares value.<br>
The script will display a grid texture of 25 squares. Clicking on a grid square will return that squares value.<br>
Line 9: Line 11:


==Script==
==Script==
<lsl>
 
//Touch and drag a texture across a prim face
<source lang="lsl2">
//Useful for interfaces and such
// Touch and drag a texture across a prim face
//Example script gives you 25 buttons to click on
// Useful for interfaces and such
//By Xen Lisle
// Example script gives you 25 buttons to click on
//Free to use
// By Xen Lisle
/////////User Variables/////////
// Free to use
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. values smaller than one will show only part of the texture
// prim face you want the script to work on
string gTexture = "69737f61-1fdd-6033-7ea3-a52e61a40137"; //your menu texture
integer gFace = 0;
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
// the UV repeats of the texture. values smaller than one will show only part of the texture
                <0.4,1.0,0.0>,<0.6,0.8,0.0>,"Button","A-2", //vector two is bottom right button corner
vector UV_TEXTURE_REPEATS = <1.0, 1.0, 0.0>;
                <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
//  your menu texture
                                                            //assigning a label helps to filter different button actions               
string MENU_TEXTURE = "69737f61-1fdd-6033-7ea3-a52e61a40137";
                <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",  
//  strided list to define button labels and values
                <0.4,0.8,0.0>,<0.6,0.6,0.0>,"Button","B-2",
//    - vector one is top left button corner
                <0.6,0.8,0.0>,<0.8,0.6,0.0>,"Button","B-3",
//    - vector two is bottom right button corner
                <0.8,0.8,0.0>,<1.0,0.6,0.0>,"Button","B-4",
//    - next two elements hold data for the buttons
               
//  elements in this example: LABEL, DATA
                <0.0,0.6,0.0>,<0.2,0.4,0.0>,"Button","C-0",
//  assigning a label helps to filter different button actions
                <0.2,0.6,0.0>,<0.4,0.4,0.0>,"Button","C-1",
// not very helpful in our example but included anyway
                <0.4,0.6,0.0>,<0.6,0.4,0.0>,"Button","C-2",
list BUTTONS_STRIDED_LIST = [
                <0.6,0.6,0.0>,<0.8,0.4,0.0>,"Button","C-3",
    <0.0, 1.0, 0.0>,   <0.2, 0.8, 0.0>,   "Button",   "A-0",
                <0.8,0.6,0.0>,<1.0,0.4,0.0>,"Button","C-4",
    <0.2, 1.0, 0.0>,   <0.4, 0.8, 0.0>,   "Button",   "A-1",
               
    <0.4, 1.0, 0.0>,   <0.6, 0.8, 0.0>,   "Button",   "A-2",
                <0.0,0.4,0.0>,<0.2,0.2,0.0>,"Button","D-0",
    <0.6, 1.0, 0.0>,   <0.8, 0.8, 0.0>,   "Button",   "A-3",
                <0.2,0.4,0.0>,<0.4,0.2,0.0>,"Button","D-1",
    <0.8, 1.0, 0.0>,   <1.0, 0.8, 0.0>,   "Button",   "A-4",
                <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.0, 0.8, 0.0>,   <0.2, 0.6, 0.0>,   "Button",   "B-0",
                <0.8,0.4,0.0>,<1.0,0.2,0.0>,"Button","D-4",
    <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.0,0.2,0.0>,<0.2,0.0,0.0>,"Button","E-0",
    <0.6, 0.8, 0.0>,   <0.8, 0.6, 0.0>,   "Button",   "B-3",
                <0.2,0.2,0.0>,<0.4,0.0,0.0>,"Button","E-1",
    <0.8, 0.8, 0.0>,   <1.0, 0.6, 0.0>,   "Button",   "B-4",
                <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.0, 0.6, 0.0>,   <0.2, 0.4, 0.0>,   "Button",   "C-0",
                <0.8,0.2,0.0>,<1.0,0.0,0.0>,"Button","E-4"
    <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 gStartST;
Line 57: Line 72:
list FindUV(vector v, list lst)
list FindUV(vector v, list lst)
{
{
    integer i;
     vector cordA;
     vector cordA;
     vector cordB;
     vector cordB;
     integer lines = llGetListLength(lst) / 4;
     integer lines = llGetListLength(lst) / 4;


     v -= <llFloor(v.x),llFloor(v.y),0.0>; //clamp values between 0 and 1
    //  roll values between 0 and 1
     for (i=0;i<lines;i++)
     v -= <llFloor(v.x), llFloor(v.y), 0.0>;
 
    integer i;
     for (i = 0; i < lines; i++)
     {
     {
         cordA = llList2Vector(lst,i * 4);
         cordA = llList2Vector(lst,i * 4);
         cordB = llList2Vector(lst, i * 4 + 1);
         cordB = llList2Vector(lst, i * 4 + 1);
         if (v.x > cordA.x && v.x < cordB.x && v.y < cordA.y && v.y > cordB.y)
 
         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 llList2List(lst, i*4 + 2, i*4 + 3);
         }
         }
     }
     }
     return [];
     return [];
}
}
Line 79: Line 99:
     state_entry()
     state_entry()
     {
     {
         if (gTexture =="") gTexture ="69737f61-1fdd-6033-7ea3-a52e61a40137";
         if (MENU_TEXTURE == "")
         llSetLinkPrimitiveParamsFast(0,[PRIM_TEXTURE,gFace,gTexture,gRepeats, ZERO_VECTOR, 0.]);
        {
            MENU_TEXTURE = "69737f61-1fdd-6033-7ea3-a52e61a40137";
        }
 
         llSetLinkPrimitiveParamsFast(0, [
            PRIM_TEXTURE, gFace, MENU_TEXTURE, UV_TEXTURE_REPEATS, ZERO_VECTOR, 0.0]);
     }
     }


     touch_start(integer total_number)
     touch_start(integer num_detected)
     {
     {
         if (llDetectedTouchFace(0) == gFace)
         integer face = llDetectedTouchFace(0);
 
        if (face == gFace)
         {
         {
             gOffsetUV = llGetTextureOffset(gFace);
             gOffsetUV = llGetTextureOffset(gFace);
             gStartST = llDetectedTouchST(0);
             gStartST = llDetectedTouchST(0);
             gStartST = <gStartST.x*gRepeats.x,gStartST.y*gRepeats.y,0.0>; //scale the touch vector to match our texture scale
 
            //  scale the touch vector to match our texture scale
             gStartST = <gStartST.x*UV_TEXTURE_REPEATS.x, gStartST.y*UV_TEXTURE_REPEATS.y, 0.0>;
         }
         }
       
     }
     }
   
 
     touch(integer total_number)
     touch(integer num_detected)
     {
     {
         if (llDetectedTouchFace(0) == gFace)
         integer face = llDetectedTouchFace(0);
 
        if (face == gFace)
         {
         {
            gEndST = llDetectedTouchST(0);
            //  scale the touch vector to match our texture scale
            gEndST = <gEndST.x*UV_TEXTURE_REPEATS.x, gEndST.y*UV_TEXTURE_REPEATS.y, 0.0>;


            gEndST = llDetectedTouchST(0);
             if (llVecDist(gStartST,gEndST) > 0.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);
                 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
                //  roll values between 0 and 1
                 newUV -= <llFloor(newUV.x), llFloor(newUV.y), 0.0>;
 
                // apply new texture offset
                 llSetLinkPrimitiveParamsFast(0, [
                    PRIM_TEXTURE, gFace, MENU_TEXTURE, UV_TEXTURE_REPEATS, newUV, 0.0]);
             }
             }
         }
         }
     }
     }
   
 
     touch_end(integer total_number)
     touch_end(integer num_detected)
     {
     {
         if (llDetectedTouchFace(0) == gFace)
         integer face = llDetectedTouchFace(0);
 
        if (face == gFace)
         {
         {
             if (llVecDist(gStartST,gEndST) > 0.); //compare mouse movement to see if texture was moved. maybe useful to do something here
             if (llVecDist(gStartST, gEndST) > 0.0)
             else //did not slide the texture. was a button press
            {
                // 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);
                 list list_data = FindUV(llDetectedTouchUV(0), BUTTONS_STRIDED_LIST);
 
                 //This next line is where we compare button labels
                 //This next line is where we compare button labels
                 if (llList2String(list_data,0) == "Button") llSay(0,llList2String(list_data,1));                              
                 if (llList2String(list_data, 0) == "Button")
                {
                    llSay(PUBLIC_CHANNEL, llList2String(list_data, 1));
                }
             }
             }
         }
         }
     }
     }
}
}
</lsl>
</source>


</div>
</div>
</div>
</div>

Latest revision as of 20:01, 24 January 2015

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

//  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

//  prim face you want the script to work on
integer gFace = 0;

//  the UV repeats of the texture. values smaller than one will show only part of the texture
vector UV_TEXTURE_REPEATS = <1.0, 1.0, 0.0>;

//  your menu texture
string MENU_TEXTURE = "69737f61-1fdd-6033-7ea3-a52e61a40137";

//  strided list to define button labels and values
//    - vector one is top left button corner
//    - vector two is bottom right button corner
//    - next two elements hold data for the buttons
//  elements in this example: LABEL, DATA
//  assigning a label helps to filter different button actions
//  not very helpful in our example but included anyway
list BUTTONS_STRIDED_LIST = [
    <0.0, 1.0, 0.0>,    <0.2, 0.8, 0.0>,    "Button",   "A-0",
    <0.2, 1.0, 0.0>,    <0.4, 0.8, 0.0>,    "Button",   "A-1",
    <0.4, 1.0, 0.0>,    <0.6, 0.8, 0.0>,    "Button",   "A-2",
    <0.6, 1.0, 0.0>,    <0.8, 0.8, 0.0>,    "Button",   "A-3",
    <0.8, 1.0, 0.0>,    <1.0, 0.8, 0.0>,    "Button",   "A-4",

    <0.0, 0.8, 0.0>,    <0.2, 0.6, 0.0>,    "Button",   "B-0",
    <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)
{
    vector cordA;
    vector cordB;
    integer lines = llGetListLength(lst) / 4;

    //  roll values between 0 and 1
    v -= <llFloor(v.x), llFloor(v.y), 0.0>;

    integer i;
    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 (MENU_TEXTURE == "")
        {
            MENU_TEXTURE = "69737f61-1fdd-6033-7ea3-a52e61a40137";
        }

        llSetLinkPrimitiveParamsFast(0, [
            PRIM_TEXTURE, gFace, MENU_TEXTURE, UV_TEXTURE_REPEATS, ZERO_VECTOR, 0.0]);
    }

    touch_start(integer num_detected)
    {
        integer face = llDetectedTouchFace(0);

        if (face == gFace)
        {
            gOffsetUV = llGetTextureOffset(gFace);
            gStartST = llDetectedTouchST(0);

            //  scale the touch vector to match our texture scale
            gStartST = <gStartST.x*UV_TEXTURE_REPEATS.x, gStartST.y*UV_TEXTURE_REPEATS.y, 0.0>;
        }
    }

    touch(integer num_detected)
    {
        integer face = llDetectedTouchFace(0);

        if (face == gFace)
        {
            gEndST = llDetectedTouchST(0);

            //  scale the touch vector to match our texture scale
            gEndST = <gEndST.x*UV_TEXTURE_REPEATS.x, gEndST.y*UV_TEXTURE_REPEATS.y, 0.0>;

            if (llVecDist(gStartST,gEndST) > 0.0)
            {
                 vector newUV = gOffsetUV + (gStartST - gEndST);

                 //  roll values between 0 and 1
                 newUV -= <llFloor(newUV.x), llFloor(newUV.y), 0.0>;

                //  apply new texture offset
                 llSetLinkPrimitiveParamsFast(0, [
                    PRIM_TEXTURE, gFace, MENU_TEXTURE, UV_TEXTURE_REPEATS, newUV, 0.0]);
            }
        }
    }

    touch_end(integer num_detected)
    {
        integer face = llDetectedTouchFace(0);

        if (face == gFace)
        {
            if (llVecDist(gStartST, gEndST) > 0.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), BUTTONS_STRIDED_LIST);

                //This next line is where we compare button labels
                if (llList2String(list_data, 0) == "Button")
                {
                    llSay(PUBLIC_CHANNEL, llList2String(list_data, 1));
                }
            }
        }
    }
}