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.

122 lines
3.8 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #ifndef VARIANT_T_H
  8. #define VARIANT_T_H
  9. #ifdef _WIN32
  10. #pragma once
  11. #endif
  12. #include "ehandle.h"
  13. #include "mathlib/vmatrix.h"
  14. class CBaseEntity;
  15. //
  16. // A variant class for passing data in entity input/output connections.
  17. //
  18. class variant_t
  19. {
  20. union
  21. {
  22. bool bVal;
  23. string_t iszVal;
  24. int iVal;
  25. float flVal;
  26. float vecVal[3];
  27. color32 rgbaVal;
  28. };
  29. CHandle<CBaseEntity> eVal; // this can't be in the union because it has a constructor.
  30. fieldtype_t fieldType;
  31. public:
  32. // constructor
  33. variant_t() : fieldType(FIELD_VOID), iVal(0) {}
  34. inline bool Bool( void ) const { return( fieldType == FIELD_BOOLEAN ) ? bVal : false; }
  35. inline const char *String( void ) const { return( fieldType == FIELD_STRING ) ? STRING(iszVal) : ToString(); }
  36. inline string_t StringID( void ) const { return( fieldType == FIELD_STRING ) ? iszVal : NULL_STRING; }
  37. inline int Int( void ) const { return( fieldType == FIELD_INTEGER ) ? iVal : 0; }
  38. inline float Float( void ) const { return( fieldType == FIELD_FLOAT ) ? flVal : 0; }
  39. inline const CHandle<CBaseEntity> &Entity(void) const;
  40. inline color32 Color32(void) const { return rgbaVal; }
  41. inline void Vector3D(Vector &vec) const;
  42. fieldtype_t FieldType( void ) { return fieldType; }
  43. void SetBool( bool b ) { bVal = b; fieldType = FIELD_BOOLEAN; }
  44. void SetString( string_t str ) { iszVal = str, fieldType = FIELD_STRING; }
  45. void SetInt( int val ) { iVal = val, fieldType = FIELD_INTEGER; }
  46. void SetFloat( float val ) { flVal = val, fieldType = FIELD_FLOAT; }
  47. void SetEntity( CBaseEntity *val );
  48. void SetVector3D( const Vector &val ) { vecVal[0] = val[0]; vecVal[1] = val[1]; vecVal[2] = val[2]; fieldType = FIELD_VECTOR; }
  49. void SetPositionVector3D( const Vector &val ) { vecVal[0] = val[0]; vecVal[1] = val[1]; vecVal[2] = val[2]; fieldType = FIELD_POSITION_VECTOR; }
  50. void SetColor32( color32 val ) { rgbaVal = val; fieldType = FIELD_COLOR32; }
  51. void SetColor32( int r, int g, int b, int a ) { rgbaVal.r = r; rgbaVal.g = g; rgbaVal.b = b; rgbaVal.a = a; fieldType = FIELD_COLOR32; }
  52. void Set( fieldtype_t ftype, void *data );
  53. void SetOther( void *data );
  54. bool Convert( fieldtype_t newType );
  55. static typedescription_t m_SaveBool[];
  56. static typedescription_t m_SaveInt[];
  57. static typedescription_t m_SaveFloat[];
  58. static typedescription_t m_SaveEHandle[];
  59. static typedescription_t m_SaveString[];
  60. static typedescription_t m_SaveColor[];
  61. static typedescription_t m_SaveVector[];
  62. static typedescription_t m_SavePositionVector[];
  63. static typedescription_t m_SaveVMatrix[];
  64. static typedescription_t m_SaveVMatrixWorldspace[];
  65. static typedescription_t m_SaveMatrix3x4Worldspace[];
  66. protected:
  67. //
  68. // Returns a string representation of the value without modifying the variant.
  69. //
  70. const char *ToString( void ) const;
  71. friend class CVariantSaveDataOps;
  72. };
  73. //-----------------------------------------------------------------------------
  74. // Purpose: Returns this variant as a vector.
  75. //-----------------------------------------------------------------------------
  76. inline void variant_t::Vector3D(Vector &vec) const
  77. {
  78. if (( fieldType == FIELD_VECTOR ) || ( fieldType == FIELD_POSITION_VECTOR ))
  79. {
  80. vec[0] = vecVal[0];
  81. vec[1] = vecVal[1];
  82. vec[2] = vecVal[2];
  83. }
  84. else
  85. {
  86. vec = vec3_origin;
  87. }
  88. }
  89. //-----------------------------------------------------------------------------
  90. // Purpose: Returns this variant as an EHANDLE.
  91. //-----------------------------------------------------------------------------
  92. inline const CHandle<CBaseEntity> &variant_t::Entity(void) const
  93. {
  94. if ( fieldType == FIELD_EHANDLE )
  95. return eVal;
  96. static CHandle<CBaseEntity> hNull;
  97. hNull.Set(NULL);
  98. return(hNull);
  99. }
  100. #endif // VARIANT_T_H