Merge pull request #5544 from heliclei/node-string-tag

CCNode:Add String tag, to support runtime manipulation
This commit is contained in:
James Chen 2014-03-10 18:26:51 +08:00
commit 63292b5ba6
3 changed files with 75 additions and 2 deletions

View File

@ -598,6 +598,17 @@ void Node::setTag(int var)
_tag = var; _tag = var;
} }
//name getter
std::string& Node::getName()
{
return _name;
}
//name setter
void Node::setName(const std::string& name)
{
_name = name;
}
/// userData setter /// userData setter
void Node::setUserData(void *var) void Node::setUserData(void *var)
{ {
@ -702,6 +713,26 @@ Node* Node::getChildByTag(int tag)
return nullptr; return nullptr;
} }
Node* Node::getChildByName(const std::string& name)
{
for (auto& child : _children)
{
if(child->_name == name)
{
return child;
}
auto found = child->getChildByName(name);
if(found != nullptr)
{
return found;
}
}
return nullptr;
}
/* "add" logic MUST only be on this method /* "add" logic MUST only be on this method
* If a class want's to extend the 'addChild' behavior it only needs * If a class want's to extend the 'addChild' behavior it only needs
* to override this method * to override this method

View File

@ -645,6 +645,15 @@ public:
* @return a Node object whose tag equals to the input parameter * @return a Node object whose tag equals to the input parameter
*/ */
virtual Node * getChildByTag(int tag); virtual Node * getChildByTag(int tag);
/**
* Gets a child from the container with its name
*
* @param name A string identifier to find the child node.
*
* @return the first Node object whose name equals to the input parameter
*/
virtual Node* getChildByName(const std::string& name);
/** /**
* Returns the array of the node's children * Returns the array of the node's children
* *
@ -759,6 +768,38 @@ public:
*/ */
virtual void setTag(int tag); virtual void setTag(int tag);
/// @{
/// @name name
/**
* Returns a name that is used to identify the node easily.
*
* You can set name to node then identify them easily.
@code
// set name
node1->setName("player");
node2->setName("monster");
node3->setName("boss");
parent->addChild(node1);
parent->addChild(node2);
parent->addChild(node3);
// identify by name
Node* player = nullptr;
player = parent->getChildByName("player")
@endcode
*
* @return A C string that identifies the node.
*/
virtual std::string& getName();
/**
* Changes the string TAG that is used to identify the node easily.
*
* Please refer to getLabel for the sample code.
*
* @param name A string that indentifies the node.
*/
virtual void setName(const std::string& name);
/** /**
* Returns a custom user data pointer * Returns a custom user data pointer
* *
@ -1404,6 +1445,8 @@ protected:
int _tag; ///< a tag. Can be any number you assigned just to identify this node int _tag; ///< a tag. Can be any number you assigned just to identify this node
std::string _name; ///<a string label, an user defined string to identify this node
void *_userData; ///< A user assingned void pointer, Can be point to any cpp object void *_userData; ///< A user assingned void pointer, Can be point to any cpp object
Ref *_userObject; ///< A user assigned Object Ref *_userObject; ///< A user assigned Object

View File

@ -814,7 +814,6 @@ bool Console::parseCommand(int fd)
const char err[] = "Unknown command. Type 'help' for options\n"; const char err[] = "Unknown command. Type 'help' for options\n";
send(fd, err, sizeof(err),0); send(fd, err, sizeof(err),0);
} }
sendPrompt(fd); sendPrompt(fd);
return true; return true;