PolySkeleton.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*
  2. Copyright (C) 2011 by Ivan Safrin
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. */
  19. #pragma once
  20. #include "PolyString.h"
  21. #include "PolyGlobals.h"
  22. #include "PolyColor.h"
  23. #include "PolyVector3.h"
  24. #include "PolyQuaternion.h"
  25. #include "PolyQuaternionCurve.h"
  26. #include "PolyEntity.h"
  27. #include <vector>
  28. namespace Polycode {
  29. class BezierCurve;
  30. class Bone;
  31. class QuaternionTween;
  32. class BezierPathTween;
  33. class _PolyExport BoneTrack : public PolyBase {
  34. public:
  35. BoneTrack(Bone *bone, Number length);
  36. ~BoneTrack();
  37. void Play(bool once=false);
  38. void Stop();
  39. void Update(Number elapsed);
  40. void Reset();
  41. void setSpeed(Number speed);
  42. BezierCurve *scaleX;
  43. BezierCurve *scaleY;
  44. BezierCurve *scaleZ;
  45. BezierCurve *QuatW;
  46. BezierCurve *QuatX;
  47. BezierCurve *QuatY;
  48. BezierCurve *QuatZ;
  49. BezierCurve *LocX;
  50. BezierCurve *LocY;
  51. BezierCurve *LocZ;
  52. Vector3 position;
  53. Vector3 scale;
  54. Quaternion boneQuat;
  55. QuaternionCurve *quatCurve;
  56. Number weight;
  57. protected:
  58. Number length;
  59. Number speed;
  60. bool paused;
  61. Number time;
  62. Bone *targetBone;
  63. bool playOnce;
  64. };
  65. /**
  66. * Skeleton animation.
  67. */
  68. class _PolyExport SkeletonAnimation : public PolyBase {
  69. public:
  70. SkeletonAnimation(const String& name, Number duration);
  71. ~SkeletonAnimation();
  72. /**
  73. * Adds a new bone track
  74. * @param boneTrack New bone track to add.
  75. */
  76. void addBoneTrack(BoneTrack *boneTrack);
  77. /**
  78. * Returns the animation name.
  79. */
  80. const String& getName() const;
  81. /**
  82. * Plays the animation.
  83. */
  84. void Play(bool once);
  85. /**
  86. * Stops the animation.
  87. */
  88. void Stop();
  89. void Reset();
  90. void Update();
  91. /**
  92. * Sets the animation multiplier speed.
  93. * @param speed Number to multiply the animation speed by.
  94. */
  95. void setSpeed(Number speed);
  96. void setWeight(Number newWeight);
  97. Number getWeight() const;
  98. bool isPlaying() const;
  99. protected:
  100. Number weight;
  101. bool playing;
  102. String name;
  103. Number duration;
  104. std::vector<BoneTrack*> boneTracks;
  105. };
  106. /**
  107. * 3D skeleton. Skeletons are applied to scene meshes and can be animated with loaded animations.
  108. */
  109. class _PolyExport Skeleton : public Entity {
  110. public:
  111. /**
  112. * Construct skeleton from a skeleton file.
  113. * @param fileName Skeleton file to load.
  114. */
  115. Skeleton(const String& fileName);
  116. /**
  117. * Construct a blank skeleton.
  118. */
  119. Skeleton();
  120. /**
  121. * Construct a blank skeleton.
  122. */
  123. static Skeleton *BlankSkeleton();
  124. /**
  125. * Loads a new skeleton from file.
  126. * @param fileName Skeleton file to load.
  127. */
  128. void loadSkeleton(const String& fileName);
  129. virtual ~Skeleton();
  130. /**
  131. * Play back a loaded animation.
  132. * @param animName Name of animation to play.
  133. * @param once If true, will only play the animation once.
  134. */
  135. void playAnimationByName(const String& animName, Number weight = 1.0, bool once = false, bool restartIfPlaying = false);
  136. void playAnimation(SkeletonAnimation *animation, Number weight = 1.0, bool once = false, bool restartIfPlaying = false);
  137. void setBaseAnimationByName(const String &animName);
  138. void setBaseAnimation(SkeletonAnimation *animation);
  139. void stopAllAnimations();
  140. SkeletonAnimation *getBaseAnimation();
  141. /**
  142. * Loads in a new animation from a file and adds it to the skeleton.
  143. * @param name Name of the new animation.
  144. * @param fileName File to load animation from.
  145. */
  146. void addAnimation(const String& name, const String& fileName);
  147. /**
  148. * Returns a SkeletonAnimation by its name.
  149. * @param Name of animation to return.
  150. */
  151. SkeletonAnimation *getAnimation(const String& name) const;
  152. void stopAnimationByName(const String &name);
  153. void stopAnimation(SkeletonAnimation *animation);
  154. void Update();
  155. /**
  156. * Get bone instance by its name
  157. * @param name Name of the bone.
  158. */
  159. Bone *getBoneByName(const String& name) const;
  160. /**
  161. * Toggles bone visibility on and off.
  162. * @param val If true, bones will be rendered, if false, they will not.
  163. */
  164. void bonesVisible(bool val);
  165. /**
  166. * Returns the number of bones in the skeleton
  167. */
  168. int getNumBones() const;
  169. /**
  170. * Returns a bone at the specified index.
  171. * @param index Bone index.
  172. */
  173. Bone *getBone(int index) const;
  174. protected:
  175. Entity *bonesEntity;
  176. SkeletonAnimation *baseAnimation;
  177. std::vector<SkeletonAnimation*> playingAnimations;
  178. std::vector<Bone*> bones;
  179. std::vector<SkeletonAnimation*> animations;
  180. };
  181. }