geode/loader/include/Geode/ui/EnterLayerEvent.hpp

79 lines
2.1 KiB
C++
Raw Normal View History

2022-10-10 13:58:47 -04:00
#pragma once
#include "../loader/Event.hpp"
#include <optional>
2022-10-10 13:58:47 -04:00
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
2022-12-07 05:35:50 -05:00
struct GEODE_DLL AEnterLayerEvent : public Event {
const std::string layerID;
cocos2d::CCNode* layer;
2022-10-10 13:58:47 -04:00
AEnterLayerEvent(
std::string const& layerID,
cocos2d::CCNode* layer
);
};
class GEODE_DLL AEnterLayerFilter : public EventFilter<AEnterLayerEvent> {
public:
using Callback = void(AEnterLayerEvent*);
protected:
std::optional<std::string> m_targetID;
public:
2024-11-04 12:42:09 -05:00
ListenerResult handle(std::function<Callback> fn, AEnterLayerEvent* event);
AEnterLayerFilter(
std::optional<std::string> const& id
);
2023-04-10 09:54:58 -04:00
AEnterLayerFilter(AEnterLayerFilter const&) = default;
};
template<InheritsCCNode T>
2024-06-20 16:00:04 -04:00
class EnterLayerEvent final : public AEnterLayerEvent {
2022-10-10 13:58:47 -04:00
public:
EnterLayerEvent(
std::string const& layerID,
T* layer
) : AEnterLayerEvent(layerID, layer) {}
2022-10-10 13:58:47 -04:00
T* getLayer() const {
2022-12-07 05:35:50 -05:00
return static_cast<T*>(this->layer);
2022-10-10 13:58:47 -04:00
}
};
template<class T, class N>
concept InheritsEnterLayer = std::is_base_of_v<EnterLayerEvent<N>, T>;
template<class N, InheritsEnterLayer<N> T = EnterLayerEvent<N>>
2024-06-20 16:00:04 -04:00
class EnterLayerFilter final : public EventFilter<EnterLayerEvent<N>> {
2022-10-10 13:58:47 -04:00
public:
using Callback = void(T*);
2022-10-10 13:58:47 -04:00
protected:
std::optional<std::string> m_targetID;
public:
2024-11-04 12:42:09 -05:00
ListenerResult handle(std::function<Callback> fn, EnterLayerEvent<N>* event) {
2022-10-10 13:58:47 -04:00
if (m_targetID == event->getID()) {
fn(static_cast<T*>(event));
2022-10-10 13:58:47 -04:00
}
return ListenerResult::Propagate;
2022-10-10 13:58:47 -04:00
}
EnterLayerFilter(
std::optional<std::string> const& id
) : m_targetID(id) {}
2023-04-10 09:54:58 -04:00
EnterLayerFilter(EnterLayerFilter const&) = default;
2022-10-10 13:58:47 -04:00
};
}