Game Development Community

Problem with the Mouse callbacks

by Dennis Harrington · in Torque Game Builder · 03/28/2006 (3:04 pm) · 17 replies

Hello - I'm a new user of T2D and I've been going through the tutorials without problem until I got to the object selection tutorial. I'm having a problem with the mouse related callbacks, such as onMouseDown, onMouseUp, onMouseMove, etc.

Here's the code that's listed in the tutorial:

function sceneWindow2D::onMouseMove( %this, %mod, %worldPos, %mouseClicks )
{
        echo("Mouse Moving");
}

function sceneWindow2D::onMouseDragged( %this, %mod, %worldPos, %mouseClicks )
{
        echo("Mouse Dragging");
}

function sceneWindow2D::onMouseDown( %this, %mod, %worldPos, %mouseClicks )
{
        echo("Mouse Down");
}

function sceneWindow2D::onMouseUp( %this, %mod, %worldPos, %mouseClicks )
{
        echo("Mouse Up");
}

function sceneWindow2D::onMouseEnter( %this, %mod, %worldPos, %mouseClicks )
{
        echo("Mouse Entered... Knew it would come crawling back");
}

function sceneWindow2D::onMouseLeave( %this, %mod, %worldPos, %mouseClicks )
{
        echo("Mouse Left... Go Get It!");
}

Is there a mouse object that needs to be instantiated or initialized? I'm not getting anything in the console. I also attempted the Strategy tutorial but ran into the some problem of the mouse events not being registered.

Any help is much appreciated!

#1
03/28/2006 (3:30 pm)
Did you enter the above exactly as it appears into your game.cs outside of any other function (or the main one)? I had no problem at all running that tutorial. What version of the builder do you have?
#2
03/28/2006 (3:41 pm)
I have the most recent Beta 2 that was released last week and I did enter the code just as you specified.

It's odd because I initially encountered this while doing the Strategy genre tutorial and it didn't work. So I went throught object selection tutorial seperately and had the same problem.
#3
03/28/2006 (5:02 pm)
It looks like I may have found the problem.

If I run T2D without any editors and comment out the sceneWindow2D.loadLevel(%level); line in game.cs then all of the mouse callbacks work just fine. If I only do one or the other then it won't work.

I'm not sure if this is a bug or if it just needs to be explained a little better. It seems odd that you wouldn't be able to use mouse callbacks while using the editors and/or loading a level.

Any ideas?
#4
03/29/2006 (11:19 am)
Looks like it's reading your mouse input into the sceneWindow2D object and not your main game window... doesn't make a whole lot of sense, but that's about the best I can come up with...
#5
03/29/2006 (12:13 pm)
The Level Builder pushes a dialog over your sceneWindow2D when you test the level (mainly for the stop button)... unfortunately that makes it steal all the mouse inputs, this will be fixed :)
#6
03/29/2006 (12:20 pm)
Ah ok. I suspected something like that might be happening. Thanks for the confirmation!
#7
04/02/2006 (1:18 pm)
Oh my! I have been struggling with this 'little' problem for hours. So glad to have found this thread. Thank you and I look forward to the fix.
#8
04/12/2006 (12:41 pm)
Ah thanks Matt. I had been wondering about this myself.
#9
04/12/2006 (3:29 pm)
Yup, that would be the same problem I encountered in my post:
"GUI buttons broken? Or, Level Control why have you forsaken me?"

Glad to hear there is a fix incoming! :)
#10
04/12/2006 (9:27 pm)
I'm having a problem with the callbacks on the objects. Relavent code follows:

new t2dSceneWindow(sceneWindow2D) {
      canSaveDynamicFields = "0";
      Profile = "GuiContentProfile";
      HorizSizing = "width";
      VertSizing = "height";
      Position = "0 0";
      Extent = "1024 768";
      MinExtent = "8 8";
      canSave = "1";
      Visible = "1";
      lockMouse = "0";
      useWindowMouseEvents = "0";
      useObjectMouseEvents = true; // also tried "1"
   };

	// Create a scene graph.
	new t2dSceneGraph(starmapScene) //__decl t2dSceneGraph starmapScene
	{
		useObjectMouseEvents = true;
	};	
	
	// Associate the new scene graph with the scene window.
	sceneWindow2D.setSceneGraph(starmapScene);
	$global::starmapScene = starmapScene;
	
	starmapScene.clearScene();
	
	$cameraPos = "512 384 1024 768";
	sceneWindow2D.setCurrentCameraPosition($cameraPos);
	
	$global::starmap = starmapCreate(125);

function starmapCreate(%numberofstars )
{
	%this = new ScriptObject()
	{
		class = "starmap";
	};
	
	// Backfield
	%this.Backfield = new t2dStaticSprite()
	{
		scenegraph = $global::starmapScene;
		imagemap = BackfieldImageMap;
		layer = 30;
		position = "512 384";
		size = "1024 768";
	};
	
	%this.stars = new SimSet();
		
	for(0; %x <%numberofstars; %x++)
	{		
		%position = getRandom(1024) SPC getRandom(768);
		%star = starCreate(%position);
		%this.stars.add( %star );
	}
}

function starCreate(%position)
{
	%star = new t2dStaticSprite()
	{
		scenegraph = $global::starmapScene;
		imagemap = starImageMap;
		layer = 1;
		className = "star";
		frame = 1;
		position = %position;
		useMouseEvents = true;
	};
	
	return %star;
}
function star::onRightMouseUp(%this, %modifier, %worldPos, %mouseClicks)
{
	echo( "star::RightMouseUP" SPC DebugString(%this) );
}

When I mouse right click on a star it doesn't echo the expected text, Any ideas?
#11
04/13/2006 (3:34 pm)
Use class = star; instead of className = "star";
#12
04/13/2006 (9:35 pm)
Well I made the change you suggested and it didn't fix the problem.
#13
04/13/2006 (9:48 pm)
Hmmm... are you having the same problem as posted already, is this being run independent of the Level Builder or from the play button ?

you can either specify a class as "class" or "superClass" or both, just like ScriptObjects.
#14
04/13/2006 (9:49 pm)
Try setting up another function ... like

function star::test(%this)
{
   echo("TEST");
}

then test that function from the console.
#15
04/13/2006 (10:18 pm)
Okay... here is my new test bed... its basically my earily code extracted to one big main.cs file to make sure it wasn't code somewhere else causing the problem.

Your test works.

main.cs
new t2dSceneWindow(sceneWindow2D) {
      canSaveDynamicFields = "0";
      Profile = "GuiContentProfile";
      HorizSizing = "width";
      VertSizing = "height";
      Position = "0 0";
      Extent = "1024 768";
      MinExtent = "8 8";
      canSave = "1";
      Visible = "1";
      lockMouse = "0";
      useWindowMouseEvents = "1";
      useObjectMouseEvents = "1";
   };   
   datablock t2dImageMapdatablock(BackfieldImageMap)
   {
      imageName = "~/data/Images/black";
      imageMode = full;
      filterPad = false;
   };
   datablock t2dImageMapdatablock(starImageMap)
   {
      imageName = "~/data/Images/particles1";
      imageMode = cell;
      cellWidth = 64;
      cellHeight = 64;
      frameCount = 4;
   };
function initializeProject()
{
   // This is function is the 1st one called by GG infrastructure
   
   Canvas.pushDialog(sceneWindow2D);
   
   	// Create a scene graph.
   	new t2dSceneGraph(starmapScene) //__decl t2dSceneGraph starmapScene
   	{
	   
   };
	
   // Associate the new scene graph with the scene window.
   sceneWindow2D.setSceneGraph(starmapScene);

   $global::starmapScene = starmapScene;
	
   starmapScene.clearScene();
	
   $cameraPos = "512 384 1024 768";
   sceneWindow2D.setCurrentCameraPosition($cameraPos);
	
   $global::starmap = newStarmap(125);
   
   new t2dStaticSprite(bill)
   {
      scenegraph = $global::starmapScene;
      imagemap = starImageMap;
      layer = 1;
      class = Star;
      frame = 1;
      position = %position;
      useObjectMouseEvents = true;
   };
}
function shutdownProject()
{
   // This is function is the last one called by GG infrastructure (I think)
}
function newStarmap(%numberofstars )
{
   %this = new ScriptObject()
   {
      class = "Starmap";
   };
	
   // Backfield
   %this.Backfield = new t2dStaticSprite()
   {
      scenegraph = $global::starmapScene;
      imagemap = BackfieldImageMap;
      layer = 30;
      position = "512 384";
      size = "1024 768";
   };
	
   %this.stars = new SimSet();
		
   for(0; %x <%numberofstars; %x++)
   {		
      %position = getRandom(1024) SPC getRandom(768);
      %star = newStar(%position);
      %this.stars.add( %star );
   }
}
function newStar(%position, %name )
{
   %star = new t2dStaticSprite()
   {
      scenegraph = $global::starmapScene;
      imagemap = starImageMap;
      layer = 1;
      class = Star;
      frame = 1;
      position = %position;
      useObjectMouseEvents = true;
   };
	
   return %star;
}
function Star::onRightMouseUp(%this, %modifier, %worldPos, %mouseClicks)
{
   echo( "Star::RightMouseUP" SPC DescribeSelf(%this) );
}
function setupKeybinds()
{
   new ActionMap(moveMap);
   //moveMap.bind("keyboard", "a", "doAction", "Action Description");
}
function SceneWindow2D::onMouseUp(%this, %modifier, %worldPos, %mouseClicks)
{
	// grab a list of objects at the point we're clicking
	%pickList = starmapScene.pickPoint(%worldPos);
	
	// first check to see if we have any objects
	if(%pickList !$= "")
	{
		// get a count of the objects in the list
		%pickCount = getWordCount(%pickList);
		
		// loop through each object
		for(%i=0;%i<%pickCount;%i++)
		{
			// grab the current object
			%obj = getWord(%pickList, %i);
			
			if( %obj == $global.starmapScene )
			{
				echo( "RightMouseUP" SPC DescribeSelf(%obj) );
			}
			else
			{
				echo( DescribeSelf(%obj) );
			}
		}
	}   
}
function DescribeSelf(%obj)
{
   %name = "not_named";
   if( ! %obj.getName() $= "" )
   {
      %name = %obj.getName() @ "[" @ %obj @ "]";
   }
   
   %class = "no_class";
   if( strcmp( %obj.class, "") != 0 )
   {
      %class = %obj.class;
   }
   
   %superclass = "no_superclass";
   if( strcmp( %obj.superclass, "") != 0 )
   {
      %superclass = %obj.superclass;
   }
   
   return "(" @ %name @ ")[" @ %obj @ "]->" @ %class @ "->" @ %superclass @ "^";
}
function star::test(%this)
{
   echo("TEST");
}

PS Hi Matt!
#16
04/15/2006 (6:58 pm)
Well, I could never figure out what is wrong with it. So here is my hack. Basically it used the scenewindow's mouse callbacks to forward them onto the first object that has a callback under the pick point. I haven't tested it for all the scenarios and I'm expecting to have problems with the dragged events, but until then:

// some code from the checkersdemo mouse.cs

// This function is triggered from the engine when the mouse is pressed "down"
function SceneWindow2D::onMouseDown(%this, %modifier, %worldPos, %mouseClicks)
{
   %this.forwardMouseCallback( 1, %modifier, %worldPos, %mouseClicks );
}

// This function is triggered from the engine when the mouse is let "up"
function SceneWindow2D::onMouseUp(%this, %modifier, %worldPos, %mouseClicks)
{
   %this.forwardMouseCallback( 2, %modifier, %worldPos, %mouseClicks );
}

// This function is triggered from the engine when the mouse is "moving"
function SceneWindow2D::onMouseMove(%this, %modifier, %worldPos, %mouseClicks)
{
   %this.forwardMouseCallback( 3, %modifier, %worldPos, %mouseClicks );
}

// This function is triggered from the engine when the mouse is "dragged"
function SceneWindow2D::onMouseDragged(%this, %modifier, %worldPos, %mouseClicks)
{
   %this.forwardMouseCallback( 4, %modifier, %worldPos, %mouseClicks );  
}

// This function is triggered from the engine when the right mouse is let "down"
function SceneWindow2D::onRightMouseDown(%this, %modifier, %worldPos, %mouseClicks)
{
   %this.forwardMouseCallback( 5, %modifier, %worldPos, %mouseClicks );
}

// This function is triggered from the engine when the right mouse is let "up"
function SceneWindow2D::onRightMouseUp(%this, %modifier, %worldPos, %mouseClicks)
{
   %this.forwardMouseCallback( 6, %modifier, %worldPos, %mouseClicks );
}

// This function is triggered from the engine when the right mouse is "dragged"
function SceneWindow2D::onRightMouseDragged(%this, %modifier, %worldPos, %mouseClicks)
{
   %this.forwardMouseCallback( 7, %modifier, %worldPos, %mouseClicks );
}

function SceneWindow2D::forwardMouseCallback( %this, %methodId, %modifier, 
   %worldPos, %mouseClicks )
   {      
      // grab a list of objects at the point we're clicking
      %pickList = %this.getSceneGraph().pickPoint( %worldPos );
      
      // first check to see if we have any objects
      if( %pickList !$= "" )
      {
         // get a count of the objects in the list
         %pickCount = getWordCount( %pickList );
         
         // loop through each object
         for( %i = 0; %i < %pickCount; %i++ )
         {
            // grab the current object
            %obj = getWord( %pickList, %i );
            
            switch( %methodId )
            {
               case 1 : // onMouseDown
                  if( %obj.isMethod( "onMouseDown" ) )
                  {
                     %obj.onMouseDown( %modifier, %worldPos, %mouseClicks );
                     return;
                  }
                  break;
               case 2 : // onMouseUp
                  if( %obj.isMethod( "onMouseUp" ) )
                  {
                     %obj.onMouseUp( %modifier, %worldPos, %mouseClicks );
                     return;
                  }
                  break;
               case 3 : // onMouseMove
                  if( %obj.isMethod( "onMouseMove" ) )
                  {
                     %obj.onMouseMove( %modifier, %worldPos, %mouseClicks );
                     return;
                  }
                  break;
               case 4 : // onMouseDragged
                  if( %obj.isMethod( "onMouseDragged" ) )
                  {
                     %obj.onMouseDragged( %modifier, %worldPos, %mouseClicks );
                     return;
                  }
                  break;
               case 5 : // onRightMouseDown
                  if( %obj.isMethod( "onRightMouseDown" ) )
                  {
                     %obj.onRightMouseDown( %modifier, %worldPos, %mouseClicks );
                     return;
                  }
                  break;
               case 6 : // onRightMouseUp
                  if( %obj.isMethod( "onRightMouseUp" ) )
                  {
                     %obj.onRightMouseUp( %modifier, %worldPos, %mouseClicks );
                     return;
                  }
                  break;
               case 7 : // onRightMouseDragged
                  if( %obj.isMethod( "onRightMouseDragged" ) )
                  {
                     %obj.onRightMouseDragged( %modifier, %worldPos, %mouseClicks );
                     return;
                  }
                  break;
               default :
                  warn( "Unexpected methodId on sceneWindow2D::forwardMouseCallback!" );
                  break;
            }
         }
      }
   }
#17
04/16/2006 (9:02 pm)
So what is the workaround to the problem that the original poster reported, and Mathew said is a known issue and will be fixed? We don't have the source to ./scripts/levelManagement.ed.cs so there isn't even a way to forward mouse events AFAIK. Help - this is a show stopper to using the Level Builder to test/debug an actual level!