Updating the menu
So, let's see our code in actual use. Do you remember MenuState
from the previous chapter? There we implemented some menu logic, so you could choose between two options. Now this can be cleaned up a lot; as a result the menu state shrinks drastically.
MenuState::MenuState(StateStack& stack, Context context) : State(stack, context) , mGUIContainer() { ... auto playButton = std::make_shared<GUI::Button>(*context.fonts, *context.textures); playButton->setPosition(100, 250); playButton->setText("Play"); playButton->setCallback([this] () { requestStackPop(); requestStackPush(States::Game); }); mGUIContainer.pack(playButton); }
The constructor initializes the buttons and packs them into the Container
. As you can see, the lambda expression we give to the Button
is the place where we actually describe the action we want the button to do. The rest of the functions are changed to use the GUI container to render and...