iTGB Touch Control System *updated May 12*
by Dean Parker · 04/24/2010 (11:18 pm) · 6 comments
Touch Control System
This system adds a new GUI Control Image that will greatly increase the performance of the touch controls. Users can now drag their finger off the buttons without it staying in the on position. It allows multi controls to be used at the same time. (updated for pressing 2 or more buttons at the same time.)
No Torque script programming required to manage and is fully automatic. Add 2 files to iTGB source and recompile.
NOTE: I have not added image fade on touch. Any image you use should be prefaded (opacity 30%).
Updated:
- In order for this system to work, you MUST enable Multiple Touch in the defaultPrefs.cs and commonConfig.xml files.
- joydisc control will send -1 when user lifts finger off image.
- fixed problem with mouse down, removed code that was not needed.
iTGB Files
1) Download the www.deanparker.com/guiControlImage3.zip and save the files into engine_source_gui_controls folder.
Contains guiControlImage.cpp and guiControlImage.h
2) Place the files in BOTH TorqueGameBuilder and TGBGame solution.
3) Recompile TorqueGameBuilder and TGBGame.
How It Works
1) Go to the GUI editor.

2) Place a GUIControlImage on the main screen, call it maskBitmap.
* Use a clear image for the bitmap. If you do not have one, you can use this www.deanparker.com/maskBitmap.png
* The maskBitmap image size MUST be 480 by 320, at position 0,0.
3) Place a GUIControlImage for any of your buttons you want.
* These MUST be on top of the maskBitmap.
* You need to create a name for EVERY control you add.
* When you are picking images for buttons, use an image that has the opacity at 30%.
The image has been COLORED to highlight the MASK/buttons. The MASK does not alter the appearance of your game.
The Green lines show where each control is.

Torque Script Code
The 'oniPhoneTouchXXXX' code is replaced with the following.
This is the NEW oniPhoneTouch code. NOTE this is only a sample and you should change it for your needs.
%val contains: ( "The name of the control", 1 (touched) or 0 (not touched) )
The final part
In your main loop with schedule()...
Using The Joydisc

In order to use an image as a Joydisc, checkmark the UseJoyDisc.

Here is a joypad image or create your own.
%val contains: ( "The name of the control", 1 (touched) or 0 (not touched), Cos, Sin, Angle )
If you want to convert the joydisc to up/down/left/right, you could use...
This system adds a new GUI Control Image that will greatly increase the performance of the touch controls. Users can now drag their finger off the buttons without it staying in the on position. It allows multi controls to be used at the same time. (updated for pressing 2 or more buttons at the same time.)
No Torque script programming required to manage and is fully automatic. Add 2 files to iTGB source and recompile.
NOTE: I have not added image fade on touch. Any image you use should be prefaded (opacity 30%).
Updated:
- In order for this system to work, you MUST enable Multiple Touch in the defaultPrefs.cs and commonConfig.xml files.
- joydisc control will send -1 when user lifts finger off image.
- fixed problem with mouse down, removed code that was not needed.
iTGB Files
1) Download the www.deanparker.com/guiControlImage3.zip and save the files into engine_source_gui_controls folder.
Contains guiControlImage.cpp and guiControlImage.h
2) Place the files in BOTH TorqueGameBuilder and TGBGame solution.
3) Recompile TorqueGameBuilder and TGBGame.
How It Works
1) Go to the GUI editor.

2) Place a GUIControlImage on the main screen, call it maskBitmap.
* Use a clear image for the bitmap. If you do not have one, you can use this www.deanparker.com/maskBitmap.png
* The maskBitmap image size MUST be 480 by 320, at position 0,0.
3) Place a GUIControlImage for any of your buttons you want.
* These MUST be on top of the maskBitmap.
* You need to create a name for EVERY control you add.
* When you are picking images for buttons, use an image that has the opacity at 30%.
The image has been COLORED to highlight the MASK/buttons. The MASK does not alter the appearance of your game.
The Green lines show where each control is.

Torque Script Code
The 'oniPhoneTouchXXXX' code is replaced with the following.
function oniPhoneTouchUp( %touchCount, %touchX, %touchY )
{
return;
}
function oniPhoneTouchDown( %touchCount, %touchX, %touchY )
{
return;
}
function oniPhoneTouchMove( %touchCount, %touchX, %touchY )
{
return;
}
function oniPhoneTouchTap ( %touchCount, %touchX, %touchY )
{
return;
}This is the NEW oniPhoneTouch code. NOTE this is only a sample and you should change it for your needs.
%val contains: ( "The name of the control", 1 (touched) or 0 (not touched) )
function oniPhoneTouch(%val)
{
%controlName = getWord(%val,0);
%doit = getWord(%val,1);
if (%controlName $= "maskBitmap")
{
$downOn = false;
$upOn = false;
$spaceOn = false;
}
if (%controlName $= "upBitmap")
if (%doit == 1)
$downOn = true;
else
$downOn = false;
if (%controlName $= "dnBitmap")
if (%doit == 1)
$upOn = true;
else
$upOn = false;
if (%controlName $= "dropBitmap")
if (%doit == 1)
$spaceOn = true;
else
$spaceOn = false;
return;
}The final part
In your main loop with schedule()...
//control if ($upOn) player.setPositionX(player.getPositionX()-1); if ($downOn) player.setPositionX(player.getPositionX()+1); if ($spaceOn) dosomething();
Using The Joydisc

In order to use an image as a Joydisc, checkmark the UseJoyDisc.

Here is a joypad image or create your own.
%val contains: ( "The name of the control", 1 (touched) or 0 (not touched), Cos, Sin, Angle )
$speed = 20;
...
function oniPhoneTouch(%val)
{
%controlName = getWord(%val,0);
%doit = getWord(%val,1);
// if user touches the mask bitmap, set velocity to zero.
if (%controlName $= "maskBitmap")
{
player.setLinearVelocity(0,0);
$spaceOn = false;
}
if (%controlName $= "joyBitmap")
{
%joyCos = getWord(%val,2);
%joySin = getWord(%val,3);
%joyAngle = getWord(%val,4);
// if finger is off, don't do a rotation.
if (joyAngle > -1)
player.setRotation(mRadToDeg(%joyAngle) + 90);
player.setLinearVelocity(%joyCos*$speed,%joySin*$speed);
}
if (%controlName $= "dropBitmap")
if (%doit == 1)
$spaceOn = true;
else
$spaceOn = false;
return;
}If you want to convert the joydisc to up/down/left/right, you could use...
if (%controlName $= "joy_0")
{
%ang = mRadToDeg(getWord(%val, 4));
if (%ang>-1)
{
%dir = (%ang <= 315 && %ang >=225) ? "up" : (%ang <= 225 && %ang > 135 ? "left" : (%ang <= 135 && %ang >= 45 ? "down" : "right"));
if (%dir $= "up")
echo("up arrow");
else if (%dir $= "left")
echo("left arrow");
else if (%dir $= "down")
echo("down arrow");
else
echo("right arrow");
}
}About the author
I started programming in early 80's on an Atari 800 and now I am coding on VS 2008 C#.
#2
Am I doing something wrong here?
05/06/2010 (1:12 am)
I am having an issue with this control, I have made 2 controls both have UseJoyDisc set. I have added the angle bit you see above. However it seems that when it executes I get one pad or the other, never really both. One Pad joy_0 moves the player, the other pad joy_1 aims the gun. If I press joy_0 my player moves and steers correctly, however once I move joy_1 it stops joy_0,function oniPhoneTouch(%val)
{
%controlName = getWord(%val,0);
%doit = getWord(%val,1);
if (%controlName $= "joy_0")
if (%doit == 1)
{
$speed = 20;
%joyCos = getWord(%val,2);
%joySin = getWord(%val,3);
%joyAngle = getWord(%val, 4);
hero.setRotation(mRadToDeg(%joyAngle) + 90);
hero.setLinearVelocity(%joyCos*$speed,%joySin*$speed);
}
else
hero.setLinearVelocity(0,0);
if (%controlName $= "joy_1")
{
%timerShoots = gun.getBehavior("timerShootsBehavior");
if(!isObject(%timerShoots))
return;
if (%doit == 1)
{
%timerShoots.shootEnable = true;
%joyAngle = getWord(%val, 4);
gun.setRotation(mRadToDeg(%joyAngle) + 90);
}
else
{
%timerShoots.shootEnable = false;
}
return;
}Am I doing something wrong here?
#3
I created the above version of controls (complete rewrite) to allow you to use any image button to do things and it fixed the drag finger off the image, added sourcerect and added image buttons.
On my game I submitted to apple, I have 4 image buttons and had to make a few changes to my code that allows multi-button action. I have not posted the update, but it is not a rewrite, just some tweaks to the code.
*************************
Let me look over your code: I will post an update to my control and post some updates to your code :)
05/06/2010 (4:13 pm)
The first d-pad example works good, until you drag your finger off the image.I created the above version of controls (complete rewrite) to allow you to use any image button to do things and it fixed the drag finger off the image, added sourcerect and added image buttons.
On my game I submitted to apple, I have 4 image buttons and had to make a few changes to my code that allows multi-button action. I have not posted the update, but it is not a rewrite, just some tweaks to the code.
*************************
Let me look over your code: I will post an update to my control and post some updates to your code :)
#4
Give the following code a try. Let me know how it works out.
05/06/2010 (4:36 pm)
I have posted the updated version of the control link is at the top of the page. This includes multi-button action, angle out, joydisc auto zeroing.Give the following code a try. Let me know how it works out.
function oniPhoneTouch(%val)
{
%controlName = getWord(%val,0);
// if user touches the mask bitmap, set velocity to zero.
// turn off gun.
if (%controlName $= "maskBitmap")
{
hero.setLinearVelocity(0,0);
%timerShoots.shootEnable = false;
}
// if user lifts finger off the joydisc guiControlImage
// script will send zero's no need to setLinearVelocity(0,0)
if (%controlName $= "joy_0")
{
%speed = 20;
%joyCos = getWord(%val,2);
%joySin = getWord(%val,3);
%joyAngle = getWord(%val,4);
// if finger is off, don't do a rotation.
if (joyAngle > -1)
hero.setRotation(mRadToDeg(%joyAngle) + 90);
hero.setLinearVelocity(%joyCos*%speed,%joySin*%speed);
}
if (%controlName $= "joy_1")
{
%timerShoots = gun.getBehavior("timerShootsBehavior");
if(!isObject(%timerShoots))
return;
%timerShoots.shootEnable = false;
%doit = getWord(%val,1);
if (%doit == 1)
{
%timerShoots.shootEnable = true;
%joyAngle = getWord(%val, 4);
gun.setRotation(mRadToDeg(%joyAngle) + 90);
}
}
return;
}
#5
With this code, you can add controls to be on left or right side in your options screen. Meaning the user selects if they want the control on the left or right side. You do NOT have to change your oniPhoneTouch() function.
In your code just after the level loads, move the control left or right and the fire left or right.
The guiControlImage system automatically converts the x,y to the correct button and location.
05/06/2010 (7:30 pm)
HenryWith this code, you can add controls to be on left or right side in your options screen. Meaning the user selects if they want the control on the left or right side. You do NOT have to change your oniPhoneTouch() function.
In your code just after the level loads, move the control left or right and the fire left or right.
The guiControlImage system automatically converts the x,y to the correct button and location.
#6
05/10/2010 (1:55 am)
I made an adjustment to the script that will send -1 for the angle in the joydisc. That way when the user picks finger off disc, it will be -1. Updated zip file and the code for Henry.
Torque 3D Owner Henry Shilling
Smokin Skull
I wanted the angle as well so that I could aim my player, since it already existed why would I do the calculations in TS?
I like the changes so that I can have more than one, I had already changed you older example in a similar manner, but it still would have been on a per project basis, this is way more flexible.
Thanks Again.