[SettingsMenuState] Keybindings are now editable in-game.

This commit is contained in:
Quentin Bazin 2019-01-20 21:33:16 +01:00
parent e62bd77236
commit 6982875301
4 changed files with 43 additions and 0 deletions

View File

@ -18,6 +18,7 @@
#include <gk/gl/Shader.hpp>
#include <gk/graphics/RectangleShape.hpp>
#include "GameKey.hpp"
#include "MenuWidget.hpp"
class SettingsMenuState : public gk::ApplicationState {
@ -45,6 +46,9 @@ class SettingsMenuState : public gk::ApplicationState {
glm::mat4 m_projectionMatrix;
gk::RectangleShape m_background;
u8 m_currentKey = GameKey::Undefined;
TextButton *m_currentKeyButton = nullptr;
};
#endif // SETTINGSMENUSTATE_HPP_

View File

@ -16,9 +16,11 @@
#include <algorithm>
#include <gk/core/ApplicationStateStack.hpp>
#include <gk/core/input/GamePad.hpp>
#include <gk/core/Mouse.hpp>
#include "Config.hpp"
#include "KeyboardHandler.hpp"
#include "SettingsMenuState.hpp"
#include "World.hpp"
@ -44,6 +46,13 @@ void SettingsMenuState::onEvent(const SDL_Event &event) {
if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_ESCAPE) {
m_stateStack->pop();
}
else if (m_currentKeyButton && event.type == SDL_KEYDOWN) {
KeyboardHandler *keyboardHandler = (KeyboardHandler *)gk::GamePad::getInputHandler();
keyboardHandler->setKeycode(m_currentKey, event.key.keysym.sym);
m_currentKeyButton->setText(m_currentKeyButton->text() + keyboardHandler->getKeyName(m_currentKey));
m_currentKeyButton = nullptr;
}
}
void SettingsMenuState::update() {
@ -106,6 +115,30 @@ void SettingsMenuState::addGraphicsButtons() {
}
void SettingsMenuState::addInputButtons() {
std::vector<std::pair<u8, std::string>> keys = {
{GameKey::Up, "Forward"},
{GameKey::Down, "Back"},
{GameKey::Left, "Left"},
{GameKey::Right, "Right"},
{GameKey::Jump, "Jump"},
{GameKey::Fly, "Jetpack"},
// {GameKey::Sneak, "Sneak"},
// {GameKey::Sprint, "Sprint"},
// {GameKey::Dig, "Dig"},
// {GameKey::Use, "Use"},
{GameKey::Inventory, "Inventory"},
};
KeyboardHandler *keyboardHandler = (KeyboardHandler *)gk::GamePad::getInputHandler();
int i = 0;
for (auto &it : keys) {
m_menuWidget.addButton(0, i++, it.second + ": " + keyboardHandler->getKeyName(it.first), [this, it] (TextButton &button) {
button.setText(it.second + ": ");
m_currentKey = it.first;
m_currentKeyButton = &button;
});
}
m_menuWidget.addButton(0, 7, "Done", [this] (TextButton &) {
m_menuWidget.reset(1, 8);
addMainButtons();

View File

@ -16,6 +16,8 @@
namespace GameKey {
enum {
Undefined,
Left,
Right,
Up,

View File

@ -25,6 +25,10 @@ class KeyboardHandler : public gk::InputHandler {
bool isKeyPressed(gk::GameKey key);
SDL_Keycode getKeyCode(gk::GameKey key) { return m_keys[key]; }
std::string getKeyName(gk::GameKey key) { return SDL_GetKeyName(m_keys[key]); }
void setKeycode(gk::GameKey key, SDL_Keycode keycode) { m_keys[key] = keycode; }
private:
std::map<gk::GameKey, SDL_Keycode> m_keys;
};