select and launch
select and launch
This is a small script that makes tap to select, and tap again to launch.
As seen in the video with the first click the item is selected, and when clicking again the item is launched.
If you click the background, the selected item, if any, is deselected.
And, if you click an item while another one is selected, the ‘selector’ indicator moves smoothly (more or less).
A nine patch is highly recommended!! This is the sample one used in the video https://www.dropbox.com/s/xmat6pf1vy6mt5c/selectable.9.png?dl=0
Note: the original script was very simple, so I added a wizard to help in the installation process…and to make the script a bit more complex 😛
So you only need to run the script in the container (remember to check the lightning menu checkbox in the script editor) and follow the instructions.
The same to uninstall it.
]]>
< ![CDATA[
//config
var velocity = 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)
//name
var name=”selectable”;
//tags
var tag=”selectable”
//classes
LL.bindClass(“android.app.AlertDialog”);
LL.bindClass(“android.content.DialogInterface”);
//vars
var e=LL.getEvent();
var i=e.getItem();
var c=e.getContainer();
var script=LL.getCurrentScript();
var t=c.getTag(tag);
if(c==null){
//no container, just exit
Android.makeNewToast(“no container”, true).show()
return
}
//more vars
var s=c.getItemByName(name);
var bools;//global variable used in the installation
if(e.getSource()==”MENU_APP”){
//script run manually
if(t==null){
install();
}else{
repair();
}
return
}
if(s==null){
//no selectable item
if(confirm(“warning, no selectable item found. Do you want to begin the installation?”))install();
return
}
if(i==null){
//no item selected, deselecting
s.setVisibility(false);
c.setTag(tag,-1);
return;
}
if(i.getId()==t){
//launch item
i.launch();
s.setVisibility(false);
c.setTag(tag,-1);
}else{
//select item
c.setTag(tag,i.getId());
//always above everything
var max=c.getItems().getLength()-1;
if(c.getItemZIndex(s.getId())!=max)c.setItemZIndex(s.getId(),max);
if(s.isVisible()){
//move with animation
//token to stop if another instance is running
var sessiontoken;
do{
sessiontoken=””+Math.random();
}while(sessiontoken==script.getTag());
script.setTag(sessiontoken);
var debug=false;
var dat=[0,i.getPositionX(),i.getPositionY(),i.getScaleX(),i.getScaleY(),i.getWidth(),i.getHeight(),i.getRotation()];
var n=center(dat);
dat[1]+=n[0];
dat[2]+=n[1];
move(s,dat);
}else{
//move without animation
s.setSize(i.getWidth(),i.getHeight());
s.setScale(i.getScaleX(),i.getScaleY());
s.setRotation(i.getRotation());
s.setPosition(i.getPositionX(),i.getPositionY());
s.setVisibility(true);
}
}
//will ask to choose between install or uninstall
function repair(){
var builder = new AlertDialog.Builder(LL.getContext());
builder.setTitle(“What do you want to do?”);
builder.setCancelable(true);
builder.setPositiveButton(“Uninstall”,new DialogInterface.OnClickListener(){
onClick:function(dialog,which){
dialog.dismiss();
setTimeout(uninstall,0);
}
});
builder.setNeutralButton(“Repair”,new DialogInterface.OnClickListener(){
onClick:function(dialog,which){
dialog.dismiss();
setTimeout(install,0);
}
});
builder.setNegativeButton(“Cancel”,null);
builder.show();
}
//will install/uninstall
function uninstall(){install(true)};
function install(un){
if(!confirm(“Welcome to the ‘selectable item’ “+(un?”uninstallation”:”installation”)+” process\nThis will prepare this “+c.getType()+” (“+c+”)\n Do you want to continue?”)) return;
var act=un?”Revert to unset”:”Set to launch this script”;
var installs=[act+” the default tap action”,act+” the tap background action”];
bools=[true,true,false];
if(s==null&&!un){
installs.push(“Create selectable item”);
bools[2]=true;
}
if(s!=null&&un){
installs.push(“Remove selectable item”);
bools[2]=true;
}
var builder = new AlertDialog.Builder(LL.getContext());
var listener = new DialogInterface.OnMultiChoiceClickListener(){
onClick:function(dialog,which,isChecked){
bools[which]=isChecked;
}
};
builder.setMultiChoiceItems(installs,bools,listener);
builder.setTitle(“Choose things to do”);
builder.setCancelable(true);
builder.setPositiveButton((un?”Uninstall”:”Install”),new DialogInterface.OnClickListener(){
onClick:function(dialog,which){
dialog.dismiss();
installSelected(un);
}
});
builder.setNegativeButton(“Exit”,null);
builder.show();
}
//will do the things chosen in the installation
function installSelected(un){
if(!(bools[0]||bools[1]||bools[2])){
Android.makeNewToast(“Nothing done”, true).show();
return;
}
c.setTag(tag,(un?null:-1));
LL.save()
var prop=c.getProperties().edit();
if(bools[0]){
if(un)prop.setEventHandler(“i.tap”,EventHandler.UNSET,null)
else prop.setEventHandler(“i.tap”,EventHandler.RUN_SCRIPT,script.getId())
}
if(bools[1]){
if(un)prop.setEventHandler(“bgTap”,EventHandler.UNSET,null)
else prop.setEventHandler(“bgTap”,EventHandler.RUN_SCRIPT,script.getId())
}
prop.commit();
if(bools[2]){
if(un){
c.removeItem(c.getItemByName(name));//get the item again because commit reloads the container
}else{
s=c.addShortcut(“Selectable item. It is recommended to set a nine patch as background and hide the label”,new Intent(),e.getTouchX(),e.getTouchY());
//properties
s.setName(name);
s.getProperties().edit()
.setBoolean(“i.onGrid”,false)
.setBoolean(“s.iconVisibility”,false)
.setBoolean(“i.enabled”,false)
.setInteger(“s.labelMaxLines”,10)
.commit();
//default background image
var im=LL.createImage(100,100);
var p=new Paint();
p.setColor(0xffffffff);
p.setStyle(Paint.Style.STROKE);
p.setStrokeWidth(5);
im.draw().drawRect(0,0,100,100,p);
s.setBoxBackground(im,”n”,true)
}
}
Android.makeNewToast((un?”Uninstallation”:”Installation”)+” completed.\nPlease restart your device…”,true).show();
Android.makeNewToast(“…just joking :P”,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!=script.getTag()){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[
That’s pretty cool…
]]>
< ![CDATA[
Good idea. Like in Windows ☺
]]>