SkeletonRenderer.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /******************************************************************************
  2. * Spine Runtimes License Agreement
  3. * Last updated January 1, 2020. Replaces all prior versions.
  4. *
  5. * Copyright (c) 2013-2020, Esoteric Software LLC
  6. *
  7. * Integration of the Spine Runtimes into software or otherwise creating
  8. * derivative works of the Spine Runtimes is permitted under the terms and
  9. * conditions of Section 2 of the Spine Editor License Agreement:
  10. * http://esotericsoftware.com/spine-editor-license
  11. *
  12. * Otherwise, it is permitted to integrate the Spine Runtimes into software
  13. * or otherwise create derivative works of the Spine Runtimes (collectively,
  14. * "Products"), provided that each user of the Products must obtain their own
  15. * Spine Editor license and redistribution of the Products in any form must
  16. * include this license and copyright notice.
  17. *
  18. * THE SPINE RUNTIMES ARE PROVIDED BY ESOTERIC SOFTWARE LLC "AS IS" AND ANY
  19. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL ESOTERIC SOFTWARE LLC BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES,
  24. * BUSINESS INTERRUPTION, OR LOSS OF USE, DATA, OR PROFITS) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  27. * THE SPINE RUNTIMES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *****************************************************************************/
  29. #ifndef SPINE_SKELETONRENDERER_H_
  30. #define SPINE_SKELETONRENDERER_H_
  31. #include "cocos2d.h"
  32. #include <spine/spine.h>
  33. namespace spine {
  34. class AttachmentVertices;
  35. /* Draws a skeleton. */
  36. class SkeletonRenderer: public cocos2d::Node, public cocos2d::BlendProtocol {
  37. public:
  38. CREATE_FUNC(SkeletonRenderer);
  39. static SkeletonRenderer* createWithSkeleton(Skeleton* skeleton, bool ownsSkeleton = false, bool ownsSkeletonData = false);
  40. static SkeletonRenderer* createWithData (SkeletonData* skeletonData, bool ownsSkeletonData = false);
  41. static SkeletonRenderer* createWithFile (const std::string& skeletonDataFile, Atlas* atlas, float scale = 1);
  42. static SkeletonRenderer* createWithFile (const std::string& skeletonDataFile, const std::string& atlasFile, float scale = 1);
  43. void update (float deltaTime) override;
  44. void draw (cocos2d::Renderer* renderer, const cocos2d::Mat4& transform, uint32_t transformFlags) override;
  45. cocos2d::Rect getBoundingBox () const override;
  46. void onEnter () override;
  47. void onExit () override;
  48. Skeleton* getSkeleton() const;
  49. void setTimeScale(float scale);
  50. float getTimeScale() const;
  51. /* */
  52. void setDebugSlotsEnabled(bool enabled);
  53. bool getDebugSlotsEnabled() const;
  54. void setDebugBonesEnabled(bool enabled);
  55. bool getDebugBonesEnabled() const;
  56. void setDebugMeshesEnabled(bool enabled);
  57. bool getDebugMeshesEnabled() const;
  58. void setDebugBoundingRectEnabled(bool enabled);
  59. bool getDebugBoundingRectEnabled() const;
  60. // --- Convenience methods for common Skeleton_* functions.
  61. void updateWorldTransform ();
  62. void setToSetupPose ();
  63. void setBonesToSetupPose ();
  64. void setSlotsToSetupPose ();
  65. /* Returns 0 if the bone was not found. */
  66. Bone* findBone (const std::string& boneName) const;
  67. /* Returns 0 if the slot was not found. */
  68. Slot* findSlot (const std::string& slotName) const;
  69. /* Sets the skin used to look up attachments not found in the SkeletonData defaultSkin. Attachments from the new skin are
  70. * attached if the corresponding attachment from the old skin was attached.
  71. * @param skin May be empty string ("") for no skin.*/
  72. void setSkin (const std::string& skinName);
  73. /** @param skin May be 0 for no skin.*/
  74. void setSkin (const char* skinName);
  75. /* Returns 0 if the slot or attachment was not found. */
  76. Attachment* getAttachment (const std::string& slotName, const std::string& attachmentName) const;
  77. /* Returns false if the slot or attachment was not found.
  78. * @param attachmentName May be empty string ("") for no attachment. */
  79. bool setAttachment (const std::string& slotName, const std::string& attachmentName);
  80. /* @param attachmentName May be 0 for no attachment. */
  81. bool setAttachment (const std::string& slotName, const char* attachmentName);
  82. /* Enables/disables two color tinting for this instance. May break batching */
  83. void setTwoColorTint(bool enabled);
  84. /* Whether two color tinting is enabled */
  85. bool isTwoColorTint();
  86. /* Sets the vertex effect to be used, set to 0 to disable vertex effects */
  87. void setVertexEffect(VertexEffect* effect);
  88. /* Sets the range of slots that should be rendered. Use -1, -1 to clear the range */
  89. void setSlotsRange(int startSlotIndex, int endSlotIndex);
  90. // --- BlendProtocol
  91. void setBlendFunc (const cocos2d::BlendFunc& blendFunc)override;
  92. const cocos2d::BlendFunc& getBlendFunc () const override;
  93. void setOpacityModifyRGB (bool value) override;
  94. bool isOpacityModifyRGB () const override;
  95. CC_CONSTRUCTOR_ACCESS:
  96. SkeletonRenderer ();
  97. SkeletonRenderer(Skeleton* skeleton, bool ownsSkeleton = false, bool ownsSkeletonData = false, bool ownsAtlas = false);
  98. SkeletonRenderer (SkeletonData* skeletonData, bool ownsSkeletonData = false);
  99. SkeletonRenderer (const std::string& skeletonDataFile, Atlas* atlas, float scale = 1);
  100. SkeletonRenderer (const std::string& skeletonDataFile, const std::string& atlasFile, float scale = 1);
  101. virtual ~SkeletonRenderer ();
  102. void initWithSkeleton(Skeleton* skeleton, bool ownsSkeleton = false, bool ownsSkeletonData = false, bool ownsAtlas = false);
  103. void initWithData (SkeletonData* skeletonData, bool ownsSkeletonData = false);
  104. void initWithJsonFile (const std::string& skeletonDataFile, Atlas* atlas, float scale = 1);
  105. void initWithJsonFile (const std::string& skeletonDataFile, const std::string& atlasFile, float scale = 1);
  106. void initWithBinaryFile (const std::string& skeletonDataFile, Atlas* atlas, float scale = 1);
  107. void initWithBinaryFile (const std::string& skeletonDataFile, const std::string& atlasFile, float scale = 1);
  108. virtual void initialize ();
  109. cocos2d::Node* getNodeForSlot(std::string slotName);
  110. protected:
  111. void setSkeletonData (SkeletonData* skeletonData, bool ownsSkeletonData);
  112. void setupGLProgramState(bool twoColorTintEnabled);
  113. virtual void drawDebug (cocos2d::Renderer* renderer, const cocos2d::Mat4& transform, uint32_t transformFlags);
  114. bool _ownsSkeletonData;
  115. bool _ownsSkeleton;
  116. bool _ownsAtlas = false;
  117. Atlas* _atlas = nullptr;
  118. AttachmentLoader* _attachmentLoader;
  119. cocos2d::CustomCommand _debugCommand;
  120. cocos2d::BlendFunc _blendFunc;
  121. bool _premultipliedAlpha;
  122. Skeleton* _skeleton = nullptr;
  123. float _timeScale;
  124. bool _debugSlots;
  125. bool _debugBones;
  126. bool _debugMeshes;
  127. bool _debugBoundingRect;
  128. SkeletonClipping* _clipper = nullptr;
  129. VertexEffect* _effect = nullptr;
  130. cocos2d::Rect _boundingRect;
  131. int _startSlotIndex;
  132. int _endSlotIndex;
  133. bool _twoColorTint;
  134. struct sSlotNode
  135. {
  136. Slot* slot;
  137. cocos2d::Node* node;
  138. };
  139. typedef std::map<std::string,sSlotNode> SlotNodeMap;
  140. typedef SlotNodeMap::iterator SlotNodeIter;
  141. SlotNodeMap m_slotNodes;
  142. };
  143. }
  144. #endif /* SPINE_SKELETONRENDERER_H_ */