CCProtectedNode.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /****************************************************************************
  2. Copyright (c) 2008-2010 Ricardo Quesada
  3. Copyright (c) 2009 Valentin Milea
  4. Copyright (c) 2010-2012 cocos2d-x.org
  5. Copyright (c) 2011 Zynga Inc.
  6. Copyright (c) 2013-2016 Chukong Technologies Inc.
  7. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  8. http://www.cocos2d-x.org
  9. Permission is hereby granted, free of charge, to any person obtaining a copy
  10. of this software and associated documentation files (the "Software"), to deal
  11. in the Software without restriction, including without limitation the rights
  12. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. copies of the Software, and to permit persons to whom the Software is
  14. furnished to do so, subject to the following conditions:
  15. The above copyright notice and this permission notice shall be included in
  16. all copies or substantial portions of the Software.
  17. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. THE SOFTWARE.
  24. ****************************************************************************/
  25. #ifndef __CPROTECTEDCNODE_H__
  26. #define __CPROTECTEDCNODE_H__
  27. #include "2d/CCNode.h"
  28. NS_CC_BEGIN
  29. /**
  30. * @addtogroup _2d
  31. * @{
  32. */
  33. /**
  34. *@brief A inner node type mainly used for UI module.
  35. * It is useful for composing complex node type and it's children are protected.
  36. */
  37. class CC_DLL ProtectedNode : public Node
  38. {
  39. public:
  40. /**
  41. * Creates a ProtectedNode with no argument.
  42. *@return A instance of ProtectedNode.
  43. */
  44. static ProtectedNode * create(void);
  45. /// @{
  46. /// @name Children and Parent
  47. /**
  48. * Adds a child to the container with z-order as 0.
  49. *
  50. * If the child is added to a 'running' node, then 'onEnter' and 'onEnterTransitionDidFinish' will be called immediately.
  51. *
  52. * @param child A child node
  53. */
  54. virtual void addProtectedChild(Node * child);
  55. /**
  56. * Adds a child to the container with a local z-order.
  57. *
  58. * If the child is added to a 'running' node, then 'onEnter' and 'onEnterTransitionDidFinish' will be called immediately.
  59. *
  60. * @param child A child node
  61. * @param localZOrder Z order for drawing priority. Please refer to `setLocalZOrder(int)`
  62. */
  63. virtual void addProtectedChild(Node * child, int localZOrder);
  64. /**
  65. * Adds a child to the container with z order and tag.
  66. *
  67. * If the child is added to a 'running' node, then 'onEnter' and 'onEnterTransitionDidFinish' will be called immediately.
  68. *
  69. * @param child A child node
  70. * @param localZOrder Z order for drawing priority. Please refer to `setLocalZOrder(int)`
  71. * @param tag An integer to identify the node easily. Please refer to `setTag(int)`
  72. */
  73. virtual void addProtectedChild(Node* child, int localZOrder, int tag);
  74. /**
  75. * Gets a child from the container with its tag.
  76. *
  77. * @param tag An identifier to find the child node.
  78. *
  79. * @return a Node object whose tag equals to the input parameter.
  80. */
  81. virtual Node * getProtectedChildByTag(int tag);
  82. ////// REMOVES //////
  83. /**
  84. * Removes a child from the container. It will also cleanup all running actions depending on the cleanup parameter.
  85. *
  86. * @param child The child node which will be removed.
  87. * @param cleanup true if all running actions and callbacks on the child node will be cleanup, false otherwise.
  88. */
  89. virtual void removeProtectedChild(Node* child, bool cleanup = true);
  90. /**
  91. * Removes a child from the container by tag value. It will also cleanup all running actions depending on the cleanup parameter.
  92. *
  93. * @param tag An integer number that identifies a child node.
  94. * @param cleanup true if all running actions and callbacks on the child node will be cleanup, false otherwise.
  95. */
  96. virtual void removeProtectedChildByTag(int tag, bool cleanup = true);
  97. /**
  98. * Removes all children from the container with a cleanup.
  99. *
  100. * @see `removeAllChildrenWithCleanup(bool)`.
  101. */
  102. virtual void removeAllProtectedChildren();
  103. /**
  104. * Removes all children from the container, and do a cleanup to all running actions depending on the cleanup parameter.
  105. *
  106. * @param cleanup true if all running actions on all children nodes should be cleanup, false otherwise.
  107. * @js removeAllChildren
  108. * @lua removeAllChildren
  109. */
  110. virtual void removeAllProtectedChildrenWithCleanup(bool cleanup);
  111. /**
  112. * Reorders a child according to a new z value.
  113. *
  114. * @param child An already added child node. It MUST be already added.
  115. * @param localZOrder Z order for drawing priority. Please refer to setLocalZOrder(int)
  116. */
  117. virtual void reorderProtectedChild(Node * child, int localZOrder);
  118. /**
  119. * Sorts the children array once before drawing, instead of every time when a child is added or reordered.
  120. * This approach can improves the performance massively.
  121. * @note Don't call this manually unless a child added needs to be removed in the same frame
  122. */
  123. virtual void sortAllProtectedChildren();
  124. /// @} end of Children and Parent
  125. /**
  126. * @js NA
  127. */
  128. virtual void visit(Renderer *renderer, const Mat4 &parentTransform, uint32_t parentFlags) override;
  129. virtual void cleanup() override;
  130. virtual void onEnter() override;
  131. /** Event callback that is invoked when the Node enters in the 'stage'.
  132. * If the Node enters the 'stage' with a transition, this event is called when the transition finishes.
  133. * If you override onEnterTransitionDidFinish, you shall call its parent's one, e.g. Node::onEnterTransitionDidFinish()
  134. * @js NA
  135. * @lua NA
  136. */
  137. virtual void onEnterTransitionDidFinish() override;
  138. /**
  139. * Event callback that is invoked every time the Node leaves the 'stage'.
  140. * If the Node leaves the 'stage' with a transition, this event is called when the transition finishes.
  141. * During onExit you can't access a sibling node.
  142. * If you override onExit, you shall call its parent's one, e.g., Node::onExit().
  143. * @js NA
  144. * @lua NA
  145. */
  146. virtual void onExit() override;
  147. /**
  148. * Event callback that is called every time the Node leaves the 'stage'.
  149. * If the Node leaves the 'stage' with a transition, this callback is called when the transition starts.
  150. * @js NA
  151. * @lua NA
  152. */
  153. virtual void onExitTransitionDidStart() override;
  154. virtual void updateDisplayedOpacity(GLubyte parentOpacity) override;
  155. virtual void updateDisplayedColor(const Color3B& parentColor) override;
  156. virtual void disableCascadeColor() override;
  157. virtual void disableCascadeOpacity()override;
  158. virtual void setCameraMask(unsigned short mask, bool applyChildren = true) override;
  159. virtual void setGlobalZOrder(float globalZOrder) override;
  160. CC_CONSTRUCTOR_ACCESS:
  161. ProtectedNode();
  162. virtual ~ProtectedNode();
  163. protected:
  164. /// helper that reorder a child
  165. void insertProtectedChild(Node* child, int z);
  166. Vector<Node*> _protectedChildren; ///< array of children nodes
  167. bool _reorderProtectedChildDirty;
  168. private:
  169. CC_DISALLOW_COPY_AND_ASSIGN(ProtectedNode);
  170. };
  171. // end of 2d group
  172. /// @}
  173. NS_CC_END
  174. #endif // __CPROTECTEDCNODE_H__