[SettingsMenuState] Prepared to add input settings.

This commit is contained in:
Quentin Bazin 2018-12-28 06:45:19 +01:00
parent e1331b4815
commit 07b150e9c8
4 changed files with 50 additions and 11 deletions

View File

@ -20,6 +20,8 @@ class MenuWidget : public Widget {
public:
MenuWidget(u16 width, u16 height, Widget *parent = nullptr);
void reset(u16 width, u16 height);
void onEvent(const SDL_Event &event) override;
TextButton &addButton(u16 x, u16 y, const std::string &text, const TextButton::Callback &callback);

View File

@ -28,6 +28,10 @@ class SettingsMenuState : public ApplicationState {
void update() override;
private:
void addMainButtons();
void addGraphicsButtons();
void addInputButtons();
void draw(RenderTarget &target, RenderStates states) const override;
MenuWidget m_menuWidget{1, 8};

View File

@ -15,9 +15,14 @@
#include "MenuWidget.hpp"
MenuWidget::MenuWidget(u16 width, u16 height, Widget *parent) : Widget(parent) {
reset(width, height);
}
void MenuWidget::reset(u16 width, u16 height) {
m_width = width;
m_height = height;
m_buttons.clear();
m_buttons.resize(m_width * m_height);
}

View File

@ -29,8 +29,36 @@ SettingsMenuState::SettingsMenuState(ApplicationState *parent) : ApplicationStat
m_background.setSize(SCREEN_WIDTH, SCREEN_HEIGHT);
m_menuWidget.setScale(GUI_SCALE, GUI_SCALE, 1);
m_menuWidget.addButton(0, 7, "Done", [this] (TextButton &) { m_stateStack->pop(); });
addMainButtons();
}
void SettingsMenuState::onEvent(const SDL_Event &event) {
m_menuWidget.onEvent(event);
if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_ESCAPE) {
m_stateStack->pop();
}
}
void SettingsMenuState::update() {
}
void SettingsMenuState::addMainButtons() {
m_menuWidget.addButton(0, 0, "Graphics...", [this] (TextButton &) {
m_menuWidget.reset(1, 8);
addGraphicsButtons();
});
m_menuWidget.addButton(0, 1, "Input...", [this] (TextButton &) {
m_menuWidget.reset(1, 8);
addInputButtons();
});
m_menuWidget.addButton(0, 7, "Done", [this] (TextButton &) { m_stateStack->pop(); });
}
void SettingsMenuState::addGraphicsButtons() {
m_menuWidget.addButton(0, 0, "Render distance: " + std::to_string(World::renderDistance), [] (TextButton &button) {
World::renderDistance = std::max(4, (World::renderDistance + 2) % 16);
button.setText("Render distance: " + std::to_string(World::renderDistance));
@ -52,17 +80,18 @@ SettingsMenuState::SettingsMenuState(ApplicationState *parent) : ApplicationStat
m_menuWidget.addButton(0, 3, "Fullscreen: OFF", [] (TextButton &) {}).setEnabled(false);
m_menuWidget.addButton(0, 4, "Use VSync: OFF", [] (TextButton &) {}).setEnabled(false);
m_menuWidget.addButton(0, 7, "Done", [this] (TextButton &) {
m_menuWidget.reset(1, 8);
addMainButtons();
});
}
void SettingsMenuState::onEvent(const SDL_Event &event) {
m_menuWidget.onEvent(event);
if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_ESCAPE) {
m_stateStack->pop();
}
}
void SettingsMenuState::update() {
void SettingsMenuState::addInputButtons() {
m_menuWidget.addButton(0, 7, "Done", [this] (TextButton &) {
m_menuWidget.reset(1, 8);
addMainButtons();
});
}
void SettingsMenuState::draw(RenderTarget &target, RenderStates states) const {
@ -78,4 +107,3 @@ void SettingsMenuState::draw(RenderTarget &target, RenderStates states) const {
target.draw(m_menuWidget, states);
}