DroidScript wiki

(was AndroidScript) unofficial documentation by the community

User Tools

Site Tools


plugins:microbitctrl_imgtouch_ontouch

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

plugins:microbitctrl_imgtouch_ontouch [2016/10/23 21:02] (current)
madlyr created
Line 1: Line 1:
 +====== imgTouch_OnTouch ======
 +
 +The **imgTouch_OnTouch** function allows add custom LED array touch function. ​
 +
 +The **ev** parameter is an event object which has 3 fields: X, Y in range 0..1 and action ("​Up",​ "​Down",​ "​Move"​).
 +
 +Default touch handler works as below:
 +<code javascript>​
 +function ( ev )
 +{
 +  //​app.Debug( "​x="​ + ev.X + " y=" + ev.Y );
 +  ​
 +  //Calculate which LED we are touching.
 +  var x = Math.floor( ​ ev.X * 5 );
 +  var y = Math.floor( ​ ev.Y * 5 );
 +  ​
 +  //Keep x and y between 0 and 4.
 +  x = Math.min( Math.max(x,​0),​ 4 );
 +  y = Math.min( Math.max(y,​0),​ 4 );
 +  //​app.Debug( "LED = " + x + ","​ + y );
 +  ​
 +  if( ev.action=="​Move"​ )
 +    _this.SetLED( x, y );
 +  else if( ev.action=="​Up"​ )
 +    _this.SendLEDStates();​
 +}
 +</​code>​
 +
 +It could be changed to different behavior.
 +
 +====Example - Custom imgTouch_OnTouch handler====
 +
 +This script has custom touch handler toggling LEDs without swipe. Only one LED is toggled at "​Down"​ action.
 +
 +<code javascript>​
 +// Load the MicroBit plugin.
 +app.LoadPlugin( "​MicroBit"​ );
 +
 +// Called when application is started.
 +function OnStart()
 +{
 +  // Lock screen orientation to Portrait.
 +  app.SetOrientation( "​Portrait"​ );
 +
 +  // Create our main layout.
 +  lay = app.CreateLayout( "​Linear",​ "​VCenter,​FillXY"​ );
 +  lay.SetBackColor( "#​222222"​ );
 +
 +  // Create the Microbit plugin.
 +  microbit = app.CreateMicroBit();​
 +
 +  // Create MicroBit controller.
 +  ctrl = microbit.CreateController( 0.9 );
 +  ​
 +  // Adds custom imgTouch_OnTouch handler
 +  ctrl.imgTouch_OnTouch = imgTouch_OnTouch;​
 +  ​
 +  lay.AddChild( ctrl );
 +
 +  //Create horizontal layout for buttons
 +  layButs = app.CreateLayout( "​Linear",​ "​Horizontal"​ );
 +  layButs.SetMargins( 0,0.05,0,0 );
 +  lay.AddChild( layButs );
 +
 +  //Create a bluetooth connect button.
 +  btnConnect = app.CreateButton( "​[fa-bluetooth-b]",​ 0.25, 0.15, "​FontAwesome"​ );
 +  btnConnect.SetTextSize( 66 );
 +  btnConnect.SetTextColor( "#​3366ff"​ );
 +  btnConnect.SetOnTouch( txtBT_OnTouchDown );
 +  layButs.AddChild( btnConnect );
 +
 +  //Create clear button.
 +  btnClear= app.CreateButton( "​[fa-times]",​ 0.25, 0.15, "​FontAwesome"​ );
 +  btnClear.SetTextSize( 66 );
 +  btnClear.SetTextColor( "#​ff4444"​ );
 +  btnClear.SetOnTouch( btnClear_OnClick );
 +  layButs.AddChild( btnClear );
 +
 +  //Add main layout to the app.
 +  app.AddLayout( lay );
 +}
 +
 +// Handle Bluetooth button press.
 +function txtBT_OnTouchDown()
 +{
 +  microbit.Scan();​
 +}
 +
 +// Handle '​clear'​ button press.
 +function btnClear_OnClick()
 +{
 +  ctrl.Clear();​
 +}
 +
 +// Handle toggling LEDs without swipe. Only one LED is toggled at "​Down"​ action.
 +function imgTouch_OnTouch( ev ) {
 +  //​app.Debug( ev.X+':'​+ev.Y+':'​+ev.action );
 +  ​
 +  //Calculate which LED we are touching.
 +  var x = Math.floor( ​ ev.X * 5 );
 +  var y = Math.floor( ​ ev.Y * 5 );
 +  ​
 +  //Keep x and y between 0 and 4.
 +  x = Math.min( Math.max(x,​0),​ 4 );
 +  y = Math.min( Math.max(y,​0),​ 4 );
 +  //​app.Debug( "LED = " + x + ","​ + y );
 +  ​
 +  if ( ev.action=="​Down"​ ) {
 +    ctrl.ToggleLED( x, y );
 +  } 
 +}
 +</​code>​
  
plugins/microbitctrl_imgtouch_ontouch.txt ยท Last modified: 2016/10/23 21:02 by madlyr