SkeletonRenderer.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /******************************************************************************
  2. * Spine Runtimes Software License v2.5
  3. *
  4. * Copyright (c) 2013-2016, Esoteric Software
  5. * All rights reserved.
  6. *
  7. * You are granted a perpetual, non-exclusive, non-sublicensable, and
  8. * non-transferable license to use, install, execute, and perform the Spine
  9. * Runtimes software and derivative works solely for personal or internal
  10. * use. Without the written permission of Esoteric Software (see Section 2 of
  11. * the Spine Software License Agreement), you may not (a) modify, translate,
  12. * adapt, or develop new applications using the Spine Runtimes or otherwise
  13. * create derivative works or improvements of the Spine Runtimes or (b) remove,
  14. * delete, alter, or obscure any trademarks or any copyright, trademark, patent,
  15. * or other intellectual property or proprietary rights notices on or in the
  16. * Software, including any copy thereof. Redistributions in binary or source
  17. * form must include this license and terms.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY ESOTERIC SOFTWARE "AS IS" AND ANY EXPRESS OR
  20. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  21. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  22. * EVENT SHALL ESOTERIC SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  24. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, BUSINESS INTERRUPTION, OR LOSS OF
  25. * USE, DATA, OR PROFITS) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  26. * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  27. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  28. * POSSIBILITY OF SUCH DAMAGE.
  29. *****************************************************************************/
  30. #ifndef SPINE_SKELETONRENDERER_H_
  31. #define SPINE_SKELETONRENDERER_H_
  32. #include <spine/spine.h>
  33. #include "cocos2d.h"
  34. namespace spine {
  35. class AttachmentVertices;
  36. /* Draws a skeleton. */
  37. class SkeletonRenderer: public cocos2d::Node, public cocos2d::BlendProtocol {
  38. public:
  39. CREATE_FUNC(SkeletonRenderer);
  40. static SkeletonRenderer* createWithData (spSkeletonData* skeletonData, bool ownsSkeletonData = false);
  41. static SkeletonRenderer* createWithFile (const std::string& skeletonDataFile, spAtlas* atlas, float scale = 1);
  42. static SkeletonRenderer* createWithFile (const std::string& skeletonDataFile, const std::string& atlasFile, float scale = 1);
  43. virtual void update (float deltaTime) override;
  44. virtual void draw (cocos2d::Renderer* renderer, const cocos2d::Mat4& transform, uint32_t transformFlags) override;
  45. virtual void drawDebug (cocos2d::Renderer* renderer, const cocos2d::Mat4& transform, uint32_t transformFlags);
  46. virtual cocos2d::Rect getBoundingBox () const override;
  47. virtual void onEnter () override;
  48. virtual void onExit () override;
  49. spSkeleton* getSkeleton();
  50. void setTimeScale(float scale);
  51. float getTimeScale() const;
  52. /* */
  53. void setDebugSlotsEnabled(bool enabled);
  54. bool getDebugSlotsEnabled() const;
  55. void setDebugBonesEnabled(bool enabled);
  56. bool getDebugBonesEnabled() const;
  57. // --- Convenience methods for common Skeleton_* functions.
  58. void updateWorldTransform ();
  59. void setToSetupPose ();
  60. void setBonesToSetupPose ();
  61. void setSlotsToSetupPose ();
  62. /* Returns 0 if the bone was not found. */
  63. spBone* findBone (const std::string& boneName) const;
  64. /* Returns 0 if the slot was not found. */
  65. spSlot* findSlot (const std::string& slotName) const;
  66. /* Sets the skin used to look up attachments not found in the SkeletonData defaultSkin. Attachments from the new skin are
  67. * attached if the corresponding attachment from the old skin was attached. Returns false if the skin was not found.
  68. * @param skin May be empty string ("") for no skin.*/
  69. bool setSkin (const std::string& skinName);
  70. /** @param skin May be 0 for no skin.*/
  71. bool setSkin (const char* skinName);
  72. /* Returns 0 if the slot or attachment was not found. */
  73. spAttachment* getAttachment (const std::string& slotName, const std::string& attachmentName) const;
  74. /* Returns false if the slot or attachment was not found.
  75. * @param attachmentName May be empty string ("") for no attachment. */
  76. bool setAttachment (const std::string& slotName, const std::string& attachmentName);
  77. /* @param attachmentName May be 0 for no attachment. */
  78. bool setAttachment (const std::string& slotName, const char* attachmentName);
  79. // --- BlendProtocol
  80. virtual void setBlendFunc (const cocos2d::BlendFunc& blendFunc)override;
  81. virtual const cocos2d::BlendFunc& getBlendFunc () const override;
  82. virtual void setOpacityModifyRGB (bool value) override;
  83. virtual bool isOpacityModifyRGB () const override;
  84. CC_CONSTRUCTOR_ACCESS:
  85. SkeletonRenderer ();
  86. SkeletonRenderer (spSkeletonData* skeletonData, bool ownsSkeletonData = false);
  87. SkeletonRenderer (const std::string& skeletonDataFile, spAtlas* atlas, float scale = 1);
  88. SkeletonRenderer (const std::string& skeletonDataFile, const std::string& atlasFile, float scale = 1);
  89. virtual ~SkeletonRenderer ();
  90. void initWithData (spSkeletonData* skeletonData, bool ownsSkeletonData = false);
  91. void initWithJsonFile (const std::string& skeletonDataFile, spAtlas* atlas, float scale = 1);
  92. void initWithJsonFile (const std::string& skeletonDataFile, const std::string& atlasFile, float scale = 1);
  93. void initWithBinaryFile (const std::string& skeletonDataFile, spAtlas* atlas, float scale = 1);
  94. void initWithBinaryFile (const std::string& skeletonDataFile, const std::string& atlasFile, float scale = 1);
  95. virtual void initialize ();
  96. protected:
  97. void setSkeletonData (spSkeletonData* skeletonData, bool ownsSkeletonData);
  98. virtual AttachmentVertices* getAttachmentVertices (spRegionAttachment* attachment) const;
  99. virtual AttachmentVertices* getAttachmentVertices (spMeshAttachment* attachment) const;
  100. bool _ownsSkeletonData;
  101. spAtlas* _atlas;
  102. spAttachmentLoader* _attachmentLoader;
  103. cocos2d::CustomCommand _debugCommand;
  104. cocos2d::BlendFunc _blendFunc;
  105. float* _worldVertices;
  106. bool _premultipliedAlpha;
  107. spSkeleton* _skeleton;
  108. float _timeScale;
  109. bool _debugSlots;
  110. bool _debugBones;
  111. };
  112. }
  113. #endif /* SPINE_SKELETONRENDERER_H_ */