Page 1 of 1
[solved] Custom EventArgs
Posted: Sun Apr 13, 2008 18:39
by mrzli777
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.
Posted: Mon Apr 14, 2008 10:14
by CrazyEddie
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.
Posted: Mon Apr 14, 2008 12:40
by mrzli777
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.
Posted: Mon Apr 14, 2008 19:26
by scriptkid
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?
Posted: Sat Apr 19, 2008 08:30
by mrzli777
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!
Posted: Mon Jun 02, 2008 10:34
by DarioFrodo
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();
Posted: Mon Jun 02, 2008 16:24
by mrzli777
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 &).
Posted: Mon Jun 02, 2008 18:39
by DarioFrodo
Posted: Tue Jun 03, 2008 06:47
by mrzli777
np