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.

262 lines
8.0 KiB

  1. //===== Copyright � 1996-2005, Valve Corporation, All rights reserved. ======//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //===========================================================================//
  7. #ifndef DMECONTROLS_UTILS_H
  8. #define DMECONTROLS_UTILS_H
  9. #ifdef _WIN32
  10. #pragma once
  11. #endif
  12. #include "tier1/keyvalues.h"
  13. #include "tier1/fmtstr.h"
  14. #include "tier1/utlbufferutil.h"
  15. #include "tier1/utlsymbollarge.h"
  16. #include "datamodel/dmelement.h"
  17. #include "datamodel/dmattribute.h"
  18. #include "datamodel/dmattributevar.h"
  19. #include "movieobjects/dmegamemodel.h"
  20. #include "movieobjects/dmeclip.h"
  21. #include "movieobjects/dmechannel.h"
  22. #include "movieobjects/dmeanimationset.h"
  23. #include "vgui_controls/TextEntry.h"
  24. #include "vgui_controls/InputDialog.h"
  25. #include "vgui_controls/Menu.h"
  26. inline int AddCheckableMenuItem( vgui::Menu *pMenu, const char *pItemName, KeyValues *pKeyValues, vgui::Panel *pTarget, bool bState, bool bEnabled = true )
  27. {
  28. int id = pMenu->AddCheckableMenuItem( pItemName, pKeyValues, pTarget );
  29. pMenu->SetMenuItemChecked( id, bState );
  30. pMenu->SetItemEnabled( id, bEnabled );
  31. return id;
  32. }
  33. inline int AddCheckableMenuItem( vgui::Menu *pMenu, const char *pItemName, const char *pKVName, vgui::Panel *pTarget, bool bState, bool bEnabled = true )
  34. {
  35. return AddCheckableMenuItem( pMenu, pItemName, new KeyValues( pKVName ), pTarget, bState, bEnabled );
  36. }
  37. //-----------------------------------------------------------------------------
  38. // Helper method to insert + extract DmElement handles into keyvalues
  39. //-----------------------------------------------------------------------------
  40. inline void SetElementKeyValue( KeyValues *pKeyValues, const char *pName, CDmElement *pElement )
  41. {
  42. pKeyValues->SetInt( pName, pElement ? pElement->GetHandle() : DMELEMENT_HANDLE_INVALID );
  43. }
  44. template< class T >
  45. T* GetElementKeyValue( KeyValues *pKeyValues, const char *pName )
  46. {
  47. DmElementHandle_t h = (DmElementHandle_t)pKeyValues->GetInt( pName, DMELEMENT_HANDLE_INVALID );
  48. return GetElement<T>( h );
  49. }
  50. inline KeyValues *CreateElementKeyValues( const char *pName, const char *pKey, CDmElement *pElement )
  51. {
  52. return new KeyValues( pName, pKey, pElement ? ( int )pElement->GetHandle() : DMELEMENT_HANDLE_INVALID );
  53. }
  54. inline void AddStandardElementKeys( KeyValues *pKeyValues, CDmElement *pElement )
  55. {
  56. SetElementKeyValue( pKeyValues, "dmeelement", pElement );
  57. if ( pElement )
  58. {
  59. char buf[ 256 ];
  60. UniqueIdToString( pElement->GetId(), buf, sizeof( buf ) );
  61. pKeyValues->SetString( "text", buf );
  62. pKeyValues->SetString( "type", pElement->GetTypeString() );
  63. }
  64. }
  65. inline void SetMatrixKeyValue( KeyValues *pKeyValues, const char *pName, const VMatrix &mat )
  66. {
  67. CUtlBuffer buf;
  68. buf.SetBufferType( true, false );
  69. Serialize( buf, mat );
  70. pKeyValues->SetString( pName, ( char* )buf.Base() );
  71. }
  72. inline void GetMatrixKeyValue( KeyValues *pKeyValues, const char *pName, VMatrix &mat )
  73. {
  74. const char *pMatrixString = pKeyValues->GetString( pName );
  75. CUtlBuffer buf( pMatrixString, V_strlen( pMatrixString ) + 1, CUtlBuffer::TEXT_BUFFER | CUtlBuffer::READ_ONLY );
  76. Unserialize( buf, mat );
  77. }
  78. //-----------------------------------------------------------------------------
  79. // Helper method to insert + extract DmeTime_t into keyvalues
  80. //-----------------------------------------------------------------------------
  81. inline void SetDmeTimeKeyValue( KeyValues *pKeyValues, const char *pName, DmeTime_t t )
  82. {
  83. pKeyValues->SetInt( pName, t.GetTenthsOfMS() );
  84. }
  85. inline DmeTime_t GetDmeTimeKeyValue( KeyValues *pKeyValues, const char *pName, DmeTime_t defaultTime = DMETIME_ZERO )
  86. {
  87. return DmeTime_t( pKeyValues->GetInt( pName, defaultTime.GetTenthsOfMS() ) );
  88. }
  89. inline bool ElementTree_IsArrayItem( KeyValues *itemData )
  90. {
  91. return !itemData->IsEmpty( "arrayIndex" );
  92. }
  93. inline CDmAttribute *ElementTree_GetAttribute( KeyValues *itemData )
  94. {
  95. CDmElement *pOwner = GetElementKeyValue< CDmElement >( itemData, "ownerelement" );
  96. if ( !pOwner )
  97. return NULL;
  98. const char *pAttributeName = itemData->GetString( "attributeName", "" );
  99. return pOwner->GetAttribute( pAttributeName );
  100. }
  101. inline DmAttributeType_t ElementTree_GetAttributeType( KeyValues *itemData )
  102. {
  103. CDmElement *pOwner = GetElementKeyValue< CDmElement >( itemData, "ownerelement" );
  104. if ( !pOwner )
  105. return AT_UNKNOWN;
  106. const char *pAttributeName = itemData->GetString( "attributeName", "" );
  107. CDmAttribute *pAttribute = pOwner->GetAttribute( pAttributeName );
  108. if ( !pAttribute )
  109. return AT_UNKNOWN;
  110. return pAttribute->GetType();
  111. }
  112. inline bool ElementTree_GetDroppableItems( CUtlVector< KeyValues * >& msglist, const char *elementType, CUtlVector< CDmElement * >& list )
  113. {
  114. int c = msglist.Count();
  115. for ( int i = 0; i < c; ++i )
  116. {
  117. KeyValues *data = msglist[ i ];
  118. CDmElement *e = GetElementKeyValue<CDmElement>( data, "dmeelement" );
  119. if ( !e )
  120. {
  121. continue;
  122. }
  123. //if ( !e->IsA( elementType ) )
  124. //{
  125. // continue;
  126. //}
  127. list.AddToTail( e );
  128. }
  129. return list.Count() != 0;
  130. }
  131. inline void ElementTree_RemoveListFromArray( CDmAttribute *pArrayAttribute, CUtlVector< CDmElement * >& list )
  132. {
  133. CDmrElementArray<> array( pArrayAttribute );
  134. int c = array.Count();
  135. for ( int i = c - 1 ; i >= 0 ; --i )
  136. {
  137. CDmElement *element = array[ i ];
  138. if ( list.Find( element ) != list.InvalidIndex() )
  139. {
  140. array.Remove( i );
  141. }
  142. }
  143. }
  144. struct PresetGroupInfo_t
  145. {
  146. PresetGroupInfo_t( CUtlSymbolLarge group = UTL_INVAL_SYMBOL_LARGE ) : presetGroupSym( group ) {}
  147. CUtlSymbolLarge presetGroupSym;
  148. bool bGroupShared;
  149. bool bGroupReadOnly;
  150. bool bGroupVisible;
  151. friend bool operator==( const PresetGroupInfo_t &lhs, const PresetGroupInfo_t &rhs ) { return lhs.presetGroupSym == rhs.presetGroupSym; }
  152. friend bool operator!=( const PresetGroupInfo_t &lhs, const PresetGroupInfo_t &rhs ) { return lhs.presetGroupSym != rhs.presetGroupSym; }
  153. };
  154. inline void CollectPresetGroupInfo( CDmeFilmClip *pFilmClip, CUtlVector< PresetGroupInfo_t > &presetInfo, bool bSkipReadOnly = false, bool bSkipInvisible = false )
  155. {
  156. CAnimSetGroupAnimSetTraversal traversal( pFilmClip );
  157. while ( CDmeAnimationSet *pAnimSet = traversal.Next() )
  158. {
  159. const CDmrElementArray< CDmePresetGroup > presetGroups = pAnimSet->GetPresetGroups();
  160. int nPresetGroups = presetGroups.Count();
  161. for ( int i = 0; i < nPresetGroups; ++i )
  162. {
  163. CDmePresetGroup *pPresetGroup = presetGroups[ i ];
  164. if ( !pPresetGroup )
  165. continue;
  166. if ( bSkipReadOnly && pPresetGroup->m_bIsReadOnly )
  167. continue;
  168. if ( bSkipInvisible && !pPresetGroup->m_bIsVisible )
  169. continue;
  170. PresetGroupInfo_t info( g_pDataModel->GetSymbol( pPresetGroup->GetName() ) );
  171. info.bGroupReadOnly = pPresetGroup->m_bIsReadOnly;
  172. info.bGroupVisible = pPresetGroup->m_bIsVisible;
  173. info.bGroupShared = pPresetGroup->IsShared();
  174. int idx = presetInfo.Find( info );
  175. if ( idx != presetInfo.InvalidIndex() )
  176. continue;
  177. presetInfo.AddToTail( info );
  178. }
  179. }
  180. }
  181. inline void CollectProceduralPresetNames( CUtlVector< CUtlSymbolLarge > &presetNames )
  182. {
  183. static const CUtlSymbolLarge proceduralPresetGroupNameSymbol = g_pDataModel->GetSymbol( PROCEDURAL_PRESET_GROUP_NAME );
  184. int idx = presetNames.AddMultipleToTail( NUM_PROCEDURAL_PRESET_TYPES - 1 );
  185. for ( int i = 1; i < NUM_PROCEDURAL_PRESET_TYPES; ++i, ++idx ) // skip PROCEDURAL_PRESET_NOT
  186. {
  187. presetNames[ idx ] = g_pDataModel->GetSymbol( GetProceduralPresetName( i ) );
  188. }
  189. }
  190. inline void CollectPresetNamesForGroup( CDmeFilmClip *pFilmClip, const char *pPresetGroupName, CUtlVector< CUtlSymbolLarge > &presetNames )
  191. {
  192. CAnimSetGroupAnimSetTraversal traversal( pFilmClip );
  193. while ( CDmeAnimationSet *pAnimSet = traversal.Next() )
  194. {
  195. CDmePresetGroup *pPresetGroup = pAnimSet->FindPresetGroup( pPresetGroupName );
  196. if ( !pPresetGroup )
  197. continue;
  198. CDmrElementArray< CDmePreset > presets = pPresetGroup->GetPresets();
  199. int cp = presets.Count();
  200. for ( int j = 0; j < cp; ++j )
  201. {
  202. CDmePreset *pPreset = presets[ j ];
  203. Assert( pPreset );
  204. if ( !pPreset )
  205. continue;
  206. CUtlSymbolLarge symPresetName = g_pDataModel->GetSymbol( pPreset->GetName() );
  207. if ( presetNames.Find( symPresetName ) != presetNames.InvalidIndex() )
  208. continue;
  209. presetNames.AddToTail( symPresetName );
  210. }
  211. }
  212. }
  213. #endif // DMECONTROLS_UTILS_H