mirror of
https://github.com/geode-sdk/geode.git
synced 2025-02-17 00:30:26 -05:00
add event listener stuff
This commit is contained in:
parent
a5e33668aa
commit
9bdd9fc526
4 changed files with 26 additions and 7 deletions
|
@ -39,6 +39,7 @@
|
|||
#include "../include/CCProtocols.h"
|
||||
#include "Layout.hpp"
|
||||
#include <any>
|
||||
#include "../../loader/Event.hpp"
|
||||
|
||||
NS_CC_BEGIN
|
||||
|
||||
|
@ -851,6 +852,7 @@ private:
|
|||
|
||||
GEODE_DLL geode::modifier::FieldContainer* getFieldContainer();
|
||||
GEODE_DLL std::optional<std::any> getAttributeInternal(std::string const& attribute);
|
||||
GEODE_DLL void addEventListenerInternal(geode::EventListenerProtocol* protocol);
|
||||
|
||||
public:
|
||||
/**
|
||||
|
@ -986,6 +988,13 @@ public:
|
|||
* @note Geode addition
|
||||
*/
|
||||
GEODE_DLL void swapChildIndices(CCNode* first, CCNode* second);
|
||||
|
||||
template <class Filter, class... Args>
|
||||
void addEventListener(Filter::Callback listener, Args&&... args) {
|
||||
this->addEventListenerInternal(new geode::EventListener<Filter>(
|
||||
listener, Filter(this, std::forward<Args>(args)...)
|
||||
));
|
||||
}
|
||||
|
||||
/// @{
|
||||
/// @name Shader Program
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
#pragma once
|
||||
|
||||
#include "../utils/casts.hpp"
|
||||
#include "../utils/MiniFunction.hpp"
|
||||
|
||||
#include <Geode/DefaultInclude.hpp>
|
||||
#include <type_traits>
|
||||
|
@ -126,7 +127,7 @@ namespace geode {
|
|||
|
||||
class GEODE_DLL [[nodiscard]] Event {
|
||||
private:
|
||||
static std::unordered_set<EventListenerProtocol*>& listeners();
|
||||
static std::vector<EventListenerProtocol*>& listeners();
|
||||
friend EventListenerProtocol;
|
||||
|
||||
public:
|
||||
|
@ -137,7 +138,7 @@ namespace geode {
|
|||
void post() {
|
||||
postFrom(getMod());
|
||||
}
|
||||
|
||||
|
||||
virtual ~Event();
|
||||
};
|
||||
}
|
||||
|
|
|
@ -22,6 +22,7 @@ private:
|
|||
Ref<Layout> m_layout = nullptr;
|
||||
std::unique_ptr<LayoutOptions> m_layoutOptions = nullptr;
|
||||
std::unordered_map<std::string, std::any> m_attributes;
|
||||
std::vector<EventListenerProtocol*> m_eventListeners;
|
||||
|
||||
friend class ProxyCCNode;
|
||||
friend class cocos2d::CCNode;
|
||||
|
@ -30,6 +31,9 @@ private:
|
|||
|
||||
virtual ~GeodeNodeMetadata() {
|
||||
delete m_fieldContainer;
|
||||
for (auto& listener : m_eventListeners) {
|
||||
delete listener;
|
||||
}
|
||||
}
|
||||
|
||||
public:
|
||||
|
@ -177,4 +181,8 @@ std::optional<std::any> CCNode::getAttributeInternal(std::string const& attr) {
|
|||
return std::nullopt;
|
||||
}
|
||||
|
||||
void CCNode::addEventListenerInternal(EventListenerProtocol* protocol) {
|
||||
GeodeNodeMetadata::set(this)->m_eventListeners.push_back(protocol);
|
||||
}
|
||||
|
||||
#pragma warning(pop)
|
||||
|
|
|
@ -1,13 +1,14 @@
|
|||
#include <Geode/loader/Event.hpp>
|
||||
#include <Geode/utils/ranges.hpp>
|
||||
|
||||
using namespace geode::prelude;
|
||||
|
||||
void EventListenerProtocol::enable() {
|
||||
Event::listeners().insert(this);
|
||||
Event::listeners().push_back(this);
|
||||
}
|
||||
|
||||
void EventListenerProtocol::disable() {
|
||||
Event::listeners().erase(this);
|
||||
ranges::remove(Event::listeners(), this);
|
||||
}
|
||||
|
||||
EventListenerProtocol::~EventListenerProtocol() {
|
||||
|
@ -19,7 +20,7 @@ Event::~Event() {}
|
|||
void Event::postFrom(Mod* m) {
|
||||
if (m) this->sender = m;
|
||||
|
||||
std::unordered_set<EventListenerProtocol*> listeners_copy = Event::listeners();
|
||||
std::vector<EventListenerProtocol*> listeners_copy = Event::listeners();
|
||||
|
||||
for (auto h : listeners_copy) {
|
||||
if (h->passThrough(this) == ListenerResult::Stop) {
|
||||
|
@ -28,7 +29,7 @@ void Event::postFrom(Mod* m) {
|
|||
}
|
||||
}
|
||||
|
||||
std::unordered_set<EventListenerProtocol*>& Event::listeners() {
|
||||
static std::unordered_set<EventListenerProtocol*> listeners;
|
||||
std::vector<EventListenerProtocol*>& Event::listeners() {
|
||||
static std::vector<EventListenerProtocol*> listeners;
|
||||
return listeners;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue