llmotioncontroller.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /**
  2. * @file llmotioncontroller.h
  3. * @brief Implementation of LLMotionController class.
  4. *
  5. * $LicenseInfo:firstyear=2001&license=viewergpl$
  6. *
  7. * Copyright (c) 2001-2009, Linden Research, Inc.
  8. *
  9. * Second Life Viewer Source Code
  10. * The source code in this file ("Source Code") is provided by Linden Lab
  11. * to you under the terms of the GNU General Public License, version 2.0
  12. * ("GPL"), unless you have obtained a separate licensing agreement
  13. * ("Other License"), formally executed by you and Linden Lab. Terms of
  14. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  15. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  16. *
  17. * There are special exceptions to the terms and conditions of the GPL as
  18. * it is applied to this Source Code. View the full text of the exception
  19. * in the file doc/FLOSS-exception.txt in this software distribution, or
  20. * online at
  21. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  22. *
  23. * By copying, modifying or distributing this software, you acknowledge
  24. * that you have read and understood your obligations described above,
  25. * and agree to abide by those obligations.
  26. *
  27. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  28. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  29. * COMPLETENESS OR PERFORMANCE.
  30. * $/LicenseInfo$
  31. */
  32. #ifndef LL_LLMOTIONCONTROLLER_H
  33. #define LL_LLMOTIONCONTROLLER_H
  34. #include "hbfastmap.h"
  35. #include "llmotion.h"
  36. #include "llpose.h"
  37. #include "llframetimer.h"
  38. #include "llstring.h"
  39. #include "lluuid.h"
  40. class LLCharacter;
  41. typedef LLMotion*(*LLMotionConstructor)(const LLUUID& id);
  42. class LLMotionRegistry final
  43. {
  44. public:
  45. ~LLMotionRegistry();
  46. // Adds motion classes to the registry, returns true on success
  47. bool registerMotion(const LLUUID& id, LLMotionConstructor create);
  48. // Creates a new instance of a named motion, returns NULL motion is not
  49. // registered
  50. LLMotion* createMotion(const LLUUID& id);
  51. // Initialization of motion failed, do not try to create this motion again
  52. void markBad(const LLUUID& id);
  53. protected:
  54. typedef fast_hmap<LLUUID, LLMotionConstructor> motion_map_t;
  55. motion_map_t mMotionTable;
  56. };
  57. class LLMotionController
  58. {
  59. protected:
  60. LOG_CLASS(LLMotionController);
  61. public:
  62. typedef std::list<LLMotion*> motion_list_t;
  63. typedef fast_hset<LLMotion*> motion_set_t;
  64. public:
  65. LLMotionController();
  66. virtual ~LLMotionController();
  67. // Sets associated character. This must be called exactly once by the
  68. // containing character class. This is generally done in the character
  69. LL_INLINE void setCharacter(LLCharacter* ch) { mCharacter = ch; }
  70. // Registers a motion with the controller (actually just forwards call to
  71. // motion registry). Returns true on success.
  72. bool registerMotion(const LLUUID& id, LLMotionConstructor create);
  73. // Creates a motion from the registry
  74. LLMotion* createMotion(const LLUUID& id);
  75. // Unregisters a motion with the controller (actually just forwards call to
  76. // motion registry). Returns true on success.
  77. void removeMotion(const LLUUID& id);
  78. // Starts motion playing the specified motion. Returns true on success.
  79. bool startMotion(const LLUUID& id, F32 start_offset);
  80. // Stops a playing motion; in reality, it begins the ease out transition
  81. // phase. Returns true on success.
  82. bool stopMotionLocally(const LLUUID& id, bool stop_now);
  83. // Moves motions from loading to loaded
  84. void updateLoadingMotions();
  85. // Updates motions. Invokes the update handlers for each active motion.
  86. // Activates sequenced motions and deactivates terminated motions.
  87. void updateMotions(bool force_update = false);
  88. // minimal update (e.g. while hidden)
  89. void updateMotionsMinimal();
  90. // NOTE: call updateMotion() or updateMotionsMinimal() every frame
  91. // Flushes motions and releases all motion instances.
  92. void flushAllMotions();
  93. // Flush is a liar.
  94. void deactivateAllMotions();
  95. // Pauses and continue all motions
  96. void pauseAllMotions();
  97. LL_INLINE void unpauseAllMotions() { mPaused = false; }
  98. LL_INLINE bool isPaused() const { return mPaused; }
  99. // Returns true when paused for more than 2 frames
  100. bool isReallyPaused() const;
  101. void setTimeStep(F32 step);
  102. LL_INLINE F32 getTimeStep() const { return mTimeStep; }
  103. LL_INLINE void setTimeFactor(F32 factor) { mTimeFactor = factor; }
  104. LL_INLINE F32 getTimeFactor() const { return mTimeFactor; }
  105. LL_INLINE F32 getAnimTime() const { return mAnimTime; }
  106. LL_INLINE motion_list_t& getActiveMotions() { return mActiveMotions; }
  107. void incMotionCounts(S32& num_motions, S32& num_loading_motions,
  108. S32& num_loaded_motions, S32& num_active_motions,
  109. S32& num_deprecated_motions);
  110. static void initClass();
  111. static void dumpStats();
  112. LL_INLINE bool isMotionActive(LLMotion* m) { return m && m->isActive(); }
  113. LL_INLINE bool isMotionLoading(LLMotion* m) { return mLoadingMotions.count(m) != 0; }
  114. LLMotion* findMotion(const LLUUID& id) const;
  115. LL_INLINE const LLFrameTimer& getFrameTimer() { return mTimer; }
  116. LL_INLINE static F32 getTimeFactorMultiplier() { return sTimeFactorMultiplier; }
  117. LL_INLINE static void setTimeFactorMultiplier(F32 factor)
  118. {
  119. sTimeFactorMultiplier = factor;
  120. }
  121. protected:
  122. // Internal operations act on motion instances directly as there can be
  123. // duplicate motions per Id during blending overlap
  124. void deleteAllMotions();
  125. bool activateMotionInstance(LLMotion* motion, F32 time);
  126. bool deactivateMotionInstance(LLMotion* motion);
  127. void deprecateMotionInstance(LLMotion* motion);
  128. bool stopMotionInstance(LLMotion* motion, bool stop_now);
  129. void removeMotionInstance(LLMotion* motion);
  130. void updateRegularMotions();
  131. void updateAdditiveMotions();
  132. void resetJointSignatures();
  133. void updateMotionsByType(LLMotion::LLMotionBlendType motion_type);
  134. void updateIdleMotion(LLMotion* motionp);
  135. void updateIdleActiveMotions();
  136. void purgeExcessMotions();
  137. void deactivateStoppedMotions();
  138. public:
  139. F32 mTimeFactorMultiplier;
  140. protected:
  141. F32 mTimeFactor;
  142. LLPoseBlender mPoseBlender;
  143. LLCharacter* mCharacter;
  144. // Life cycle of an animation:
  145. //
  146. // Animations are instantiated and immediately put in the mAllMotions map
  147. // for their entire lifetime. If the animations depend on any asset data,
  148. // the appropriate data is fetched from the data server, and the animation
  149. // is put on the mLoadingMotions list. Once an animations is loaded, it
  150. // will be initialized and put on the mLoadedMotions list. Any animation
  151. // that is currently playing also sits in the mActiveMotions list.
  152. typedef fast_hmap<LLUUID, LLMotion*> motion_map_t;
  153. motion_map_t mAllMotions;
  154. motion_set_t mLoadingMotions;
  155. motion_set_t mLoadedMotions;
  156. motion_list_t mActiveMotions;
  157. motion_set_t mDeprecatedMotions;
  158. LLFrameTimer mTimer;
  159. F32 mPrevTimerElapsed;
  160. F32 mAnimTime;
  161. F32 mLastTime;
  162. bool mHasRunOnce;
  163. bool mPaused;
  164. U32 mPausedFrame;
  165. F32 mTimeStep;
  166. S32 mTimeStepCount;
  167. F32 mLastInterp;
  168. U8 mJointSignature[2][LL_CHARACTER_MAX_ANIMATED_JOINTS];
  169. // Value to use for initialization of mTimeFactorMultiplier
  170. static F32 sTimeFactorMultiplier;
  171. static LLMotionRegistry sRegistry;
  172. private:
  173. static uuid_vec_t sMotionsToKill;
  174. };
  175. //-----------------------------------------------------------------------------
  176. // Class declaractions
  177. //-----------------------------------------------------------------------------
  178. #include "llcharacter.h"
  179. #endif // LL_LLMOTIONCONTROLLER_H