Skip to content

Commit

Permalink
work on removing custom keybinds
Browse files Browse the repository at this point in the history
  • Loading branch information
LimeGradient committed Jan 30, 2025
1 parent 1702179 commit 8b51e86
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 20 deletions.
2 changes: 1 addition & 1 deletion mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
45 changes: 28 additions & 17 deletions src/managers/keybinds.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,16 @@ namespace globed {
}
}

void KeybindsManager::handlePress(Key key) {
void KeybindsManager::handlePress(Key key, std::function<void(globed::Key)> callback) {
if (key == Key::None) return;

heldKeys[key] = true;
callback(key);
}

void KeybindsManager::handleRelease(Key key) {
void KeybindsManager::handleRelease(Key key, std::function<void(globed::Key)> callback) {
if (key == Key::None) return;

heldKeys[key] = false;
callback(key);
}

bool KeybindsManager::isHeld(Key key) {
Expand Down Expand Up @@ -309,19 +309,30 @@ Key convertCocosKey(enumKeyCodes key) {
}
}

// TODO: mac m1 crash?
// #include <Geode/modify/CCKeyboardDispatcher.hpp>
#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
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;
}
22 changes: 20 additions & 2 deletions src/managers/keybinds.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include <string>
#include <map>

#include <Geode/Geode.hpp>

// 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

Expand Down Expand Up @@ -46,10 +48,26 @@ class KeybindsManager : public SingletonBase<KeybindsManager> {
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<void(globed::Key)> callback);
void handleRelease(globed::Key, std::function<void(globed::Key)> callback);
bool isHeld(globed::Key key);

globed::Key voiceChatKey;
globed::Key voiceDeafenKey;

private:
std::map<globed::Key, bool> heldKeys;
};
26 changes: 26 additions & 0 deletions src/ui/menu/settings/keybind_setting.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include "keybind_setting.hpp"

#include <managers/keybinds.hpp>

using namespace geode::prelude;

void KeybindSettingNode::onButton(CCObject*) {

}

KeybindSettingNode* KeybindSettingNode::create(std::shared_ptr<KeybindSetting> 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<KeybindSetting>(shared_from_this()),
width
);
}
49 changes: 49 additions & 0 deletions src/ui/menu/settings/keybind_setting.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#pragma once

#include <Geode/loader/SettingV3.hpp>
#include <Geode/loader/Mod.hpp>

#include <Geode/binding/ButtonSprite.hpp>
#include <Geode/binding/CCMenuItemSpriteExtra.hpp>

class KeybindSetting : public geode::SettingV3 {
public:
static geode::Result<std::shared_ptr<geode::SettingV3>> 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<KeybindSetting> 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<KeybindSetting> setting, float width);
bool hasUncommittedChanges() const override {
return false;
}
bool hasNonDefaultValue() const override {
return false;
}
std::shared_ptr<KeybindSetting> getSetting() const {
return std::static_pointer_cast<KeybindSetting>(geode::SettingNodeV3::getSetting());
}
};

0 comments on commit 8b51e86

Please sign in to comment.