User Tools

Site Tools


script_gesture_launcher

====== About the script ====== * Purpose : This script is will use an item as gesture detection area, so you can launch any shortcut by a gesture! * Author : [[https://plus.google.com/+LukasMorawietz|LM13]] * Current Version : 1.1 * Link (with video): https://plus.google.com/115366157037831519359/posts/ZzXD1hKNrf2 ====== Changelog ====== * Version 1.0 (31/8/2014): initial release in wiki * Version 1.1 (2/9/2014): More stable loading, First gesture Bug fix (hopefully) ====== How to use the script ====== * create a shortcut and hide it's label and icon * Resize it to the wished drawport size * set this script to the Touchevent * enable it in item menu * Change the configuration to your data, especially the itemID * to add or change a gesture: launch this script on the shortcut you want to assign a gesture to, then draw the new gesture. * to remove a gesture: delete the shortcut to which the gesture was assigned ====== Issues and hints ====== * With too much gestures it may stop working * the first gesture after LL started may be not fully drawn or not detected. * if more than one gesture is matching, one is selected by database, so avoid similar gestures * straight horizontal or vertical lines may not work as gesture. //please report all bugs in the g+ community// ====== Script ====== <sxh javascript;>//config var tolerance=7; var itemID=0x060029; var bgColor=0x10ffffff; var gestureColor=0xffffffff; //endconfig if(typeof add==='undefined')add=null; try{ switch(event.getAction()) { case MotionEvent.ACTION_DOWN: paint=new Paint(Paint.ANTI_ALIAS_FLAG); paint.setColor(gestureColor); paint.setStrokeWidth(5); paint.setStrokeJoin(Paint.Join.ROUND); image=item.getBoxBackground("n"); if(image.getWidth()!=item.getWidth()||image.getHeight()!=item.getHeight())image=LL.createImage(item.getWidth(),item.getHeight()); canvas=image.draw(); image.getBitmap().eraseColor(Color.TRANSPARENT); canvas.drawColor(bgColor); this.gesture=[]; this.gesture.push([event.getX(),event.getY()]); image.update(); item.setBoxBackground(image,"n"); done=false; setTimeout(loadGestures,0); break; case MotionEvent.ACTION_MOVE: this.gesture.push([event.getX(),event.getY()]); canvas.drawLine(this.gesture[this.gesture.length-2][0],this.gesture[this.gesture.length-2][1],event.getX(),event.getY(),paint); image.update();item.setBoxBackground(image,"n"); break; case MotionEvent.ACTION_UP: this.gesture.push([event.getX(),event.getY()]); canvas.drawLine(this.gesture[this.gesture.length-2][0],this.gesture[this.gesture.length-2][1],event.getX(),event.getY(),paint); while(!done); if(add!=null) { gestures.push([scaleGesture(this.gesture),add]); item.setTag(JSON.stringify(gestures)); add=null; Android.makeNewToast("Saved!",true).show(); } else { var sg=scaleGesture(this.gesture); schleife:for(var x=0;x<gestures.length;x++) { var sp=gestures[x][0]; var gesDiff=0; for(var i=0;i<sg.length;i++) { var p0=sg[i]; var diff=200; for(var a=Math.round((i/sg.length)*sp.length)-Math.round(sp.length/4)<0?0:Math.round((i/sg.length)*sp.length)-Math.round(sp.length/4);a<=Math.round((i/sg.length)*sp.length)+Math.round(sp.length/4)&&a<sp.length;a++) { var p1=sp[a]; var newDiff=Math.sqrt(Math.pow(p0[0]-p1[0],2)+Math.pow(p0[1]-p1[1],2)); if(newDiff<diff) { diff=newDiff; } } if(diff>tolerance*4) { continue schleife; } gesDiff=gesDiff+diff; } if(gesDiff/sg.length<tolerance) { break; } } if(x==gestures.length)Android.makeNewToast("No gesture detected",true).show(); else LL.startActivity(LL.getItemById(gestures[x][1]).getIntent()); } image.getBitmap().eraseColor(Color.TRANSPARENT); canvas.drawColor(bgColor); image.save(); item.setBoxBackground(image,"n"); break; } } catch(NullPointerException) { item=LL.getItemById(itemID);gestures=JSON.parse(item.getTag())||[]; for(var a=0;a<gestures.length;a++) { if(LL.getItemById(gestures[a][1])==null)gestures.splice(a,1); } event=LL.getEvent(); if(event.getItem().getType()!="Shortcut"&&event.getItem().getType()!="Folder") { Android.makeNewToast("Error: Item has no Intent",true).show(); return; } for(var a=0;a<gestures.length;a++) { if(gestures[a][1]==event.getItem().getId()) { paint=new Paint(Paint.ANTI_ALIAS_FLAG); paint.setColor(gestureColor); paint.setStrokeWidth(5); paint.setStrokeJoin(Paint.Join.ROUND); image=item.getBoxBackground("n"); if(image.getWidth()!=item.getWidth()||image.getHeight()!=item.getHeight())image=LL.createImage(item.getWidth(),item.getHeight()); canvas=image.draw(); image.getBitmap().eraseColor(Color.TRANSPARENT); canvas.drawColor(bgColor); paint.setColor(gestureColor); for(var z=1;z<gestures[a][0].length;z++) { canvas.drawLine(gestures[a][0][z-1][0]/100*item.getWidth(),gestures[a][0][z-1][1]/100*item.getHeight(),gestures[a][0][z][0]/100*item.getWidth(),gestures[a][0][z][1]/100*item.getHeight(),paint); } image.update(); item.setBoxBackground(image,"n"); if(!confirm("This shortcut has already a gesture. It is now displayed until something is drawn. Do you want to replace it?"))return; gestures.splice(a,1); break; } } item.setTag(JSON.stringify(gestures)); Android.makeNewToast("The next drawn Gesture will be set to this shortcut",true).show(); add=event.getItem().getId(); } function scaleGesture(GESTURE) { var maxx=0,minx=item.getWidth(),maxy=0,miny=item.getHeight(); for(var i=0;i<GESTURE.length;i++) { if(GESTURE[i][0]<minx)minx=GESTURE[i][0]; if(GESTURE[i][0]>maxx)maxx=GESTURE[i][0]; if(GESTURE[i][1]<miny)miny=GESTURE[i][1]; if(GESTURE[i][1]>maxy)maxy=GESTURE[i][1]; } var scaled=[]; for(var i=0;i<GESTURE.length;i++) { scaled.push([(GESTURE[i][0]-minx)/(maxx-minx)*100,(GESTURE[i][1]-miny)/(maxy-miny)*100]); } return scaled; } function loadGestures() { if(typeof gestures==='undefined')gestures=JSON.parse(item.getTag())||[]; for(var a=0;a<gestures.length;a++) { if(LL.getItemById(gestures[a][1])==null)gestures.splice(a,1); } item.setTag(JSON.stringify(gestures)); done=true; } </sxh>

script_gesture_launcher.txt · Last modified: 2014/12/20 12:31 by lm13