GuiControl::onMouseEnterBounds
by Orion Elenzil · 01/18/2006 (5:17 pm) · 4 comments
Motivation:
You've got a container control and you want to know when the mouse leaves or enters its boundary.
However, the container has say a button on it.
When the mouse transitions from being over just the container to being over both the container and the buton, the container will receive an onMouseLeave(). Similarly when you mouse off the button back onto just the container, the container gets an onMouseEnter.
The Resource seems to work fairly well,
including the case where the child control is right at the edge of the parent.
Let me know i'm overlooking something.
code:
guiControl.h
after "S32 mVertSizing; ///< Set from vertSizingOptions.:, add:
after "virtual void onMouseLeave(const GuiEvent &event);", add:
guiControl.cc
after "mVertSizing = vertResizeBottom;", add:
after the function onMouseLeave, add:
guiCanvas.cc
Two additional calls in the function findMouseControl() are shown in bold:
You've got a container control and you want to know when the mouse leaves or enters its boundary.
However, the container has say a button on it.
When the mouse transitions from being over just the container to being over both the container and the buton, the container will receive an onMouseLeave(). Similarly when you mouse off the button back onto just the container, the container gets an onMouseEnter.
The Resource seems to work fairly well,
including the case where the child control is right at the edge of the parent.
Let me know i'm overlooking something.
code:
guiControl.h
after "S32 mVertSizing; ///< Set from vertSizingOptions.:, add:
bool mMouseInBounds;
after "virtual void onMouseLeave(const GuiEvent &event);", add:
/// these don't trigger due to transitions with child controls:
virtual void onMouseEnterBounds (const GuiEvent &event);
virtual void onMouseLeaveBounds (const GuiEvent &event);
virtual void tryMouseEnterBounds(const GuiEvent &event);
virtual void tryMouseLeaveBounds(const GuiEvent &event);guiControl.cc
after "mVertSizing = vertResizeBottom;", add:
mMouseInBounds = false;
after the function onMouseLeave, add:
void GuiControl::tryMouseEnterBounds(const GuiEvent &event)
{
if (!mMouseInBounds) {
mMouseInBounds = true;
onMouseEnterBounds(event);
GuiControl *parent = getParent();
if ( parent )
parent->tryMouseEnterBounds( event );
}
}
void GuiControl::tryMouseLeaveBounds(const GuiEvent &event)
{
if (mMouseInBounds) {
if (!cursorInControl()) {
mMouseInBounds = !true;
onMouseLeaveBounds(event);
GuiControl *parent = getParent();
if ( parent ) {
parent->tryMouseLeaveBounds( event );
}
}
}
}
void GuiControl::onMouseEnterBounds(const GuiEvent &event)
{
}
void GuiControl::onMouseLeaveBounds(const GuiEvent &event)
{
}guiCanvas.cc
Two additional calls in the function findMouseControl() are shown in bold:
void GuiCanvas::findMouseControl(const GuiEvent &event)
{
if(size() == 0)
{
mMouseControl = NULL;
return;
}
GuiControl *controlHit = findHitControl(event.mousePoint);
if(controlHit != static_cast<GuiControl*>(mMouseControl))
{
if(bool(mMouseControl)) {
mMouseControl->onMouseLeave(event);
[b]mMouseControl->tryMouseLeaveBounds(event);[/b]
}
mMouseControl = controlHit;
mMouseControl->onMouseEnter(event);
[b]mMouseControl->tryMouseEnterBounds(event);[/b]
}
}About the author
#3
08/17/2007 (10:10 am)
Thanks Orion. Your code works great with 1.5.2 and it really helped my work.
#4
a shortcoming with this resource is the following:
suppose you have a setup like this:
- ctrl B receives enter bounds event
* mouse continues through point 2.
- ctrl A does *not* receive an event
.. well, that's sufficient to show the problem.
the intent was that a given control would receive this event whenever the mouse entered or left the area contained by that control, but it doesn't always work out that way.
i'm looking at how to make it work that way,
but i'm not sure i'll ever actually do it, as i think the performance price will be relatively high.
10/23/2008 (11:38 pm)
note,a shortcoming with this resource is the following:
suppose you have a setup like this:
________
| |
| |
| |
____________| |_______
| | | |
| | | |
| ctrl A | ctrl B 3 4
| | | |
|____________| 2 |_______|
| |
| |
| |
|____1___| 5* mouse enters ctrl B at point 1.- ctrl B receives enter bounds event
* mouse continues through point 2.
- ctrl A does *not* receive an event
.. well, that's sufficient to show the problem.
the intent was that a given control would receive this event whenever the mouse entered or left the area contained by that control, but it doesn't always work out that way.
i'm looking at how to make it work that way,
but i'm not sure i'll ever actually do it, as i think the performance price will be relatively high.

Torque 3D Owner Brandon Maness
I've looked through your recent resources and think they are quite useful. I really need to post some little fixes/tweaks I've made to 1.4.. See, you're getting me motivated!
Thanks for taking the time to share with the community.
Brandon.