[solved] Custom EventArgs

For help with general CEGUI usage:
- Questions about the usage of CEGUI and its features, if not explained in the documentation.
- Problems with the CMAKE configuration or problems occuring during the build process/compilation.
- Errors or unexpected behaviour.

Moderators: CEGUI MVP, CEGUI Team

mrzli777
Just popping in
Just popping in
Posts: 19
Joined: Fri Jan 04, 2008 11:22

[solved] Custom EventArgs

Postby mrzli777 » Sun Apr 13, 2008 18:39

Hello all.

When I click a button i need to pass a string to the button-click event handler, so I defined a class derived from EventArgs (named MeshSelectedEventArgs) that contains that string.
How can I pass the object of MeshSelectEventArgs type to the event handler?


Here is the class:

Code: Select all

class MeshSelectEventArgs : public CEGUI::EventArgs
{
public:
   string m_meshName;
};



This code and event handler works only for standard EventArgs

Code: Select all

...
pMenuItem->subscribeEvent(CEGUI::PushButton::EventClicked, CEGUI::Event::Subscriber(&CApplication::handleMeshSelect, this));
...

...
bool CApplication::handleMeshSelect(const CEGUI::EventArgs& e)
{
   ...
   return true;
}
...


Thanks in advance.
Last edited by mrzli777 on Sat Apr 19, 2008 08:31, edited 1 time in total.

User avatar
CrazyEddie
CEGUI Project Lead
Posts: 6760
Joined: Wed Jan 12, 2005 12:06
Location: England
Contact:

Postby CrazyEddie » Mon Apr 14, 2008 10:14

Hi,

The button click handler is called by the system, so it's not possible to change the type of the args structure that gets passed (unless you change the code and recompile, of course).

I'm not 100% certain what you're doing, but you may be able to set a user string on the button and extract that from within the handler - you can use the Window::setUserString for that purpose (and Window::getUserString to retrieve it in the handler).

HTH

CE.

mrzli777
Just popping in
Just popping in
Posts: 19
Joined: Fri Jan 04, 2008 11:22

Postby mrzli777 » Mon Apr 14, 2008 12:40

Here the problem:

I have several Ogre mesh files defined inside a folder. I also have a menu in which I can select one of those meshes by clicking on a corresponding menu item.
Since I don't know what meshes will be in that folder at compile time, I add one menu item for each mesh dynamically at run-time. For the same reason, I cannot add a separate event handler for each menu item, so I have only one handler for them all.
What I need is a way to identify which menu item called the handler, or the string of the file name (mesh) linked with that menu item . EventArgs doesn't hold that information.
With setUserString() I can set a string to a window, but I still don't know which window called the event handler.

User avatar
scriptkid
Home away from home
Home away from home
Posts: 1178
Joined: Wed Jan 12, 2005 12:06
Location: The Hague, The Netherlands
Contact:

Postby scriptkid » Mon Apr 14, 2008 19:26

With setUserString() I can set a string to a window, but I still don't know which window called the event handler.


Yes you do :) I think that you can safely cast EventArgs to WindowEventArgs, which contains a member to the context Window:

Code: Select all

bool CApplication::handleMeshSelect(const CEGUI::EventArgs& e)
{
  Window* pContext = static_cast<const WindowEventArgs&>(e).window;

  // Its label
  String text = pContext->getText();
  // Its user data (string)
  String userStr = pContext->getUserString();
  // Its user data (anything)
  void* userData = pContext->getUserData();

  return true;
}


Would that help you?
Check out my released snake game using Cegui!

mrzli777
Just popping in
Just popping in
Posts: 19
Joined: Fri Jan 04, 2008 11:22

Postby mrzli777 » Sat Apr 19, 2008 08:30

First of all, apologies for not replying sooner. I was a bit busy and not online too often, so I didn't check this forum for a few days :)

Anyway, that worked. Thanks a lot!

DarioFrodo
Just popping in
Just popping in
Posts: 4
Joined: Fri May 30, 2008 17:56

Postby DarioFrodo » Mon Jun 02, 2008 10:34

Code: Select all

bool CApplication::handleMeshSelect(const CEGUI::EventArgs& e)
{
  Window* pContext = static_cast<const WindowEventArgs&>(e).window;

  // Its label
  String text = pContext->getText();
  // Its user data (string)
  String userStr = pContext->getUserString();
  // Its user data (anything)
  void* userData = pContext->getUserData();

  return true;
}


I' ve tried this code, but by me it didn't work.

Code: Select all

CEGUI::ButtonBase* button = (CEGUI::ButtonBase*)CEGUI::WindowManager::getSingleton().createWindow("TaharezLook/Button", "Build/1");
button->subscribeEvent(CEGUI::PushButton::EventClicked,  CEGUI::Event::Subscriber(&BuildMenuEventHandler));

bool BuildMenuEventHandler(CEGUI::EventArgs e)
{
   CEGUI::Window* pContext = static_cast<const CEGUI::WindowEventArgs&>(e).window;
   
   DRLog.WriteToLog("Button: %s,", pContext->getName().data());
   return false;
}


And always, if I push that button, my programm crashed and when I debug, the name is a invalid pointer.

Any Idea, what I'm doing wrong?

PS:
DRLog.WriteToLog getting the same parameters as printf();

mrzli777
Just popping in
Just popping in
Posts: 19
Joined: Fri Jan 04, 2008 11:22

Postby mrzli777 » Mon Jun 02, 2008 16:24

Try changing from:

Code: Select all

bool BuildMenuEventHandler(CEGUI::EventArgs e) { ... }

to:

Code: Select all

bool BuildMenuEventHandler(const CEGUI::EventArgs &e) { ... }

So add a const and add make it a reference (add &).

DarioFrodo
Just popping in
Just popping in
Posts: 4
Joined: Fri May 30, 2008 17:56

Postby DarioFrodo » Mon Jun 02, 2008 18:39

Great :D , it works

Thanks so much.

:p :D :D :D :D :D :D :D :D :D :D :p

mrzli777
Just popping in
Just popping in
Posts: 19
Joined: Fri Jan 04, 2008 11:22

Postby mrzli777 » Tue Jun 03, 2008 06:47

np


Return to “Help”

Who is online

Users browsing this forum: No registered users and 23 guests