Hi again folks.
1-Prim Book Page REVISED Mar.23.2012,
( see next posting for 3-Prim Book )Please Note:The scripts in this posting work in OpenSim Master Dev and I have not tested them in SL but they should work without issue or incident. They are written & commented in such a fashion as to help assist people to learn from & extend for their own use. As a result of being written in this fashion, some optimizations and tweaks that are possible have been left out for the purposes of readability & understanding.
Someone a few days ago asked me to adjust a book script.... after looking at that and several others avalable out there, I decided to make another one. This one works fairly straight forward and no modifying of the script is required for use. Edit a notecard and away you go.
Quick Description:
- responds to touch, opens / closes book
-- if open, touch the page face to change pages forward/backward
- pre-caches the next 2 or previous 2 pages to reduce rez time of texture.
- can be set to Owner Only use in script (only thing to edit there if needed)
All the details are in the script header & the notecard header.
1-prim-book.lslCode:
// ----------------------------------------------------------------
// Script Title: 1-Prim Book
// Created by: WhiteStar Magic
// Creation Date: Feb.06.2012
// Platforms:
// SecondLife: Not Tested
// OpenSim: Yes
//
// Revision: V.0.3
// Revision History:
//
// Origination:
// Initially Based loosely off the Improved book script Script @ http://forums.osgrid.org/viewtopic.php?f=5&t=2400
//
// Revision Contributors:
// WhiteStar V.0.3 March.23.2012
// - small mod, adding BLANK_TEXTURE @ end of pages list IF the list is an odd number
// corrects for the last page not being shown properly if odd pages in list
// WhiteStar V.0.2
// - Streamlined page handling
// - Divided pages up so each inside face holds a full texture
// - Modified texture pre-caching to use "spine & bottom" edges to preload up coming textures
// - added facility to use front & back covers
// - add states for "run" and "GET_NOTECARDS"
// - Installed Notecard reader facility to eliminate script from having to be edited.
// - Size= allows use of REG, LG & XLG book sizes depending on material
// - CoverFR= for front Cover entry into NoteCard
// - CoverBK= for back Cover entry into NoteCard
// - Page= to specify the pages. 1 per line using name(if contained in prim) or UUID, in sequential order. Page-1, Page-2 Page-3 etc
//
// ================================================================
// ** SCRIPT NOTES **
//
// drop script & notecard into a BOX prim and it will set up.
// edit the Book.CFG and add the texture UUID's or Texture Names if contained in prim.
// -- sit back & enjoy your new book.
//================================================================
// ** Licence **
// !Attribution-NonCommercial-ShareAlike 3.0 Unported (CC BY-NC-SA 3.0)
// http://creativecommons.org/licenses/by-nc-sa/3.0/
//================================================================
// adjust below to make book only respond to owner
integer OWNER_ONLY = FALSE; // default FALSE allows anyone to use book
//
// ====================================== \\
// Make changes below at your own peril ! \\
// ====================================== \\
//
// Global Variables
vector sz_open; // open & closed changed in notecard reader
vector sz_closed;
vector sz_reg_o = <0.050,1.00,0.75>; // reg sizes
vector sz_reg_c = <0.025,0.50,0.75>;
vector sz_lg_o = <0.050,1.50,1.00>; // lg sizes
vector sz_lg_c = <0.025,0.75,1.00>;
vector sz_xlg_o = <0.050,3.00,2.00>; // xlg sizes
vector sz_xlg_c = <0.025,1.50,2.00>;
//
list pcut_close = [PRIM_TYPE, PRIM_TYPE_BOX, 0, <0.0,1.0,0.0>, 0.0, <0.0,0.0,0.0>, <1.0,1.0,1.0>, <0.0,0.0,0.0>];
list pcut_open = [PRIM_TYPE, PRIM_TYPE_BOX, 0, <0.0,0.700,0.0>, 0.0, <0.0,0.0,0.0>, <1.0,1.0,1.0>, <0.0,0.0,0.0>];
// FR=Front, BK=Back covers. from notecard. UUID or TextureName if contained
string cover_FR;
string cover_BK;
//
string bookstate = "closed";
string bookconfig = "Book.CFG";
key NChandle = NULL_KEY;
integer line = 0;
//
list Lst_pages; // page= data from notecard (uuid or name)
integer page_qty; // # of pages in Lst_pages
integer page_crnt = 0;
integer page_next = 1;
//
integer tLT = 6; // Left page Touchface
integer tRT = 5; // Right page Touchface
float timer_delay = 5.0;
//
// ==================== \\
// SCRIPT STARTS HERE ! \\
//======================\\
set_pages() // set textures for both inner pages
{
key Lt_pg = (key)llList2String(Lst_pages,page_crnt); // ODD Pages on left
key Rt_pg = (key)llList2String(Lst_pages,(page_crnt+1)); // EVEN pages on Right
llSetPrimitiveParams([PRIM_TEXTURE, 5, Rt_pg, <1.0,1.0,0.0>, <0.0,0,0>, 0.0]); // Right Face: full image
llSetPrimitiveParams([PRIM_TEXTURE, 6, Lt_pg, <1.0,1.0,0.0>, <0.0,0,0>, 0.0]); // Left Face: full image
}
pre_cache()
{
llSetTimerEvent(0.0); // do this only once
if(bookstate=="closed") return;
key oddcache=llList2Key(Lst_pages,page_next+1);
key evencache=llList2Key(Lst_pages,page_next+2);
//llSetColor(<0,0,0>, 3); // colour over left face
llSetTexture(oddcache,3); // left face edge (spine) when open
//llSetColor(<0,0,0>, 4); // colour over bottom face
llSetTexture(evencache,4); // bottom face edge when open
}
openbook()
{
bookstate="open";
llSetPrimitiveParams([PRIM_SIZE, sz_open]); // set new size
llSetPrimitiveParams(pcut_open); // set the cut params
llSetPrimitiveParams([PRIM_TEXTURE, 2, cover_BK, <2.0,1.0,0.0>, <0.50,0,0>, 0.0]); // set back cover & scale for display on new prim form
}
closebook()
{
bookstate="closed";
page_crnt=0;
page_next=1;
//
llSetPrimitiveParams([PRIM_SIZE, sz_closed]);
llSetPrimitiveParams(pcut_close);
llSetPrimitiveParams([PRIM_TEXTURE, 4, cover_FR, <1.0,1.0,0.0>, <0.0,0,0>, 0.0]); // Front Cover
llSetPrimitiveParams([PRIM_TEXTURE, 2, cover_BK, <1.0,1.0,0.0>, <0.0,0,0>, 0.0]); // Back Cover, rescaled for prim form
//
//llSetColor(<1.0,1.0,1.0>, 4); // re-colour cover white IF bottom face edge was blackened in pre_cache()
//
llSetTexture(TEXTURE_BLANK,0); // top face edge
llSetTexture(TEXTURE_BLANK,1); // rigt side edge
llSetTexture(TEXTURE_BLANK,3); // left side edge (spine)
llSetTexture(TEXTURE_BLANK,5); // bottom face adge
}
// START //
default
{
state_entry()
{
state GET_NOTECARDS;
}
}
// RUNNING //
state run
{
state_entry()
{
closebook();
llOwnerSay("[ "+ llGetListLength(Lst_pages)+" ] Pages loaded. Ready, click on cover to read");
page_qty = llGetListLength(Lst_pages);
float d = ((float)page_qty / 2);
integer e = (page_qty / 2);
if((float)d != (float)e) {Lst_pages += [TEXTURE_BLANK]; page_qty = llGetListLength(Lst_pages);} // ODD # so add BLANK TEXTURE to make EVEN #
}
touch_start(integer num_times)
{
if(OWNER_ONLY && llDetectedKey(0)!=llGetOwner()) return; // only owner is set, to ignore others
//
if(bookstate=="closed")
{
openbook();
set_pages();
pre_cache();
}
else if(bookstate=="open")
{
if(llDetectedTouchFace(0) == tRT) // RTface touched
{
page_crnt=page_crnt + 2;
if(page_crnt>(page_qty-2))
{
closebook();
}
else
{
set_pages();
page_next = page_crnt+1;
}
if ( page_next < llGetListLength(Lst_pages) )
{
llSetTimerEvent(timer_delay);
}
else llSetTimerEvent(0.0);
}
else if(llDetectedTouchFace(0) == tLT) // LTface touched
{
page_crnt=page_crnt - 2;
if(page_crnt< 0)
{
closebook();
}
else
{
set_pages();
page_next = page_crnt-1;
}
if ( page_next < llGetListLength(Lst_pages) )
{
llSetTimerEvent(timer_delay);
}
else llSetTimerEvent(0.0);
}
}
}
timer()
{
pre_cache(); // preload next page after a delay
}
changed(integer change)
{
if (change & CHANGED_INVENTORY)
{
llOwnerSay("The inventory changed. Restarting book");
llResetScript();
}
if (change & 1024) llResetScript();
}
}
//-------------------\\
// Get Notecard Data \\
//-------------------\\
state GET_NOTECARDS
{
state_entry()
{
line = 0;
NChandle = llGetNotecardLine(bookconfig, 0);
}
dataserver(key query_id, string data)
{
if(query_id == NChandle) // Filtering the call
{
if (data == EOF)
{
state run;
}
else
{
data = llStringTrim(data, STRING_TRIM_HEAD);
if (llGetSubString (data, 0, 0) != "#") // If it's # skip the line, it's a comment
{
integer s = llSubStringIndex(data, "=");
if(~s) // does it have an "=" in it? if YES it's Valid!
{
string token = llToLower(llStringTrim(llDeleteSubString(data, s, -1), STRING_TRIM)); // trim up token & lower case it
data = llStringTrim(llDeleteSubString(data, 0, s), STRING_TRIM);
if(token == "size") // Set Sizing
{
if(llToLower(data)=="reg")
{
sz_closed = sz_reg_c;
sz_open = sz_reg_o;
}
else if(llToLower(data)=="lg")
{
sz_closed = sz_lg_c;
sz_open = sz_lg_o;
}
else if(llToLower(data)=="xlg")
{
sz_closed = sz_xlg_c;
sz_open = sz_xlg_o;
}
}
if (token == "coverfr") // Set Front Cover
{
if(data=="") llOwnerSay("CoverFR= is not identified in "+bookconfig+" No Front Cover will be shown");
else cover_FR = data;
}
else if (token == "coverbk") // Set Back Cover
{
if(data=="") llOwnerSay("CoverBK= is not identified in "+bookconfig+" No Back Cover will be shown");
else cover_BK = data;
}
else if (token == "page")
{
if(data !="")
{
//llOwnerSay("The Data is: "+(string)data);
Lst_pages += [data];
}
else llOwnerSay("Loading Page= from "+bookconfig+" NoteCard: Data is Empty ! No Pages Available");
}
}
}
NChandle = llGetNotecardLine(bookconfig,++line);
}
}
}
}
Book.CFG notecardCode:
# 1-Prim Book V.0.2 Configuration File
# by WhiteStar Magic Feb.06.2012
# the # symbol is a COMMENT. These lines are ignored
#
# size= reg, lg, xlg
# reg = (0.50m W x 0.75m H) closed -- (1.00m W x 0.75m H)
# lg = (0.75m W x 1.00m H) closed -- (1.50m W x 1.00m H)
# xlg = (1.50m W x 2.00m H) closed -- (3.00m W x 2.00m H)
#
# CoverFR= Front Cover of book. Texture UUID or ( Texture Name if contained in book prim. )
# CoverBK= Back Cover of book. Texture UUID or ( Texture Name if contained in book prim. )
#
# Page= Texture UUID or ( Texture Name if contained in book prim. )
# NOTE: Pages must be in sequence, 1,2,3,4,5 etc....
# SPECIAL NOTE !!!
# IF using UUID then DO NOT INCLUDE <> Just the UUID Only
# IF using Texture Name place the name in "quotes" DO NOT INCLUDE <>
# ============================================
size=reg
CoverFR=<uuid or texture name>
CoverBK=<uuid or texture name>
#
## insert your pages below, in sequence from 1,2,3,4 etc
#
Page=<uuid or texture name>
Page=<uuid or texture name>
Page=<uuid or texture name>
Page=<uuid or texture name>
Enjoy, Good Luck & Have FUN !