Skip to content

Commit

Permalink
builds on v4
Browse files Browse the repository at this point in the history
  • Loading branch information
matcool committed Nov 11, 2024
1 parent 53615e9 commit 52f82ba
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 65 deletions.
7 changes: 3 additions & 4 deletions include/Keybinds.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#pragma once

#include <Geode/DefaultInclude.hpp>
#include <Geode/utils/MiniFunction.hpp>
#include <Geode/utils/cocos.hpp>
#include <Geode/loader/Mod.hpp>
#include <Geode/loader/Event.hpp>
Expand Down Expand Up @@ -234,7 +233,7 @@ namespace keybinds {
public:
using Callback = geode::ListenerResult(InvokeBindEvent*);

geode::ListenerResult handle(geode::utils::MiniFunction<Callback> fn, InvokeBindEvent* event);
geode::ListenerResult handle(std::function<Callback> fn, InvokeBindEvent* event);
InvokeBindFilter(cocos2d::CCNode* target, ActionID const& id);
};

Expand All @@ -253,7 +252,7 @@ namespace keybinds {
public:
using Callback = geode::ListenerResult(PressBindEvent*);

geode::ListenerResult handle(geode::utils::MiniFunction<Callback> fn, PressBindEvent* event);
geode::ListenerResult handle(std::function<Callback> fn, PressBindEvent* event);
PressBindFilter();
};

Expand All @@ -276,7 +275,7 @@ namespace keybinds {
public:
using Callback = void(DeviceEvent*);

geode::ListenerResult handle(geode::utils::MiniFunction<Callback> fn, DeviceEvent* event);
geode::ListenerResult handle(std::function<Callback> fn, DeviceEvent* event);
DeviceFilter(std::optional<DeviceID> id = std::nullopt);
};

Expand Down
6 changes: 3 additions & 3 deletions mod.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"geode": "3.6.0",
"geode": "4.0.0",
"gd": {
"win": "2.206",
"win": "2.2073",
"android": "2.206"
},
"version": "v1.9.0",
Expand Down Expand Up @@ -29,7 +29,7 @@
"incompatibilities": [],
"settings": {
"open-menu": {
"type": "custom"
"type": "custom:open-menu"
}
}
}
6 changes: 3 additions & 3 deletions src/ControllerBind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ ControllerBind* ControllerBind::create(enumKeyCodes button) {

ControllerBind* ControllerBind::parse(matjson::Value const& value) {
return ControllerBind::create(
static_cast<enumKeyCodes>(value["button"].as_double())
static_cast<enumKeyCodes>(value["button"].asInt().unwrapOr(0))
);
}

matjson::Value ControllerBind::save() const {
return matjson::Object {
return matjson::makeObject({
{ "button", static_cast<int>(m_button) },
};
});
}

enumKeyCodes ControllerBind::getButton() const {
Expand Down
8 changes: 4 additions & 4 deletions src/Keybind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@ Keybind* Keybind::create(enumKeyCodes key, Modifier modifiers) {

Keybind* Keybind::parse(matjson::Value const& value) {
return Keybind::create(
static_cast<enumKeyCodes>(value["key"].as_int()),
static_cast<Modifier>(value["modifiers"].as_int())
static_cast<enumKeyCodes>(value["key"].asInt().unwrapOr(0)),
static_cast<Modifier>(value["modifiers"].asInt().unwrapOr(0))
);
}

matjson::Value Keybind::save() const {
return matjson::Object {
return matjson::makeObject({
{ "key", static_cast<int>(m_key) },
{ "modifiers", static_cast<int>(m_modifiers) }
};
});
}

enumKeyCodes Keybind::getKey() const {
Expand Down
47 changes: 24 additions & 23 deletions src/Keybinds.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ bool InvokeBindEvent::isDown() const {
return m_down;
}

ListenerResult InvokeBindFilter::handle(utils::MiniFunction<Callback> fn, InvokeBindEvent* event) {
ListenerResult InvokeBindFilter::handle(std::function<Callback> fn, InvokeBindEvent* event) {
if (event->getID() == m_id) {
return fn(event);
}
Expand All @@ -211,7 +211,7 @@ bool PressBindEvent::isDown() const {
return m_down;
}

geode::ListenerResult PressBindFilter::handle(MiniFunction<Callback> fn, PressBindEvent* event) {
geode::ListenerResult PressBindFilter::handle(std::function<Callback> fn, PressBindEvent* event) {
return fn(event);
}

Expand All @@ -232,7 +232,7 @@ bool DeviceEvent::wasDetached() const {
return !m_attached;
}

ListenerResult DeviceFilter::handle(MiniFunction<Callback> fn, DeviceEvent* event) {
ListenerResult DeviceFilter::handle(std::function<Callback> fn, DeviceEvent* event) {
if (!m_id || m_id == event->getID()) {
fn(event);
}
Expand Down Expand Up @@ -309,7 +309,10 @@ matjson::Value BindManager::saveBind(Bind* bind) const {

Bind* BindManager::loadBind(matjson::Value const& json) const {
try {
auto device = json["device"].as_string();
auto res = json["device"].asString();
if (!res)
return nullptr;
auto device = res.unwrap();
if (!m_devices.contains(device)) {
return nullptr;
}
Expand All @@ -321,9 +324,9 @@ Bind* BindManager::loadBind(matjson::Value const& json) const {
}

bool BindManager::loadActionBinds(ActionID const& action) {
try {
auto value = Mod::get()->template getSavedValue<matjson::Object>(action);
for (auto bind : value["binds"].as_array()) {
auto inner = [&]() -> Result<> {
auto value = Mod::get()->getSavedValue<matjson::Value>(action);
for (auto bind : value["binds"]) {
// try directly parsing the bind from a string if the device it's for
// is already connected
if (auto b = this->loadBind(bind)) {
Expand All @@ -334,9 +337,9 @@ bool BindManager::loadActionBinds(ActionID const& action) {
else {
// if device ID exists, then add this to the list of unbound
// binds
if (bind.contains("device")) {
if (bind.contains("device") && bind["device"].isString()) {
try {
m_devicelessBinds[bind["device"].as_string()][action].insert(bind);
m_devicelessBinds[bind["device"].asString().unwrap()][action].insert(bind);
}
catch(...) {}
}
Expand All @@ -345,36 +348,34 @@ bool BindManager::loadActionBinds(ActionID const& action) {
}
// load repeat options
if (value.contains("repeat")) {
auto rep = value["repeat"].as_object();
auto rep = value["repeat"];
auto opts = RepeatOptions();
opts.enabled = rep["enabled"].as_bool();
opts.rate = rep["rate"].as_int();
opts.delay = rep["delay"].as_int();
GEODE_UNWRAP_INTO(opts.enabled, rep["enabled"].asBool());
GEODE_UNWRAP_INTO(opts.rate, rep["rate"].asInt());
GEODE_UNWRAP_INTO(opts.delay, rep["delay"].asInt());
this->setRepeatOptionsFor(action, opts);
}
return true;
}
catch(...) {
return false;
}
return Ok();
};
return inner().isOk();
}

void BindManager::saveActionBinds(ActionID const& action) {
auto obj = matjson::Object();
auto binds = matjson::Array();
auto obj = matjson::Value::object();
auto binds = matjson::Value::array();
for (auto& bind : this->getBindsFor(action)) {
binds.push_back(this->saveBind(bind));
binds.push(this->saveBind(bind));
}
for (auto& [device, actions] : m_devicelessBinds) {
if (actions.contains(action)) {
for (auto& bind : actions.at(action)) {
binds.push_back(bind);
binds.push(bind);
}
}
}
obj["binds"] = binds;
if (auto opts = this->getRepeatOptionsFor(action)) {
auto rep = matjson::Object();
auto rep = matjson::Value::object();
rep["enabled"] = opts.value().enabled;
rep["rate"] = opts.value().rate;
rep["delay"] = opts.value().delay;
Expand Down
4 changes: 2 additions & 2 deletions src/KeybindsLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ bool EditRepeatPopup::setup(BindableNode* node) {
rateInput->setString(std::to_string(m_options.rate));
rateInput->setCallback([this](std::string const& text) {
if (auto num = numFromString<int>(text)) {
m_options.rate = *num;
m_options.rate = num.unwrap();
}
});
rateInput->setScale(.75f);
Expand All @@ -272,7 +272,7 @@ bool EditRepeatPopup::setup(BindableNode* node) {
delayInput->getInputNode()->setString(std::to_string(m_options.delay));
delayInput->setCallback([this](std::string const& text) {
if (auto num = numFromString<int>(text)) {
m_options.delay = *num;
m_options.delay = num.unwrap();
}
});
delayInput->setScale(.75f);
Expand Down
1 change: 0 additions & 1 deletion src/KeybindsLayer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

#include <Geode/ui/Popup.hpp>
#include <Geode/ui/ScrollLayer.hpp>
#include <Geode/ui/InputNode.hpp>
#include <Geode/binding/TextInputDelegate.hpp>
#include "../include/Keybinds.hpp"

Expand Down
8 changes: 4 additions & 4 deletions src/MouseBind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ MouseBind* MouseBind::create(MouseButton button, Modifier modifiers) {
}
MouseBind* MouseBind::parse(matjson::Value const& value) {
return MouseBind::create(
static_cast<MouseButton>(value["button"].as_int()),
static_cast<Modifier>(value["modifiers"].as_int())
static_cast<MouseButton>(value["button"].asInt().unwrapOrDefault()),
static_cast<Modifier>(value["modifiers"].asInt().unwrapOrDefault())
);
}

Expand Down Expand Up @@ -54,8 +54,8 @@ DeviceID MouseBind::getDeviceID() const {
return "mouse"_spr;
}
matjson::Value MouseBind::save() const {
return matjson::Object {
return matjson::makeObject({
{ "button", static_cast<int>(m_button) },
{ "modifiers", static_cast<int>(m_modifiers) },
};
});
}
10 changes: 5 additions & 5 deletions src/UILayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ struct $modify(PauseLayer) {

// Remove any popups (looking at you, confirm exit)
CCScene* active = CCDirector::sharedDirector()->getRunningScene();
if (auto alert = getChildOfType<FLAlertLayer>(active, 0)) {
if (auto alert = active->getChildByType<FLAlertLayer>(0)) {
return ListenerResult::Propagate;
}
this->onResume(nullptr);
Expand Down Expand Up @@ -96,13 +96,13 @@ struct $modify(UILayer) {

bool isPaused() {
return !this->isCurrentPlayLayer()
|| getChildOfType<PauseLayer>(PlayLayer::get()->getParent(), 0) != nullptr
|| getChildOfType<GJDropDownLayer>(PlayLayer::get(), 0) != nullptr;
|| PlayLayer::get()->getParent()->getChildByType<PauseLayer>(0) != nullptr
|| PlayLayer::get()->getChildByType<GJDropDownLayer>(0) != nullptr;
}

bool isCurrentPlayLayer() {
auto playLayer = getChildOfType<PlayLayer>(CCScene::get(), 0);
return playLayer != nullptr && playLayer == PlayLayer::get() && getChildOfType<UILayer>(playLayer, 0) == this;
auto playLayer = CCScene::get()->getChildByType<PlayLayer>(0);
return playLayer != nullptr && playLayer == PlayLayer::get() && playLayer->getChildByType<UILayer>(0) == this;
}

void pressKeyFallthrough(enumKeyCodes key, bool down) {
Expand Down
30 changes: 14 additions & 16 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include <Geode/binding/ButtonSprite.hpp>
#include <Geode/ui/Notification.hpp>
#include <Geode/modify/Modify.hpp>
#include <Geode/loader/SettingNode.hpp>
#include <Geode/loader/Setting.hpp>
#include <Geode/cocos/robtop/keyboard_dispatcher/CCKeyboardDelegate.h>
#include <Geode/cocos/robtop/keyboard_dispatcher/CCKeyboardDispatcher.h>
Expand Down Expand Up @@ -279,18 +278,18 @@ class ControllerChecker : public CCObject {
}

// Have to make a SettingValue even if it holds no value
class DummySettingValue : public SettingValue {
class DummySetting : public SettingBaseValue<int> {
public:
DummySettingValue(std::string const& key, std::string const& mod) : SettingValue(key, mod) {}
bool load(matjson::Value const& json) override { return true; }
bool save(matjson::Value&) const override { return true; }
static Result<std::shared_ptr<SettingV3>> parse(std::string const&, std::string const&, matjson::Value const&) {
return Ok(std::make_shared<DummySetting>());
};
SettingNode* createNode(float width) override;
};

class ButtonSettingNode : public SettingNode {
class ButtonSettingNode : public SettingValueNode<DummySetting> {
protected:
bool init(SettingValue* value, float width) {
if (!SettingNode::init(value))
bool init(std::shared_ptr<DummySetting>& setting, float width) {
if (!SettingValueNodeV3::init(setting, width))
return false;

this->setContentSize({ width, 40.f });
Expand All @@ -308,12 +307,11 @@ class ButtonSettingNode : public SettingNode {
KeybindsLayer::create()->show();
}
public:
void commit() override { this->dispatchCommitted(); }
bool hasUncommittedChanges() override { return false; }
bool hasNonDefaultValue() override { return false; }
void resetToDefault() override {}
void updateState(CCNode* invoker) override {
SettingValueNodeV3::updateState(invoker);
}

static ButtonSettingNode* create(SettingValue* value, float width) {
static ButtonSettingNode* create(std::shared_ptr<DummySetting> value, float width) {
auto ret = new ButtonSettingNode();
if (ret && ret->init(value, width)) {
ret->autorelease();
Expand All @@ -324,10 +322,10 @@ class ButtonSettingNode : public SettingNode {
}
};

SettingNode* DummySettingValue::createNode(float width) {
return ButtonSettingNode::create(this, width);
SettingNode* DummySetting::createNode(float width) {
return ButtonSettingNode::create(std::static_pointer_cast<DummySetting>(shared_from_this()), width);
}

$execute {
Mod::get()->registerCustomSetting("open-menu", std::make_unique<DummySettingValue>(std::string("open-menu"), Mod::get()->getID()));
(void) Mod::get()->registerCustomSettingType("open-menu", &DummySetting::parse);
}

0 comments on commit 52f82ba

Please sign in to comment.