animationComponent.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  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
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef ANIMATION_COMPONENT_H
  23. #define ANIMATION_COMPONENT_H
  24. #ifndef COMPONENT_H
  25. #include "T3D/components/component.h"
  26. #endif
  27. #ifndef _TSSHAPE_H_
  28. #include "ts/tsShapeInstance.h"
  29. #endif
  30. #ifndef ENTITY_H
  31. #include "T3D/entity.h"
  32. #endif
  33. #ifndef RENDER_COMPONENT_INTERFACE_H
  34. #include "T3D/components/render/renderComponentInterface.h"
  35. #endif
  36. class SceneRenderState;
  37. class AnimationComponent : public Component
  38. {
  39. typedef Component Parent;
  40. public:
  41. enum PublicConstants {
  42. ThreadSequenceBits = 6,
  43. MaxSequenceIndex = (1 << ThreadSequenceBits) - 1,
  44. MaxScriptThreads = 16, ///< Should be a power of 2
  45. };
  46. enum MaskBits {
  47. ThreadMaskN = Parent::NextFreeMask << 0,
  48. ThreadMask = (ThreadMaskN << MaxScriptThreads) - ThreadMaskN,
  49. NextFreeMask = ThreadMaskN << MaxScriptThreads
  50. };
  51. protected:
  52. struct Thread
  53. {
  54. /// State of the animation thread.
  55. enum State
  56. {
  57. Play, Stop, Pause, Destroy
  58. };
  59. TSThread* thread; ///< Pointer to 3space data.
  60. U32 state; ///< State of the thread
  61. ///
  62. /// @see Thread::State
  63. S32 sequence; ///< The animation sequence which is running in this thread.
  64. F32 timescale; ///< Timescale
  65. U32 sound; ///< Handle to sound.
  66. bool atEnd; ///< Are we at the end of this thread?
  67. F32 position;
  68. bool transition;
  69. };
  70. Thread mAnimationThreads[MaxScriptThreads];
  71. protected:
  72. RenderComponentInterface * mOwnerRenderInst;
  73. TSShapeInstance *mOwnerShapeInstance;
  74. public:
  75. AnimationComponent();
  76. virtual ~AnimationComponent();
  77. DECLARE_CONOBJECT(AnimationComponent);
  78. virtual bool onAdd();
  79. virtual void onRemove();
  80. static void initPersistFields();
  81. virtual void onComponentAdd();
  82. virtual void componentAddedToOwner(Component *comp);
  83. virtual void componentRemovedFromOwner(Component *comp);
  84. virtual U32 packUpdate(NetConnection *con, U32 mask, BitStream *stream);
  85. virtual void unpackUpdate(NetConnection *con, BitStream *stream);
  86. TSShape* getShape();
  87. void targetShapeChanged(RenderComponentInterface* instanceInterface);
  88. virtual void processTick();
  89. virtual void advanceTime(F32 dt);
  90. const char *getThreadSequenceName(U32 slot);
  91. bool setThreadSequence(U32 slot, S32 seq, bool reset = true, bool transition = true, F32 transitionTime = 0.5);
  92. void updateThread(Thread& st);
  93. bool stopThread(U32 slot);
  94. bool destroyThread(U32 slot);
  95. bool pauseThread(U32 slot);
  96. bool playThread(U32 slot);
  97. bool playThread(U32 slot, const char* name, bool transition, F32 transitionTime);
  98. bool setThreadAnimation(U32 slot, const char* name);
  99. bool setThreadPosition(U32 slot, F32 pos);
  100. bool setThreadDir(U32 slot, bool forward);
  101. bool setThreadTimeScale(U32 slot, F32 timeScale);
  102. void stopThreadSound(Thread& thread);
  103. void startSequenceSound(Thread& thread);
  104. void advanceThreads(F32 dt);
  105. S32 getThreadSequenceID(S32 slot);
  106. //other helper functions
  107. S32 getAnimationCount();
  108. S32 getAnimationIndex(const char* name);
  109. const char* getAnimationName(S32 index);
  110. //callbacks
  111. DECLARE_CALLBACK(void, onAnimationStart, (Component* obj, const String& animName));
  112. DECLARE_CALLBACK(void, onAnimationEnd, (Component* obj, const char* animName));
  113. DECLARE_CALLBACK(void, onAnimationTrigger, (Component* obj, const String& animName, S32 triggerID));
  114. };
  115. #endif //_ANIMATION_COMPONENT_H