diff --git a/loader/include/Geode/cocos/base_nodes/CCNode.h b/loader/include/Geode/cocos/base_nodes/CCNode.h index d376e294..30f5c46b 100644 --- a/loader/include/Geode/cocos/base_nodes/CCNode.h +++ b/loader/include/Geode/cocos/base_nodes/CCNode.h @@ -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 { + public: + using Callback = void(AttributeSetEvent*); + + protected: + std::string m_targetID; + + public: + ListenerResult handle(utils::MiniFunction fn, AttributeSetEvent* event); + + AttributeSetFilter(std::string const& id); + }; +} + #endif // __PLATFORM_CCNODE_H__ diff --git a/loader/src/hooks/GeodeNodeMetadata.cpp b/loader/src/hooks/GeodeNodeMetadata.cpp index 64cb94ba..beef927b 100644 --- a/loader/src/hooks/GeodeNodeMetadata.cpp +++ b/loader/src/hooks/GeodeNodeMetadata.cpp @@ -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 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 CCNode::getAttributeInternal(std::string const& attr) {