Skip to content

Commit d737735

Browse files
committed
optional api im fucking done
1 parent b94839b commit d737735

File tree

3 files changed

+242
-0
lines changed

3 files changed

+242
-0
lines changed

include/Keybinds.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ namespace keybinds {
157157

158158
public:
159159
Category() = default;
160+
Category(Category const&) = default;
160161
Category(const char* path);
161162
Category(std::string const& path);
162163
std::vector<std::string> getPath() const;
@@ -195,6 +196,7 @@ namespace keybinds {
195196
bool isRepeatable() const;
196197

197198
BindableAction() = default;
199+
BindableAction(BindableAction const&) = default;
198200
BindableAction(
199201
ActionID const& id,
200202
std::string const& name,

include/OptionalAPI.hpp

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
#pragma once
2+
3+
#include "Keybinds.hpp"
4+
#include <Geode/loader/Dispatch.hpp>
5+
6+
#ifdef MY_MOD_ID
7+
#undef MY_MOD_ID
8+
#endif
9+
#define MY_MOD_ID "geode.custom-keybinds"
10+
11+
namespace keybinds {
12+
enum class Modifier2 : unsigned int {
13+
None = 0b0000,
14+
Control = 0b0001,
15+
Shift = 0b0010,
16+
Alt = 0b0100,
17+
Command = 0b1000,
18+
};
19+
20+
inline Modifier2 operator|(Modifier2 const& a, Modifier2 const& b) {
21+
return static_cast<Modifier2>(static_cast<unsigned int>(a) | static_cast<unsigned int>(b));
22+
}
23+
24+
inline Modifier2& operator|=(Modifier2& a, Modifier2 const& b) {
25+
a = a | b;
26+
return a;
27+
}
28+
29+
inline bool operator&(Modifier2 const& a, Modifier2 const& b) {
30+
return (static_cast<unsigned int>(a) & static_cast<unsigned int>(b)) != 0;
31+
}
32+
33+
// TODO: move actual classes into these in v2
34+
class KeybindV2 final {
35+
class Impl;
36+
std::unique_ptr<Impl> m_impl;
37+
public:
38+
static geode::Result<Keybind*> create(cocos2d::enumKeyCodes key, Modifier modifiers = Modifier::None) GEODE_EVENT_EXPORT(&KeybindV2::create, (key, modifiers));
39+
};
40+
41+
class MouseBindV2 final {
42+
class Impl;
43+
std::unique_ptr<Impl> m_impl;
44+
public:
45+
static geode::Result<MouseBind*> create(MouseButton button, Modifier modifiers = Modifier::None) GEODE_EVENT_EXPORT(&MouseBindV2::create, (button, modifiers));
46+
};
47+
48+
class ControllerBindV2 final {
49+
class Impl;
50+
std::unique_ptr<Impl> m_impl;
51+
public:
52+
static geode::Result<ControllerBind*> create(cocos2d::enumKeyCodes button) GEODE_EVENT_EXPORT(&ControllerBindV2::create, (button));
53+
};
54+
55+
56+
class CategoryV2 final {
57+
class Impl;
58+
std::unique_ptr<Impl> m_impl;
59+
public:
60+
static geode::Result<std::shared_ptr<Category>> create(std::string_view path) GEODE_EVENT_EXPORT(&CategoryV2::create, (path));
61+
62+
static inline auto const PLAY = CategoryV2::create("Play");
63+
static inline auto const PLAY_PAUSE = CategoryV2::create("Play/Pause");
64+
static inline auto const EDITOR = CategoryV2::create("Editor");
65+
static inline auto const GLOBAL = CategoryV2::create("Global");
66+
static inline auto const EDITOR_UI = CategoryV2::create("Editor/UI");
67+
static inline auto const EDITOR_MODIFY = CategoryV2::create("Editor/Modify");
68+
static inline auto const EDITOR_MOVE = CategoryV2::create("Editor/Move");
69+
};
70+
71+
class BindableActionV2 final {
72+
class Impl;
73+
std::unique_ptr<Impl> m_impl;
74+
public:
75+
static geode::Result<std::shared_ptr<BindableAction>> create(
76+
ActionID const& id,
77+
std::string const& name,
78+
std::string const& description = "",
79+
std::vector<geode::Ref<Bind>> const& defaults = {},
80+
std::shared_ptr<Category> category = {},
81+
bool repeatable = true,
82+
geode::Mod* owner = geode::Mod::get()
83+
) GEODE_EVENT_EXPORT(&BindableActionV2::create, (id, name, description, defaults, category, repeatable, owner));
84+
};
85+
86+
class InvokeBindEventV2 final : public geode::Event {
87+
private:
88+
ActionID m_id;
89+
bool m_down;
90+
91+
friend class BindManager;
92+
friend class InvokeBindFilterV2;
93+
94+
public:
95+
InvokeBindEventV2(ActionID const& id, bool down) : m_id(id), m_down(down) {}
96+
ActionID getID() const {
97+
return m_id;
98+
}
99+
bool isDown() const {
100+
return m_down;
101+
}
102+
};
103+
104+
class InvokeBindFilterV2 final : public geode::EventFilter<InvokeBindEventV2> {
105+
private:
106+
cocos2d::CCNode* m_target;
107+
ActionID m_id;
108+
109+
public:
110+
using Callback = geode::ListenerResult(InvokeBindEventV2*);
111+
112+
geode::ListenerResult handle(std::function<Callback> fn, InvokeBindEventV2* event) {
113+
if (event->getID() == m_id) {
114+
return fn(event);
115+
}
116+
return geode::ListenerResult::Propagate;
117+
}
118+
InvokeBindFilterV2(cocos2d::CCNode* target, ActionID const& id) : m_target(target), m_id(id) {}
119+
};
120+
121+
class BindManagerV2 {
122+
class Impl;
123+
std::unique_ptr<Impl> m_impl;
124+
public:
125+
static geode::Result<bool> registerBindable(
126+
std::shared_ptr<BindableAction> action,
127+
ActionID const& after = ""
128+
) GEODE_EVENT_EXPORT(&BindManagerV2::registerBindable, (action, after));
129+
static geode::Result<> removeBindable(ActionID const& action) GEODE_EVENT_EXPORT(&BindManagerV2::removeBindable, (action));
130+
static geode::Result<std::shared_ptr<BindableAction>> getBindable(ActionID const& action) GEODE_EVENT_EXPORT(&BindManagerV2::getBindable, (action));
131+
static geode::Result<std::vector<std::shared_ptr<BindableAction>>> getAllBindables() GEODE_EVENT_EXPORT(&BindManagerV2::getAllBindables, ());
132+
static geode::Result<std::vector<std::shared_ptr<BindableAction>>> getBindablesIn(std::shared_ptr<Category> category, bool sub = false) GEODE_EVENT_EXPORT(&BindManagerV2::getBindablesIn, (category, sub));
133+
static geode::Result<std::vector<std::shared_ptr<BindableAction>>> getBindablesFor(Bind* bind) GEODE_EVENT_EXPORT(&BindManagerV2::getBindablesFor, (bind));
134+
135+
static geode::Result<std::vector<std::shared_ptr<Category>>> getAllCategories() GEODE_EVENT_EXPORT(&BindManagerV2::getAllCategories, ());
136+
static geode::Result<> addCategory(std::shared_ptr<Category> category) GEODE_EVENT_EXPORT(&BindManagerV2::addCategory, (category));
137+
static geode::Result<> removeCategory(std::shared_ptr<Category> category) GEODE_EVENT_EXPORT(&BindManagerV2::removeCategory, (category));
138+
139+
static geode::Result<std::vector<geode::Ref<Bind>>> getBindsFor(ActionID const& action) GEODE_EVENT_EXPORT(&BindManagerV2::getBindsFor, (action));
140+
};
141+
}

src/Keybinds.cpp

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#define GEODE_DEFINE_EVENT_EXPORTS
2+
#include "../include/OptionalAPI.hpp"
13
#include "../include/Keybinds.hpp"
24

35
#include <Geode/cocos/robtop/keyboard_dispatcher/CCKeyboardDelegate.h>
@@ -645,6 +647,12 @@ ListenerResult BindManager::onDispatch(PressBindEvent* event) {
645647
) {
646648
ret = ListenerResult::Stop;
647649
}
650+
if (
651+
ret != ListenerResult::Stop &&
652+
InvokeBindEventV2(action, event->isDown()).post() == ListenerResult::Stop
653+
) {
654+
ret = ListenerResult::Stop;
655+
}
648656
}
649657
return ret;
650658
}
@@ -754,3 +762,94 @@ void BindManager::save() {
754762
$on_mod(DataSaved) {
755763
BindManager::get()->save();
756764
}
765+
766+
Result<Keybind*> KeybindV2::create(cocos2d::enumKeyCodes key, Modifier modifiers) {
767+
return Ok(Keybind::create(key, modifiers));
768+
}
769+
770+
Result<MouseBind*> MouseBindV2::create(MouseButton button, Modifier modifiers) {
771+
return Ok(MouseBind::create(button, modifiers));
772+
}
773+
774+
Result<ControllerBind*> ControllerBindV2::create(
775+
cocos2d::enumKeyCodes button
776+
) {
777+
return Ok(ControllerBind::create(button));
778+
}
779+
780+
Result<std::shared_ptr<Category>> CategoryV2::create(std::string_view path) {
781+
return Ok(std::make_shared<Category>(path.data()));
782+
}
783+
784+
Result<std::shared_ptr<BindableAction>> BindableActionV2::create(
785+
ActionID const& id,
786+
std::string const& name,
787+
std::string const& description,
788+
std::vector<Ref<Bind>> const& defaults,
789+
std::shared_ptr<Category> category,
790+
bool repeatable,
791+
Mod* owner
792+
) {
793+
return Ok(std::make_shared<BindableAction>(id, name, description, defaults, *category, repeatable, owner));
794+
}
795+
796+
Result<bool> BindManagerV2::registerBindable(std::shared_ptr<BindableAction> action, ActionID const& after) {
797+
return Ok(BindManager::get()->registerBindable(*action, after));
798+
}
799+
800+
geode::Result<> BindManagerV2::removeBindable(ActionID const& action) {
801+
BindManager::get()->removeBindable(action);
802+
return Ok();
803+
}
804+
805+
geode::Result<std::shared_ptr<BindableAction>> BindManagerV2::getBindable(ActionID const& action) {
806+
auto bindable = BindManager::get()->getBindable(action);
807+
if (!bindable) return Ok(nullptr);
808+
return Ok(std::make_shared<BindableAction>(*bindable));
809+
}
810+
811+
geode::Result<std::vector<std::shared_ptr<BindableAction>>> BindManagerV2::getAllBindables() {
812+
std::vector<std::shared_ptr<BindableAction>> bindables;
813+
for (auto& bindable : BindManager::get()->getAllBindables()) {
814+
bindables.push_back(std::make_shared<BindableAction>(bindable));
815+
}
816+
return Ok(bindables);
817+
}
818+
819+
geode::Result<std::vector<std::shared_ptr<BindableAction>>> BindManagerV2::getBindablesIn(std::shared_ptr<Category> category, bool sub) {
820+
std::vector<std::shared_ptr<BindableAction>> bindables;
821+
for (auto& bindable : BindManager::get()->getBindablesIn(*category, sub)) {
822+
bindables.push_back(std::make_shared<BindableAction>(bindable));
823+
}
824+
return Ok(bindables);
825+
}
826+
827+
geode::Result<std::vector<std::shared_ptr<BindableAction>>> BindManagerV2::getBindablesFor(Bind* bind) {
828+
std::vector<std::shared_ptr<BindableAction>> bindables;
829+
for (auto& bindable : BindManager::get()->getBindablesFor(bind)) {
830+
bindables.push_back(std::make_shared<BindableAction>(bindable));
831+
}
832+
return Ok(bindables);
833+
}
834+
835+
geode::Result<std::vector<std::shared_ptr<Category>>> BindManagerV2::getAllCategories() {
836+
std::vector<std::shared_ptr<Category>> categories;
837+
for (auto& category : BindManager::get()->getAllCategories()) {
838+
categories.push_back(std::make_shared<Category>(category));
839+
}
840+
return Ok(categories);
841+
}
842+
843+
geode::Result<> BindManagerV2::addCategory(std::shared_ptr<Category> category) {
844+
BindManager::get()->addCategory(*category);
845+
return Ok();
846+
}
847+
848+
geode::Result<> BindManagerV2::removeCategory(std::shared_ptr<Category> category) {
849+
BindManager::get()->removeCategory(*category);
850+
return Ok();
851+
}
852+
853+
geode::Result<std::vector<geode::Ref<Bind>>> BindManagerV2::getBindsFor(ActionID const& action) {
854+
return Ok(BindManager::get()->getBindsFor(action));
855+
}

0 commit comments

Comments
 (0)