Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

283 lines
7.4 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================//
  6. #ifndef ANIMATIONCONTROLLER_H
  7. #define ANIMATIONCONTROLLER_H
  8. #ifdef _WIN32
  9. #pragma once
  10. #endif
  11. #include <vgui_controls/Panel.h>
  12. #include <vgui_controls/PHandle.h>
  13. #include "tier1/utlsymbol.h"
  14. #include "tier1/utlvector.h"
  15. namespace vgui
  16. {
  17. //-----------------------------------------------------------------------------
  18. // Purpose: Handles controlling panel animation
  19. // It is never visible, but needs to be a panel so that can receive messages
  20. //-----------------------------------------------------------------------------
  21. class AnimationController : public Panel
  22. {
  23. DECLARE_CLASS_SIMPLE( AnimationController, Panel );
  24. public:
  25. AnimationController(Panel *parent);
  26. ~AnimationController();
  27. // sets which script file to use
  28. bool SetScriptFile( VPANEL sizingPanel, const char *fileName, bool wipeAll = false );
  29. // reloads the currently set script file
  30. void ReloadScriptFile();
  31. // runs a frame of animation (time is passed in so slow motion, etc. works)
  32. void UpdateAnimations( float curtime );
  33. int GetNumActiveAnimations( void ) { return m_ActiveAnimations.Count(); }
  34. // plays all animations to completion instantly
  35. void RunAllAnimationsToCompletion();
  36. // stops all animations
  37. void CancelAllAnimations();
  38. // starts an animation sequence script
  39. bool StartAnimationSequence(const char *sequenceName, bool bCanBeCancelled = true );
  40. bool StartAnimationSequence(Panel *pWithinParent, const char *sequenceName, bool bCanBeCancelled = true );
  41. bool StopAnimationSequence( Panel *pWithinParent, const char *sequenceName );
  42. void CancelAnimationsForPanel( Panel *pWithinParent );
  43. // gets the length of an animation sequence, in seconds
  44. float GetAnimationSequenceLength(const char *sequenceName);
  45. // sets that the script file should be reloaded each time a script is ran
  46. // used for development
  47. void SetAutoReloadScript(bool state);
  48. enum Interpolators_e
  49. {
  50. INTERPOLATOR_LINEAR,
  51. INTERPOLATOR_ACCEL,
  52. INTERPOLATOR_DEACCEL,
  53. INTERPOLATOR_PULSE,
  54. INTERPOLATOR_FLICKER,
  55. INTERPOLATOR_SIMPLESPLINE, // ease in / out
  56. INTERPOLATOR_BOUNCE, // gravitational bounce
  57. INTERPOLATOR_BIAS,
  58. INTERPOLATOR_GAIN,
  59. };
  60. // runs the specific animation command (doesn't use script file at all)
  61. void RunAnimationCommand(vgui::Panel *panel, const char *variable, float targetValue, float startDelaySeconds, float durationSeconds, Interpolators_e interpolator, float animParameter = 0 );
  62. void RunAnimationCommand(vgui::Panel *panel, const char *variable, Color targetValue, float startDelaySeconds, float durationSeconds, Interpolators_e interpolator, float animParameter = 0 );
  63. private:
  64. bool UpdateScreenSize();
  65. bool LoadScriptFile(const char *fileName);
  66. bool ParseScriptFile(char *pMem, int length);
  67. void UpdatePostedMessages(bool bRunToCompletion);
  68. void UpdateActiveAnimations(bool bRunToCompletion);
  69. bool m_bAutoReloadScript;
  70. float m_flCurrentTime;
  71. enum AnimCommandType_e
  72. {
  73. CMD_ANIMATE,
  74. CMD_RUNEVENT,
  75. CMD_STOPEVENT,
  76. CMD_STOPANIMATION,
  77. CMD_STOPPANELANIMATIONS,
  78. CMD_SETFONT,
  79. CMD_SETTEXTURE,
  80. CMD_SETSTRING,
  81. CMD_RUNEVENTCHILD,
  82. CMD_FIRECOMMAND,
  83. CMD_PLAYSOUND,
  84. CMD_SETVISIBLE,
  85. CMD_SETINPUTENABLED,
  86. };
  87. enum RelativeAlignment
  88. {
  89. a_northwest = 0,
  90. a_north,
  91. a_northeast,
  92. a_west,
  93. a_center,
  94. a_east,
  95. a_southwest,
  96. a_south,
  97. a_southeast,
  98. };
  99. struct RelativeAlignmentLookup
  100. {
  101. RelativeAlignment align;
  102. char const *name;
  103. };
  104. // a single animatable value
  105. // some var types use 1, 2, 3 or all 4 of the values
  106. struct Value_t
  107. {
  108. float a, b, c, d;
  109. };
  110. struct AnimAlign_t
  111. {
  112. // For Position, Xpos, YPos
  113. bool relativePosition;
  114. UtlSymId_t alignPanel;
  115. RelativeAlignment alignment;
  116. };
  117. // info for the animate command
  118. struct AnimCmdAnimate_t
  119. {
  120. UtlSymId_t panel;
  121. UtlSymId_t variable;
  122. Value_t target;
  123. int interpolationFunction;
  124. float interpolationParameter;
  125. float startTime;
  126. float duration;
  127. AnimAlign_t align;
  128. };
  129. // info for the run event command
  130. struct AnimCmdEvent_t
  131. {
  132. UtlSymId_t event;
  133. UtlSymId_t variable;
  134. UtlSymId_t variable2;
  135. float timeDelay;
  136. };
  137. // holds a single command from an animation sequence
  138. struct AnimCommand_t
  139. {
  140. AnimCommandType_e commandType;
  141. union
  142. {
  143. AnimCmdAnimate_t animate;
  144. AnimCmdEvent_t runEvent;
  145. } cmdData;
  146. };
  147. // holds a full sequence
  148. struct AnimSequence_t
  149. {
  150. UtlSymId_t name;
  151. float duration;
  152. CUtlVector<AnimCommand_t> cmdList;
  153. };
  154. // holds the list of sequences
  155. CUtlVector<AnimSequence_t> m_Sequences;
  156. // list of active animations
  157. struct ActiveAnimation_t
  158. {
  159. PHandle panel;
  160. UtlSymId_t seqName; // the sequence this belongs to
  161. UtlSymId_t variable;
  162. bool started;
  163. Value_t startValue;
  164. Value_t endValue;
  165. int interpolator;
  166. float interpolatorParam;
  167. float startTime;
  168. float endTime;
  169. bool canBeCancelled;
  170. AnimAlign_t align;
  171. };
  172. CUtlVector<ActiveAnimation_t> m_ActiveAnimations;
  173. // posted messages
  174. struct PostedMessage_t
  175. {
  176. AnimCommandType_e commandType;
  177. UtlSymId_t seqName;
  178. UtlSymId_t event;
  179. UtlSymId_t variable;
  180. UtlSymId_t variable2;
  181. float startTime;
  182. PHandle parent;
  183. bool canBeCancelled;
  184. };
  185. CUtlVector<PostedMessage_t> m_PostedMessages;
  186. struct RanEvent_t
  187. {
  188. UtlSymId_t event;
  189. Panel *pParent;
  190. bool operator==( const RanEvent_t &other ) const
  191. {
  192. return ( event == other.event && pParent == other.pParent );
  193. }
  194. };
  195. // variable names
  196. UtlSymId_t m_sPosition, m_sSize, m_sFgColor, m_sBgColor;
  197. UtlSymId_t m_sXPos, m_sYPos, m_sWide, m_sTall;
  198. UtlSymId_t m_sModelPos;
  199. // file name
  200. CUtlVector<UtlSymId_t> m_ScriptFileNames;
  201. // runs a single line of the script
  202. void ExecAnimationCommand(UtlSymId_t seqName, AnimCommand_t &animCommand, Panel *pWithinParent, bool bCanBeCancelled);
  203. // removes all commands belonging to a script
  204. void RemoveQueuedAnimationCommands(UtlSymId_t seqName, vgui::Panel *panel = NULL);
  205. // removes an existing instance of a command
  206. void RemoveQueuedAnimationByType(vgui::Panel *panel, UtlSymId_t variable, UtlSymId_t sequenceToIgnore);
  207. // handlers
  208. void StartCmd_Animate(UtlSymId_t seqName, AnimCmdAnimate_t &cmd, Panel *pWithinParent, bool bCanBeCancelled);
  209. void StartCmd_Animate(Panel *panel, UtlSymId_t seqName, AnimCmdAnimate_t &cmd, bool bCanBeCancelled);
  210. void RunCmd_RunEvent(PostedMessage_t &msg);
  211. void RunCmd_StopEvent(PostedMessage_t &msg);
  212. void RunCmd_StopPanelAnimations(PostedMessage_t &msg);
  213. void RunCmd_StopAnimation(PostedMessage_t &msg);
  214. void RunCmd_SetFont(PostedMessage_t &msg);
  215. void RunCmd_SetTexture(PostedMessage_t &msg);
  216. void RunCmd_SetString(PostedMessage_t &msg);
  217. // value access
  218. Value_t GetValue(ActiveAnimation_t& anim, Panel *panel, UtlSymId_t var);
  219. void SetValue(ActiveAnimation_t& anim, Panel *panel, UtlSymId_t var, Value_t &value);
  220. // interpolation
  221. Value_t GetInterpolatedValue(int interpolator, float interpolatorParam, float currentTime, float startTime, float endTime, Value_t &startValue, Value_t &endValue);
  222. void SetupPosition( AnimCmdAnimate_t& cmd, float *output, char const *psz, int screendimension );
  223. static RelativeAlignment LookupAlignment( char const *token );
  224. static RelativeAlignmentLookup g_AlignmentLookup[];
  225. int GetRelativeOffset( AnimAlign_t& cmd, bool xcoord );
  226. VPANEL m_hSizePanel;
  227. int m_nScreenBounds[ 4 ];
  228. };
  229. // singleton accessor for use only by other vgui_controls
  230. extern AnimationController *GetAnimationController();
  231. } // namespace vgui
  232. #endif // ANIMATIONCONTROLLER_H