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.

188 lines
6.9 KiB

  1. //====== Copyright � 1996-2009, Valve Corporation, All rights reserved. =======
  2. //
  3. // Purpose: Declaration of the CDmeConnectionOperator class, a CDmeOperator
  4. // which copies one attribute value to another, providing similar functionality
  5. // to CDmeChannel, but does not store a log and is not effected by the
  6. // recording mode.
  7. //
  8. //=============================================================================
  9. #include "movieobjects/dmeconnectionoperator.h"
  10. #include "datamodel/dmelementfactoryhelper.h"
  11. #include "tier1/fmtstr.h"
  12. // memdbgon must be the last include file in a .cpp file!!!
  13. #include "tier0/memdbgon.h"
  14. //-------------------------------------------------------------------------------------------------
  15. // Expose this class to the scene database
  16. //-------------------------------------------------------------------------------------------------
  17. IMPLEMENT_ELEMENT_FACTORY( DmeConnectionOperator, CDmeConnectionOperator );
  18. //-------------------------------------------------------------------------------------------------
  19. // Purpose: Constructor, initializes attributes, create the embedded target
  20. //-------------------------------------------------------------------------------------------------
  21. void CDmeConnectionOperator::OnConstruction()
  22. {
  23. m_Input.InitAndCreate( this, "input" );
  24. m_Outputs.Init( this, "outputs" );
  25. }
  26. //-------------------------------------------------------------------------------------------------
  27. // Purpose: Perform destruction tasks, destroy the internal elements of the constraint.
  28. //-------------------------------------------------------------------------------------------------
  29. void CDmeConnectionOperator::OnDestruction()
  30. {
  31. g_pDataModel->DestroyElement( m_Input.GetHandle() );
  32. int nOutputs = m_Outputs.Count();
  33. for ( int i = 0 ;i < nOutputs; ++i )
  34. {
  35. if ( m_Outputs[ i ] )
  36. {
  37. g_pDataModel->DestroyElement( m_Outputs[ i ]->GetHandle() );
  38. }
  39. }
  40. m_Outputs.RemoveAll();
  41. }
  42. //-------------------------------------------------------------------------------------------------
  43. // Purpose: Run the operator, which copies the value from the source attribute to the destination
  44. // attributes.
  45. //-------------------------------------------------------------------------------------------------
  46. void CDmeConnectionOperator::Operate()
  47. {
  48. if ( !m_Input->IsValid() )
  49. return;
  50. int nOutputs = m_Outputs.Count();
  51. if ( nOutputs == 0 )
  52. return;
  53. DmAttributeType_t inputType = AT_UNKNOWN;
  54. const void *pValue = m_Input->GetAttributeValue( inputType );
  55. for ( int iOutput = 0; iOutput < nOutputs; ++iOutput )
  56. {
  57. m_Outputs[ iOutput ]->SetAttributeValue( pValue, inputType );
  58. }
  59. }
  60. //-------------------------------------------------------------------------------------------------
  61. // Purpose: Determine if data has changed and the operator needs to be updated
  62. //-------------------------------------------------------------------------------------------------
  63. bool CDmeConnectionOperator::IsDirty()
  64. {
  65. CDmAttribute* pAttr = m_Input->GetReferencedAttribute();
  66. if ( pAttr )
  67. {
  68. return pAttr->IsFlagSet( FATTRIB_DIRTY );
  69. }
  70. return false;
  71. }
  72. //-------------------------------------------------------------------------------------------------
  73. // Purpose: Add the input attribute used by the operator to the provided list of attributes, This
  74. // is generally used by the evaluation process to find the attributes an operator is dependent on.
  75. //-------------------------------------------------------------------------------------------------
  76. void CDmeConnectionOperator::GetInputAttributes( CUtlVector< CDmAttribute * > &attrs )
  77. {
  78. CDmAttribute *pInputAttr = m_Input->GetReferencedAttribute();
  79. if ( pInputAttr )
  80. {
  81. attrs.AddToTail( pInputAttr );
  82. }
  83. }
  84. //-------------------------------------------------------------------------------------------------
  85. // Purpose: Add each of attributes the connection operator outputs to the provided list of
  86. // attributes. This is generally used by the evaluation process to find out what attributes are
  87. // written by the operator in order to determine what other operators are dependent on this
  88. // operator.
  89. //-------------------------------------------------------------------------------------------------
  90. void CDmeConnectionOperator::GetOutputAttributes( CUtlVector< CDmAttribute * > &attrs )
  91. {
  92. int nOutputs = m_Outputs.Count();
  93. for ( int iOutput = 0; iOutput < nOutputs; ++iOutput )
  94. {
  95. CDmAttribute *pOutputAttr = m_Outputs[ iOutput ]->GetReferencedAttribute();
  96. if ( pOutputAttr )
  97. {
  98. attrs.AddToTail( pOutputAttr );
  99. }
  100. }
  101. }
  102. //-------------------------------------------------------------------------------------------------
  103. // Purpose: Set the input attribute of the connection.
  104. //-------------------------------------------------------------------------------------------------
  105. void CDmeConnectionOperator::SetInput( CDmElement* pElement, const char* pchAttributeName, int index )
  106. {
  107. m_Input->SetAttribute( pElement, pchAttributeName, index );
  108. }
  109. //-------------------------------------------------------------------------------------------------
  110. // Purpose: Add an attribute to be written to by the connection.
  111. //-------------------------------------------------------------------------------------------------
  112. void CDmeConnectionOperator::AddOutput( CDmElement* pElement, const char* pchAttributeName, int index )
  113. {
  114. if ( ( pElement == NULL ) || ( pchAttributeName == NULL ) )
  115. return;
  116. CDmeAttributeReference *pAttrRef = CreateElement< CDmeAttributeReference >( CFmtStr( "%s_%s", pElement->GetName() , pchAttributeName ), GetFileId() );
  117. if ( pAttrRef )
  118. {
  119. if ( pAttrRef->SetAttribute( pElement, pchAttributeName, index ) )
  120. {
  121. // Add the new reference to the list of outputs of the connection.
  122. m_Outputs.AddToTail( pAttrRef );
  123. }
  124. else
  125. {
  126. // If the specified attribute was not valid destroy the reference.
  127. g_pDataModel->DestroyElement( pAttrRef->GetHandle() );
  128. }
  129. }
  130. }
  131. //-------------------------------------------------------------------------------------------------
  132. // Purpose: Get the number of output attributes
  133. //-------------------------------------------------------------------------------------------------
  134. int CDmeConnectionOperator::NumOutputAttributes() const
  135. {
  136. return m_Outputs.Count();
  137. }
  138. //-------------------------------------------------------------------------------------------------
  139. // Purpose: Get the specified output attribute
  140. //-------------------------------------------------------------------------------------------------
  141. CDmAttribute *CDmeConnectionOperator::GetOutputAttribute( int index ) const
  142. {
  143. if ( index >= m_Outputs.Count() )
  144. return NULL;
  145. return m_Outputs[ index ]->GetReferencedAttribute();
  146. }
  147. //-------------------------------------------------------------------------------------------------
  148. // Purpose: Get the input attribute
  149. //-------------------------------------------------------------------------------------------------
  150. CDmAttribute *CDmeConnectionOperator::GetInputAttribute()
  151. {
  152. return m_Input.GetAttribute();
  153. }