Mix mouse and multitouch
16th March, 2011
When creating a game for a mobile device with multitouch capabilities, it’s difficult to test the code inside ShiVa Editor on a computer with no multitouch. This tutorial explains you how to simulate the multitouch using the mouse.
To mix multitouch and mouse events, we will redirect the mouse events to multitouch handlers when the multitouch is off.
Create the mouse handlers (onMouseButtonDown, onMouseMove and onMouseButtonUp) and the multitouch handlers (onTouchSequenceBegin, onTouchSequenceChange and onTouchSequenceEnd ).
To know if the multitouch is enabled, add a variable bMultitouchEnabled in your AIModel and in a onInit handler, write the line:
this.bMultitouchEnabled ( input.enableMultiTouch ( this.getUser ( ), true ) )
You also have to add a variable bMouseDown to know if a mouse button is pressed.
Mouse events redirection
Now, you have to redirect mouse events to multitouch events when the multitouch is disabled.
In the onMouseButtonDown handler, set the bMouseDown variable to true and send an event to the onTouchSequenceBegin handler. Of course, this code has to be executed only if multitouch is disabled:
if ( not this.bMultitouchEnabled ( ) ) then this.bMouseDown ( true ) this.sendEvent ( "onTouchSequenceBegin" ) end
The code is pretty the same for the onMouseButtonUp handler, but the event has to be sent to the onTouchSequenceEnd handler, and you have to set the mouse bMouseDown variable to false.
if ( not this.bMultitouchEnabled ( ) ) then this.bMouseDown ( false ) this.sendEvent ( "onTouchSequenceEnd" ) end
This handler will works like the 2 others, but have another restriction: the bMouseDown has to be true because on a multitouch device, it is not possible to interact with the screen without touching it, pressing the mouse button simulate the finger which is touching the screen.
You also have to pass the nPointX and nPointY parameters of the onMouseMove handler to the onTouchSequenceChange handler.
if ( not this.bMultitouchEnabled ( ) and this.bMouseDown ( ) ) then this.sendEvent ( “onTouchSequenceChange”, 1, nPointX, nPointY, -1, 0, 0, -1, 0, 0 ) end
The value “1” mean the touch n°0 is running. The two “-1” values mean the touch n°1 and n°2 are not running (refer the the onTouchSequenceChange to know all the parameters).
Leave a Reply