Adding Mouse Events to Gui Controls
by Tim Newell · 12/21/2001 (2:40 pm) · 21 comments
Adding Mouse Events to Gui Controls
I was thinking that things would be so much easier if controls like GuiTextCtrl and GuiBitmapCtrl had their own mouse events so I wouldn't have to add all of those GuiMouseEventCtrls on top of them. Since the engine does use C++ and OOP I found a very easy solution. All of the Gui components inherit from GuiControl. So I made the simple change of having GuiTextCtrl inherit from GuiMouseEventCtrl instead and voila the guiTextCtrl has mouse events just like if it had a guiMouseEventCtrl on top of it. Since GuiMouseEventCtrl inherits from GuiControl, GuiTextCtrl also still does...just one more step down the "family tree."
Here are the code changes that you need to make. First pick which control you want to have mouse events. (I chose GuitextCtrl and GuiBitmapCtrl since i used them the most and they needed the events) Open the chosen gui control's .h header file (the headers are located in /engine/gui) and make the following changes to the top: (Using GuiTextCtrl for an example)
Add:
and change:
To:
It is as simple as that!
Note: Remember that some controls have their own onMouse events so the MouseEventCtrls that are inherited will be overridden by them.
Edit:Take a look at Micha
I was thinking that things would be so much easier if controls like GuiTextCtrl and GuiBitmapCtrl had their own mouse events so I wouldn't have to add all of those GuiMouseEventCtrls on top of them. Since the engine does use C++ and OOP I found a very easy solution. All of the Gui components inherit from GuiControl. So I made the simple change of having GuiTextCtrl inherit from GuiMouseEventCtrl instead and voila the guiTextCtrl has mouse events just like if it had a guiMouseEventCtrl on top of it. Since GuiMouseEventCtrl inherits from GuiControl, GuiTextCtrl also still does...just one more step down the "family tree."
Here are the code changes that you need to make. First pick which control you want to have mouse events. (I chose GuitextCtrl and GuiBitmapCtrl since i used them the most and they needed the events) Open the chosen gui control's .h header file (the headers are located in /engine/gui) and make the following changes to the top: (Using GuiTextCtrl for an example)
Add:
#ifndef _GUIMOUSEEVENTCTRL_H_ #include "gui/guiMouseEventCtrl.h" #endif
and change:
class GuiTextCtrl : public GuiControl
To:
class GuiTextCtrl : public GuiMouseEventCtrl
It is as simple as that!
Note: Remember that some controls have their own onMouse events so the MouseEventCtrls that are inherited will be overridden by them.
Edit:Take a look at Micha
#2
01/22/2002 (7:57 pm)
mmm... When I did that my engine didin't conpile :-/
#3
01/24/2002 (12:18 am)
What was the errors it gave?
#4
I'll see if I can make a BitmapButton class. That has 2 bitmaps as entries in the gui file. And then does the image switching in the code.
And of course react to the button click.
That would clean up the gui files even more. Because the 3 functions will no longer be needed
01/28/2002 (1:53 pm)
Nice one Tim.. good thinking.I'll see if I can make a BitmapButton class. That has 2 bitmaps as entries in the gui file. And then does the image switching in the code.
And of course react to the button click.
That would clean up the gui files even more. Because the 3 functions will no longer be needed
#5
under the Class definition there is a typedef (in the .h file)
Change it from:
01/29/2002 (11:27 am)
btw.. don't forget to do this to:under the Class definition there is a typedef (in the .h file)
Change it from:
typedef GuiControl Parent;to
typedef GuiMouseEventCtrl Parent;
#6
02/01/2002 (1:48 am)
Yeah I saw that not too long ago but didnt update this do to the fact I never tested to see if it made any kinda side effects.
#7
02/01/2002 (3:44 am)
Well Micha
#9
In the .cc file add:
void GuiBitmapCtrl::onMouseDown(const GuiEvent & event)
{
Parent::onMouseDown(event);
onAction();
}
and in the public section of the .h file add:
void onMouseDown(const GuiEvent & event);
11/22/2002 (1:50 pm)
I had to do a little bit more to get this working, but I like the results :)In the .cc file add:
void GuiBitmapCtrl::onMouseDown(const GuiEvent & event)
{
Parent::onMouseDown(event);
onAction();
}
and in the public section of the .h file add:
void onMouseDown(const GuiEvent & event);
#10
The changes mentioned by Micha
03/25/2006 (2:31 pm)
I just implemented this successfully in TGE 1.4 with just one minor change -- GUI controls under 1.4 are organized under /gui/controls and the define you need to add now looks like this:#ifndef _GUIMOUSEEVENTCTRL_H_
#include "gui/utility/guiMouseEventCtrl.h"
#endifThe changes mentioned by Micha
#11
thanks
04/06/2006 (11:17 am)
nice tutorial, I am curious how how guiMouseEventCtrl actuall works. do you put it on top of another guicontrol? for example, if I want to have a GUiTextCtrl that change its font color when the mouse enters, how can this be done?thanks
#12
void onMouseEnter(const GuiEvent & event);
void onMouseLeave(const GuiEvent & event);
So you could probably use those
04/07/2006 (11:59 pm)
guiMouseEventCtrl has void onMouseEnter(const GuiEvent & event);
void onMouseLeave(const GuiEvent & event);
So you could probably use those
#14
07/01/2006 (1:34 pm)
Can you please recommend a list of other controls which would be a good idea to add this to? I would like to add this to my base code.
#16
01/10/2008 (8:03 am)
Has anyone tried this for TGEA?
#17
Easy implementation.
However, not clear how to use exactly.
I am trying to do an onMouseLeave event for my GuiBitmapCtrl
can anyone walk me thru?
01/10/2008 (9:04 am)
the mod built successfully, thanks.Easy implementation.
However, not clear how to use exactly.
I am trying to do an onMouseLeave event for my GuiBitmapCtrl
can anyone walk me thru?
#18
03/25/2008 (2:13 am)
very good ,thanks
#19
03/25/2008 (7:54 pm)
please, while mouse move on button how switch mouse img? Thanks
#20
03/30/2008 (9:06 pm)

Torque Owner Matt W
I for oen HATE having to go around and add (now) needless guimouseeventctrl
Hurray! Thank you :)