fix dispatch

This commit is contained in:
altalk23 2024-02-25 10:38:46 +03:00
parent f33f116ced
commit afd54a64e4
4 changed files with 33 additions and 10 deletions
loader
include/Geode/loader
src/loader
test
dependency
main

View file

@ -9,6 +9,8 @@
namespace geode {
// Mod interoperability
std::unordered_map<std::string, EventListenerPool*>& dispatchPools();
template <class... Args>
class DispatchEvent : public Event {
protected:
@ -26,6 +28,13 @@ namespace geode {
std::string getID() const {
return m_id;
}
EventListenerPool* getPool() const override {
if (dispatchPools().count(m_id) == 0) {
dispatchPools()[m_id] = new DefaultEventListenerPool();
}
return dispatchPools()[m_id];
}
};
template <class... Args>
@ -33,20 +42,15 @@ namespace geode {
protected:
std::string m_id;
static auto& pools() {
static std::unordered_map<std::string, EventListenerPool*> s_pools;
return s_pools;
}
public:
using Ev = DispatchEvent<Args...>;
using Callback = ListenerResult(Args...);
EventListenerPool* getPool() const {
if (pools().count(m_id) == 0) {
pools()[m_id] = new DefaultEventListenerPool();
if (dispatchPools().count(m_id) == 0) {
dispatchPools()[m_id] = new DefaultEventListenerPool();
}
return pools()[m_id];
return dispatchPools()[m_id];
}
ListenerResult handle(utils::MiniFunction<Callback> fn, Ev* event) {

View file

@ -0,0 +1,8 @@
#include <Geode/loader/Dispatch.hpp>
using namespace geode::prelude;
std::unordered_map<std::string, EventListenerPool*>& geode::dispatchPools() {
static std::unordered_map<std::string, EventListenerPool*> pools;
return pools;
}

View file

@ -171,5 +171,5 @@ $on_mod(Loaded) {
label->setZOrder(99999);
gl->addChild(label);
return ListenerResult::Propagate;
}, MyDispatchFilter("geode.testdep/test-garage-open"));
}, MyDispatchFilter("geode.test/test-garage-open"));
}

View file

@ -16,10 +16,13 @@ $on_mod(Loaded) {
log::info("Loaded");
}
static std::string s_recievedEvent;
// Events
$execute {
new EventListener<TestEventFilter>(+[](TestEvent* event) {
log::info("Received event: {}", event->getData());
s_recievedEvent = event->getData();
});
}
@ -99,7 +102,15 @@ struct GJGarageLayerTest : Modify<GJGarageLayerTest, GJGarageLayer> {
addChild(label2);
// Dispatch system pt. 1
MyDispatchEvent("test-garage-open"_spr, this).post();
MyDispatchEvent("geode.test/test-garage-open", this).post();
if (s_recievedEvent.size() > 0) {
auto label = CCLabelBMFont::create("Event works!", "bigFont.fnt");
label->setPosition(100, 70);
label->setScale(.4f);
label->setZOrder(99999);
addChild(label);
}
return true;
}