User Tools

Site Tools


script_interpolation_lagrange

====== About the script ====== * Purpose : This script moves the items of the desktop based on Lagrange interpolation. * Author : [[https://plus.google.com/+TrianguloY|TrianguloY]] * Link: https://plus.google.com/+TrianguloY/posts/6pqgDR3nNex (with video with instructions) ====== How to use the script ====== You specify data of an item in some pages and it will move smoothly from one to another. It is made as efficient as possible. No saved data is not counted, and even with so much pages saved it should be unnoticeable. (Of course this depends on the device) To use it only set it in the position change event of the desired container. Then save the data of each item (one by one, sorry) as seen in the video. (check the link before) ====== Script code ====== <sxh javascript> var extrapolate=false;//set this to true to extrapolate //clases LL.bindClass("android.app.AlertDialog"); LL.bindClass("android.content.DialogInterface"); //vars var event=LL.getEvent(); var item=event.getItem(); var cont=event.getContainer(); var source=event.getSource(); //the list of parameters var parameters = ["Position X","Position Y","Rotation","Scale X","Scale Y","Alpha","Width","Height"]; //vars related to the interpolation data var data=JSON.parse(cont.getTag("intpol"))||{}; var d; var page; var flags=[]; if(source=="C_POSITION_CHANGED"){ //interpolate apply(); }else if(item!=null){ //apply settings for the item //create the object that will contain the data d=(data[item.getId()])||{flags:[],lagrange:[],pages:[]}; while(d.flags.length<parameters.length)d.flags.push(false); while(d.pages.length<parameters.length)d.pages.push({}); //the list of properties for(var t in d.flags)flags[t]=d.flags[t]; choose(); }else{ //no item alert("run the script from an item"); //prompt("",cont.getTag("intpol"));//for debug } function choose(){ //shows the dialogs //asks for the page (selected: current) var w=cont.getWidth(); var x=cont.getPositionX(); var b=cont.getBoundingBox(); page=w*LL.pickNumericValue("save for which page? (horizontally, 0 is the main page)", Math.round(x/w), "FLOAT", Math.floor(b.getLeft()/w)-1, Math.ceil(b.getRight()/w), 1, "page"); if(page==null)return; //parameters var builder=new AlertDialog.Builder(LL.getContext()); builder.setTitle("Choose parameters to save/remove"); builder.setMultiChoiceItems(parameters,flags,new DialogInterface.OnMultiChoiceClickListener(){onClick:function(dialog,which,checked){flags[which]=checked;}}); builder.setNegativeButton("Cancel",null); builder.setPositiveButton("Save",new DialogInterface.OnClickListener(){onClick:save}); builder.setNeutralButton("Remove",new DialogInterface.OnClickListener(){onClick:remove}); builder.create().show(); } function save(){ //save the data of the selected parameters if(flags[0])d.pages[0][page]=item.getPositionX(); if(flags[1])d.pages[1][page]=item.getPositionY(); if(flags[2])d.pages[2][page]=item.getRotation(); if(flags[3])d.pages[3][page]=item.getScaleX(); if(flags[4])d.pages[4][page]=item.getScaleY(); if(flags[5])d.pages[5][page]=item.getProperties().getInteger("i.alpha"); if(flags[6])d.pages[6][page]=item.getWidth(); if(flags[7])d.pages[7][page]=item.getHeight(); update(); } function update(){ //create the polynomial for each animated parameter for(var t=0;t<d.flags.length;++t){ difdiv(d.pages[t],Object.keys(d.pages[t]).sort(function(a,b){return parseInt(a)-parseInt(b);}),t); } //save the data data[item.getId()]=d; cont.setTag("intpol",JSON.stringify(data)); } function remove(){ //removed the data of the selected parameters for(var t=0;t<flags.length;++t){ if(flags[t]) delete d.pages[t][page]; } update(); } function apply(){ //sets the data when moving the desktop var ids=Object.keys(data); for(var t=0;t<ids.length;++t){ var item=LL.getItemById(ids[t]); if(item==null){ //if a saved item is not found, removes the data of that item delete data[ids[t]]; cont.setTag("intpol",JSON.stringify(data)); continue; } //apply the interpolation d=data[ids[t]]; if(d.flags[6]||d.flags[7])item.setSize(d.flags[6]?intpol(6):item.getWidth(),d.flags[7]?intpol(7):item.getHeight()); if(d.flags[2])item.setRotation(intpol(2)); if(d.flags[3]||d.flags[4])item.setScale(d.flags[3]?intpol(3):item.getScaleX(),d.flags[4]?intpol(4):item.getScaleY()); if(d.flags[0]||d.flags[1])item.setPosition(d.flags[0]?intpol(0):item.getPositionX(),d.flags[1]?intpol(1):item.getPositionY()); if(d.flags[5])item.getProperties().edit().setInteger("i.alpha",intpol(5,0,255)).commit(); } } function intpol(prop,min,max){ //fixed function: based of the saved polynomial returns the value var c=cont.getPositionX(); var index=d.lagrange[prop]; var n=index[0].length-1; if(!extrapolate){ if(c>index[1][n]) return d.pages[prop][index[1][n]]; if(c<index[1][0]) return d.pages[prop][index[1][0]]; } var out=index[0][n]; for(var t=n-1;t>=0;--t){ out=index[0][t]+(c-index[1][t])*out; } if(min!=null&&out<min)return min; if(max!=null&&out>max)return max; return out; } function difdiv(values,nodes,prop){ //fixed function: calculates the data of the interpolation polynomial based on the saved data var y=[[]]; var n=nodes.length; if(n<=0){ delete d.lagrange[prop]; d.flags[prop]=false; }else{ d.flags[prop]=true; } for(var t=0;t<n;++t){ y[t]=[]; y[t][0]=values[nodes[t]]; } for(var k=1;k<n;++k)for(var i=0;i<n-k;++i){ y[i][k]=(y[i+1][k-1]-y[i][k-1])/(nodes[i+k]-nodes[i]); } if(d.lagrange[prop]==null)d.lagrange[prop]=[]; d.lagrange[prop][0]=y[0]; d.lagrange[prop][1]=nodes; } /* data:{ <id>:{ flags:[,] //will animate or not lagrange:[property]:{ [0]: d of the polynomial [1]: nodes } pages:[property]{ [page] value of that property at the current page } } } */ </sxh>

script_interpolation_lagrange.txt · Last modified: 2018/08/14 16:58 by f43nd1r