Game Development Community

OnMouseDown event of gui control doesn't work?

by Harry Chow · in Torque Game Engine · 09/07/2004 (7:10 pm) · 10 replies

Hi guys here.

I just need onMouseDown event of a guicontrol, codes are here:

new GuiButtonCtrl(MouseTest) {
profile = "GuiButtonProfile";
horizSizing = "right";
vertSizing = "top";
position = "36 367";
extent = "110 20";
minExtent = "8 8";
visible = "1";
command = "MouseTest::OnMouseDown()";
text = "MouseTest";
groupNum = "-1";
buttonType = "PushButton";
helpTag = "0";
};

function MouseTest::OnMouseDown()
{
echo("OnMouseDown");
}

But it doesn't work and log shows syntax error.
Also, I tried this:

new GuiButtonCtrl(MouseTest) {
profile = "GuiButtonProfile";
horizSizing = "right";
vertSizing = "top";
position = "36 367";
extent = "110 20";
minExtent = "8 8";
visible = "1";
command = "EchoMe()";
text = "MouseTest";
groupNum = "-1";
buttonType = "PushButton";
helpTag = "0";
};

EchoMe() works, but it is called when mouse is up.

Any one can help me? Thanks a lot.

#1
09/07/2004 (8:19 pm)
[quote]
syntax error
[quote]

what is the error you get?

OnMouseDown works, it depends on how you setup your GUI. If you have this control on top of another that also processes the OnMouseDown event, then your control will not get it unless you tell the parent control to pass it to the children.
#2
09/07/2004 (8:21 pm)
"Error in input" is the syntax error message.

There is only one GUIControl has onMouseDown event.

Will you please give me a simple sample?
#3
09/07/2004 (11:32 pm)
The log should say what line and on what character the syntax error occured. Can you post that information?
#4
09/08/2004 (1:40 am)
Hmm, I think the problem is:
wrong line:
...
command = "MouseTest::OnMouseDown()";
...

You have yo add the ;

...
command = "MouseTest::OnMouseDown()[b];[/b]";
....

Ooops, I've seen later that echoMe() works, so the problem shouldn't be the missing ;
Anyways the command is executed on mouse up, becuase this is the way that the buttons work, to obtain what you want I suppose you have to change the C++ code
#5
09/08/2004 (3:42 pm)
Sorry for the delay an answering Vincent, and I should of remembered I made changes myself.

The OnMouseDown is not a normal GuiComponent called script event. GuiButtonCtrl does not perform a script level OnMouseDown.

There are two ways you can go about this.

You can add GuiMouseEventCtrl to every control in your GUI you want to capture mouse events because this control DOES fire off the appropriate script.

OR

you can change your inheritance (which is what I had done)

class GuiButtonBaseCtrl : public GuiControl

to

class GuiButtonBaseCtrl : public GuiMouseEventCtrl

and a few other spots in the code.

The command in the GUI script as in your example only gets called on mouse up events, and the call is done via C++ not script.

If you look at the following function in GuiButtonBaseCtrl
void GuiButtonBaseCtrl::onMouseUp(const GuiEvent &)
{
   if (! mActive)
      return;
   
   mouseUnlock();

   setUpdate();
   
   //if we released the mouse within this control, perform the action
   if (mDepressed)
      onAction();

   mDepressed = false;
}

Your will see that it calls a C++ function called onAction()
void GuiControl::onAction()
{
   if (! mActive)
      return;
   
   //execute the console command
   if (mConsoleCommand[0])
   {
   	  char buf[16];
      dSprintf(buf, sizeof(buf), "%d", getId());
      Con::setVariable("$ThisControl", buf);
      Con::evaluate(mConsoleCommand, false);
   }
   else
      Con::executef(this, 1, "onAction");
}

onAction evaluates the 'command' you gave it in the GUI. Also note, if no command, it will call a script level onAction. All this from a mouse up event.

As a side note, there is also an altCommand. Not all the controls will use it. But I made a minor change to my button code so that a right mouse click would evaluate it.


humm, I went on a bit of a tangent, but I thought a more indepth explaination was required.

Hope that helps
#6
09/08/2004 (4:40 pm)
Thank you all guys.

Finally I have modified engine source to achieve my target and the solution is only on statement:-)

As we know GuiButtonBaseCtrl is the basic class of all gui buttion controls and I add:

Con::executef(this, 1, "onMouseDown");
at the end of function
void GuiButtonBaseCtrl::onMouseDown(const GuiEvent &event)

and

Con::executef(this, 1, "onMouseEnter");
at the end of function
void GuiButtonBaseCtrl::onMouseEnter(const GuiEvent &event)

and

Con::executef(this, 1, "onMouseLeave");
at the end of function
void GuiButtonBaseCtrl::onMouseLeave(const GuiEvent &)

Now we can do something at the right moment. For example in the script:

First we declare a gui button like above:
new GuiButtonCtrl(MouseTest) {
profile = "GuiButtonProfile";
horizSizing = "right";
vertSizing = "top";
position = "36 367";
extent = "110 20";
minExtent = "8 8";
visible = "1";
text = "MouseTest";
groupNum = "-1";
buttonType = "PushButton";
helpTag = "0";
};

and then......
function MouseTest::onMouseDown(%this)
{
// do whatever you want here
echo("I made it!");
}

My addtional functions are very simple. You guys can add more features such as more parameters to enhance them.
#7
09/18/2004 (2:46 pm)
I found another way to get those mouse events using only script:

// Begin Code

//
// Icon
//

if(isObject(Icon))
Icon.delete();

%ihandle = new GuiBitmapCtrl(Icon)
{
bitmap = "neural.fps/client/ui/gglogo150";
extent = "50 50";
position = "10 10";

//new GuiBitmapCtrl()

};

%ireact = new guiMouseEventCtrl(IconReact)
{
parent = %ihandle;
bitmap = "neural.fps/client/ui/gglogo150";
extent = "50 50";
position = "0 0";

};

%ihandle.add(%ireact);

function IconReact::onMouseDown(%this)
{
echo(%this);
echo(%this.parent);
//%this.parent.delete();
}

function IconReact::onMouseDragged(%this)
{
//echo(%this);
//echo(%this.parent);
//%this.parent.delete();

}

//echo(%ihandle);
//TestRoot.add(Icon);
NeuralEdit.add(Icon);

// End Code

Try it out. It seems to work great.

Does anybody know how to get the mouse position through script? I need this to implement my drag function.

Thanks,
Frank
#8
09/09/2005 (12:40 pm)
Bringing this back instead of starting a new thread. Has anybody found an easier way to script mousedown events in the gui. No engine changes, just through scripting. What Frank has above makes no sense to me and seems a little difficult to implement in what I'm doing, not a experienced scripter at all. Also, they talked about adding a MouseEventCtrl to a button, which could work. I can't find anything about how to do that or what sort of syntax is involved. thanks to everybody and for any help
#9
10/07/2005 (10:11 am)
@Daniel

If you haven't yet figured out how to do this (and for anyone else who is looking at the same problem)...
Making a MouseEventCtrl is rather simple. The easiest way is to load up the exe and open up the GUI Editor (F10 by default).
Find the GUI you want to add mouse events to and click that. (not sure if that step is really needed, but I do it anyway)
Click "New Control" and find GuiMouseEventCtrl.
Now you will get a transparent looking area with resize handles.
Resize that to the area you want the mouse clicks to be captured in.
Now in the properties of that control name it something useful: i.e. mouseHandler.
MAKE SURE YOU HIT APPLY (I forget all the time)
Now save the Gui to the directory you are using.

Open up the gui file in a script/text editor.
Add this:
function mouseHandler::onMouseDown(%this)
  {
    //Do something in here
  }
Replace mouseHandler with whatever you named your control and you're set!
Acceptable ones are onMouseDown, onMouseUp, onMouseMove, onMouseDragged... I think that's all. May want to double check that.
#10
10/07/2005 (11:18 am)
Mod Note: Dan's explanation belongs in TDN!