Skip to content

Commit

Permalink
[SettingsMenuState] Mod-defined keys can now be remapped.
Browse files Browse the repository at this point in the history
  • Loading branch information
Unarelith committed Jun 19, 2020
1 parent 54c81a3 commit d11d500
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 13 deletions.
7 changes: 6 additions & 1 deletion source/client/states/GameState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ GameState::GameState()
: m_textureAtlas(gk::ResourceHandler::getInstance().get<TextureAtlas>("atlas-blocks"))
{
Registry::setInstance(m_registry);
Registry::isActive = true;

initShaders();

Expand All @@ -61,6 +62,10 @@ GameState::GameState()
m_world.setCamera(m_player.camera());
}

GameState::~GameState() {
Registry::isActive = false;
}

void GameState::init() {
m_eventHandler->addListener<GuiScaleChangedEvent>(&GameState::onGuiScaleChanged, this);
}
Expand Down Expand Up @@ -120,7 +125,7 @@ void GameState::onEvent(const sf::Event &event) {
}
else if (event.type == sf::Event::KeyPressed) {
for (auto &key : m_registry.keys()) {
if (event.key.code == key.defaultKeyCode()) {
if (event.key.code == key.keycode()) {
m_clientCommandHandler.sendKeyPressed(key.id());
}
}
Expand Down
1 change: 1 addition & 0 deletions source/client/states/GameState.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class TextureAtlas;
class GameState : public gk::ApplicationState {
public:
GameState();
~GameState();

void init() override;

Expand Down
30 changes: 26 additions & 4 deletions source/client/states/SettingsMenuState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include "Config.hpp"
#include "Events.hpp"
#include "KeyboardHandler.hpp"
#include "Registry.hpp"
#include "SettingsMenuState.hpp"
#include "World.hpp"

Expand Down Expand Up @@ -77,11 +78,21 @@ void SettingsMenuState::onEvent(const sf::Event &event) {
doneButtonAction();
}
else if (m_currentKeyButton && event.type == sf::Event::KeyPressed) {
KeyboardHandler *keyboardHandler = dynamic_cast<KeyboardHandler *>(gk::GamePad::getInputHandler());
keyboardHandler->setKeycode(m_currentKey, event.key.code);
if (!m_key) {
KeyboardHandler *keyboardHandler = dynamic_cast<KeyboardHandler *>(gk::GamePad::getInputHandler());
keyboardHandler->setKeycode(m_currentKey, event.key.code);

m_currentKeyButton->setText(m_currentKeyButton->text() + keyboardHandler->getKeyName(m_currentKey));
m_currentKeyButton = nullptr;
m_currentKeyButton->setText(m_currentKeyButton->text() + keyboardHandler->getKeyName(m_currentKey));
m_currentKeyButton = nullptr;
}
else{
m_key->setKeycode(event.key.code);

m_currentKeyButton->setText(m_currentKeyButton->text() + gk::KeyboardUtils::getNameFromKey(m_key->keycode()));
m_currentKeyButton = nullptr;

m_key = nullptr;
}
}
}
}
Expand Down Expand Up @@ -236,6 +247,17 @@ void SettingsMenuState::addInputButtons() {
});
}

if (Registry::isActive) {
for (auto &it : Registry::getInstance().keys()) {
m_menuWidget.addButton(it.name() + ": " + gk::KeyboardUtils::getNameFromKey(it.keycode()), [this, &it] (TextButton &button) {
button.setText(it.name() + ": ");
m_currentKey = it.id();
m_currentKeyButton = &button;
m_key = &it;
});
}
}

m_menuWidget.addButton("Mouse sensitivity: " + std::to_string(Config::mouseSensitivity), [] (TextButton &button) {
Config::mouseSensitivity = std::max(2, (Config::mouseSensitivity + 2) % 14);
button.setText("Mouse sensitivity: " + std::to_string(Config::mouseSensitivity));
Expand Down
4 changes: 3 additions & 1 deletion source/client/states/SettingsMenuState.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include "MenuWidget.hpp"

struct GuiScaleChangedEvent;
class Key;

class SettingsMenuState : public InterfaceState {
public:
Expand Down Expand Up @@ -63,8 +64,9 @@ class SettingsMenuState : public InterfaceState {
MenuWidget m_menuWidget;
TextButton m_doneButton;

u8 m_currentKey = GameKey::Undefined;
u16 m_currentKey = GameKey::Undefined;
TextButton *m_currentKeyButton = nullptr;
Key *m_key = nullptr;

enum class MenuState {
Main,
Expand Down
11 changes: 9 additions & 2 deletions source/common/core/Registry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,12 @@
#include <gk/core/Exception.hpp>

#include "CraftingRecipe.hpp"
#include "GameKey.hpp"
#include "SmeltingRecipe.hpp"
#include "Registry.hpp"

bool Registry::isActive = false;

Registry *Registry::s_instance = nullptr;

Item &Registry::registerItem(const TilesDef &tiles, const std::string &stringID, const std::string &label) {
Expand Down Expand Up @@ -118,7 +121,7 @@ Dimension &Registry::registerSerializedDimension(sf::Packet &packet) {
}

Key &Registry::registerKey(const std::string &stringID, const std::string &label) {
u16 id = m_keys.size();
u16 id = GameKey::KeyCount + m_keys.size();
m_keysID.emplace(stringID, id);
m_keys.emplace_back(id, stringID, label);
return m_keys.back();
Expand All @@ -128,7 +131,7 @@ Key &Registry::registerSerializedKey(sf::Packet &packet) {
m_keys.emplace_back();
m_keys.back().deserialize(packet);

u16 id = m_keys.size() - 1;
u16 id = GameKey::KeyCount + m_keys.size() - 1;
m_keysID.emplace(m_biomes.back().stringID(), id);

return m_keys.back();
Expand All @@ -147,6 +150,10 @@ entt::entity Registry::registerEntity(const std::string &stringID) {
return entt::null;
}

const Key &Registry::getKey(u16 id) const {
return m_keys.at(id - GameKey::KeyCount);
}

const Block &Registry::getBlockFromStringID(const std::string &stringID) {
if (stringID.empty()) return getBlock(0);
auto it = m_blocksID.find(stringID);
Expand Down
5 changes: 3 additions & 2 deletions source/common/core/Registry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ class Registry : public ISerializable {
const Tree &getTree(u16 id) const { return m_trees.at(id); }
const Biome &getBiome(u16 id) const { return m_biomes.at(id); }
const Dimension &getDimension(u16 id) const { return m_dimensions.at(id); }
const Key &getKey(u16 id) const { return m_keys.at(id); }
const Key &getKey(u16 id) const;

const Block &getBlockFromStringID(const std::string &stringID);
const Item &getItemFromStringID(const std::string &stringID);
Expand All @@ -120,8 +120,9 @@ class Registry : public ISerializable {
const std::vector<Tree> &trees() const { return m_trees; }
const std::vector<Biome> &biomes() const { return m_biomes; }
const std::vector<Dimension> &dimensions() const { return m_dimensions; }
const std::vector<Key> &keys() const { return m_keys; }
std::vector<Key> &keys() { return m_keys; }

static bool isActive;
static Registry &getInstance() { return *s_instance; }
static void setInstance(Registry &instance) { s_instance = &instance; }

Expand Down
2 changes: 2 additions & 0 deletions source/common/core/input/GameKey.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ namespace GameKey {
Command,

Shift,

KeyCount
};
}

Expand Down
11 changes: 8 additions & 3 deletions source/common/world/Key.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
#include "ISerializable.hpp"
#include "NetworkUtils.hpp"

#include <gk/core/Debug.hpp>

class Key : public ISerializable {
public:
Key() = default;
Expand All @@ -49,15 +51,18 @@ class Key : public ISerializable {

void deserialize(sf::Packet &packet) override {
packet >> m_id >> m_stringID >> m_name >> m_defaultKey;
m_defaultKeyCode = gk::KeyboardUtils::getKeyFromName(m_defaultKey);
m_keycode = gk::KeyboardUtils::getKeyFromName(m_defaultKey);
}

u16 id() const { return m_id; }

const std::string &stringID() const { return m_stringID; }
const std::string &name() const { return m_name; }

sf::Keyboard::Key defaultKeyCode() const { return m_defaultKeyCode; }
sf::Keyboard::Key keycode() const { return m_keycode; }
void setKeycode(sf::Keyboard::Key keycode) { m_keycode = keycode; }

const std::string &defaultKey() const { return m_defaultKey; }
void setDefaultKey(const std::string &defaultKey) { m_defaultKey = defaultKey; }

const sol::unsafe_function &callback() const { return m_callback; }
Expand All @@ -70,7 +75,7 @@ class Key : public ISerializable {
std::string m_name;

std::string m_defaultKey;
sf::Keyboard::Key m_defaultKeyCode = sf::Keyboard::Unknown;
sf::Keyboard::Key m_keycode = sf::Keyboard::Unknown;

sol::unsafe_function m_callback;
};
Expand Down

0 comments on commit d11d500

Please sign in to comment.