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.

138 lines
3.8 KiB

  1. //========= Copyright 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 "datamodel/dmelement.h"
  14. #include "datamodel/dmattribute.h"
  15. #include "datamodel/dmattributevar.h"
  16. #include "movieobjects/timeutils.h"
  17. //-----------------------------------------------------------------------------
  18. // Helper method to insert + extract DmElement handles into keyvalues
  19. //-----------------------------------------------------------------------------
  20. inline void SetElementKeyValue( KeyValues *pKeyValues, const char *pName, CDmElement *pElement )
  21. {
  22. pKeyValues->SetInt( pName, pElement ? pElement->GetHandle() : DMELEMENT_HANDLE_INVALID );
  23. }
  24. template< class T >
  25. T* GetElementKeyValue( KeyValues *pKeyValues, const char *pName )
  26. {
  27. DmElementHandle_t h = (DmElementHandle_t)pKeyValues->GetInt( pName, DMELEMENT_HANDLE_INVALID );
  28. return GetElement<T>( h );
  29. }
  30. inline KeyValues *CreateElementKeyValues( const char *pName, const char *pKey, CDmElement *pElement )
  31. {
  32. return new KeyValues( pName, pKey, pElement ? ( int )pElement->GetHandle() : DMELEMENT_HANDLE_INVALID );
  33. }
  34. inline void AddStandardElementKeys( KeyValues *pKeyValues, CDmElement *pElement )
  35. {
  36. SetElementKeyValue( pKeyValues, "dmeelement", pElement );
  37. if ( pElement )
  38. {
  39. char buf[ 256 ];
  40. UniqueIdToString( pElement->GetId(), buf, sizeof( buf ) );
  41. pKeyValues->SetString( "text", buf );
  42. pKeyValues->SetString( "type", pElement->GetTypeString() );
  43. }
  44. }
  45. //-----------------------------------------------------------------------------
  46. // Helper method to insert + extract DmeTime_t into keyvalues
  47. //-----------------------------------------------------------------------------
  48. inline void SetDmeTimeKeyValue( KeyValues *pKeyValues, const char *pName, DmeTime_t t )
  49. {
  50. pKeyValues->SetInt( pName, t.GetTenthsOfMS() );
  51. }
  52. inline DmeTime_t GetDmeTimeKeyValue( KeyValues *pKeyValues, const char *pName, DmeTime_t defaultTime = DMETIME_ZERO )
  53. {
  54. return DmeTime_t( pKeyValues->GetInt( pName, defaultTime.GetTenthsOfMS() ) );
  55. }
  56. inline bool ElementTree_IsArrayItem( KeyValues *itemData )
  57. {
  58. return !itemData->IsEmpty( "arrayIndex" );
  59. }
  60. inline CDmAttribute *ElementTree_GetAttribute( KeyValues *itemData )
  61. {
  62. CDmElement *pOwner = GetElementKeyValue< CDmElement >( itemData, "ownerelement" );
  63. if ( !pOwner )
  64. return NULL;
  65. const char *pAttributeName = itemData->GetString( "attributeName", "" );
  66. return pOwner->GetAttribute( pAttributeName );
  67. }
  68. inline DmAttributeType_t ElementTree_GetAttributeType( KeyValues *itemData )
  69. {
  70. CDmElement *pOwner = GetElementKeyValue< CDmElement >( itemData, "ownerelement" );
  71. if ( !pOwner )
  72. return AT_UNKNOWN;
  73. const char *pAttributeName = itemData->GetString( "attributeName", "" );
  74. CDmAttribute *pAttribute = pOwner->GetAttribute( pAttributeName );
  75. if ( !pAttribute )
  76. return AT_UNKNOWN;
  77. return pAttribute->GetType();
  78. }
  79. inline bool ElementTree_GetDroppableItems( CUtlVector< KeyValues * >& msglist, const char *elementType, CUtlVector< CDmElement * >& list )
  80. {
  81. int c = msglist.Count();
  82. for ( int i = 0; i < c; ++i )
  83. {
  84. KeyValues *data = msglist[ i ];
  85. CDmElement *e = GetElementKeyValue<CDmElement>( data, "dmeelement" );
  86. if ( !e )
  87. {
  88. continue;
  89. }
  90. //if ( !e->IsA( elementType ) )
  91. //{
  92. // continue;
  93. //}
  94. list.AddToTail( e );
  95. }
  96. return list.Count() != 0;
  97. }
  98. inline void ElementTree_RemoveListFromArray( CDmAttribute *pArrayAttribute, CUtlVector< CDmElement * >& list )
  99. {
  100. CDmrElementArray<> array( pArrayAttribute );
  101. int c = array.Count();
  102. for ( int i = c - 1 ; i >= 0 ; --i )
  103. {
  104. CDmElement *element = array[ i ];
  105. if ( list.Find( element ) != list.InvalidIndex() )
  106. {
  107. array.Remove( i );
  108. }
  109. }
  110. }
  111. #endif // DMECONTROLS_UTILS_H