mirror of
https://github.com/geode-sdk/geode.git
synced 2024-11-14 19:15:05 -05:00
add SceneManager::getPersistedNodes
requested by alphalaneous
This commit is contained in:
parent
a5ace74b75
commit
5009caa391
2 changed files with 31 additions and 21 deletions
|
@ -3,28 +3,40 @@
|
|||
#include "../DefaultInclude.hpp"
|
||||
|
||||
#include <cocos2d.h>
|
||||
|
||||
namespace cocos2d {
|
||||
class CCArray;
|
||||
class CCNode;
|
||||
}
|
||||
#include <vector>
|
||||
#include <span>
|
||||
#include <Geode/utils/cocos.hpp>
|
||||
|
||||
namespace geode {
|
||||
class GEODE_DLL SceneManager final {
|
||||
protected:
|
||||
cocos2d::CCArray* m_persistedNodes;
|
||||
std::vector<Ref<cocos2d::CCNode>> m_persistedNodes;
|
||||
cocos2d::CCScene* m_lastScene = nullptr;
|
||||
|
||||
bool setup();
|
||||
|
||||
virtual ~SceneManager();
|
||||
|
||||
public:
|
||||
static SceneManager* get();
|
||||
|
||||
/**
|
||||
* Adds a node to the list of persisted nodes, which are kept across scene changes.
|
||||
* @param node The node to keep across scenes.
|
||||
*/
|
||||
void keepAcrossScenes(cocos2d::CCNode* node);
|
||||
|
||||
/**
|
||||
* Removes a node from the list of persisted nodes.
|
||||
* @param node The node to forget.
|
||||
*/
|
||||
void forget(cocos2d::CCNode* node);
|
||||
|
||||
/**
|
||||
* Gets a span of the persisted nodes. To add new nodes to the list, use keepAcrossScenes.
|
||||
*/
|
||||
std::span<Ref<cocos2d::CCNode> const> getPersistedNodes();
|
||||
|
||||
// This method should only be called by geode itself
|
||||
// TODO(v4): hide this
|
||||
void willSwitchToScene(cocos2d::CCScene* scene);
|
||||
};
|
||||
}
|
||||
|
|
|
@ -3,39 +3,37 @@
|
|||
|
||||
using namespace geode::prelude;
|
||||
|
||||
bool SceneManager::setup() {
|
||||
m_persistedNodes = CCArray::create();
|
||||
m_persistedNodes->retain();
|
||||
return true;
|
||||
}
|
||||
|
||||
SceneManager* SceneManager::get() {
|
||||
static SceneManager* inst = nullptr;
|
||||
if (!inst) {
|
||||
inst = new SceneManager();
|
||||
inst->setup();
|
||||
}
|
||||
return inst;
|
||||
}
|
||||
|
||||
SceneManager::~SceneManager() {
|
||||
m_persistedNodes->release();
|
||||
}
|
||||
SceneManager::~SceneManager() {}
|
||||
|
||||
void SceneManager::keepAcrossScenes(CCNode* node) {
|
||||
if (ranges::contains(m_persistedNodes, node)) {
|
||||
return;
|
||||
}
|
||||
if (m_lastScene) {
|
||||
node->removeFromParentAndCleanup(false);
|
||||
m_lastScene->addChild(node);
|
||||
}
|
||||
m_persistedNodes->addObject(node);
|
||||
m_persistedNodes.push_back(node);
|
||||
}
|
||||
|
||||
void SceneManager::forget(CCNode* node) {
|
||||
m_persistedNodes->removeObject(node);
|
||||
std::erase(m_persistedNodes, node);
|
||||
}
|
||||
|
||||
std::span<Ref<CCNode> const> SceneManager::getPersistedNodes() {
|
||||
return m_persistedNodes;
|
||||
}
|
||||
|
||||
void SceneManager::willSwitchToScene(CCScene* scene) {
|
||||
for (auto node : CCArrayExt<CCNode*>(m_persistedNodes)) {
|
||||
for (auto& node : m_persistedNodes) {
|
||||
// no cleanup in order to keep actions running
|
||||
node->removeFromParentAndCleanup(false);
|
||||
scene->addChild(node);
|
||||
|
|
Loading…
Reference in a new issue