Skip to content

Commit

Permalink
feat(event): add globalListen + EventListener operator=
Browse files Browse the repository at this point in the history
  • Loading branch information
Fleeym committed Jan 13, 2025
1 parent 591a9b7 commit 4fd9dd8
Showing 1 changed file with 38 additions and 2 deletions.
40 changes: 38 additions & 2 deletions loader/include/Geode/loader/Event.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
#include "../utils/casts.hpp"

#include <Geode/DefaultInclude.hpp>
#include <Geode/loader/Log.hpp>
#include <functional>
#include <memory>
#include <type_traits>
#include <mutex>
#include <deque>
#include <unordered_set>
#include <atomic>
#include <vector>

namespace geode {
class Mod;
Expand Down Expand Up @@ -190,6 +193,32 @@ namespace geode {
this->enable();
}

EventListener& operator=(EventListener const& other) {
if (this == &other) {
return *this;
}

m_callback = other.m_callback;
m_filter = other.m_filter;
m_filter.setListener(this);

return *this;
}

EventListener& operator=(EventListener&& other) {
if (this == &other) {
return *this;
}

m_callback = std::move(other.m_callback);
m_filter = std::move(other.m_filter);

m_filter.setListener(this);
other.disable();

return *this;
}

void bind(std::function<Callback> fn) {
m_callback = fn;
}
Expand Down Expand Up @@ -239,4 +268,11 @@ namespace geode {

virtual ~Event();
};
}

// Creates an EventListener that is active for the entire lifetime of the game. You have no way of disabling the listener, so only use this if you want to always listen for certain events!
template <is_filter T>
void globalListen(typename T::Callback callback, T filter = T()) {
static std::vector<std::unique_ptr<EventListenerProtocol>> listeners;
listeners.push_back(std::make_unique<EventListener<T>>(EventListener<T> {callback, filter}));
}
}

0 comments on commit 4fd9dd8

Please sign in to comment.