Save layout

Save layout
So, I’m fully back again…and with a new script!
This is something I started time ago, and I have been improving it adding rotation, custom velocity, and more. Now it’s time to publish it, but of course you can suggest improvements.
This script is something visually beautiful, perhaps a bit slow with too much items, but even I am impress with the final result. It saves the position, size, scale and rotation of the items in free mode, and restore them when launched moving them smoothly (more or less).
I’m good at scripting, but no so good at creating layouts. If you are one of the masters that creates awesome layouts I let you create one with this script, that would be awesome.
It is more than just some lines of code, so I wrote a description after the config. If you want to share a layout with the script the only thing I ask you is to leave those comments. You can modify it if you want (and know).
Instructions:
The script will save the data of the items in the tag of the item/container.
How to move the items: just launch the script from the item with the data (click, swipe…) or even from a container (tap, long tap…)
How to save the data: Launch the script from the scripts menu (LL pop-up –> scripts) and choose the velocity (the first time you can simply just executing normally).
Characteristics:
If the item have a different tag it will ask before save.
Items in grid mode won’t be saved, and also won’t be moved even if they were saved while in free mode.
Rotation works from the center of the item (something obvious, but not script-side)
Debug mode to see if an item is stuck in the animation.
When the launcher lost focus, items are automatically moved to the final position and the script finishes.
If you start a different instance of the script while another one is running, the previous one stops automatically.
Problems:
Items with skew can behave wrong.
———————-
The script is in the first comment. Just click it and copy all.
Tell me if you prefer this way.
]]>
< ![CDATA[
var defvelocity = 0.2;//The default velocity. The bigger, the faster (between (0,1] warning: outside that range it can crash)
var frecuency = 60;//ticks per second [Recommended 60] (set to 0 for fastest possible, in case you have a lot of items)
/*
Script made by TrianguloY you can contact me in Google+
This script will move the items in free mode to the position they had when saved.
To save the position just place the items where you want in free mode and run this script from the scripts menu.
The data is saved in the tag of the item/container.
modified: no, original script
(change this if you want to modify something and publish it)
*/
var debug = false;
//vars
var event = LL.getEvent();
var cont = event.getContainer();
var item = event.getItem();
var tagged=item||cont;
var source=event.getSource();
var tag = tagged.getTag();
var data=JSON.parse(tag);
//save mode
if(source==”MENU_ITEM” || source==”MENU_APP” || tag==undefined || data[0][0]!=’save layout’){
save();
return;
}
//token to stop if another instance is running
var sessiontoken;
do{
sessiontoken=””+Math.random();
}while(sessiontoken==LL.getScriptTag());
LL.setScriptTag(sessiontoken);
var velocity = data[0][1]||defvelocity;
//execute in each item
for(var i=data.length-1;i>0;–i){
var item= LL.getItemById(data[i][0]);
//move the item to their position if in free mode
if(item!=null && item.getCell()==null) {
if(debug)item.setLabel(“@”+item.getLabel());
move(item, data[i]);
}
}
return;
//save function
function save(){
//initial checks
var vel=defvelocity;
if(data!=null && data[0][0]!=’save layout’) {
alert(“Diferent tag found:\n”+data);
}else{
if(data!=null)vel=data[0][1];
}
var vel=prompt(“Save?\n Velocity: the bigger the faster\nbetween (0,1]”,vel);
if(vel==null)return;
vel=vel>1?1:vel< =0?defvelocity:vel;
//data storing procces
data=[];
data[0]=[“save layout”,vel];
var i = 1;
var items = cont.getItems();
for(var j=items.getLength()-1;j>=0;–j){
//save the data of each item
var t = items.getAt(j);
//avoid grid items
if(t.getCell()!=null)continue;
data[i]=[];//data of the item (in rows to see where each one is stored)
data[i][1]=t.getPositionX();
data[i][2]=t.getPositionY();
data[i][3]=t.getScaleX();
data[i][4]=t.getScaleY();
data[i][5]=t.getWidth();
data[i][6]=t.getHeight();
data[i][7]=t.getRotation();
//convert to the center of the item
var c=center(data[i]);
data[i][1]+=c[0];
data[i][2]+=c[1];
data[i][0]=t.getId();
++i;
};
//final save
tagged.setTag(JSON.stringify(data));
Android.makeNewToast(“Saved “+(i-1)+” item’s data in “+tagged,true).show();
}
//custom lerp. It moves the item a bit and repeat, stops when finish, when LL is not active or when another instance of the script runs
function move(item,dat){
//another script is running
if( sessiontoken!=LL.getScriptTag()){return;}
//actual data
var now = [
0,
item.getPositionX(),
item.getPositionY(),
item.getScaleX(),
item.getScaleY(),
item.getWidth(),
item.getHeight(),
item.getRotation()
];
var n=center(now);
now[1]+=n[0];
now[2]+=n[1];
//calculate the next step
var cut=[0,0.5,0.5,0.01,0.01,1,1,0.99];
var step = [];
var flag = “”;
for(var j=dat.length-1;j>0;–j){
step[j]=dat[j]-now[j];
if(Math.abs(step[j])>cut[j]){
flag+=j;
if (cut[j]==1){
step[j]=now[j]+(step[j]>0?Math.max(step[j]*velocity,cut[j]):Math.min(step[j]*velocity,-cut[j]));
}else step[j]=now[j]+step[j]*velocity;
}else step[j]=dat[j];
}
//if nothing changed or LL paused
if( flag==”” || LL.isPaused()){
item.setScale(dat[3],dat[4]);
item.setSize(dat[5],dat[6]);
item.setRotation(dat[7]);
var d = center(dat);
item.setPosition(dat[1]-d[0],dat[2]-d[1]);
if(debug)item.setLabel(item.getLabel());
return;
}
if(debug)item.setLabel(flag+”@”+item.getLabel());
//sets the next step and repeat
item.setSize(step[5],step[6]);
item.setScale(step[3],step[4]);
item.setRotation(step[7]);
var s=center(step);
item.setPosition(step[1]-s[0],step[2]-s[1]);
velocity=(1+999*velocity)/1000;
setTimeout(function(){ move(item,dat);},frecuency==0?0:1000/frecuency);
}
//custom function to get the center of the item d=[,,,sizx,sizy,scax,scay,rot]
function center(d){
var c=[];
var r=d[7]*Math.PI/180; c[0]=Math.abs(d[4]*d[6]*Math.sin(r))/2+Math.abs(d[3]*d[5]*Math.cos(r))/2;
c[1]=Math.abs(d[3]*d[5]*Math.sin(r))/2+Math.abs(d[4]*d[6]*Math.cos(r))/2;
return c;
}
]]>
< 
]]>
< ![CDATA[
Missing in the wiki, or am I blind?
]]>
< 
I need to take my time (and be in front of the computer! )
]]>