Page 1 of 1

EventMouseEnters is not firing

Posted: Mon Apr 23, 2007 19:53
by sjcomp
I am trying to handle event when mouse enters a push button but it's not firing. I can handle mouse clicks just fine

Code: Select all

// This works!
CEGUI::GlobalEventSet::getSingleton().subscribeEvent(
   CEGUI::PushButton::EventNamespace + "/" + CEGUI::PushButton::EventClicked,
   CEGUI::Event::Subscriber(&CCeguiGUI::ButtonPressed,this));

// This does not work
CEGUI::GlobalEventSet::getSingleton().subscribeEvent(
   CEGUI::Window::EventMouseEnters,
   CEGUI::Event::Subscriber(&CCeguiGUI::ButtonEnter,this));


I also tried this, but with no luck:

Code: Select all

mRoot->subscribeEvent(CEGUI::Window::EventMouseEnters,
   CEGUI::Event::Subscriber(&CCeguiGUI::ButtonEnter,this));

What am I doing wrong?

Posted: Tue Apr 24, 2007 13:45
by lindquist
You are not prefixing the event namespace properly. You do it correctly in the example that works, but not in the one that doesn't.

e.g:

Code: Select all

// This should work
CEGUI::GlobalEventSet::getSingleton().subscribeEvent(
      CEGUI::Window::EventNamespace + "/" + CEGUI::Window::EventMouseEnters,
   CEGUI::Event::Subscriber(&CCeguiGUI::ButtonEnter,this));


note that this will fire for ALL widgets. not just buttons.

Posted: Tue Apr 24, 2007 19:06
by sjcomp
Thanks, lindquist. Now I see. I want to have only push buttons, so I've tried:

Code: Select all

CEGUI::PushButton::EventNamespace + "/" + CEGUI::PushButton::EventMouseEnters,

But this does not seem to work. (It works for all the windows as you show). Basically I want to play a sound when mouse hovers over a button (first enters it) a-la half life style.

How do I limit these events to buttons only?

Posted: Tue Apr 24, 2007 19:11
by scriptkid
Hi,

i have not checked the sources for a solution, but a workaround might be a check on the window type which causes the event. And only play a sound when the event was fired from a real button.

HTH.

Posted: Tue Apr 24, 2007 19:40
by sjcomp
Thanks, scriptkid. That is what I am doing. But type of the button depends on the style, so it's given as "TaharezLook/Button". Of course, I could check if type contains "/Button" at the end, but it still looks like a hack as opposed to a proper solution.

Posted: Tue Apr 24, 2007 20:31
by Rackle
I modified WidgetGalore to play with this idea. Here are the two modifications, within the WidgetGalore.h file.

Just below the btnClose->setText("Exit"); line, within the initialiseSample() function:

Code: Select all

btnClose->subscribeEvent(CEGUI::PushButton::EventMouseEnters, CEGUI::Event::Subscriber(&DemoSample::onMouseEnters,   this));
btnClose->subscribeEvent(CEGUI::PushButton::EventMouseLeaves, CEGUI::Event::Subscriber(&DemoSample::onMouseLeaves,   this));


After the void cleanupSample(void) function, two new functions:

Code: Select all

bool onMouseEnters(const CEGUI::EventArgs& e)
{
   const CEGUI::WindowEventArgs& we = static_cast<const CEGUI::WindowEventArgs&>(e);
   CEGUI::WindowManager& winMgr = CEGUI::WindowManager::getSingleton();
   if(we.window)
   {
      we.window->setText("Enters");
   }
   return true;
}

bool onMouseLeaves(const CEGUI::EventArgs& e)
{
   const CEGUI::WindowEventArgs& we = static_cast<const CEGUI::WindowEventArgs&>(e);
   CEGUI::WindowManager& winMgr = CEGUI::WindowManager::getSingleton();
   if(we.window)
   {
      we.window->setText("Leaves");
   }
   return true;
}


This code is "aware" of the mouse starting to hover over the pushbutton and when it leaves the pubshbutton. That code is also generic; multiple pushbuttons can use it, as long as each calls subscribeEvent().

Maybe you need to replace CEGUI::Window with CEGUI::PushButton.

Hope this helps.

Posted: Tue Apr 24, 2007 22:52
by Rackle
Here's nearly the same code, but this time with global events:

Code: Select all

CEGUI::GlobalEventSet::getSingleton().subscribeEvent(CEGUI::Window::EventNamespace + "/" + CEGUI::PushButton::EventMouseEnters, CEGUI::Event::Subscriber(&DemoSample::onMouseEnters,   this));
CEGUI::GlobalEventSet::getSingleton().subscribeEvent(CEGUI::Window::EventNamespace + "/" + CEGUI::PushButton::EventMouseLeaves, CEGUI::Event::Subscriber(&DemoSample::onMouseLeaves,   this));


Code: Select all

bool onMouseEnters(const CEGUI::EventArgs& e)
{
   const CEGUI::WindowEventArgs& we = static_cast<const CEGUI::WindowEventArgs&>(e);
   if(we.window->testClassName( CEGUI::PushButton::EventNamespace )
      && !we.window->isAutoWindow() )
   {
      // It's a push button (and not an auto window, from scrollbars)
      CEGUI::WindowManager& winMgr = CEGUI::WindowManager::getSingleton();
      CEGUI::PushButton* btnClose = static_cast<CEGUI::PushButton*>(winMgr.getWindow("btnClose"));
      CEGUI::Window* frameWindow = static_cast<CEGUI::Window*>(winMgr.getWindow("StaticText"));
      btnClose->setText("Enters");
      frameWindow->setText( we.window->getName() );
   }
   return true;
}

bool onMouseLeaves(const CEGUI::EventArgs& e)
{
   const CEGUI::WindowEventArgs& we = static_cast<const CEGUI::WindowEventArgs&>(e);
   if(we.window->testClassName( CEGUI::PushButton::EventNamespace )
      && !we.window->isAutoWindow() )
   {
      // It's a push button (and not an auto window, from scrollbars)
      CEGUI::WindowManager& winMgr = CEGUI::WindowManager::getSingleton();
      CEGUI::PushButton* btnClose = static_cast<CEGUI::PushButton*>(winMgr.getWindow("btnClose"));
      CEGUI::Window* frameWindow = static_cast<CEGUI::Window*>(winMgr.getWindow("StaticText"));
      btnClose->setText("Leaves");
      frameWindow->setText( we.window->getName() );
   }
   return true;
}


The trick was to use CEGUI::Window::EventNamespace + "/" + CEGUI::PushButton::EventMouseEnters rather than CEGUI::PushButton::EventNamespace + "/" + CEGUI::PushButton::EventMouseEnters.

I used CEGUI::PushButton::EventNamespace as an attempt to use a pre-defined "PushButton" string, rather than using a litteral string. It feels like a hack, but PushButton::WidgetTypeName is not adequate; it contains "CEGUI/PushButton", with the "CEGUI/" prefix preventing it from working with the call to testClassName().

These global events are nifty; I didn't even know they existed. But now I see so many ways in which they can be useful...

Posted: Wed Apr 25, 2007 13:43
by sjcomp
Rackle wrote:The trick was to use CEGUI::Window::EventNamespace + "/" + CEGUI::PushButton::EventMouseEnters rather than CEGUI::PushButton::EventNamespace + "/" + CEGUI::PushButton::EventMouseEnters.

It does work :) Though there is still a need to check if it is a button that fired this event. So I might as well use Window in both parts. On the other hand I liked the way you tested if it is a button or no. It looks like an appropriate way to do so. Thanks a lot.