geode/loader/include/Geode/ui/EnterLayerEvent.hpp
HJfod d701563534 lots of stuff
- add TableView virtuals on windows
 - rework events to match new events system
 - rename NodeStringIDManager to NodeIDs and add a syntactically sugary NodeIDs::provideFor function
 - change test mod to use event callbacks instead of exported ones
2022-11-12 14:55:25 +02:00

80 lines
2 KiB
C++

#pragma once
#include "../loader/Event.hpp"
namespace cocos2d {
class CCNode;
}
namespace geode {
template<class T>
concept InheritsCCNode = std::is_base_of_v<cocos2d::CCNode, T>;
// Base class; exists so event listeners can be placed dynamically at runtime
class GEODE_DLL AEnterLayerEvent : public Event {
protected:
std::string m_layerID;
cocos2d::CCNode* m_layer;
public:
AEnterLayerEvent(
std::string const& layerID,
cocos2d::CCNode* layer
);
std::string getID() const;
cocos2d::CCNode* getLayer() const;
};
class GEODE_DLL AEnterLayerFilter : public EventFilter<AEnterLayerEvent> {
public:
using Callback = std::function<void(AEnterLayerEvent*)>;
protected:
std::optional<std::string> m_targetID;
public:
ListenerResult handle(Callback fn, AEnterLayerEvent* event);
AEnterLayerFilter(
std::optional<std::string> const& id
);
};
template<InheritsCCNode T>
class EnterLayerEvent : public AEnterLayerEvent {
public:
EnterLayerEvent(
std::string const& layerID,
T* layer
) : AEnterLayerEvent(layerID, layer) {}
T* getLayer() const {
return static_cast<T*>(m_layer);
}
};
template<class T, class N>
concept InheritsEnterLayer = std::is_base_of_v<EnterLayerEvent<N>, T>;
template<class N, InheritsEnterLayer<N> T>
class EnterLayerFilter : public EventFilter<EnterLayerEvent<N>> {
public:
using Callback = void(T*);
protected:
std::optional<std::string> m_targetID;
public:
ListenerResult handle(std::function<Callback> fn, EnterLayerEvent<N>* event) {
if (m_targetID == event->getID()) {
fn(static_cast<T*>(event));
}
return ListenerResult::Propagate;
}
EnterLayerFilter(
std::optional<std::string> const& id
) : m_targetID(id) {}
};
}