From 414d1bf133e482364c50511aac399b39fafe5556 Mon Sep 17 00:00:00 2001 From: HJfod <60038575+HJfod@users.noreply.github.com> Date: Mon, 15 Jul 2024 20:26:00 +0300 Subject: [PATCH] support for mouse side buttons --- changelog.md | 4 ++++ include/Keybinds.hpp | 24 ++++++++++++++++++++++ mod.json | 4 ++-- src/Keybinds.cpp | 48 ++++++++++++++++++++++++++++++++++++++++++++ src/main.cpp | 32 ++++++++++++++++++++++++++++- 5 files changed, 109 insertions(+), 3 deletions(-) diff --git a/changelog.md b/changelog.md index dcd9baa..bf55c3d 100644 --- a/changelog.md +++ b/changelog.md @@ -1,5 +1,9 @@ # Changelog +## v1.7.0 + + - Allow assigning mouse side buttons as keybinds + ## v1.6.4 - Fix spacebar in PlayLayer popups diff --git a/include/Keybinds.hpp b/include/Keybinds.hpp index 9d2ebf0..fe23752 100644 --- a/include/Keybinds.hpp +++ b/include/Keybinds.hpp @@ -95,6 +95,30 @@ namespace keybinds { matjson::Value save() const override; }; + enum class MouseButton { + PageBack, + PageNext, + }; + + class CUSTOM_KEYBINDS_DLL Mousebind final : public Bind { + protected: + MouseButton m_button; + Modifier m_modifiers; + + public: + static Mousebind* create(MouseButton button, Modifier modifiers = Modifier::None); + static Mousebind* parse(matjson::Value const&); + + MouseButton getButton() const; + Modifier getModifiers() const; + + size_t getHash() const override; + bool isEqual(Bind* other) const override; + std::string toString() const override; + DeviceID getDeviceID() const override; + matjson::Value save() const override; + }; + class CUSTOM_KEYBINDS_DLL ControllerBind final : public Bind { protected: cocos2d::enumKeyCodes m_button; diff --git a/mod.json b/mod.json index f7e1d76..6ee9096 100644 --- a/mod.json +++ b/mod.json @@ -1,10 +1,10 @@ { - "geode": "3.1.1", + "geode": "3.2.0", "gd": { "win": "2.206", "android": "2.206" }, - "version": "v1.6.5", + "version": "v1.7.0", "id": "geode.custom-keybinds", "name": "Custom Keybinds", "developer": "Geode Team", diff --git a/src/Keybinds.cpp b/src/Keybinds.cpp index ee67662..da669f9 100644 --- a/src/Keybinds.cpp +++ b/src/Keybinds.cpp @@ -156,6 +156,53 @@ std::string Keybind::getDeviceID() const { return "keyboard"_spr; } +Mousebind* Mousebind::create(MouseButton button, Modifier modifiers) { + auto ret = new Mousebind(); + ret->m_button = button; + ret->m_modifiers = modifiers; + ret->autorelease(); + return ret; +} +Mousebind* Mousebind::parse(matjson::Value const& value) { + return Mousebind::create( + static_cast(value["button"].as_int()), + static_cast(value["modifiers"].as_int()) + ); +} + +MouseButton Mousebind::getButton() const { + return m_button; +} +Modifier Mousebind::getModifiers() const { + return m_modifiers; +} + +size_t Mousebind::getHash() const { + return static_cast(m_button) | (static_cast(m_modifiers) << 29); +} +bool Mousebind::isEqual(Bind* other) const { + if (auto o = typeinfo_cast(other)) { + return m_button == o->m_button && m_modifiers == o->m_modifiers; + } + return false; +} +std::string Mousebind::toString() const { + switch (m_button) { + case MouseButton::PageBack: return "Page Back"; + case MouseButton::PageNext: return "Page Next"; + default: return "Unknown (Mouse)"; + } +} +DeviceID Mousebind::getDeviceID() const { + return "mouse"_spr; +} +matjson::Value Mousebind::save() const { + return matjson::Object { + { "button", static_cast(m_button) }, + { "modifiers", static_cast(m_modifiers) }, + }; +} + ControllerBind* ControllerBind::create(enumKeyCodes button) { if (!keyIsController(button)) { return nullptr; @@ -423,6 +470,7 @@ BindManager::BindManager() { this->addCategory(Category::PLAY); this->addCategory(Category::EDITOR); this->attachDevice("keyboard"_spr, &Keybind::parse); + this->attachDevice("mouse"_spr, &Mousebind::parse); this->retain(); } diff --git a/src/main.cpp b/src/main.cpp index ca218b9..b55af8d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -42,6 +42,35 @@ class $modify(CCEGLView){ } CCEGLView::onGLFWKeyCallback(window, key, scancode, action, mods); } + + void onGLFWMouseCallBack(GLFWwindow* window, int button, int action, int mods) { + std::optional mb; + switch (button) { + case 3: mb = MouseButton::PageBack; break; + case 4: mb = MouseButton::PageNext; break; + default: break; + } + if (mb) { + Modifier modifiers; + if (mods & GLFW_MOD_SHIFT) { + modifiers |= Modifier::Shift; + } + if (mods & GLFW_MOD_ALT) { + modifiers |= Modifier::Alt; + } + if (mods & GLFW_MOD_CONTROL) { + modifiers |= Modifier::Control; + } + if (auto bind = Mousebind::create(*mb, modifiers)) { + if (PressBindEvent(bind, action == GLFW_PRESS).post() == ListenerResult::Stop) { + return; + } + } + } + else { + return CCEGLView::onGLFWMouseCallBack(window, button, action, mods); + } + } }; #endif @@ -64,7 +93,8 @@ class $modify(CCKeyboardDispatcher) { if (PressBindEvent(ControllerBind::create(key), down).post() == ListenerResult::Stop) { return true; } - } else { + } + else { if (!keyIsModifier(key)) { if (down) { s_held.insert(key);