mirror of
https://github.com/geode-sdk/geode.git
synced 2024-11-23 07:57:51 -05:00
fix event listeners vector containing freed listeners
This commit is contained in:
parent
4801f6725f
commit
c1d4a89f8b
2 changed files with 19 additions and 5 deletions
|
@ -4,7 +4,9 @@
|
||||||
using namespace geode::prelude;
|
using namespace geode::prelude;
|
||||||
|
|
||||||
void EventListenerProtocol::enable() {
|
void EventListenerProtocol::enable() {
|
||||||
Event::listeners().push_back(this);
|
if (!ranges::contains(Event::listeners(), this)) {
|
||||||
|
Event::listeners().push_back(this);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void EventListenerProtocol::disable() {
|
void EventListenerProtocol::disable() {
|
||||||
|
@ -20,7 +22,16 @@ Event::~Event() {}
|
||||||
|
|
||||||
void Event::postFrom(Mod* m) {
|
void Event::postFrom(Mod* m) {
|
||||||
if (m) this->sender = m;
|
if (m) this->sender = m;
|
||||||
|
auto& listeners = Event::listeners();
|
||||||
|
listeners.erase(std::remove_if(
|
||||||
|
listeners.begin(),
|
||||||
|
listeners.end(),
|
||||||
|
[](auto& a) {
|
||||||
|
return Event::removedListeners().contains(a);
|
||||||
|
}
|
||||||
|
), listeners.end());
|
||||||
|
Event::removedListeners().clear();
|
||||||
|
|
||||||
std::vector<EventListenerProtocol*> listeners_copy = Event::listeners();
|
std::vector<EventListenerProtocol*> listeners_copy = Event::listeners();
|
||||||
for (auto h : listeners_copy) {
|
for (auto h : listeners_copy) {
|
||||||
// if an event listener gets destroyed in the middle of this loop, we
|
// if an event listener gets destroyed in the middle of this loop, we
|
||||||
|
@ -30,7 +41,6 @@ void Event::postFrom(Mod* m) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Event::removedListeners().clear();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std::unordered_set<EventListenerProtocol*>& Event::removedListeners() {
|
std::unordered_set<EventListenerProtocol*>& Event::removedListeners() {
|
||||||
|
|
|
@ -328,8 +328,12 @@ void geode::cocos::limitNodeSize(cocos2d::CCNode* spr, cocos2d::CCSize const& si
|
||||||
}
|
}
|
||||||
|
|
||||||
bool geode::cocos::nodeIsVisible(cocos2d::CCNode* node) {
|
bool geode::cocos::nodeIsVisible(cocos2d::CCNode* node) {
|
||||||
if (!node->isVisible()) return false;
|
if (!node->isVisible()) {
|
||||||
if (node->getParent()) return nodeIsVisible(node->getParent());
|
return false;
|
||||||
|
}
|
||||||
|
if (node->getParent()) {
|
||||||
|
return nodeIsVisible(node->getParent());
|
||||||
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue