CCBone.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /****************************************************************************
  2. Copyright (c) 2013-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 __CCBONE_H__
  21. #define __CCBONE_H__
  22. #include "editor-support/cocostudio/CCArmatureDefine.h"
  23. #include "editor-support/cocostudio/CCDatas.h"
  24. #include "editor-support/cocostudio/CCTween.h"
  25. #include "editor-support/cocostudio/CCDecorativeDisplay.h"
  26. #include "editor-support/cocostudio/CCDisplayManager.h"
  27. #include "editor-support/cocostudio/CocosStudioExport.h"
  28. #include "2d/CCNode.h"
  29. #include "math/CCMath.h"
  30. namespace cocostudio {
  31. class Armature;
  32. class CC_STUDIO_DLL Bone : public cocos2d::Node
  33. {
  34. public:
  35. /**
  36. * Allocates and initializes a bone.
  37. * @return A initialized bone which is marked as "autorelease".
  38. */
  39. static Bone *create();
  40. /**
  41. * Allocates and initializes a bone.
  42. *
  43. * @param name If name is not null, then set name to the bone's name
  44. * @return A initialized bone which is marked as "autorelease".
  45. */
  46. static Bone *create(const std::string& name);
  47. public:
  48. /**
  49. * @js ctor
  50. */
  51. Bone();
  52. /**
  53. * @js NA
  54. * @lua NA
  55. */
  56. virtual ~Bone(void);
  57. /**
  58. * Initializes an empty Bone with nothing init.
  59. */
  60. virtual bool init() override;
  61. /**
  62. * Initializes a Bone with the specified name
  63. * @param name Bone's name.
  64. */
  65. virtual bool init(const std::string& name);
  66. /**
  67. * Add display and use displayData to init the display.
  68. * If index already have a display, then replace it.
  69. * If index is current display index, then also change display to _index
  70. *
  71. * @param displayData it include the display information, like DisplayType.
  72. * If you want to create a sprite display, then create a SpriteDisplayData param
  73. *
  74. * @param index the index of the display you want to replace or add to
  75. * -1 : append display from back
  76. */
  77. void addDisplay(DisplayData *displayData, int index);
  78. void addDisplay(cocos2d::Node *display, int index);
  79. void removeDisplay(int index);
  80. CC_DEPRECATED_ATTRIBUTE void changeDisplayByIndex(int index, bool force);
  81. CC_DEPRECATED_ATTRIBUTE void changeDisplayByName(const std::string& name, bool force);
  82. void changeDisplayWithIndex(int index, bool force);
  83. void changeDisplayWithName(const std::string& name, bool force);
  84. /**
  85. * Add a child to this bone, and it will let this child call setParent(Bone *parent) function to set self to it's parent
  86. * @param child the child you want to add
  87. */
  88. void addChildBone(Bone *child);
  89. /**
  90. * Set parent bone.
  91. * If parent is null, then also remove this bone from armature.
  92. * It will not set the Armature, if you want to add the bone to a Armature, you should use Armature::addBone(Bone *bone, const char* parentName).
  93. *
  94. * @param parent the parent bone.
  95. * nullptr : remove this bone from armature
  96. */
  97. void setParentBone(Bone *parent);
  98. /**
  99. * Get parent bone
  100. * @return parent bone
  101. */
  102. Bone *getParentBone();
  103. using Node::removeFromParent;
  104. /**
  105. * Remove itself from its parent.
  106. * @param recursion whether or not to remove childBone's display
  107. */
  108. void removeFromParent(bool recursion);
  109. /**
  110. * Removes a child Bone
  111. * @param bone the bone you want to remove
  112. */
  113. void removeChildBone(Bone *bone, bool recursion);
  114. void update(float delta) override;
  115. void updateDisplayedColor(const cocos2d::Color3B &parentColor) override;
  116. void updateDisplayedOpacity(GLubyte parentOpacity) override;
  117. //! Update color to render display
  118. virtual void updateColor() override;
  119. //! Update zorder
  120. void updateZOrder();
  121. virtual void setLocalZOrder(int zOrder) override;
  122. Tween *getTween();
  123. /*
  124. * Whether or not the bone's transform property changed. if true, the bone will update the transform.
  125. */
  126. virtual void setTransformDirty(bool dirty) { _boneTransformDirty = dirty; }
  127. virtual bool isTransformDirty() { return _boneTransformDirty; }
  128. virtual cocos2d::Mat4 getNodeToArmatureTransform() const;
  129. virtual cocos2d::Mat4 getNodeToWorldTransform() const override;
  130. cocos2d::Node *getDisplayRenderNode();
  131. DisplayType getDisplayRenderNodeType();
  132. /*
  133. * Get the ColliderBody list in this bone. The object in the Array is ColliderBody.
  134. */
  135. virtual ColliderDetector* getColliderDetector() const;
  136. #if ENABLE_PHYSICS_BOX2D_DETECT || ENABLE_PHYSICS_CHIPMUNK_DETECT
  137. virtual void setColliderFilter(ColliderFilter *filter);
  138. virtual ColliderFilter *getColliderFilter();
  139. #endif
  140. virtual void setBoneData(BoneData *boneData);
  141. virtual BoneData *getBoneData() const;
  142. virtual void setArmature(Armature *armature);
  143. virtual Armature *getArmature() const;
  144. virtual void setChildArmature(Armature *childArmature);
  145. virtual Armature *getChildArmature() const;
  146. virtual DisplayManager *getDisplayManager() const { return _displayManager; }
  147. /**
  148. * @lua NA
  149. */
  150. virtual void setIgnoreMovementBoneData(bool ignore) { _ignoreMovementBoneData = ignore; }
  151. virtual bool isIgnoreMovementBoneData() const { return _ignoreMovementBoneData; }
  152. /*
  153. * This function is deprecated, please use isIgnoreMovementBoneData()
  154. * @lua NA
  155. */
  156. CC_DEPRECATED_ATTRIBUTE virtual bool getIgnoreMovementBoneData() const { return isIgnoreMovementBoneData(); }
  157. /*
  158. * Set blend function
  159. */
  160. virtual void setBlendFunc(const cocos2d::BlendFunc& blendFunc);
  161. virtual cocos2d::BlendFunc getBlendFunc(void) { return _blendFunc; }
  162. /*
  163. * Set if blend function is dirty
  164. */
  165. virtual void setBlendDirty(bool dirty) { _blendDirty = dirty; }
  166. virtual bool isBlendDirty(void) { return _blendDirty; }
  167. virtual FrameData *getTweenData() const { return _tweenData; }
  168. virtual BaseData *getWorldInfo() const { return _worldInfo; }
  169. protected:
  170. void applyParentTransform(Bone *parent);
  171. /*
  172. * The origin state of the Bone. Display's state is effected by _boneData, m_pNode, _tweenData
  173. * when call setData function, it will copy from the BoneData.
  174. */
  175. BoneData *_boneData;
  176. //! A weak reference to the Armature
  177. Armature *_armature;
  178. //! A weak reference to the child Armature
  179. Armature *_childArmature;
  180. DisplayManager *_displayManager;
  181. /*
  182. * When Armature play an animation, if there is not a MovementBoneData of this bone in this MovementData, this bone will be hidden.
  183. * Set IgnoreMovementBoneData to true, then this bone will also be shown.
  184. */
  185. bool _ignoreMovementBoneData;
  186. cocos2d::BlendFunc _blendFunc;
  187. bool _blendDirty;
  188. Tween *_tween; //! Calculate tween effect
  189. //! Used for making tween effect in every frame
  190. FrameData *_tweenData;
  191. Bone *_parentBone; //! A weak reference to its parent
  192. bool _boneTransformDirty; //! Whether or not transform dirty
  193. //! self Transform, use this to change display's state
  194. cocos2d::Mat4 _worldTransform;
  195. BaseData *_worldInfo;
  196. //! Armature's parent bone
  197. Bone *_armatureParentBone;
  198. //! Data version
  199. float _dataVersion;
  200. };
  201. }
  202. #endif /*__CCBONE_H__*/