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.

204 lines
6.5 KiB

  1. //====== Copyright � 1996-2005, Valve Corporation, All rights reserved. =======
  2. //
  3. // Purpose: A particle system definition
  4. //
  5. //=============================================================================
  6. #ifndef DMEPARTICLESYSTEMDEFINITION_H
  7. #define DMEPARTICLESYSTEMDEFINITION_H
  8. #ifdef _WIN32
  9. #pragma once
  10. #endif
  11. #include "datamodel/dmelement.h"
  12. #include "datamodel/dmattribute.h"
  13. #include "datamodel/dmattributevar.h"
  14. #include "datamodel/dmehandle.h"
  15. #include "datamodel/dmattributevar.h"
  16. #include "particles/particles.h"
  17. // key names for clipboard keyvalues entries
  18. #define PARTICLE_CLIPBOARD_FUNCTIONS_STR "fnc"
  19. #define PARTICLE_CLIPBOARD_DEFINITION_STR "pcf"
  20. #define PARTICLE_CLIPBOARD_DEF_BODY_STR "def"
  21. //-----------------------------------------------------------------------------
  22. // Forward declarations
  23. //-----------------------------------------------------------------------------
  24. class CDmeEditorTypeDictionary;
  25. class CDmeParticleSystemDefinition;
  26. //-----------------------------------------------------------------------------
  27. // Base class for particle functions inside a particle system definition
  28. //-----------------------------------------------------------------------------
  29. class CDmeParticleFunction : public CDmElement
  30. {
  31. DEFINE_ELEMENT( CDmeParticleFunction, CDmElement );
  32. public:
  33. virtual const char *GetFunctionType() const { return NULL; }
  34. virtual void Resolve();
  35. virtual void OnElementUnserialized();
  36. // Used for backward compat
  37. void AddMissingFields( const DmxElementUnpackStructure_t *pUnpack );
  38. // Returns the editor type dictionary
  39. CDmeEditorTypeDictionary* GetEditorTypeDictionary();
  40. // Marks a particle system as a new instance
  41. // This is basically a workaround to prevent newly-copied particle functions
  42. // from recompiling themselves a zillion times
  43. void MarkNewInstance();
  44. // should be called if a function was shallow-copied, so it has its own type dictionary
  45. void InstanceTypeDictionary();
  46. protected:
  47. void UpdateAttributes( const DmxElementUnpackStructure_t *pUnpack );
  48. private:
  49. // Defines widgets to edit this bad boy
  50. CDmaElement< CDmeEditorTypeDictionary > m_hTypeDictionary;
  51. bool m_bSkipNextResolve;
  52. };
  53. //-----------------------------------------------------------------------------
  54. // Something that updates particles
  55. //-----------------------------------------------------------------------------
  56. class CDmeParticleOperator : public CDmeParticleFunction
  57. {
  58. DEFINE_ELEMENT( CDmeParticleOperator, CDmeParticleFunction );
  59. public:
  60. // Sets the particle operator
  61. void SetFunction( IParticleOperatorDefinition *pDefinition );
  62. // Returns the function type
  63. virtual const char *GetFunctionType() const;
  64. private:
  65. CDmaString m_FunctionName;
  66. };
  67. //-----------------------------------------------------------------------------
  68. // A child of a particle system
  69. //-----------------------------------------------------------------------------
  70. class CDmeParticleChild : public CDmeParticleFunction
  71. {
  72. DEFINE_ELEMENT( CDmeParticleChild, CDmeParticleFunction );
  73. public:
  74. // Sets the particle operator
  75. void SetChildParticleSystem( CDmeParticleSystemDefinition *pDef, IParticleOperatorDefinition *pDefinition );
  76. // Returns the function type
  77. virtual const char *GetFunctionType() const;
  78. CDmaElement< CDmeParticleSystemDefinition > m_Child;
  79. };
  80. //-----------------------------------------------------------------------------
  81. // Represents an editable entity; draws its helpers
  82. //-----------------------------------------------------------------------------
  83. class CDmeParticleSystemDefinition : public CDmElement
  84. {
  85. DEFINE_ELEMENT( CDmeParticleSystemDefinition, CDmElement );
  86. public:
  87. virtual void OnElementUnserialized();
  88. virtual void Resolve();
  89. // Add, remove
  90. CDmeParticleFunction* AddOperator( ParticleFunctionType_t type, const char *pFunctionName );
  91. CDmeParticleFunction* AddCopyOfOperator( CDmeParticleFunction *pFunc );
  92. CDmeParticleFunction* AddChild( CDmeParticleSystemDefinition *pChild );
  93. void RemoveFunction( ParticleFunctionType_t type, CDmeParticleFunction *pParticleFunction );
  94. void OverrideAttributesFromOtherDefinition( CDmeParticleSystemDefinition *pDef );
  95. // Find
  96. int FindFunction( ParticleFunctionType_t type, CDmeParticleFunction *pParticleFunction );
  97. int FindFunction( ParticleFunctionType_t type, const char *pFunctionName );
  98. // Iteration
  99. int GetParticleFunctionCount( ParticleFunctionType_t type ) const;
  100. CDmeParticleFunction *GetParticleFunction( ParticleFunctionType_t type, int nIndex );
  101. // Reordering
  102. void MoveFunctionUp( ParticleFunctionType_t type, CDmeParticleFunction *pElement );
  103. void MoveFunctionDown( ParticleFunctionType_t type, CDmeParticleFunction *pElement );
  104. // Returns the editor type dictionary
  105. CDmeEditorTypeDictionary* GetEditorTypeDictionary();
  106. // Recompiles the particle system when a change occurs
  107. void RecompileParticleSystem();
  108. // Marks a particle system as a new instance
  109. // This is basically a workaround to prevent newly-copied particle functions
  110. // from recompiling themselves a zillion times
  111. void MarkNewInstance();
  112. // Should we use name-based lookup?
  113. bool UseNameBasedLookup() const;
  114. void RemoveInvalidFunctions();
  115. // Remove DM attributes that aren't needed
  116. void Compact();
  117. private:
  118. CDmaElementArray< CDmeParticleFunction > m_ParticleFunction[PARTICLE_FUNCTION_COUNT];
  119. CDmaVar< bool > m_bPreventNameBasedLookup;
  120. // Defines widgets to edit this bad boy
  121. CDmeHandle< CDmeEditorTypeDictionary > m_hTypeDictionary;
  122. };
  123. //-----------------------------------------------------------------------------
  124. // Should we use name-based lookup?
  125. //-----------------------------------------------------------------------------
  126. inline bool CDmeParticleSystemDefinition::UseNameBasedLookup() const
  127. {
  128. return !m_bPreventNameBasedLookup;
  129. }
  130. //-----------------------------------------------------------------------------
  131. // Human readable string for the particle functions
  132. //-----------------------------------------------------------------------------
  133. const char *GetParticleFunctionTypeName( ParticleFunctionType_t type );
  134. //-----------------------------------------------------------------------------
  135. // Helper function
  136. template<class T> T* ReadParticleClassFromKV( KeyValues *pKV, const char *pKeyName )
  137. {
  138. const char *pData = pKV->GetString( pKeyName );
  139. int nLen = pData ? Q_strlen( pData ) : 0;
  140. if ( nLen )
  141. {
  142. CUtlBuffer buf( pData, nLen, CUtlBuffer::TEXT_BUFFER | CUtlBuffer::READ_ONLY );
  143. DmElementHandle_t hRoot;
  144. if ( !g_pDataModel->Unserialize( buf, "keyvalues2", "pcf", NULL, "paste", CR_FORCE_COPY, hRoot ) )
  145. {
  146. return NULL;
  147. }
  148. return GetElement<T>( hRoot );
  149. }
  150. return NULL;
  151. }
  152. #endif // DMEPARTICLESYSTEMDEFINITION_H