event stuff

This commit is contained in:
hjfod 2023-03-25 18:22:34 +02:00
parent e962d5b89f
commit 52c308e65d
4 changed files with 23 additions and 6 deletions

View file

@ -990,11 +990,14 @@ public:
GEODE_DLL void swapChildIndices(CCNode* first, CCNode* second);
template <class Filter, class... Args>
void addEventListener(typename Filter::Callback listener, Args&&... args) {
this->addEventListenerInternal(new geode::EventListener<Filter>(
geode::EventListenerProtocol* addEventListener(typename Filter::Callback listener, Args&&... args) {
auto listener = new geode::EventListener<Filter>(
listener, Filter(this, std::forward<Args>(args)...)
));
);
this->addEventListenerInternal(listener);
return listener;
}
GEODE_DLL void removeEventListener(geode::EventListenerProtocol* listener);
/// @{
/// @name Shader Program

View file

@ -127,7 +127,6 @@ namespace geode {
class GEODE_DLL [[nodiscard]] Event {
private:
static std::vector<EventListenerProtocol*>& listeners();
friend EventListenerProtocol;
public:
@ -140,5 +139,12 @@ namespace geode {
}
virtual ~Event();
static std::vector<EventListenerProtocol*>& listeners();
/**
* Move an event listener to the front of the queue so it is always hit
* first
*/
static void prioritize(EventListenerProtocol* listener);
};
}

View file

@ -181,8 +181,12 @@ 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);
void CCNode::addEventListenerInternal(EventListenerProtocol* listener) {
GeodeNodeMetadata::set(this)->m_eventListeners.push_back(listener);
}
void CCNode::removeEventListener(EventListenerProtocol* listener) {
ranges::remove(GeodeNodeMetadata::set(this)->m_eventListeners, listener);
}
#pragma warning(pop)

View file

@ -33,3 +33,7 @@ std::vector<EventListenerProtocol*>& Event::listeners() {
static std::vector<EventListenerProtocol*> listeners;
return listeners;
}
void Event::prioritize(EventListenerProtocol* listener) {
ranges::move(Event::listeners(), listener, 0);
}