CCClippingNode.h 5.5 KB

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