add AttributeSetEvent

This commit is contained in:
hjfod 2023-03-29 16:05:20 +03:00
parent 7963469f1e
commit 69bd0d99ef
2 changed files with 38 additions and 1 deletions

View file

@ -1725,4 +1725,27 @@ protected:
NS_CC_END
namespace geode {
struct GEODE_DLL AttributeSetEvent : public Event {
cocos2d::CCNode* node;
const std::string id;
json::Value& value;
AttributeSetEvent(cocos2d::CCNode* node, std::string const& id, json::Value& value);
};
class GEODE_DLL AttributeSetFilter : public EventFilter<AttributeSetEvent> {
public:
using Callback = void(AttributeSetEvent*);
protected:
std::string m_targetID;
public:
ListenerResult handle(utils::MiniFunction<Callback> fn, AttributeSetEvent* event);
AttributeSetFilter(std::string const& id);
};
}
#endif // __PLATFORM_CCNODE_H__

View file

@ -169,8 +169,22 @@ void CCNode::updateLayout(bool updateChildOrder) {
}
}
AttributeSetEvent::AttributeSetEvent(CCNode* node, std::string const& id, json::Value& value)
: node(node), id(id), value(value) {}
ListenerResult AttributeSetFilter::handle(MiniFunction<Callback> fn, AttributeSetEvent* event) {
if (event->id == m_targetID) {
fn(event);
}
return ListenerResult::Propagate;
}
AttributeSetFilter::AttributeSetFilter(std::string const& id) : m_targetID(id) {}
void CCNode::setAttribute(std::string const& attr, json::Value const& value) {
GeodeNodeMetadata::set(this)->m_attributes[attr] = value;
auto meta = GeodeNodeMetadata::set(this);
meta->m_attributes[attr] = value;
AttributeSetEvent(this, attr, meta->m_attributes.at(attr)).post();
}
std::optional<json::Value> CCNode::getAttributeInternal(std::string const& attr) {