CCClippingNode.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. * Copyright (c) 2012 Pierre-David Bélanger
  3. * Copyright (c) 2012 cocos2d-x.org
  4. * Copyright (c) 2013-2017 Chukong Technologies Inc.
  5. *
  6. * http://www.cocos2d-x.org
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. *
  26. */
  27. #ifndef __MISCNODE_CCCLIPPING_NODE_H__
  28. #define __MISCNODE_CCCLIPPING_NODE_H__
  29. #include "2d/CCNode.h"
  30. #include "platform/CCGL.h"
  31. #include "renderer/CCGroupCommand.h"
  32. #include "renderer/CCCustomCommand.h"
  33. NS_CC_BEGIN
  34. class StencilStateManager;
  35. /**
  36. * @addtogroup _2d
  37. * @{
  38. */
  39. /** ClippingNode is a subclass of Node.
  40. * It draws its content (children) clipped using a stencil.
  41. * The stencil is an other Node that will not be drawn.
  42. * The clipping is done using the alpha part of the stencil (adjusted with an alphaThreshold).
  43. */
  44. class CC_DLL ClippingNode : public Node
  45. {
  46. public:
  47. /** Creates and initializes a clipping node without a stencil.
  48. *
  49. * @return An autorelease ClippingNode.
  50. */
  51. static ClippingNode* create();
  52. /** Creates and initializes a clipping node with an other node as its stencil.
  53. * The stencil node will be retained.
  54. * @param stencil The stencil node.
  55. */
  56. static ClippingNode* create(Node *stencil);
  57. /** The Node to use as a stencil to do the clipping.
  58. * The stencil node will be retained.
  59. * This default to nil.
  60. *
  61. * @return The stencil node.
  62. */
  63. Node* getStencil() const;
  64. /** Set the Node to use as a stencil to do the clipping.
  65. *
  66. * @param stencil The Node to use as a stencil to do the clipping.
  67. */
  68. void setStencil(Node *stencil);
  69. /** If stencil has no children it will not be drawn.
  70. * If you have custom stencil-based node with stencil drawing mechanics other then children-based,
  71. * then this method should return true every time you wish stencil to be visited.
  72. * By default returns true if has any children attached.
  73. *
  74. * @return If you have custom stencil-based node with stencil drawing mechanics other then children-based,
  75. * then this method should return true every time you wish stencil to be visited.
  76. * By default returns true if has any children attached.
  77. * @js NA
  78. */
  79. virtual bool hasContent() const;
  80. /** The alpha threshold.
  81. * The content is drawn only where the stencil have pixel with alpha greater than the alphaThreshold.
  82. * Should be a float between 0 and 1.
  83. * This default to 1 (so alpha test is disabled).
  84. *
  85. * @return The alpha threshold value,Should be a float between 0 and 1.
  86. */
  87. GLfloat getAlphaThreshold() const;
  88. /** Set the alpha threshold.
  89. *
  90. * @param alphaThreshold The alpha threshold.
  91. */
  92. void setAlphaThreshold(GLfloat alphaThreshold);
  93. /** Inverted. If this is set to true,
  94. * the stencil is inverted, so the content is drawn where the stencil is NOT drawn.
  95. * This default to false.
  96. *
  97. * @return If the clippingNode is Inverted, it will be return true.
  98. */
  99. bool isInverted() const;
  100. /** Set the ClippingNode whether or not invert.
  101. *
  102. * @param inverted A bool Type,to set the ClippingNode whether or not invert.
  103. */
  104. void setInverted(bool inverted);
  105. // Overrides
  106. /**
  107. * @lua NA
  108. */
  109. virtual void onEnter() override;
  110. /**
  111. * @lua NA
  112. */
  113. virtual void onEnterTransitionDidFinish() override;
  114. /**
  115. * @lua NA
  116. */
  117. virtual void onExitTransitionDidStart() override;
  118. /**
  119. * @lua NA
  120. */
  121. virtual void onExit() override;
  122. virtual void visit(Renderer *renderer, const Mat4 &parentTransform, uint32_t parentFlags) override;
  123. virtual void setCameraMask(unsigned short mask, bool applyChildren = true) override;
  124. CC_CONSTRUCTOR_ACCESS:
  125. ClippingNode();
  126. /**
  127. * @js NA
  128. * @lua NA
  129. */
  130. virtual ~ClippingNode();
  131. /** Initializes a clipping node without a stencil.
  132. */
  133. virtual bool init() override;
  134. /** Initializes a clipping node with an other node as its stencil.
  135. The stencil node will be retained, and its parent will be set to this clipping node.
  136. */
  137. virtual bool init(Node *stencil);
  138. protected:
  139. Node* _stencil;
  140. GLProgram* _originStencilProgram;
  141. StencilStateManager* _stencilStateManager;
  142. GroupCommand _groupCommand;
  143. CustomCommand _beforeVisitCmd;
  144. CustomCommand _afterDrawStencilCmd;
  145. CustomCommand _afterVisitCmd;
  146. private:
  147. CC_DISALLOW_COPY_AND_ASSIGN(ClippingNode);
  148. };
  149. /** @} */
  150. NS_CC_END
  151. #endif // __MISCNODE_CCCLIPPING_NODE_H__