diff --git a/src/data/setting.hpp b/src/data/setting.hpp index 8872ee49..2afa610c 100644 --- a/src/data/setting.hpp +++ b/src/data/setting.hpp @@ -42,13 +42,13 @@ public: : Setting(format), initial(value), value(value) { } - observer_handler observe(consumer callback, bool callOnStart = false) { + ObserverHandler observe(consumer callback, bool callOnStart = false) { const int id = nextid++; observers.emplace(id, callback); if (callOnStart) { callback(value); } - return observer_handler([this, id]() { + return ObserverHandler([this, id]() { observers.erase(id); }); } diff --git a/src/engine/Engine.cpp b/src/engine/Engine.cpp index d9c1b96c..640176b9 100644 --- a/src/engine/Engine.cpp +++ b/src/engine/Engine.cpp @@ -174,13 +174,13 @@ void Engine::loadControls() { } void Engine::updateHotkeys() { - if (input->jpressed(keycode::F2)) { + if (input->jpressed(Keycode::F2)) { saveScreenshot(); } - if (input->jpressed(keycode::F8)) { + if (input->jpressed(Keycode::F8)) { gui->toggleDebug(); } - if (input->jpressed(keycode::F11)) { + if (input->jpressed(Keycode::F11)) { settings.display.fullscreen.toggle(); } } diff --git a/src/frontend/hud.cpp b/src/frontend/hud.cpp index 225007ce..f39c72c7 100644 --- a/src/frontend/hud.cpp +++ b/src/frontend/hud.cpp @@ -252,15 +252,15 @@ void Hud::updateHotbarControl() { player.setChosenSlot(slot); } for ( - int i = static_cast(keycode::NUM_1); - i <= static_cast(keycode::NUM_9); + int i = static_cast(Keycode::NUM_1); + i <= static_cast(Keycode::NUM_9); i++ ) { - if (input.jpressed(static_cast(i))) { - player.setChosenSlot(i - static_cast(keycode::NUM_1)); + if (input.jpressed(static_cast(i))) { + player.setChosenSlot(i - static_cast(Keycode::NUM_1)); } } - if (input.jpressed(keycode::NUM_0)) { + if (input.jpressed(Keycode::NUM_0)) { player.setChosenSlot(9); } } diff --git a/src/frontend/screens/LevelScreen.cpp b/src/frontend/screens/LevelScreen.cpp index be7762d2..44b81765 100644 --- a/src/frontend/screens/LevelScreen.cpp +++ b/src/frontend/screens/LevelScreen.cpp @@ -180,13 +180,13 @@ void LevelScreen::saveWorldPreview() { void LevelScreen::updateHotkeys() { auto& settings = engine.getSettings(); - if (input.jpressed(keycode::O)) { + if (input.jpressed(Keycode::O)) { settings.graphics.frustumCulling.toggle(); } - if (input.jpressed(keycode::F1)) { + if (input.jpressed(Keycode::F1)) { hudVisible = !hudVisible; } - if (input.jpressed(keycode::F3)) { + if (input.jpressed(Keycode::F3)) { debug = !debug; hud->setDebug(debug); renderer->setDebug(debug); diff --git a/src/graphics/ui/GUI.cpp b/src/graphics/ui/GUI.cpp index a25bbf8b..79883136 100644 --- a/src/graphics/ui/GUI.cpp +++ b/src/graphics/ui/GUI.cpp @@ -135,7 +135,7 @@ void GUI::actMouse(float delta, const CursorState& cursor) { } this->hover = hover; - if (input.jclicked(mousecode::BUTTON_1)) { + if (input.jclicked(Mousecode::BUTTON_1)) { if (pressed == nullptr && this->hover) { pressed = hover; if (doubleClickTimer < doubleClickDelay) { @@ -158,13 +158,13 @@ void GUI::actMouse(float delta, const CursorState& cursor) { focus->defocus(); focus = nullptr; } - } else if (!input.clicked(mousecode::BUTTON_1) && pressed) { + } else if (!input.clicked(Mousecode::BUTTON_1) && pressed) { pressed->mouseRelease(cursor.pos.x, cursor.pos.y); pressed = nullptr; } if (hover) { - for (mousecode code : MOUSECODES_ALL) { + for (Mousecode code : MOUSECODES_ALL) { if (input.jclicked(code)) { hover->clicked(code); } @@ -173,7 +173,7 @@ void GUI::actMouse(float delta, const CursorState& cursor) { } void GUI::actFocused() { - if (input.jpressed(keycode::ESCAPE)) { + if (input.jpressed(Keycode::ESCAPE)) { focus->defocus(); focus = nullptr; return; @@ -187,8 +187,8 @@ void GUI::actFocused() { const auto& cursor = input.getCursor(); if (!cursor.locked) { - if (input.clicked(mousecode::BUTTON_1) && - (input.jclicked(mousecode::BUTTON_1) || cursor.delta.x || + if (input.clicked(Mousecode::BUTTON_1) && + (input.jclicked(Mousecode::BUTTON_1) || cursor.delta.x || cursor.delta.y)) { if (!doubleClicked) { focus->mouseMove(cursor.pos.x, cursor.pos.y); diff --git a/src/graphics/ui/elements/InputBindBox.cpp b/src/graphics/ui/elements/InputBindBox.cpp index ba96462f..ade35ff5 100644 --- a/src/graphics/ui/elements/InputBindBox.cpp +++ b/src/graphics/ui/elements/InputBindBox.cpp @@ -25,15 +25,15 @@ void InputBindBox::drawBackground(const DrawContext& pctx, const Assets&) { label->setText(util::str2wstr_utf8(binding.text())); } -void InputBindBox::clicked(mousecode button) { +void InputBindBox::clicked(Mousecode button) { if (isFocused()) { binding.reset(button); defocus(); } } -void InputBindBox::keyPressed(keycode key) { - if (key != keycode::ESCAPE) { +void InputBindBox::keyPressed(Keycode key) { + if (key != Keycode::ESCAPE) { binding.reset(key); } defocus(); diff --git a/src/graphics/ui/elements/InputBindBox.hpp b/src/graphics/ui/elements/InputBindBox.hpp index 0cef6273..a6719996 100644 --- a/src/graphics/ui/elements/InputBindBox.hpp +++ b/src/graphics/ui/elements/InputBindBox.hpp @@ -19,8 +19,8 @@ namespace gui { const DrawContext& pctx, const Assets& assets ) override; - virtual void clicked(mousecode button) override; - virtual void keyPressed(keycode key) override; + virtual void clicked(Mousecode button) override; + virtual void keyPressed(Keycode key) override; virtual bool isFocuskeeper() const override { return true; } diff --git a/src/graphics/ui/elements/InventoryView.cpp b/src/graphics/ui/elements/InventoryView.cpp index 507a1629..b41dce98 100644 --- a/src/graphics/ui/elements/InventoryView.cpp +++ b/src/graphics/ui/elements/InventoryView.cpp @@ -281,7 +281,7 @@ bool SlotView::isHighlighted() const { void SlotView::performLeftClick(ItemStack& stack, ItemStack& grabbed) { const auto& input = gui.getInput(); - if (layout.taking && input.pressed(keycode::LEFT_SHIFT)) { + if (layout.taking && input.pressed(Keycode::LEFT_SHIFT)) { if (layout.shareFunc) { layout.shareFunc(layout.index, stack); } @@ -348,7 +348,7 @@ void SlotView::performRightClick(ItemStack& stack, ItemStack& grabbed) { } } -void SlotView::clicked(mousecode button) { +void SlotView::clicked(Mousecode button) { if (bound == nullptr) return; auto exchangeSlot = @@ -359,9 +359,9 @@ void SlotView::clicked(mousecode button) { ItemStack& grabbed = exchangeSlot->getStack(); ItemStack& stack = *bound; - if (button == mousecode::BUTTON_1) { + if (button == Mousecode::BUTTON_1) { performLeftClick(stack, grabbed); - } else if (button == mousecode::BUTTON_2) { + } else if (button == Mousecode::BUTTON_2) { performRightClick(stack, grabbed); } if (layout.updateFunc) { @@ -370,7 +370,7 @@ void SlotView::clicked(mousecode button) { } void SlotView::onFocus() { - clicked(mousecode::BUTTON_1); + clicked(Mousecode::BUTTON_1); } const std::wstring& SlotView::getTooltip() const { diff --git a/src/graphics/ui/elements/InventoryView.hpp b/src/graphics/ui/elements/InventoryView.hpp index 93ec859b..64ec5c99 100644 --- a/src/graphics/ui/elements/InventoryView.hpp +++ b/src/graphics/ui/elements/InventoryView.hpp @@ -93,7 +93,7 @@ namespace gui { void setHighlighted(bool flag); bool isHighlighted() const; - virtual void clicked(mousecode) override; + virtual void clicked(Mousecode) override; virtual void onFocus() override; virtual const std::wstring& getTooltip() const override; diff --git a/src/graphics/ui/elements/TextBox.cpp b/src/graphics/ui/elements/TextBox.cpp index 99d2937e..3ab4306c 100644 --- a/src/graphics/ui/elements/TextBox.cpp +++ b/src/graphics/ui/elements/TextBox.cpp @@ -824,10 +824,10 @@ void TextBox::onInput() { refreshSyntax(); } -void TextBox::performEditingKeyboardEvents(keycode key) { - bool shiftPressed = gui.getInput().pressed(keycode::LEFT_SHIFT); +void TextBox::performEditingKeyboardEvents(Keycode key) { + bool shiftPressed = gui.getInput().pressed(Keycode::LEFT_SHIFT); bool breakSelection = getSelectionLength() != 0 && !shiftPressed; - if (key == keycode::BACKSPACE) { + if (key == Keycode::BACKSPACE) { if (!eraseSelected() && caret > 0 && input.length() > 0) { if (caret > input.length()) { caret = input.length(); @@ -839,7 +839,7 @@ void TextBox::performEditingKeyboardEvents(keycode key) { onInput(); } } - } else if (key == keycode::DELETE) { + } else if (key == Keycode::DELETE) { if (!eraseSelected() && caret < input.length()) { historian->onErase(caret, input.substr(caret, 1)); input = input.substr(0, caret) + input.substr(caret + 1); @@ -847,7 +847,7 @@ void TextBox::performEditingKeyboardEvents(keycode key) { onInput(); } } - } else if (key == keycode::ENTER) { + } else if (key == Keycode::ENTER) { if (multiline) { paste(L"\n"); } else { @@ -856,42 +856,42 @@ void TextBox::performEditingKeyboardEvents(keycode key) { consumer(getText()); } } - } else if (key == keycode::TAB) { + } else if (key == Keycode::TAB) { paste(L" "); - } else if (key == keycode::LEFT) { + } else if (key == Keycode::LEFT) { stepLeft(shiftPressed, breakSelection); - } else if (key == keycode::RIGHT) { + } else if (key == Keycode::RIGHT) { stepRight(shiftPressed, breakSelection); - } else if (key == keycode::UP && onUpPressed) { + } else if (key == Keycode::UP && onUpPressed) { onUpPressed(); - } else if (key == keycode::DOWN && onDownPressed) { + } else if (key == Keycode::DOWN && onDownPressed) { onDownPressed(); } } -void TextBox::keyPressed(keycode key) { +void TextBox::keyPressed(Keycode key) { const auto& inputEvents = gui.getInput(); if (editable) { performEditingKeyboardEvents(key); } - if (inputEvents.pressed(keycode::LEFT_CONTROL) && key != keycode::LEFT_CONTROL) { + if (inputEvents.pressed(Keycode::LEFT_CONTROL) && key != Keycode::LEFT_CONTROL) { if (controlCombinationsHandler) { if (controlCombinationsHandler(static_cast(key))) { return; } } // Copy selected text to clipboard - if (key == keycode::C || key == keycode::X) { + if (key == Keycode::C || key == Keycode::X) { std::string text = util::wstr2str_utf8(getSelection()); if (!text.empty()) { gui.getInput().setClipboardText(text.c_str()); } - if (editable && key == keycode::X) { + if (editable && key == Keycode::X) { eraseSelected(); } } // Paste text from clipboard - if (key == keycode::V && editable) { + if (key == Keycode::V && editable) { const char* text = inputEvents.getClipboardText(); if (text) { historian->sync(); // flush buffer before combination @@ -902,18 +902,18 @@ void TextBox::keyPressed(keycode key) { } } // Select/deselect all - if (key == keycode::A) { + if (key == Keycode::A) { if (selectionStart == selectionEnd) { select(0, input.length()); } else { resetSelection(); } } - if (key == keycode::Z) { + if (key == Keycode::Z) { historian->undo(); refreshSyntax(); } - if (key == keycode::Y) { + if (key == Keycode::Y) { historian->redo(); refreshSyntax(); } @@ -948,11 +948,11 @@ std::shared_ptr TextBox::getAt(const glm::vec2& pos) { void TextBox::setOnUpPressed(const runnable& callback) { if (callback == nullptr) { onUpPressed = [this]() { - if (inputEvents.pressed(keycode::LEFT_CONTROL)) { + if (inputEvents.pressed(Keycode::LEFT_CONTROL)) { scrolled(1); return; } - bool shiftPressed = inputEvents.pressed(keycode::LEFT_SHIFT); + bool shiftPressed = inputEvents.pressed(Keycode::LEFT_SHIFT); bool breakSelection = getSelectionLength() != 0 && !shiftPressed; stepDefaultUp(shiftPressed, breakSelection); }; @@ -964,11 +964,11 @@ void TextBox::setOnUpPressed(const runnable& callback) { void TextBox::setOnDownPressed(const runnable& callback) { if (callback == nullptr) { onDownPressed = [this]() { - if (inputEvents.pressed(keycode::LEFT_CONTROL)) { + if (inputEvents.pressed(Keycode::LEFT_CONTROL)) { scrolled(-1); return; } - bool shiftPressed = inputEvents.pressed(keycode::LEFT_SHIFT); + bool shiftPressed = inputEvents.pressed(Keycode::LEFT_SHIFT); bool breakSelection = getSelectionLength() != 0 && !shiftPressed; stepDefaultDown(shiftPressed, breakSelection); }; diff --git a/src/graphics/ui/elements/TextBox.hpp b/src/graphics/ui/elements/TextBox.hpp index b9914f99..321d71eb 100644 --- a/src/graphics/ui/elements/TextBox.hpp +++ b/src/graphics/ui/elements/TextBox.hpp @@ -87,7 +87,7 @@ namespace gui { /// @brief Set maxLocalCaret to local (line) caret position void resetMaxLocalCaret(); - void performEditingKeyboardEvents(keycode key); + void performEditingKeyboardEvents(Keycode key); void refreshLabel(); @@ -238,7 +238,7 @@ namespace gui { virtual void draw(const DrawContext& pctx, const Assets& assets) override; virtual void drawBackground(const DrawContext& pctx, const Assets& assets) override; virtual void typed(unsigned int codepoint) override; - virtual void keyPressed(keycode key) override; + virtual void keyPressed(Keycode key) override; virtual std::shared_ptr getAt(const glm::vec2& pos) override; virtual void setOnUpPressed(const runnable &callback); virtual void setOnDownPressed(const runnable &callback); diff --git a/src/graphics/ui/elements/UINode.hpp b/src/graphics/ui/elements/UINode.hpp index 90cd189c..abbb331e 100644 --- a/src/graphics/ui/elements/UINode.hpp +++ b/src/graphics/ui/elements/UINode.hpp @@ -175,7 +175,7 @@ namespace gui { virtual void onFocus() {focused = true;} virtual void doubleClick(int x, int y); virtual void click(int x, int y); - virtual void clicked(mousecode button) {} + virtual void clicked(Mousecode button) {} virtual void mouseMove(int x, int y) {}; virtual void mouseRelease(int x, int y); virtual void scrolled(int value); @@ -188,7 +188,7 @@ namespace gui { virtual bool isFocuskeeper() const {return false;} virtual void typed(unsigned int codepoint) {}; - virtual void keyPressed(keycode key) {}; + virtual void keyPressed(Keycode key) {}; /// @brief Check if screen position is inside of the element /// @param pos screen position diff --git a/src/graphics/ui/gui_util.cpp b/src/graphics/ui/gui_util.cpp index abe9ca37..76ed43ca 100644 --- a/src/graphics/ui/gui_util.cpp +++ b/src/graphics/ui/gui_util.cpp @@ -68,11 +68,11 @@ void guiutil::alert( panel->refresh(); auto& input = engine.getInput(); - panel->keepAlive(input.addKeyCallback(keycode::ENTER, [on_hidden_final]() { + panel->keepAlive(input.addKeyCallback(Keycode::ENTER, [on_hidden_final]() { on_hidden_final(); return true; })); - panel->keepAlive(input.addKeyCallback(keycode::ESCAPE, [on_hidden_final]() { + panel->keepAlive(input.addKeyCallback(Keycode::ESCAPE, [on_hidden_final]() { on_hidden_final(); return true; })); @@ -138,11 +138,11 @@ void guiutil::confirm( })); panel->add(subpanel); - panel->keepAlive(input.addKeyCallback(keycode::ENTER, [=]() { + panel->keepAlive(input.addKeyCallback(Keycode::ENTER, [=]() { on_confirm_final(); return true; })); - panel->keepAlive(input.addKeyCallback(keycode::ESCAPE, [=]() { + panel->keepAlive(input.addKeyCallback(Keycode::ESCAPE, [=]() { on_deny_final(); return true; })); diff --git a/src/logic/scripting/lua/libs/libinput.cpp b/src/logic/scripting/lua/libs/libinput.cpp index fbc54b4d..c5f20cc4 100644 --- a/src/logic/scripting/lua/libs/libinput.cpp +++ b/src/logic/scripting/lua/libs/libinput.cpp @@ -36,7 +36,7 @@ static int l_add_callback(lua::State* L) { lua::pushvalue(L, 2); auto actual_callback = lua::create_simple_handler(L); - observer_handler handler; + ObserverHandler handler; auto& gui = engine->getGUI(); auto& input = engine->getInput(); diff --git a/src/typedefs.hpp b/src/typedefs.hpp index 1a41fd69..e598b9da 100644 --- a/src/typedefs.hpp +++ b/src/typedefs.hpp @@ -5,7 +5,7 @@ using scriptenv = std::shared_ptr; -class observer_handler; +class ObserverHandler; /// @brief dynamic integer type (64 bit signed integer) using integer_t = int64_t; diff --git a/src/util/HandlersList.hpp b/src/util/HandlersList.hpp index 9f3a74b9..aa6fd5e2 100644 --- a/src/util/HandlersList.hpp +++ b/src/util/HandlersList.hpp @@ -31,12 +31,12 @@ namespace util { nextid = o.nextid; } - observer_handler add(std::function handler) { + ObserverHandler add(std::function handler) { std::lock_guard lock(mutex); int id = nextid++; handlers[id] = std::move(handler); order.push_back(id); - return observer_handler([this, id]() { + return ObserverHandler([this, id]() { std::lock_guard lock(mutex); handlers.erase(id); order.erase( diff --git a/src/util/ObjectsKeeper.hpp b/src/util/ObjectsKeeper.hpp index 2cd079d3..e4fb75df 100644 --- a/src/util/ObjectsKeeper.hpp +++ b/src/util/ObjectsKeeper.hpp @@ -9,7 +9,7 @@ namespace util { /// @brief Keeps shared pointers alive until destruction class ObjectsKeeper { std::vector> ptrs; - std::vector handlers; + std::vector handlers; public: ObjectsKeeper() = default; @@ -29,7 +29,7 @@ namespace util { ptrs.push_back(std::move(ptr)); } - virtual void keepAlive(observer_handler&& ptr) { + virtual void keepAlive(ObserverHandler&& ptr) { handlers.emplace_back(std::move(ptr)); } diff --git a/src/util/RunnablesList.hpp b/src/util/RunnablesList.hpp index c9771de3..8eae1e04 100644 --- a/src/util/RunnablesList.hpp +++ b/src/util/RunnablesList.hpp @@ -26,11 +26,11 @@ namespace util { nextid = o.nextid; } - observer_handler add(runnable callback) { + ObserverHandler add(runnable callback) { std::lock_guard lock(mutex); int id = nextid++; runnables[id] = std::move(callback); - return observer_handler(new int(id), [this](int* id) { //-V508 + return ObserverHandler(new int(id), [this](int* id) { //-V508 std::lock_guard lock(mutex); runnables.erase(*id); delete id; diff --git a/src/util/observer_handler.hpp b/src/util/observer_handler.hpp index 2a636a77..8dee7b05 100644 --- a/src/util/observer_handler.hpp +++ b/src/util/observer_handler.hpp @@ -2,21 +2,21 @@ #include -class observer_handler { +class ObserverHandler { public: - observer_handler() = default; + ObserverHandler() = default; - observer_handler(std::function destructor) + ObserverHandler(std::function destructor) : destructor(std::move(destructor)) { } - observer_handler(const observer_handler&) = delete; + ObserverHandler(const ObserverHandler&) = delete; - observer_handler(observer_handler&& handler) noexcept + ObserverHandler(ObserverHandler&& handler) noexcept : destructor(std::move(handler.destructor)) { } - ~observer_handler() { + ~ObserverHandler() { if (destructor) { destructor(); } @@ -26,9 +26,9 @@ public: return destructor == nullptr; } - observer_handler& operator=(const observer_handler& handler) = delete; + ObserverHandler& operator=(const ObserverHandler& handler) = delete; - observer_handler& operator=(observer_handler&& handler) noexcept { + ObserverHandler& operator=(ObserverHandler&& handler) noexcept { if (destructor) { destructor(); } diff --git a/src/window/detail/GLFWWindow.cpp b/src/window/detail/GLFWWindow.cpp index dbb0c636..d6b30477 100644 --- a/src/window/detail/GLFWWindow.cpp +++ b/src/window/detail/GLFWWindow.cpp @@ -168,10 +168,10 @@ public: uint currentFrame = 0; uint frames[KEYS_BUFFER_SIZE] {}; std::vector codepoints; - std::vector pressedKeys; + std::vector pressedKeys; Bindings bindings; bool keys[KEYS_BUFFER_SIZE] {}; - std::unordered_map> keyCallbacks; + std::unordered_map> keyCallbacks; GLFWInput(GLFWwindow* window) : window(window) { @@ -196,11 +196,11 @@ public: bool newstate = false; switch (binding.type) { - case inputtype::keyboard: - newstate = pressed(static_cast(binding.code)); + case InputType::KEYBOARD: + newstate = pressed(static_cast(binding.code)); break; - case inputtype::mouse: - newstate = clicked(static_cast(binding.code)); + case InputType::MOUSE: + newstate = clicked(static_cast(binding.code)); break; } @@ -224,13 +224,13 @@ public: keys[key] = pressed; frames[key] = currentFrame; if (pressed && !prevPressed) { - const auto& callbacks = keyCallbacks.find(static_cast(key)); + const auto& callbacks = keyCallbacks.find(static_cast(key)); if (callbacks != keyCallbacks.end()) { callbacks->second.notify(); } } if (pressed) { - pressedKeys.push_back(static_cast(key)); + pressedKeys.push_back(static_cast(key)); } } @@ -251,24 +251,24 @@ public: return scroll; } - bool pressed(keycode key) const override { + bool pressed(Keycode key) const override { int keycode = static_cast(key); if (keycode < 0 || keycode >= KEYS_BUFFER_SIZE) { return false; } return keys[keycode]; } - bool jpressed(keycode keycode) const override { + bool jpressed(Keycode keycode) const override { return pressed(keycode) && frames[static_cast(keycode)] == currentFrame; } - bool clicked(mousecode code) const override { + bool clicked(Mousecode code) const override { return pressed( - static_cast(MOUSE_KEYS_OFFSET + static_cast(code)) + static_cast(MOUSE_KEYS_OFFSET + static_cast(code)) ); } - bool jclicked(mousecode code) const override { + bool jclicked(Mousecode code) const override { return clicked(code) && frames[static_cast(code) + MOUSE_KEYS_OFFSET] == currentFrame; @@ -311,11 +311,11 @@ public: return bindings; } - observer_handler addKeyCallback(keycode key, KeyCallback callback) override { + ObserverHandler addKeyCallback(Keycode key, KeyCallback callback) override { return keyCallbacks[key].add(std::move(callback)); } - const std::vector& getPressedKeys() const override { + const std::vector& getPressedKeys() const override { return pressedKeys; } diff --git a/src/window/input.cpp b/src/window/input.cpp index 56c19cfd..dfb769db 100644 --- a/src/window/input.cpp +++ b/src/window/input.cpp @@ -56,7 +56,7 @@ static std::unordered_map mousecodes { static std::unordered_map keynames {}; static std::unordered_map buttonsnames{}; -std::string input_util::get_name(mousecode code) { +std::string input_util::get_name(Mousecode code) { auto found = buttonsnames.find(static_cast(code)); if (found == buttonsnames.end()) { return "unknown"; @@ -64,7 +64,7 @@ std::string input_util::get_name(mousecode code) { return found->second; } -std::string input_util::get_name(keycode code) { +std::string input_util::get_name(Keycode code) { auto found = keynames.find(static_cast(code)); if (found == keynames.end()) { return "unknown"; @@ -72,17 +72,17 @@ std::string input_util::get_name(keycode code) { return found->second; } -void Binding::reset(inputtype type, int code) { +void Binding::reset(InputType type, int code) { this->type = type; this->code = code; } -void Binding::reset(keycode code) { - reset(inputtype::keyboard, static_cast(code)); +void Binding::reset(Keycode code) { + reset(InputType::KEYBOARD, static_cast(code)); } -void Binding::reset(mousecode code) { - reset(inputtype::mouse, static_cast(code)); +void Binding::reset(Mousecode code) { + reset(InputType::MOUSE, static_cast(code)); } void input_util::initialize() { @@ -103,23 +103,23 @@ void input_util::initialize() { } } -keycode input_util::keycode_from(const std::string& name) { +Keycode input_util::keycode_from(const std::string& name) { const auto& found = keycodes.find(name); if (found == keycodes.end()) { - return keycode::UNKNOWN; + return Keycode::UNKNOWN; } - return static_cast(found->second); + return static_cast(found->second); } -mousecode input_util::mousecode_from(const std::string& name) { +Mousecode input_util::mousecode_from(const std::string& name) { const auto& found = mousecodes.find(name); if (found == mousecodes.end()) { - return mousecode::UNKNOWN; + return Mousecode::UNKNOWN; } - return static_cast(found->second); + return static_cast(found->second); } -std::string input_util::to_string(keycode code) { +std::string input_util::to_string(Keycode code) { int icode_repr = static_cast(code); const char* name = glfwGetKeyName(icode_repr, glfwGetKeyScancode(icode_repr)); @@ -212,21 +212,21 @@ std::string input_util::to_string(keycode code) { return std::string(name); } -std::string input_util::to_string(mousecode code) { +std::string input_util::to_string(Mousecode code) { switch (code) { - case mousecode::BUTTON_1: + case Mousecode::BUTTON_1: return "LMB"; - case mousecode::BUTTON_2: + case Mousecode::BUTTON_2: return "RMB"; - case mousecode::BUTTON_3: + case Mousecode::BUTTON_3: return "MMB"; - case mousecode::BUTTON_4: - case mousecode::BUTTON_5: - case mousecode::BUTTON_6: - case mousecode::BUTTON_7: - case mousecode::BUTTON_8: + case Mousecode::BUTTON_4: + case Mousecode::BUTTON_5: + case Mousecode::BUTTON_6: + case Mousecode::BUTTON_7: + case Mousecode::BUTTON_8: return "XButton " + std::to_string(static_cast(code) - - static_cast(mousecode::BUTTON_3)); + static_cast(Mousecode::BUTTON_3)); default: return "unknown button"; } @@ -251,13 +251,13 @@ void Bindings::read(const dv::value& map, BindType bindType) { for (auto& [name, value] : section.asObject()) { auto key = sectionName + "." + name; auto [prefix, codename] = util::split_at(value.asString(), ':'); - inputtype type; + InputType type; int code; if (prefix == "key") { - type = inputtype::keyboard; + type = InputType::KEYBOARD; code = static_cast(input_util::keycode_from(codename)); } else if (prefix == "mouse") { - type = inputtype::mouse; + type = InputType::MOUSE; code = static_cast(input_util::mousecode_from(codename)); } else { logger.error() @@ -282,15 +282,15 @@ std::string Bindings::write() const { const auto& binding = entry.second; std::string value; switch (binding.type) { - case inputtype::keyboard: + case InputType::KEYBOARD: value = "key:" + - input_util::get_name(static_cast(binding.code)); + input_util::get_name(static_cast(binding.code)); break; - case inputtype::mouse: + case InputType::MOUSE: value = "mouse:" + - input_util::get_name(static_cast(binding.code)); + input_util::get_name(static_cast(binding.code)); break; default: throw std::runtime_error("unsupported control type"); diff --git a/src/window/input.hpp b/src/window/input.hpp index 9868c253..d5b9efb8 100644 --- a/src/window/input.hpp +++ b/src/window/input.hpp @@ -15,7 +15,7 @@ enum class BindType { }; /// @brief Represents glfw3 keycode values. -enum class keycode : int { +enum class Keycode : int { SPACE = 32, APOSTROPHE = 39, COMMA = 44, @@ -108,7 +108,7 @@ enum class keycode : int { /// @brief Represents glfw3 mouse button IDs. /// @details There is a subset of glfw3 mouse button IDs. -enum class mousecode : int { +enum class Mousecode : int { BUTTON_1 = 0, // Left mouse button BUTTON_2 = 1, // Right mouse button BUTTON_3 = 2, // Middle mouse button @@ -120,43 +120,49 @@ enum class mousecode : int { UNKNOWN = -1, }; -inline mousecode MOUSECODES_ALL[] { - mousecode::BUTTON_1, mousecode::BUTTON_2, mousecode::BUTTON_3, mousecode::BUTTON_4, - mousecode::BUTTON_5, mousecode::BUTTON_6, mousecode::BUTTON_7, mousecode::BUTTON_8 }; +inline Mousecode MOUSECODES_ALL[] { + Mousecode::BUTTON_1, + Mousecode::BUTTON_2, + Mousecode::BUTTON_3, + Mousecode::BUTTON_4, + Mousecode::BUTTON_5, + Mousecode::BUTTON_6, + Mousecode::BUTTON_7, + Mousecode::BUTTON_8}; namespace input_util { void initialize(); - keycode keycode_from(const std::string& name); - mousecode mousecode_from(const std::string& name); + Keycode keycode_from(const std::string& name); + Mousecode mousecode_from(const std::string& name); /// @return Key label by keycode - std::string to_string(keycode code); + std::string to_string(Keycode code); /// @return Mouse button label by keycode - std::string to_string(mousecode code); + std::string to_string(Mousecode code); /// @return Key name by keycode - std::string get_name(keycode code); + std::string get_name(Keycode code); /// @return Mouse button name by keycode - std::string get_name(mousecode code); + std::string get_name(Mousecode code); } -enum class inputtype { - keyboard, - mouse, +enum class InputType { + KEYBOARD, + MOUSE, }; struct Binding { util::HandlersList<> onactived; - inputtype type; + InputType type; int code; bool state = false; bool justChange = false; bool enabled = true; Binding() = default; - Binding(inputtype type, int code) : type(type), code(code) { + Binding(InputType type, int code) : type(type), code(code) { } bool active() const { @@ -167,17 +173,17 @@ struct Binding { return state && justChange; } - void reset(inputtype, int); - void reset(keycode); - void reset(mousecode); + void reset(InputType, int); + void reset(Keycode); + void reset(Mousecode); inline std::string text() const { switch (type) { - case inputtype::keyboard: { - return input_util::to_string(static_cast(code)); + case InputType::KEYBOARD: { + return input_util::to_string(static_cast(code)); } - case inputtype::mouse: { - return input_util::to_string(static_cast(code)); + case InputType::MOUSE: { + return input_util::to_string(static_cast(code)); } } return ""; @@ -223,11 +229,11 @@ public: const Binding& require(const std::string& name) const; - void bind(const std::string& name, inputtype type, int code) { + void bind(const std::string& name, InputType type, int code) { bindings.try_emplace(name, Binding(type, code)); } - void rebind(const std::string& name, inputtype type, int code) { + void rebind(const std::string& name, InputType type, int code) { require(name) = Binding(type, code); } @@ -262,11 +268,11 @@ public: virtual int getScroll() = 0; - virtual bool pressed(keycode keycode) const = 0; - virtual bool jpressed(keycode keycode) const = 0; + virtual bool pressed(Keycode keycode) const = 0; + virtual bool jpressed(Keycode keycode) const = 0; - virtual bool clicked(mousecode mousecode) const = 0; - virtual bool jclicked(mousecode mousecode) const = 0; + virtual bool clicked(Mousecode mousecode) const = 0; + virtual bool jclicked(Mousecode mousecode) const = 0; virtual CursorState getCursor() const = 0; @@ -277,12 +283,12 @@ public: virtual const Bindings& getBindings() const = 0; - virtual observer_handler addKeyCallback(keycode key, KeyCallback callback) = 0; + virtual ObserverHandler addKeyCallback(Keycode key, KeyCallback callback) = 0; - virtual const std::vector& getPressedKeys() const = 0; + virtual const std::vector& getPressedKeys() const = 0; virtual const std::vector& getCodepoints() const = 0; - observer_handler addCallback(const std::string& name, KeyCallback callback) { + ObserverHandler addCallback(const std::string& name, KeyCallback callback) { return getBindings().require(name).onactived.add(callback); } };