Counter Strike : Global Offensive Source Code
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.

270 lines
7.0 KiB

  1. //========= Copyright � 1996-2005, 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);
  40. bool StartAnimationSequence(Panel *pWithinParent, const char *sequenceName);
  41. // gets the length of an animation sequence, in seconds
  42. float GetAnimationSequenceLength(const char *sequenceName);
  43. // sets that the script file should be reloaded each time a script is ran
  44. // used for development
  45. void SetAutoReloadScript(bool state);
  46. enum Interpolators_e
  47. {
  48. INTERPOLATOR_LINEAR,
  49. INTERPOLATOR_ACCEL,
  50. INTERPOLATOR_DEACCEL,
  51. INTERPOLATOR_PULSE,
  52. INTERPOLATOR_FLICKER,
  53. INTERPOLATOR_SIMPLESPLINE, // ease in / out
  54. INTERPOLATOR_BOUNCE, // gravitational bounce
  55. };
  56. // runs the specific animation command (doesn't use script file at all)
  57. void RunAnimationCommand(vgui::Panel *panel, const char *variable, float targetValue, float startDelaySeconds, float durationSeconds, Interpolators_e interpolator, float animParameter = 0 );
  58. void RunAnimationCommand(vgui::Panel *panel, const char *variable, Color targetValue, float startDelaySeconds, float durationSeconds, Interpolators_e interpolator, float animParameter = 0 );
  59. private:
  60. bool UpdateScreenSize();
  61. bool LoadScriptFile(const char *fileName);
  62. bool ParseScriptFile(char *pMem, int length);
  63. void UpdatePostedMessages(bool bRunToCompletion);
  64. void UpdateActiveAnimations(bool bRunToCompletion);
  65. bool m_bAutoReloadScript;
  66. float m_flCurrentTime;
  67. enum AnimCommandType_e
  68. {
  69. CMD_ANIMATE,
  70. CMD_RUNEVENT,
  71. CMD_STOPEVENT,
  72. CMD_STOPANIMATION,
  73. CMD_STOPPANELANIMATIONS,
  74. CMD_SETFONT,
  75. CMD_SETTEXTURE,
  76. CMD_SETSTRING,
  77. };
  78. enum RelativeAlignment
  79. {
  80. a_northwest = 0,
  81. a_north,
  82. a_northeast,
  83. a_west,
  84. a_center,
  85. a_east,
  86. a_southwest,
  87. a_south,
  88. a_southeast,
  89. };
  90. struct RelativeAlignmentLookup
  91. {
  92. RelativeAlignment align;
  93. char const *name;
  94. };
  95. // a single animatable value
  96. // some var types use 1, 2, 3 or all 4 of the values
  97. struct Value_t
  98. {
  99. float a, b, c, d;
  100. };
  101. struct AnimAlign_t
  102. {
  103. // For Position, Xpos, YPos
  104. bool relativePosition;
  105. UtlSymId_t alignPanel;
  106. RelativeAlignment alignment;
  107. };
  108. // info for the animate command
  109. struct AnimCmdAnimate_t
  110. {
  111. UtlSymId_t panel;
  112. UtlSymId_t variable;
  113. Value_t target;
  114. int interpolationFunction;
  115. float interpolationParameter;
  116. float startTime;
  117. float duration;
  118. AnimAlign_t align;
  119. };
  120. // info for the run event command
  121. struct AnimCmdEvent_t
  122. {
  123. UtlSymId_t event;
  124. UtlSymId_t variable;
  125. UtlSymId_t variable2;
  126. float timeDelay;
  127. };
  128. // holds a single command from an animation sequence
  129. struct AnimCommand_t
  130. {
  131. AnimCommandType_e commandType;
  132. union
  133. {
  134. AnimCmdAnimate_t animate;
  135. AnimCmdEvent_t runEvent;
  136. } cmdData;
  137. };
  138. // holds a full sequence
  139. struct AnimSequence_t
  140. {
  141. UtlSymId_t name;
  142. float duration;
  143. CUtlVector<AnimCommand_t> cmdList;
  144. };
  145. // holds the list of sequences
  146. CUtlVector<AnimSequence_t> m_Sequences;
  147. // list of active animations
  148. struct ActiveAnimation_t
  149. {
  150. PHandle panel;
  151. UtlSymId_t seqName; // the sequence this belongs to
  152. UtlSymId_t variable;
  153. bool started;
  154. Value_t startValue;
  155. Value_t endValue;
  156. int interpolator;
  157. float interpolatorParam;
  158. float startTime;
  159. float endTime;
  160. AnimAlign_t align;
  161. };
  162. CUtlVector<ActiveAnimation_t> m_ActiveAnimations;
  163. // posted messages
  164. struct PostedMessage_t
  165. {
  166. AnimCommandType_e commandType;
  167. UtlSymId_t seqName;
  168. UtlSymId_t event;
  169. UtlSymId_t variable;
  170. UtlSymId_t variable2;
  171. float startTime;
  172. PHandle parent;
  173. };
  174. CUtlVector<PostedMessage_t> m_PostedMessages;
  175. struct RanEvent_t
  176. {
  177. UtlSymId_t event;
  178. Panel *pParent;
  179. bool operator==( const RanEvent_t &other ) const
  180. {
  181. return ( event == other.event && pParent == other.pParent );
  182. }
  183. };
  184. // variable names
  185. UtlSymId_t m_sPosition, m_sSize, m_sFgColor, m_sBgColor;
  186. UtlSymId_t m_sXPos, m_sYPos, m_sWide, m_sTall;
  187. // file name
  188. CUtlVector<UtlSymId_t> m_ScriptFileNames;
  189. // runs a single line of the script
  190. void ExecAnimationCommand(UtlSymId_t seqName, AnimCommand_t &animCommand, Panel *pWithinParent);
  191. // removes all commands belonging to a script
  192. void RemoveQueuedAnimationCommands(UtlSymId_t seqName, vgui::Panel *panel = NULL);
  193. // removes an existing instance of a command
  194. void RemoveQueuedAnimationByType(vgui::Panel *panel, UtlSymId_t variable, UtlSymId_t sequenceToIgnore);
  195. // handlers
  196. void StartCmd_Animate(UtlSymId_t seqName, AnimCmdAnimate_t &cmd, Panel *pWithinParent);
  197. void StartCmd_Animate(Panel *panel, UtlSymId_t seqName, AnimCmdAnimate_t &cmd);
  198. void RunCmd_RunEvent(PostedMessage_t &msg);
  199. void RunCmd_StopEvent(PostedMessage_t &msg);
  200. void RunCmd_StopPanelAnimations(PostedMessage_t &msg);
  201. void RunCmd_StopAnimation(PostedMessage_t &msg);
  202. void RunCmd_SetFont(PostedMessage_t &msg);
  203. void RunCmd_SetTexture(PostedMessage_t &msg);
  204. void RunCmd_SetString(PostedMessage_t &msg);
  205. // value access
  206. Value_t GetValue(ActiveAnimation_t& anim, Panel *panel, UtlSymId_t var);
  207. void SetValue(ActiveAnimation_t& anim, Panel *panel, UtlSymId_t var, Value_t &value);
  208. // interpolation
  209. Value_t GetInterpolatedValue(int interpolator, float interpolatorParam, float currentTime, float startTime, float endTime, Value_t &startValue, Value_t &endValue);
  210. void SetupPosition( AnimCmdAnimate_t& cmd, float *output, char const *psz, int screendimension );
  211. static RelativeAlignment LookupAlignment( char const *token );
  212. static RelativeAlignmentLookup g_AlignmentLookup[];
  213. int GetRelativeOffset( AnimAlign_t& cmd, bool xcoord );
  214. VPANEL m_hSizePanel;
  215. int m_nScreenBounds[ 4 ];
  216. };
  217. // singleton accessor for use only by other vgui_controls
  218. extern AnimationController *GetAnimationController();
  219. } // namespace vgui
  220. #endif // ANIMATIONCONTROLLER_H