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.

165 lines
5.8 KiB

  1. //====== Copyright � 1996-2009, Valve Corporation, All rights reserved. =======
  2. //
  3. // Purpose: Implementation of the CDmeAttributeReference class, a simple
  4. // DmElement used to store references to attributes within other DmElements.
  5. //
  6. //=============================================================================
  7. #include "movieobjects/dmeattributereference.h"
  8. #include "datamodel/dmelementfactoryhelper.h"
  9. // memdbgon must be the last include file in a .cpp file!!!
  10. #include "tier0/memdbgon.h"
  11. //-------------------------------------------------------------------------------------------------
  12. // Expose the CDmeAttributeReference class to the scene database
  13. //-------------------------------------------------------------------------------------------------
  14. IMPLEMENT_ELEMENT_FACTORY( DmeAttributeReference, CDmeAttributeReference );
  15. //-------------------------------------------------------------------------------------------------
  16. // Purpose: Perform construction tasks, initializes attributes
  17. //-------------------------------------------------------------------------------------------------
  18. void CDmeAttributeReference::OnConstruction()
  19. {
  20. m_Element.Init( this, "element", FATTRIB_HAS_CALLBACK | FATTRIB_NEVERCOPY );
  21. m_AttributeName.Init( this, "attribute", FATTRIB_HAS_CALLBACK | FATTRIB_TOPOLOGICAL );
  22. m_AttributeIndex.Init( this, "index", FATTRIB_TOPOLOGICAL );
  23. m_AttributeHandle = DMATTRIBUTE_HANDLE_INVALID;
  24. }
  25. //-------------------------------------------------------------------------------------------------
  26. // Purpose: Perform destruction tasks
  27. //-------------------------------------------------------------------------------------------------
  28. void CDmeAttributeReference::OnDestruction()
  29. {
  30. }
  31. //-------------------------------------------------------------------------------------------------
  32. // Purpose: Process notifications of changed attributes and update the attribute handles if needed.
  33. //-------------------------------------------------------------------------------------------------
  34. void CDmeAttributeReference::OnAttributeChanged( CDmAttribute *pAttribute )
  35. {
  36. if ( ( pAttribute == m_Element.GetAttribute() ) || ( pAttribute == m_AttributeName.GetAttribute() ) )
  37. {
  38. // Invalidate the attribute handle so that it will be updated.
  39. m_AttributeHandle = DMATTRIBUTE_HANDLE_INVALID;
  40. return;
  41. }
  42. BaseClass::OnAttributeChanged( pAttribute );
  43. }
  44. //-------------------------------------------------------------------------------------------------
  45. // Purpose: Set the attribute by specifying the element, name of the attribute and optionally the
  46. // array index of the attribute.
  47. //-------------------------------------------------------------------------------------------------
  48. bool CDmeAttributeReference::SetAttribute( CDmElement* pElement, const char* pchAttributeName, int index )
  49. {
  50. m_Element = pElement;
  51. m_AttributeName = pchAttributeName;
  52. m_AttributeIndex = index;
  53. CDmAttribute *pAttr = LookupAttributeHandle();
  54. return ( pAttr != NULL );
  55. }
  56. //-------------------------------------------------------------------------------------------------
  57. // Purpose: Lookup and cache the attribute handle
  58. //-------------------------------------------------------------------------------------------------
  59. CDmAttribute *CDmeAttributeReference::LookupAttributeHandle() const
  60. {
  61. m_AttributeHandle = DMATTRIBUTE_HANDLE_INVALID;
  62. CDmElement *pElement = m_Element.GetElement();
  63. const char *pName= m_AttributeName.Get();
  64. if ( pElement == NULL || pName == NULL || !pName[0] )
  65. return NULL;
  66. CDmAttribute *pAttr = pElement->GetAttribute( pName );
  67. if ( !pAttr )
  68. return NULL;
  69. m_AttributeHandle = pAttr->GetHandle();
  70. return pAttr;
  71. }
  72. //-------------------------------------------------------------------------------------------------
  73. // Purpose: Get the attribute using the cached handle or looking up the handle if needed.
  74. //-------------------------------------------------------------------------------------------------
  75. CDmAttribute* CDmeAttributeReference::GetReferencedAttribute() const
  76. {
  77. CDmAttribute *pAttribute = g_pDataModel->GetAttribute( m_AttributeHandle );
  78. if ( !pAttribute )
  79. {
  80. pAttribute = LookupAttributeHandle();
  81. }
  82. return pAttribute;
  83. }
  84. //-------------------------------------------------------------------------------------------------
  85. // Purpose: Get the value of the referenced attribute
  86. //-------------------------------------------------------------------------------------------------
  87. const void *CDmeAttributeReference::GetAttributeValue( DmAttributeType_t &type ) const
  88. {
  89. CDmAttribute *pAttribute = GetReferencedAttribute();
  90. if ( pAttribute == NULL )
  91. return NULL;
  92. const void *pValue = NULL;
  93. type = pAttribute->GetType();
  94. if ( IsArrayType( type ) )
  95. {
  96. CDmrGenericArray array( pAttribute );
  97. pValue = array.GetUntyped( m_AttributeIndex.Get() );
  98. type = ArrayTypeToValueType( type );
  99. }
  100. else
  101. {
  102. pValue = pAttribute->GetValueUntyped();
  103. }
  104. return pValue;
  105. }
  106. //-------------------------------------------------------------------------------------------------
  107. // Purpose: Set the value of the referenced attribute
  108. //-------------------------------------------------------------------------------------------------
  109. void CDmeAttributeReference::SetAttributeValue( const void *pValue, DmAttributeType_t type ) const
  110. {
  111. CDmAttribute *pAttribute = GetReferencedAttribute();
  112. if ( pAttribute == NULL )
  113. return;
  114. if ( IsArrayType( pAttribute->GetType() ) )
  115. {
  116. CDmrGenericArray array( pAttribute );
  117. array.Set( m_AttributeIndex.Get(), type, pValue );
  118. }
  119. else
  120. {
  121. pAttribute->SetValue( type, pValue );
  122. }
  123. }
  124. //-------------------------------------------------------------------------------------------------
  125. // Purpose: Determine if the attribute reference points to a valid attribute
  126. //-------------------------------------------------------------------------------------------------
  127. bool CDmeAttributeReference::IsValid() const
  128. {
  129. return ( GetReferencedAttribute() != NULL );
  130. }