2022-10-10 20:58:47 +03:00
|
|
|
#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>;
|
|
|
|
|
2022-10-26 21:52:38 +03:00
|
|
|
// Base class; exists so event listeners can be placed dynamically at runtime
|
2022-12-07 12:35:50 +02:00
|
|
|
struct GEODE_DLL AEnterLayerEvent : public Event {
|
|
|
|
const std::string layerID;
|
|
|
|
cocos2d::CCNode* layer;
|
2022-10-10 20:58:47 +03:00
|
|
|
|
2022-10-26 21:52:38 +03:00
|
|
|
AEnterLayerEvent(
|
|
|
|
std::string const& layerID,
|
|
|
|
cocos2d::CCNode* layer
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2022-11-12 14:55:25 +02:00
|
|
|
class GEODE_DLL AEnterLayerFilter : public EventFilter<AEnterLayerEvent> {
|
2022-10-26 21:52:38 +03:00
|
|
|
public:
|
2023-01-24 00:13:20 +02:00
|
|
|
using Callback = void(AEnterLayerEvent*);
|
2022-10-26 21:52:38 +03:00
|
|
|
|
|
|
|
protected:
|
|
|
|
std::optional<std::string> m_targetID;
|
|
|
|
|
|
|
|
public:
|
2023-02-08 18:25:07 +03:00
|
|
|
ListenerResult handle(utils::MiniFunction<Callback> fn, AEnterLayerEvent* event);
|
2022-10-26 21:52:38 +03:00
|
|
|
|
2022-11-12 14:55:25 +02:00
|
|
|
AEnterLayerFilter(
|
|
|
|
std::optional<std::string> const& id
|
2022-10-26 21:52:38 +03:00
|
|
|
);
|
2023-04-10 16:54:58 +03:00
|
|
|
AEnterLayerFilter(AEnterLayerFilter const&) = default;
|
2022-10-26 21:52:38 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
template<InheritsCCNode T>
|
|
|
|
class EnterLayerEvent : public AEnterLayerEvent {
|
2022-10-10 20:58:47 +03:00
|
|
|
public:
|
|
|
|
EnterLayerEvent(
|
|
|
|
std::string const& layerID,
|
|
|
|
T* layer
|
2022-10-26 21:52:38 +03:00
|
|
|
) : AEnterLayerEvent(layerID, layer) {}
|
2022-10-10 20:58:47 +03:00
|
|
|
|
|
|
|
T* getLayer() const {
|
2022-12-07 12:35:50 +02:00
|
|
|
return static_cast<T*>(this->layer);
|
2022-10-10 20:58:47 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<class T, class N>
|
|
|
|
concept InheritsEnterLayer = std::is_base_of_v<EnterLayerEvent<N>, T>;
|
|
|
|
|
|
|
|
template<class N, InheritsEnterLayer<N> T>
|
2022-11-12 14:55:25 +02:00
|
|
|
class EnterLayerFilter : public EventFilter<EnterLayerEvent<N>> {
|
2022-10-10 20:58:47 +03:00
|
|
|
public:
|
2022-11-12 14:55:25 +02:00
|
|
|
using Callback = void(T*);
|
2022-10-10 20:58:47 +03:00
|
|
|
|
|
|
|
protected:
|
|
|
|
std::optional<std::string> m_targetID;
|
|
|
|
|
|
|
|
public:
|
2023-02-08 18:25:07 +03:00
|
|
|
ListenerResult handle(utils::MiniFunction<Callback> fn, EnterLayerEvent<N>* event) {
|
2022-10-10 20:58:47 +03:00
|
|
|
if (m_targetID == event->getID()) {
|
2022-11-12 14:55:25 +02:00
|
|
|
fn(static_cast<T*>(event));
|
2022-10-10 20:58:47 +03:00
|
|
|
}
|
2022-11-12 14:55:25 +02:00
|
|
|
return ListenerResult::Propagate;
|
2022-10-10 20:58:47 +03:00
|
|
|
}
|
|
|
|
|
2022-11-12 14:55:25 +02:00
|
|
|
EnterLayerFilter(
|
|
|
|
std::optional<std::string> const& id
|
|
|
|
) : m_targetID(id) {}
|
2023-04-10 16:54:58 +03:00
|
|
|
EnterLayerFilter(EnterLayerFilter const&) = default;
|
2022-10-10 20:58:47 +03:00
|
|
|
};
|
|
|
|
}
|