Difference between revisions of "Identifying Multiple Event Sources From A Single Callback"

From CEGUI Wiki - Crazy Eddie's GUI System (Open Source)
Jump to: navigation, search
 
m (Lets use <source> instead of <syntaxhighlight>, it is much less typo-prone)
 
(13 intermediate revisions by 5 users not shown)
Line 1: Line 1:
Sometimes you would like to identify the sender of an event from within your callback function. For example, you would like to execute some common code for your buttons, like triggering a sound to be played on CEGUI::PushButton::EventClicked. Here is an example CEGUI::PushButton we would like to wire up:
+
Sometimes you would like to identify the sender of an event from within your callback function. For example, you would like to execute some common code for your buttons, like triggering a sound to be played on CEGUI::PushButton::EventClicked. Here is an example [http://www.cegui.org.uk/api_reference/classCEGUI_1_1PushButton.html CEGUI::PushButton] we would like to wire up:
  
<pre>
+
<source lang="cpp">
CEGUI::PushButton* button;
+
CEGUI::PushButton* pushButton =
button->subscribeEvent(CEGUI::PushButton::EventClicked, CEGUI::Event::Subscriber(&MainMenu::OnButtonClick, this));
+
  static_cast<CEGUI::PushButton*>
</pre>
+
    (CEGUI::WindowManager::getSingleton().createWindow("WindowsLook/Button", "TheButton"));
  
To do this, simply cast the CEGUI::EventArgs in your callback to a CEGUI::WindowEventArgs. The CEGUI::WindowEventArgs object contains a pointer to the window that sent the event.  Now its as easy as calling CEGUI::Window::getName().  Obviously, you can do much more now that you have a pointer to the window who fired the event, but this is just an example:
+
pushButton->subscribeEvent(CEGUI::PushButton::EventClicked,
 +
                          CEGUI::Event::Subscriber(&MainMenu::OnButtonClick, this));
 +
</source>
  
<pre>
+
To do this, simply cast the [http://www.cegui.org.uk/api_reference/classCEGUI_1_1EventArgs.html CEGUI::EventArgs] in your callback to a [http://www.cegui.org.uk/api_reference/classCEGUI_1_1WindowEventArgs.html CEGUI::WindowEventArgs]. The CEGUI::WindowEventArgs object contains a pointer to the [http://www.cegui.org.uk/api_reference/classCEGUI_1_1Window.html CEGUI::Window] that sent the event. Now its as easy as calling CEGUI::Window::getName(). Obviously, you can do much more now that you have a pointer to the window who fired the event, but this is just an example:
 +
 
 +
<source lang="cpp">
 
bool MainMenu::OnButtonClick(const CEGUI::EventArgs& e)
 
bool MainMenu::OnButtonClick(const CEGUI::EventArgs& e)
 
{
 
{
using namespace CEGUI;
+
  const CEGUI::MouseEventArgs& we =
 +
    static_cast<const CEGUI::MouseEventArgs&>(e);
  
const CEGUI::WindowEventArgs& we = (const CEGUI::WindowEventArgs&)e;
+
  CEGUI::String senderID = we.window->getName();
std::string senderID = we.window->getName().c_str();
+
  
if (senderID == "QuitButton")
+
  if (senderID == "TheButton")
        {
+
  {
            // code for dealing with a "QuitButton"
+
    // code for dealing with a "TheButton"
        }
+
  }
 +
 
 +
  return true;
 
}
 
}
</pre>
+
</source>
 +
 
 +
[[Category:HowTo]]

Latest revision as of 11:20, 28 February 2011

Sometimes you would like to identify the sender of an event from within your callback function. For example, you would like to execute some common code for your buttons, like triggering a sound to be played on CEGUI::PushButton::EventClicked. Here is an example CEGUI::PushButton we would like to wire up:

CEGUI::PushButton* pushButton = 
  static_cast<CEGUI::PushButton*>
    (CEGUI::WindowManager::getSingleton().createWindow("WindowsLook/Button", "TheButton"));
 
pushButton->subscribeEvent(CEGUI::PushButton::EventClicked,
                           CEGUI::Event::Subscriber(&MainMenu::OnButtonClick, this));

To do this, simply cast the CEGUI::EventArgs in your callback to a CEGUI::WindowEventArgs. The CEGUI::WindowEventArgs object contains a pointer to the CEGUI::Window that sent the event. Now its as easy as calling CEGUI::Window::getName(). Obviously, you can do much more now that you have a pointer to the window who fired the event, but this is just an example:

bool MainMenu::OnButtonClick(const CEGUI::EventArgs& e)
{
  const CEGUI::MouseEventArgs& we = 
    static_cast<const CEGUI::MouseEventArgs&>(e);
 
  CEGUI::String senderID = we.window->getName();
 
  if (senderID == "TheButton")
  {
    // code for dealing with a "TheButton"
  }
 
  return true;
}