Code: Select all
typedef void BUTTON_CALLBACK(void* something);
//first i define an array to hold pointers to callback functions
BUTTON_CALLBACK *button_callbaks[GB_NUM_OF_GUI_BUTTONS];
//to set a callback i use
void GameManager::SetButtonCallback( GUI_BUTTONS button, BUTTON_CALLBACK* function ) {
this->button_callbaks[button] = function;
}
// when i create a button i do this
void ShowMainMenu() {
irr::core::rect<irr::s32> buttonpos(SX(10),SY(200),SX(140),SY(240));
gameManager.SetButtonCallback(GB_PRACTICE,c_StartPracticeGame); grr.env->addButton(buttonpos, 0,GB_PRACTICE, L"Practice"); buttonpos.UpperLeftCorner.Y += SY(50); buttonpos.LowerRightCorner.Y += SY(50);
gameManager.SetButtonCallback(GB_MULTIPLAYER,c_ShowMultiPlayerMenu); grr.env->addButton(buttonpos, 0, GB_MULTIPLAYER, L"Multiplayer");
}
//then in my eventhandler i do this
int id = event.GUIEvent.Caller->getID();
switch(event.GUIEvent.EventType) {
case irr::gui::EGET_BUTTON_CLICKED: {
(*(this->button_callbaks[id]))(NULL); //so clean :)
}
}