From 8b51e86fd62f667db88823a5318ed78fe6cf028d Mon Sep 17 00:00:00 2001 From: LimeGradient <87611796+LimeGradient@users.noreply.github.com> Date: Thu, 30 Jan 2025 17:02:12 -0500 Subject: [PATCH] work on removing custom keybinds --- mod.json | 2 +- src/managers/keybinds.cpp | 45 ++++++++++++++-------- src/managers/keybinds.hpp | 22 ++++++++++- src/ui/menu/settings/keybind_setting.cpp | 26 +++++++++++++ src/ui/menu/settings/keybind_setting.hpp | 49 ++++++++++++++++++++++++ 5 files changed, 124 insertions(+), 20 deletions(-) create mode 100644 src/ui/menu/settings/keybind_setting.cpp create mode 100644 src/ui/menu/settings/keybind_setting.hpp diff --git a/mod.json b/mod.json index 2ff43a7a1..9ea5caad8 100644 --- a/mod.json +++ b/mod.json @@ -2,7 +2,7 @@ "id": "dankmeme.globed2", "name": "Globed", "developer": "dankmeme", - "geode": "4.1.0", + "geode": "4.2.0", "version": "v1.7.2", "gd": { "win": "2.2074", diff --git a/src/managers/keybinds.cpp b/src/managers/keybinds.cpp index 8790dd3e5..f95844892 100644 --- a/src/managers/keybinds.cpp +++ b/src/managers/keybinds.cpp @@ -43,16 +43,16 @@ namespace globed { } } -void KeybindsManager::handlePress(Key key) { +void KeybindsManager::handlePress(Key key, std::function callback) { if (key == Key::None) return; - heldKeys[key] = true; + callback(key); } -void KeybindsManager::handleRelease(Key key) { +void KeybindsManager::handleRelease(Key key, std::function callback) { if (key == Key::None) return; - heldKeys[key] = false; + callback(key); } bool KeybindsManager::isHeld(Key key) { @@ -309,19 +309,30 @@ Key convertCocosKey(enumKeyCodes key) { } } -// TODO: mac m1 crash? -// #include +#endif -// class $modify(CCKeyboardDispatcher) { -// bool dispatchKeyboardMSG(enumKeyCodes key, bool down, bool repeat) { -// if (down) { -// KeybindsManager::get().handlePress(convertCocosKey(key)); -// } else if (!down) { -// KeybindsManager::get().handleRelease(convertCocosKey(key)); -// } +using namespace geode::prelude; -// return CCKeyboardDispatcher::dispatchKeyboardMSG(key, down, repeat); -// } -// }; +bool KeybindsManager::KeybindRegisterLayer::init(globed::Key key) { + if (!CCLayer::init()) return false; + + return true; +} + +void KeybindsManager::KeybindRegisterLayer::keyDown(enumKeyCodes keyCode) { + KeybindsManager::get().handlePress(convertCocosKey(keyCode), [this](auto key) { + this->key = key; + }); -#endif \ No newline at end of file + this->removeFromParent(); +} + +KeybindsManager::KeybindRegisterLayer* KeybindsManager::KeybindRegisterLayer::create(globed::Key key) { + auto ret = new KeybindsManager::KeybindRegisterLayer(); + if (ret && ret->init(key)) { + ret->autorelease(); + return ret; + } + CC_SAFE_DELETE(ret); + return nullptr; +} \ No newline at end of file diff --git a/src/managers/keybinds.hpp b/src/managers/keybinds.hpp index b71a75d36..e38d25f72 100644 --- a/src/managers/keybinds.hpp +++ b/src/managers/keybinds.hpp @@ -4,6 +4,8 @@ #include #include +#include + // Big shoutout to eclipse menu i am too lazy to type all that out myself // https://github.com/EclipseMenu/EclipseMenu/blob/main/src/modules/keybinds/manager.hpp @@ -46,10 +48,26 @@ class KeybindsManager : public SingletonBase { friend class SingletonBase; public: - void handlePress(globed::Key); - void handleRelease(globed::Key); + class KeybindRegisterLayer : public cocos2d::CCLayer { + protected: + bool init(globed::Key key); + + $override + void keyDown(cocos2d::enumKeyCodes keyCode); + + globed::Key key; + + public: + static KeybindRegisterLayer* create(globed::Key key); + }; + + void handlePress(globed::Key, std::function callback); + void handleRelease(globed::Key, std::function callback); bool isHeld(globed::Key key); + globed::Key voiceChatKey; + globed::Key voiceDeafenKey; + private: std::map heldKeys; }; diff --git a/src/ui/menu/settings/keybind_setting.cpp b/src/ui/menu/settings/keybind_setting.cpp new file mode 100644 index 000000000..9a790bb86 --- /dev/null +++ b/src/ui/menu/settings/keybind_setting.cpp @@ -0,0 +1,26 @@ +#include "keybind_setting.hpp" + +#include + +using namespace geode::prelude; + +void KeybindSettingNode::onButton(CCObject*) { + +} + +KeybindSettingNode* KeybindSettingNode::create(std::shared_ptr setting, float width) { + auto ret = new KeybindSettingNode(); + if (ret && ret->init(setting, width)) { + ret->autorelese(); + return ret; + } + CC_SAFE_DELETE(ret); + return nullptr; +} + +SettingNodeV3* KeybindSetting::createNode(float width) { + return KeybindSettingNode::create( + std::static_pointer_cast(shared_from_this()), + width + ); +} \ No newline at end of file diff --git a/src/ui/menu/settings/keybind_setting.hpp b/src/ui/menu/settings/keybind_setting.hpp new file mode 100644 index 000000000..5c209569d --- /dev/null +++ b/src/ui/menu/settings/keybind_setting.hpp @@ -0,0 +1,49 @@ +#pragma once + +#include +#include + +#include +#include + +class KeybindSetting : public geode::SettingV3 { +public: + static geode::Result> parse(std::string const& key, std::string const& modID, matjson::Value const& json); + bool load(matjson::Value const& json) override { + return true; + } + bool save(matjson::Value& json) const override { + return true; + } + bool isDefaultValue() const override { + return true; + } + void reset() override {} + + geode::SettingNodeV3* createNode(float width) override; +}; + +class KeybindSettingNode : public geode::SettingNodeV3 { +protected: + ButtonSprite* m_buttonSprite; + CCMenuItemSpriteExtra* m_button; + + bool init(std::shared_ptr setting, float width); + void updateState(cocos2d::CCNode* invoker) override; + void onButton(cocos2d::CCObject*); + void onCommit() override {} + void onResetToDefault() override {} + +public: + static KeybindSettingNode* create(std::shared_ptr setting, float width); + bool hasUncommittedChanges() const override { + return false; + } + bool hasNonDefaultValue() const override { + return false; + } + std::shared_ptr getSetting() const { + return std::static_pointer_cast(geode::SettingNodeV3::getSetting()); + } +}; +