Page 1 of 1

Same event to all Button element. [solved]

Posted: Thu Mar 05, 2009 18:27
by Toge
Hello.

How can I set the same event-function for all Button? I want to play a sound when whatever button is pushed.

Thanks.



for each Button
{
button->subscribeEvent(PushButton::EventMouseClick, Event::Subscriber(&MyClass::playSound, this));

}

Posted: Thu Mar 05, 2009 19:46
by CrazyEddie
Hi,

There is a global event set that you can subscribe to. Each type of object that fires events also defines an event namespace, this is used in conjunction with the event name.

So, for the PushButton, you have CEGUI::PushButton::EventNamespace and CEGUI::PushButton::EventClicked. You use those like this (contrived example):

Code: Select all

using namespace CEGUI;

String globalEventName( PushButton::EventNamespace + '/' + PushButton::EventClicked );

GlobalEventSet::getSingleton().subscribeEvent( globalEventName, myHandlerFunction );


It's important to note that the namespace comes from the originating class. This means that for events such as EventMouseClick (which is different to PushButton::EventClicked) the namespace comes from the Window base class, so Window::EventNamespace.

If you don't want to use the 'EventNamespace' constants, you can use strings also, so you could just specify "PushButton/Clicked" which is shorter, but you may have to look up the namespace and event strings in the code ;)

HTH

CE.