Skip to content

Commit

Permalink
support for mouse side buttons
Browse files Browse the repository at this point in the history
  • Loading branch information
HJfod committed Jul 15, 2024
1 parent 89b9f5e commit 414d1bf
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 3 deletions.
4 changes: 4 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## v1.7.0

- Allow assigning mouse side buttons as keybinds

## v1.6.4

- Fix spacebar in PlayLayer popups
Expand Down
24 changes: 24 additions & 0 deletions include/Keybinds.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions mod.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
48 changes: 48 additions & 0 deletions src/Keybinds.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<MouseButton>(value["button"].as_int()),
static_cast<Modifier>(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<size_t>(m_button) | (static_cast<size_t>(m_modifiers) << 29);
}
bool Mousebind::isEqual(Bind* other) const {
if (auto o = typeinfo_cast<Mousebind*>(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<int>(m_button) },
{ "modifiers", static_cast<int>(m_modifiers) },
};
}

ControllerBind* ControllerBind::create(enumKeyCodes button) {
if (!keyIsController(button)) {
return nullptr;
Expand Down Expand Up @@ -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();
}

Expand Down
32 changes: 31 additions & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<MouseButton> 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

Expand All @@ -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);
Expand Down

0 comments on commit 414d1bf

Please sign in to comment.