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.

321 lines
9.0 KiB

  1. //========= Copyright � Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include "gamegraphic.h"
  8. #include "animdata.h"
  9. #include "gameuisystemmgr.h"
  10. #include "inputgameui.h"
  11. #include "dmxloader/dmxelement.h"
  12. #include "tier1/fmtstr.h"
  13. #include "graphicgroup.h"
  14. #include "gameuiscript.h"
  15. // A list of script handles exposed by the system
  16. static int32 g_iSerialHandle = 0x01000000;
  17. static CUtlMap< int32, CGameGraphic * > g_mapScriptHandles( DefLessFunc( int32 ) );
  18. //-----------------------------------------------------------------------------
  19. // Constructor, destructor
  20. //-----------------------------------------------------------------------------
  21. CGameGraphic::CGameGraphic() :
  22. m_iScriptHandle( ++g_iSerialHandle )
  23. {
  24. g_mapScriptHandles.InsertOrReplace( m_iScriptHandle, this );
  25. m_pName = "";
  26. m_pGroup = NULL;
  27. m_bCanAcceptInput = false;
  28. m_CurrentState = -1;
  29. m_flAnimTime = DMETIME_ZERO;
  30. }
  31. CGameGraphic::~CGameGraphic()
  32. {
  33. if ( m_pGroup )
  34. {
  35. m_pGroup->RemoveFromGroup( this );
  36. }
  37. int nCount = m_Anims.Count();
  38. for ( int i = 0; i < nCount; ++i )
  39. {
  40. delete m_Anims[i];
  41. m_Anims[i] = NULL;
  42. }
  43. m_Anims.RemoveAll();
  44. g_mapScriptHandles.Remove( m_iScriptHandle );
  45. }
  46. CGameGraphic * CGameGraphic::FromScriptHandle( int32 iScriptHandle )
  47. {
  48. unsigned short usIdx = g_mapScriptHandles.Find( iScriptHandle );
  49. return ( usIdx == g_mapScriptHandles.InvalidIndex() ) ? NULL : g_mapScriptHandles.Element( usIdx );
  50. }
  51. //-----------------------------------------------------------------------------
  52. //
  53. //-----------------------------------------------------------------------------
  54. DmeTime_t CGameGraphic::GetAnimationTimePassed()
  55. {
  56. if ( m_Geometry.m_bAnimate )
  57. {
  58. m_flAnimTime = g_pGameUISystemMgrImpl->GetTime() - m_Geometry.m_AnimStartTime;
  59. }
  60. return m_flAnimTime;
  61. }
  62. KeyValues * CGameGraphic::HandleScriptCommand( KeyValues *args )
  63. {
  64. char const *szCommand = args->GetName();
  65. if ( !Q_stricmp( "SetCenter", szCommand ) )
  66. {
  67. SetCenter( args->GetFloat( "x" ), args->GetFloat( "y" ) );
  68. return NULL;
  69. }
  70. else if ( !Q_stricmp( "SetScale", szCommand ) )
  71. {
  72. SetScale( args->GetFloat( "x" ), args->GetFloat( "y" ) );
  73. return NULL;
  74. }
  75. else if ( !Q_stricmp( "SetRotation", szCommand ) )
  76. {
  77. m_Geometry.m_Rotation = args->GetFloat( "rotation", 0.0f );
  78. return NULL;
  79. }
  80. else if ( !Q_stricmp( "SetVisible", szCommand ) )
  81. {
  82. SetVisible( args->GetBool( "visible", true ) );
  83. return NULL;
  84. }
  85. else if ( !Q_stricmp( "GetVisible", szCommand ) )
  86. {
  87. return new KeyValues( "", "visible", GetVisible() ? 1 : 0 );
  88. }
  89. else if ( !Q_stricmp( "SetHorizGradient", szCommand ) )
  90. {
  91. m_Geometry.m_bHorizontalGradient = args->GetBool( "horizgradient", true );
  92. return NULL;
  93. }
  94. else if ( !Q_stricmp( "SetColor", szCommand ) )
  95. {
  96. Color c = args->GetColor( "color", Color( 255, 255, 255, 255 ) );
  97. color32 color;
  98. color.r = c[0];
  99. color.g = c[1];
  100. color.b = c[2];
  101. color.a = c[3];
  102. SetColor( color );
  103. return NULL;
  104. }
  105. else if ( !Q_stricmp( "SetState", szCommand ) )
  106. {
  107. SetState( args->GetString( "state" ), args->GetBool( "play", true ) );
  108. SetAnimationTimePassed( DmeTime_t( args->GetFloat( "time" ) ) );
  109. return NULL;
  110. }
  111. else if ( !Q_stricmp( "GetState", szCommand ) )
  112. {
  113. return new KeyValues( "", "state", GetState() );
  114. }
  115. else if ( !Q_stricmp( "IsDonePlaying", szCommand ) )
  116. {
  117. return new KeyValues( "", "done", IsDonePlaying() ? 1 : 0 );
  118. }
  119. DevWarning( "CGameGraphic::HandleScriptCommand for unknown command %s!\n", args->GetName() );
  120. return NULL;
  121. }
  122. //-----------------------------------------------------------------------------
  123. // If you don't want to use the clock, you can set the time yourself.
  124. //----------------------------------------------------------------------------
  125. void CGameGraphic::SetAnimationTimePassed( DmeTime_t time )
  126. {
  127. m_flAnimTime = time;
  128. }
  129. //-----------------------------------------------------------------------------
  130. // Populate lists for rendering
  131. //-----------------------------------------------------------------------------
  132. void CGameGraphic::UpdateRenderData( color32 parentColor, CUtlVector< RenderGeometryList_t > &renderGeometryLists, int firstListIndex )
  133. {
  134. if ( !m_Geometry.m_bVisible )
  135. return;
  136. m_Geometry.SetResultantColor( parentColor );
  137. m_Geometry.UpdateRenderData( renderGeometryLists, firstListIndex );
  138. }
  139. //-----------------------------------------------------------------------------
  140. // Have to do this separately because extents are drawn as rects.
  141. //-----------------------------------------------------------------------------
  142. void CGameGraphic::DrawExtents( CUtlVector< RenderGeometryList_t > &renderGeometryLists, int firstListIndex )
  143. {
  144. Assert( !IsGroup() );
  145. color32 extentLineColor = { 0, 255, 0, 255 };
  146. m_Geometry.DrawExtents( renderGeometryLists, firstListIndex, extentLineColor );
  147. }
  148. //-----------------------------------------------------------------------------
  149. // Populate lists for rendering
  150. //-----------------------------------------------------------------------------
  151. void CGameGraphic::UpdateRenderTransforms( const StageRenderInfo_t &stageRenderInfo )
  152. {
  153. m_Geometry.UpdateRenderTransforms( stageRenderInfo, GetGroup() );
  154. }
  155. //-----------------------------------------------------------------------------
  156. // Return the index of the anim this state name maps to.
  157. //-----------------------------------------------------------------------------
  158. bool CGameGraphic::HasState( const char *pStateName )
  159. {
  160. return (GetStateIndex( pStateName ) != -1);
  161. }
  162. //-----------------------------------------------------------------------------
  163. // Set the animation state of this graphic.
  164. // Default is to start playing it. If it is called with startplaying false, it will stop it.
  165. //-----------------------------------------------------------------------------
  166. void CGameGraphic::SetState( const char *pStateName, bool bStartPlaying )
  167. {
  168. int newState = GetStateIndex( pStateName );
  169. if ( ( newState != -1 ) && ( m_CurrentState != newState ) )
  170. {
  171. m_CurrentState = newState;
  172. if ( bStartPlaying )
  173. {
  174. StartPlaying();
  175. }
  176. }
  177. if ( !bStartPlaying )
  178. {
  179. StopPlaying();
  180. }
  181. }
  182. //-----------------------------------------------------------------------------
  183. // Return true if anim is done playing its current state.
  184. //-----------------------------------------------------------------------------
  185. bool CGameGraphic::IsDonePlaying()
  186. {
  187. if ( m_CurrentState == -1 )
  188. return true;
  189. return m_Anims[m_CurrentState]->IsDone( GetAnimationTimePassed() );
  190. }
  191. //-----------------------------------------------------------------------------
  192. // Return the index of the anim this state name maps to.
  193. //-----------------------------------------------------------------------------
  194. int CGameGraphic::GetStateIndex( const char *pStateName )
  195. {
  196. for ( int i = 0; i < m_Anims.Count(); i++ )
  197. {
  198. if ( Q_stricmp( pStateName, m_Anims[i]->m_pStateName ) == 0 )
  199. {
  200. if ( Q_stricmp( m_Anims[i]->m_pAnimAlias, "" ) != 0 )
  201. {
  202. return GetStateIndex( m_Anims[i]->m_pAnimAlias );
  203. }
  204. else
  205. {
  206. return i;
  207. }
  208. }
  209. }
  210. return -1;
  211. }
  212. //-----------------------------------------------------------------------------
  213. //
  214. //-----------------------------------------------------------------------------
  215. const char *CGameGraphic::GetState()
  216. {
  217. if ( m_CurrentState == -1 )
  218. return "";
  219. if ( Q_stricmp( m_Anims[m_CurrentState]->m_pAnimAlias, "" ) != 0 )
  220. {
  221. return m_Anims[m_CurrentState]->m_pAnimAlias;
  222. }
  223. else
  224. {
  225. return m_Anims[m_CurrentState]->m_pStateName;
  226. }
  227. }
  228. //-----------------------------------------------------------------------------
  229. //
  230. //-----------------------------------------------------------------------------
  231. void CGameGraphic::StartPlaying()
  232. {
  233. m_Geometry.m_bAnimate = true;
  234. m_Geometry.m_AnimStartTime = g_pGameUISystemMgrImpl->GetTime();
  235. }
  236. //-----------------------------------------------------------------------------
  237. //
  238. //-----------------------------------------------------------------------------
  239. void CGameGraphic::StopPlaying()
  240. {
  241. m_Geometry.m_bAnimate = false;
  242. }
  243. //-----------------------------------------------------------------------------
  244. // for demo and unit tests
  245. //-----------------------------------------------------------------------------
  246. void CGameGraphic::AdvanceState()
  247. {
  248. StopPlaying();
  249. int numStates = m_Anims.Count();
  250. if ( numStates == 0 )
  251. return;
  252. int state = m_CurrentState;
  253. state++;
  254. if ( state > numStates -1 )
  255. state = 0;
  256. m_CurrentState = state;
  257. }
  258. //-----------------------------------------------------------------------------
  259. // Is this the graphic with this name?
  260. //-----------------------------------------------------------------------------
  261. bool CGameGraphic::IsGraphicNamed( const char *pName )
  262. {
  263. if ( !Q_stricmp( m_pName.Get(), pName ) )
  264. {
  265. // Match.
  266. return true;
  267. }
  268. return false;
  269. }
  270. //-----------------------------------------------------------------------------
  271. // Does this graphic own a graphic with this name?
  272. //-----------------------------------------------------------------------------
  273. CGameGraphic *CGameGraphic::FindGraphicByName( const char *pName ) const
  274. {
  275. return NULL;
  276. }