Adds getScene() to Node

helper function to get the Scene containing the Node
This commit is contained in:
Ricardo Quesada 2013-12-21 12:28:49 -08:00
parent d35846b0d4
commit 4f435a8613
4 changed files with 21 additions and 0 deletions

View File

@ -518,6 +518,13 @@ void Node::setShaderProgram(GLProgram *pShaderProgram)
_shaderProgram = pShaderProgram; _shaderProgram = pShaderProgram;
} }
Scene* Node::getScene()
{
if(!_parent)
return nullptr;
return _parent->getScene();
}
Rect Node::getBoundingBox() const Rect Node::getBoundingBox() const
{ {
Rect rect = Rect(0, 0, _contentSize.width, _contentSize.height); Rect rect = Rect(0, 0, _contentSize.width, _contentSize.height);

View File

@ -53,6 +53,7 @@ class ActionManager;
class Component; class Component;
class ComponentContainer; class ComponentContainer;
class EventDispatcher; class EventDispatcher;
class Scene;
#ifdef CC_USE_PHYSICS #ifdef CC_USE_PHYSICS
class PhysicsBody; class PhysicsBody;
#endif #endif
@ -910,6 +911,11 @@ public:
*/ */
virtual void visit(); virtual void visit();
/** Returns the Scene that contains the Node.
It returns `nullptr` if the node doesn't belong to any Scene.
This function recursively calls parent->getScene() until parent is a Scene object. The results are not cached. It is that the user caches the results in case this functions is being used inside a loop.
*/
virtual Scene* getScene();
/** /**
* Returns a "local" axis aligned bounding box of the node. * Returns a "local" axis aligned bounding box of the node.

View File

@ -83,6 +83,11 @@ std::string Scene::getDescription() const
return StringUtils::format("<Scene | tag = %d>", _tag); return StringUtils::format("<Scene | tag = %d>", _tag);
} }
Scene* Scene::getScene()
{
return this;
}
#ifdef CC_USE_PHYSICS #ifdef CC_USE_PHYSICS
Scene *Scene::createWithPhysics() Scene *Scene::createWithPhysics()
{ {

View File

@ -56,6 +56,8 @@ public:
static Scene *createWithPhysics(); static Scene *createWithPhysics();
#endif #endif
// Overrides
virtual Scene *getScene() override;
#ifdef CC_USE_PHYSICS #ifdef CC_USE_PHYSICS
public: public:
@ -76,6 +78,7 @@ protected:
#endif // CC_USE_PHYSICS #endif // CC_USE_PHYSICS
protected: protected:
Scene(); Scene();
virtual ~Scene(); virtual ~Scene();