====== 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:
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();
}
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.
// 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 );
}
}