Game Development Community

Mouse Binding Problems

by MrPhil (Philip Ludington) · in Torque Game Builder · 02/27/2005 (11:04 pm) · 23 replies

So its 1:55 AM and I'm going to bed. Hopefully someone can solve my problem while I rest and I'm might have missed something obvious (tired) and if so I apologize. Here's my code and only the handleMouseWheel event seems to be working!?!? Did I get the names of the other events wrong? (Where is that info. anyway? I had to scratch through the forums for a while to find all these.)

// Create a new action map.
	new ActionMap(playerMap);

	// Bind keys to actions.
	playerMap.bind( mouse, "zaxis", handleMouseWheel );
	playerMap.bind( mouse, "yaxis", handleMouseY );
	playerMap.bind( mouse, "xaxis", handleMouseX );
	playerMap.bindCmd( mouse, "button0", "handleMousePickDown();", "handleMousePickUp();"  );
	
	// Activate the action map.
	playerMap.push();
	
	echo( "setupT2DScene complete." );
}
function handleMouseWheel( %val )
{   
	echo( "MouseWheel" SPC %val );
}

function handleMouseY( %val )
{
	echo( "MouseY" SPC %val );
	$MouseY = %val;
}
function handleMouseX( %val )
{
	echo( "MouseX" SPC %val );
	$MouseX = %val;
}
function handleMousePickDown()
{
	%position = $MouseX SPC $MouseY;
	
	echo( %position );
}

Edit: Fixed a bunch of typos in the code.

About the author

My name is Philip Ludington, I'm a professional programmer by day and I dabble in game development by night. Learn more about my projects at www.MrPhilGames.com

Page «Previous 1 2
#1
02/27/2005 (11:09 pm)
Use mouse0 not mouse
#2
02/27/2005 (11:52 pm)
I'm having the same problem. I've been trying:

playerMap.bind(mouse, xaxis, doMouseX);

playerMap.bind(mouse, "xaxis", doMouseX);

playerMap.bind(mouse0, xaxis, doMouseX);

playerMap.bind(mouse0, "xaxis", doMouseX);

and playerMap.bind("mouse0", "xaxis", doMouseX);

None of which seem to work... My doMouseX() function never seems to get called.

Hey LabRat: can you post a few lines of TorqueScript that work for you?
#3
02/28/2005 (12:17 am)
Whoops... you need to call

cursorOff();

to switch from GUI mode to control mode for the mouse.

NOTE: You can't do this in the console window.. it will turn the mouse cursor off.. but closing the console will turn it back on again.
#4
02/28/2005 (12:25 am)
That did it! Thanks LabRat.

If I want the mouse cursor to work and get windowed coordinates, do you know if I should use a GuiInputControl?
#5
02/28/2005 (4:56 am)
No, not at all.

The fxSceneWindow2D has all the mouse callbacks you'll ever need.

Look in the reference guide under "fxSceneWindow2D", under the callback section....

If you wanted to track the mouse-details, you can use these callbacks. They provide world-position coordinates for the mouse no matter what the camera is doing. You can use these coordinates directly for positioning objects in your scene. If you change your camera view, zoom or move it automatically, the coordinates are calculated in realtime for you.

Here's a code snippet that echos the mouse position ( in world coordinates ) to the console.

%this = the scene-window object.
%modifier = alt/ctrl/shift key modifiers etc.
%worldPosition = mouse position in world units.
%mouseClicks = how many mouse clicks.

function sceneWindow2D::onMouseMove( %this, %modifier, %worldPosition, %mouseClicks )
{
	echo( %worldPosition );
}

Note that there are ones for left and right mouse down, up and dragged.

- Melv.
#6
02/28/2005 (1:26 pm)
Thanks guys!

I guess I kind of have a follow up question. Is there some sort of golden rule or guideline to help understand when to making something part of the GUI or scene?

PS T2D rocks, thank you so much for saving me many hours and letting me drive right into my game play itself.
#7
02/28/2005 (2:18 pm)
The only golden rule I can see is if it's necessary to have the visual element you're talking about be position in the world and be affected by the camera I guess.

- Melv.
#8
02/28/2005 (2:53 pm)
*click*

That's the sound of my brain putting a whole lot of information together. Thanks Melv this is exactly the kind of thing I hadn't thought about.

Woo Hoo!

That's the sound of me being excited because I'm working on my game instead of hacking away at a home brewed engine.
#9
03/01/2005 (12:29 am)
You are quite welcome. :)

So cool to see T2D making people happy ... makes me all warm and Fuzzy inside!

You the "Mr.Phil" from IndieGamer right? Just saying Hi!

- Melv.
#10
03/01/2005 (8:53 am)
Yes I am ;)

Hi!
#11
09/12/2005 (4:58 pm)
Is there support for mouse wheel in these callbacks? I don't see them listed in the 1.0.2 docs.

Also, could there be an onMouseStop() callback for when the mouse stops moving?
#12
08/09/2006 (10:48 pm)
STOP! I did not read the release notes with enough care STOP!

As of version 1_1_1 :

- Disallowed the use of mouse axes with bindCmd - this was crashing the engine.

STOP! I did not read the release notes with enough care STOP!


@All,

I know this is an old thread, but I need to raise it again.

I'm experiencing some odd behavior in TGB 1_1_0+ and want to see if I'm missing something simple.

I've written the following code:

playerMap.bind( mouse0 , "xaxis" , xaxis);
   playerMap.bind( mouse0 , "yaxis" , yaxis);
   playerMap.bind( mouse0 , "zaxis" , zaxis);
   playerMap.bind( mouse0 , "button0" , button0);

// ...   

function xaxis( %val ) 
{
   echo("xaxis( ", %val ," )");
}

function yaxis( %val ) 
{
   echo("yaxis( ", %val ," )");
}

function zaxis( %val ) 
{
   echo("zaxis( ", %val ," )");
}

function button0( %val ) 
{
   echo("button0( ", %val ," )");
}

Now, the problem I'm experiencing is that neither xaxis() nor yaxis() is ever called.

As I would expect, button0() is only called if I also call canvas.cursorOff();

Interestingly, zaxis() is always called when I roll the mousewheel (even while the console is running).

So, my question to TGB + Mouse users is, "What's the trick?"

I've been searching the forums and TDN and am on the brink of running through the code, but I thought perhaps it would be faster to ask you folks.

I must admit, this is an embarassing question to have to ask, but it is also kind of frustrating because it should just work.

Thanks a ton to any and all who answer!

www.hallofworlds.com/how.ico Hall Of Worlds - For Gamers
EdM|GPGT


fixed typos
#13
08/11/2006 (9:36 am)
@Jason: SceneWindow2d supports onMouseWheelDown and onMouseWheelUp by default (for scrolling).

A full list of sceneWindow2d mouse events can be found in t2dSceneWindow.h [line 230]

Adding onMiddleMouseDown, onMiddleMouseUp, and onMiddleMouseDragged in code is really easy if you need it:

www.garagegames.com/mg/forums/result.thread.php?qt=48344

-Unk
#14
08/15/2006 (9:18 am)
Edward,

Note that the release notes say that bindCmd was disabled, not bind. It worked for me in 1.1.0, but not now in 1.1.1. There's no reason axis binding should not work anymore. Perhaps they accidentally did remove mouse axis binding/locking altogether. I sure hope not.

It took me awhile in 1.1.0 to find the "magic word" to get it working... hideCursor(); That is not the magic word now, however. Hopefully someone who knows the magic word will have something to say.
#15
08/15/2006 (12:17 pm)
Ok, I found a solution to the problem. See this thread (4th post) for details.
#17
09/04/2006 (8:43 am)
Hmm, I use 1_1_1 and I tried everything (5 hours of testing!) , all combinations of

hideCursor();
canvas.cursorOff();
mainScreenGui.nocursor=true;

I added noCursor = "1"; or noCursor = true; to all mainScreenGui components,
but the xaxis and yaxis still don't work. Only zaxis. I cannot see cursor, wheel and button work,
but nothing more.

Do you have any idea, where could be the problem? My code:

----------------------------------------------------------------
playerMap.bind(mouse0, "xaxis", player_mouse_x);
...
function player_mouse_x(%val)
{
echo("mouse_X");
}
----------------------------------------------------------------

have I do anything enlse? Set values of sceneWindow2D (as useWindowMouseEvents, useObjectMouseEvents) to any values?

I have published three games in TGB but the engine still make me crazy :)
#18
09/04/2006 (11:11 am)
@pavel - I have not tried this but am guessing that GUI you are trying to disable the cursor for is not necessarily called 'mainScreenGui'. The default gui that is created when you create a new project in the editor is simply called 'MainScreen'. So maybe try 'mainscreen.nocursor = true' or whichever gui you are trying to disable the cursor for?? (You can see the gui file names by looking in your 'TGB/gamename/gui folder').

(Note: This is assuming that 'mainScreenGui' refers to an actual gui name vs. a class/keyword called mainScreenGui...)

-Unk
#19
09/04/2006 (12:36 pm)
Unk: I have "mainScreenGui", so I have no idea, where is the problem. I added gamepad support binding, everything works, only xaxis and yaxis not :|
#20
09/04/2006 (12:53 pm)
@Pavel

Please try this:

1. Create a blank TGB game (with Level Builder)

2. Save the level (so you can run it).

3. Locate ~/gameScripts/game.cs

4. Replace the entire file contents with this:
//---------------------------------------------------------------------------------------------
// Torque Game Builder
// Copyright (C) GarageGames.com, Inc.
//---------------------------------------------------------------------------------------------

//---------------------------------------------------------------------------------------------
// startGame
// All game logic should be set up here. This will be called by the level builder when you
// select "Run Game" or by the startup process of your game to load the first level.
//---------------------------------------------------------------------------------------------
function startGame(%level)
{
   // Set The GUI.
   Canvas.setContent(mainScreenGui);
   Canvas.setCursor(DefaultCursor);
   
   if( isFile( %level ) || isFile( %level @ ".dso"))
      sceneWindow2D.loadLevel(%level);

   initializePlayerMap(); //EFM
   playerMap.push(); //EFM
   
}

//---------------------------------------------------------------------------------------------
// endGame
// Game cleanup should be done here.
//---------------------------------------------------------------------------------------------
function endGame()
{
   sceneWindow2D.endLevel();
   playerMap.pop(); //EFM
}

function initializePlayerMap() //EFM
{
   new ActionMap( playerMap );

   $pref::Input::MouseEnabled = 1;
   enableMouse();

   playerMap.bind( mouse0 , "xaxis" , showXAxis  );
   playerMap.bind( mouse0 , "yaxis" , showYAxis  );
   
   // Hack to enable mouse xaxis and yaxis
   // This needs a better solution.
   //
   mainScreenGui.noCursor=true; 
}

function showXAxis( %val ) //EFM
{
   echo("XAXIS: " , %val );
}

function showYAxis( %val ) //EFM
{
   echo("YAXIS: " , %val );
}
or download here MOUSE BIND EXAMPLE (copy contents into game.cs).

5. Reload the project.

6. Run the game.

7. Move the mouse around (notice there is no cursor).

8. Open the console and you should see something like this:
XAXIS: 3
YAXIS: 12
XAXIS: 6
YAXIS: 12
XAXIS: 8
YAXIS: 11
XAXIS: 9
YAXIS: 10
XAXIS: 11
YAXIS: 9
XAXIS: 26
YAXIS: 16

I hope this gets your started.

Note: I marked important lines/functions with my initials: EFM

www.hallofworlds.com/how.ico Hall Of Worlds - For Gamers
EdM|GPGT


updated spelling and list numbering (I guess I'm still learning to count :] )
Page «Previous 1 2