CCBoneNode.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /****************************************************************************
  2. Copyright (c) 2015-2017 Chukong Technologies Inc.
  3. http://www.cocos2d-x.org
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. ****************************************************************************/
  20. #ifndef __CCBONENODE_H__
  21. #define __CCBONENODE_H__
  22. #include "base/CCProtocols.h"
  23. #include "2d/CCNode.h"
  24. #include "renderer/CCCustomCommand.h"
  25. #include "editor-support/cocostudio/ActionTimeline/CCTimelineMacro.h"
  26. #include "editor-support/cocostudio/CocosStudioExport.h"
  27. #include "editor-support/cocostudio/ActionTimeline/CCSkinNode.h"
  28. NS_TIMELINE_BEGIN
  29. class SkeletonNode;
  30. class CC_STUDIO_DLL BoneNode : public cocos2d::Node, public cocos2d::BlendProtocol
  31. {
  32. public:
  33. static BoneNode* create();
  34. static BoneNode* create(int length);
  35. using Node::addChild;
  36. // add child, and add child to bone list and skeleton's sub bone map or add it to skin list
  37. virtual void addChild(cocos2d::Node* child, int localZOrder, const std::string &name) override;
  38. virtual void addChild(cocos2d::Node* child, int localZOrder, int tag) override;
  39. // remove child, and remove child from bone list and skeleton's sub bone map or remove it from skin list
  40. virtual void removeChild(Node* child, bool cleanup) override;
  41. // get child bone list
  42. virtual const cocos2d::Vector<BoneNode*>& getChildBones() const { return _childBones; }
  43. virtual cocos2d::Vector<BoneNode*>& getChildBones() { return _childBones; }
  44. // get this bone's root skeleton, return null while bone is not in a skeleton
  45. virtual SkeletonNode* getRootSkeletonNode() const;
  46. /**
  47. * @brief: get all bones in this bone tree
  48. */
  49. cocos2d::Vector<BoneNode*> getAllSubBones() const;
  50. /**
  51. *@brief: add a skin
  52. *@param: display, whether display this skin
  53. */
  54. virtual void addSkin(SkinNode* skin, bool display);
  55. /**
  56. *@brief: add a skin
  57. *@param: display, whether display this skin
  58. *@param: hideOthers, whether hide other skins added to this bone
  59. */
  60. virtual void addSkin(SkinNode* skin, bool display, bool hideOthers);
  61. /**
  62. * @brief: display skin
  63. * @param: hideOthers, set other skins invisible
  64. */
  65. virtual void displaySkin(SkinNode* skin, bool hideOthers);
  66. /**
  67. * @brief: display all skins named skinName, if hide display only one skin,
  68. * prefer to use display(SkinNode* skin, bool hideOthers = false)
  69. * @param: hideOthers, set other skins invisible
  70. */
  71. virtual void displaySkin(const std::string &skinName, bool hideOthers );
  72. // get the skins which is visible (displaying skins)
  73. virtual cocos2d::Vector<SkinNode*> getVisibleSkins() const;
  74. /**
  75. * get skins in this bone's children
  76. */
  77. virtual const cocos2d::Vector<SkinNode*>& getSkins() const { return _boneSkins; }
  78. virtual cocos2d::Vector<SkinNode*>& getSkins() { return _boneSkins; }
  79. /**
  80. * @brief: get all skins in this bone tree
  81. */
  82. cocos2d::Vector<SkinNode*> getAllSubSkins() const;
  83. // blendFunc
  84. virtual void setBlendFunc(const cocos2d::BlendFunc &blendFunc) override;
  85. virtual const cocos2d::BlendFunc & getBlendFunc() const override { return _blendFunc; }
  86. // debug draw show, bone's debugdraw can be draw when bone is visible
  87. // when bone's added to skeleton, DebugDrawEnabled controlled by skeleton's DebugDrawEnabled
  88. virtual void setDebugDrawEnabled(bool isDebugDraw);
  89. virtual bool isDebugDrawEnabled() const { return _isRackShow; }
  90. // bone's debug draw's length
  91. virtual void setDebugDrawLength(float length);
  92. virtual float getDebugDrawLength() const { return _rackLength; }
  93. // bone's debug draw's width
  94. virtual void setDebugDrawWidth(float width);
  95. virtual float getDebugDrawWidth() const { return _rackWidth; }
  96. // bone's debug draw's width
  97. virtual void setDebugDrawColor(const cocos2d::Color4F &color);
  98. virtual cocos2d::Color4F getDebugDrawColor() const { return _rackColor; }
  99. // get bone's bounding box, depends on getVisibleSkinsRect, apply on node to parent's transform
  100. cocos2d::Rect getBoundingBox() const override;
  101. /**
  102. *get displayings rect in self transform
  103. */
  104. virtual cocos2d::Rect getVisibleSkinsRect() const;
  105. // transform & draw
  106. virtual void draw(cocos2d::Renderer *renderer, const cocos2d::Mat4 &transform, uint32_t flags) override;
  107. // set local zorder, and dirty the debugdraw to make debugdraw's render layer right
  108. virtual void setLocalZOrder(int localZOrder) override;
  109. // set name, and replace the subbone map in skeleton
  110. virtual void setName(const std::string& name) override;
  111. // set visible, and dirty the debugdraw to make debugdraw's render layer right
  112. virtual void setVisible(bool visible) override;
  113. // set contentsize, and recalculate debugdraw
  114. virtual void setContentSize(const cocos2d::Size& contentSize) override;
  115. // set localzorder, and recalculate debugdraw
  116. virtual void setAnchorPoint(const cocos2d::Vec2& anchorPoint) override;
  117. #ifdef CC_STUDIO_ENABLED_VIEW
  118. // hit test , bonePoint is in self coordinate
  119. virtual bool isPointOnRack(const cocos2d::Vec2& bonePoint);
  120. #endif
  121. CC_CONSTRUCTOR_ACCESS:
  122. BoneNode();
  123. virtual ~BoneNode();
  124. virtual bool init() override;
  125. protected:
  126. virtual void addToChildrenListHelper(Node * child);
  127. virtual void removeFromChildrenListHelper(Node * child);
  128. // add bone to children bone list, and add bone to skeleton's subbone map
  129. virtual void addToBoneList(BoneNode* bone);
  130. // remove bone from children bone list, and remove bone from skeleton's subbone map
  131. virtual void removeFromBoneList(BoneNode* bone);
  132. // add skin to skin list
  133. virtual void addToSkinList(SkinNode* skin);
  134. // remove skin from skin list
  135. virtual void removeFromSkinList(SkinNode* skin);
  136. // sort all _children , bone list and skin list
  137. virtual void sortAllChildren() override;
  138. virtual void updateVertices();
  139. virtual void updateColor() override;
  140. // bone's color and opacity cannot cascade to bone
  141. virtual void updateDisplayedColor(const cocos2d::Color3B& parentColor) override;
  142. virtual void updateDisplayedOpacity(GLubyte parentOpacity) override;
  143. virtual void disableCascadeOpacity() override;
  144. virtual void disableCascadeColor() override;
  145. virtual void onDraw(const cocos2d::Mat4 &transform, uint32_t flags);
  146. // override Node::visit, just visit bones in children
  147. virtual void visit(cocos2d::Renderer *renderer, const cocos2d::Mat4& parentTransform, uint32_t parentFlags) override;
  148. // a help function for SkeletonNode
  149. // for batch bone's draw to _rootSkeleton
  150. virtual void batchBoneDrawToSkeleton(BoneNode* bone) const;
  151. // a help function for SkeletonNode
  152. // @param bone, visit bone's skins
  153. virtual void visitSkins(cocos2d::Renderer* renderer, BoneNode* bone) const;
  154. // a help function for SkeletonNode
  155. // set bone's rootSkeleton = skeleton
  156. void setRootSkeleton(BoneNode* bone, SkeletonNode* skeleton) const;
  157. protected:
  158. cocos2d::CustomCommand _customCommand;
  159. cocos2d::BlendFunc _blendFunc;
  160. bool _isRackShow;
  161. cocos2d::Color4F _rackColor;
  162. int _rackLength;
  163. int _rackWidth;
  164. cocos2d::Vector<BoneNode*> _childBones;
  165. cocos2d::Vector<SkinNode*> _boneSkins;
  166. SkeletonNode* _rootSkeleton;
  167. private:
  168. cocos2d::Vec2 _squareVertices[4];
  169. cocos2d::Color4F _squareColors[4];
  170. cocos2d::Vec3 _noMVPVertices[4];
  171. CC_DISALLOW_COPY_AND_ASSIGN(BoneNode);
  172. };
  173. NS_TIMELINE_END
  174. #endif //__CCBONENODE_H__