Game Development Community

Assigning methods to dynamically created controls

by John Doppler Schiff · in Torque Game Engine · 03/08/2007 (3:41 pm) · 6 replies

Hi guys,
I'm sitting here with the outline of my spacebar embossed on my forehead because I've been banging my head against the keyboard for days.

I'm trying to create multiple instances of a chat window. I've got a function that creates the MessageVector and the GUI elements, no problem.

The gotcha is that I want to capture mouseEnter and mouseLeave events with a GUIMouseEventsCtrl and fire off a particular function.

How can I assign a custom onMouseEnter() function to the dynamically created control?

The GUI control is generated like this, pretty straightforward...
new GuiMouseEventCtrl(%mouseTrapName) {
      canSaveDynamicFields = "0";
      Profile = "GuiDefaultProfile";
      HorizSizing = "relative";
      VertSizing = "relative";
      position = %windowPos;
      Extent = %windowExt;
      MinExtent = "8 2";
      canSave = "1";
      Visible = "1";
      hovertime = "1000";
      lockMouse = "0";
   };

This won't work, however...
%mouseTrapName::onMouseEnter() {
     echo("Snap!  Mouse entered the window.");
}

TIA!

#1
03/08/2007 (4:13 pm)
You could do something like this:
new GuiMouseEventCtrl(AChatWindow) {
      blah   = blah;
      myName = %mouseTrapName;
   };
   
...


function AChatWindow::onMouseEnter(%this)
{
   switch$(%this.myName)
      case "foo":
         blah();
      case "bar":
         blah();
}
#2
03/08/2007 (8:58 pm)
Ah, that should work... I was looking for something a little more OOP-y, but that should do nicely.

Thanks again!
#3
03/08/2007 (9:26 pm)
Use the classname as the namespace:

function GuiMouseEventCtrl::onMouseEnter(%this)
{
  echo("Mouse has entered chat window ID:" SPC %this.getId());
}
#4
03/09/2007 (12:05 am)
Quote:I was looking for something a little more OOP-y

roger that.

an alternative which avoids the Switch could be something like this:

function makeAControl(%handler)
{
   new GuiMouseEventCtrl(AChatWindow) {
      blah   = blah;
      myHandler = %handler;
   };
}

...

function AChatWindow::onMouseEnter(%this)
{
   call(%this.myHandler, %this);
}

function myHandler1(%ctrl)
{
   echo("myHandler1: mouse entered control" SPC %ctrl);
}

function myHandler2(%ctrl)
{
   echo("myHandler2: mouse entered control" SPC %ctrl);
}

...

makeAControl("myHandler1");
makeAControl("myHandler2");

etc.

..
#5
03/09/2007 (10:25 am)
Beautiful! =D
#6
03/09/2007 (11:12 am)
W00t !