C ++ callback in another class
Here is my problem, I have two classes, MainMenuState and MainMenuUI. MainMenuState has a MainMenuUI member. Basically, I want to do all my GUI initializations in MainMenuUI
void GameUI::MainMenuUI::initUi()
{
std::shared_ptr<GUI::Label> testLabel2(new GUI::Label);
testLabel2->setFont(m_font);
testLabel2->setText("TestLabel");
testLabel2->setFontColor(sf::Color(0, 0, 0));
testLabel2->setFontSize(16);
testLabel2->setRelativePosition(GUI::Position::RIGHT, GUI::Position::BOTTOM, -5, -5);
addComponent("label2", testLabel2);
std::shared_ptr<GUI::MainMenuPanel> mainMenu(new GUI::MainMenuPanel(font));
mainMenu->setRelativePosition(GUI::Position::MIDDLE, GUI::Position::MIDDLE);
mainMenu->open();
// Problem !---------------
/*mainMenu->setPlayButtonCallback([this]{
requestStackPop(); // This method belongs to MainMenuState base class and is protected
requestStackPush(States::AnotherState); // This method belongs to MainMenuState base class and is protected
});
mainMenu->setExitButtonCallback([this]{
requestStackPop(); // This method belongs to MainMenuState base class and is protected
});*/
//--------------------
addComponent("mainMenu", mainMenu);
}
As you can see, the problem is "How do I set my callbacks directly in my MainMenuUI class?" Can friends help me?
thank
+3
source to share
1 answer
If you don't want to expose these methods to the world, you can create a class that can access those methods and provide an interface like this and pass an instance of that class as a parameter to the callback:
mainMenu->setPlayButtonCallback([this](Accessor acc){
acc.requestStackPop();
acc.requestStackPush(States::AnotherState);
});
+1
source to share