//////////
// Slide Toggler
// Version 1.2
//
// Author: Peter R. Bloomfield (SL: Pedro McMillan)
// Web: http://www.avid-insight.co.uk
//
// If you want to present a few slides in Second Life, then you can rez each
//  one in relatively small form, and put this script in. The "front" of the slide
//  should be on the +X side.
//
// You can then click each slide to make it toggle forward to the foreground.
// Slide can be customized to automatically reset to background after a period of time.
// (Modify variable "resetTime" to adjust this).
// By default, background slides are faded and transparent.
// Foreground slides are full bright and opaque.
//
// Notes:
//  - the "foreground" size is always relative to the starting size.
//  - the "foreground" position is +X LOCAL of the start position (so you can rotate your slides however you like)
//  - can be customized to respond to owner-only, group-only, or anybody
//  - this is only really suitable for a small number of slides
//  - nearly everything can be customized using the variables below.
//
// WARNING: make sure your slides are at your desired "background" size when this script is added or recompiled.
//
//////////
//
// GPL:
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
//
//////////
 
 
///// DATA /////
 
// Number of seconds before foreground slide should automatically reset (0.0 for no auto reset)
float resetTime = 0.0;
 
// Amount to move forward by when activated (in metres)
float movementToForeground = 0.75;
// Amount to increase size by when activated (fraction of original size)
float scaleToForeground = 2.5;
 
// Colours when activated and deactivated
vector foregroundColour = <1.0, 1.0, 1.0>;
vector backgroundColour = <0.9, 0.9, 0.9>;
// Alpha values when activated and deactivated
float foregroundAlpha = 1.0;
float backgroundAlpha = 0.75;
 
// Access level:
//  0 = public (responds to anybody)
//  1 = group (responds to avatars in the same group, but they have to be in the same sim)
//  2 = owner (responds only to the owner)
integer accessLevel = 2;
 
 
///// FUNCTIONS /////
 
// Checks if the identified avatar is allowed to control this object.
// Returns TRUE if so or FALSE if not.
integer checkAccess( key av )
{
    // Always give the owner access
    if (av == llGetOwner()) return TRUE;
 
    // Public access
    if (accessLevel == 0) return TRUE;
    // Group access
    if (accessLevel == 1) return llSameGroup(av);
 
    return FALSE;
}
 
///// STATES /////
 
// In this state, the slide is small, in the background
default
{
    touch_start(integer total_number)
    {
        // Activate this slide if the owner touched us
        if (!checkAccess(llDetectedKey(0))) return;
        state focus;
    }
}
 
 
// In this state, the slide is enlarged in the foreground
state focus
{
    state_entry()
    {        
        // Go to full visibility
        llSetPrimitiveParams([PRIM_FULLBRIGHT, ALL_SIDES, TRUE, PRIM_COLOR, ALL_SIDES, foregroundColour, foregroundAlpha]);
 
        // Scale up
        vector bgscale = llGetScale();
        vector fgscale = <bgscale.x, bgscale.y * scaleToForeground, bgscale.z * scaleToForeground>;
        llSetScale(fgscale);
        // Move forward
        vector bgpos = llGetPos();
        vector fgpos = llGetPos() + (<movementToForeground, 0.0, 0.0> * llGetRot());
        llSetPos(fgpos);
 
        llSetTimerEvent(resetTime);
    }
 
    state_exit()
    {
        llSetTimerEvent(0.0);
 
        // Go to reduced visibility
        llSetPrimitiveParams([PRIM_FULLBRIGHT, ALL_SIDES, FALSE, PRIM_COLOR, ALL_SIDES, backgroundColour, backgroundAlpha]);
 
        // Scale down
        vector fgscale = llGetScale();
        vector bgscale = <fgscale.x, fgscale.y / scaleToForeground, fgscale.z / scaleToForeground>;
        llSetScale(bgscale);
        // Move back
        vector fgpos = llGetPos();
        vector bgpos = llGetPos() - (<movementToForeground, 0.0, 0.0> * llGetRot());
        llSetPos(bgpos);
    }
 
    touch_start(integer total_number)
    {
        // Deactivated this slide if the owner touched us
        if (!checkAccess(llDetectedKey(0))) return;
        state default;
    }
 
    timer()
    {
        state default;
    }
}