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.

177 lines
4.6 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include "stdafx.h"
  8. #include "MapDefs.h"
  9. #include "MapGroup.h"
  10. #include "SaveInfo.h"
  11. #include "hammer.h"
  12. // memdbgon must be the last include file in a .cpp file!!!
  13. #include <tier0/memdbgon.h>
  14. IMPLEMENT_MAPCLASS(CMapGroup)
  15. //-----------------------------------------------------------------------------
  16. // Purpose: Sets the new child's color to our own.
  17. // Input : pChild - Object being added to this group.
  18. //-----------------------------------------------------------------------------
  19. void CMapGroup::AddChild(CMapClass *pChild)
  20. {
  21. pChild->SetRenderColor(r,g,b);
  22. CMapClass::AddChild(pChild);
  23. Vector2D mins, maxs;
  24. GetRenderLogicalBox(mins, maxs);
  25. m_vecLogicalPosition = ( mins + maxs ) / 2;
  26. }
  27. //-----------------------------------------------------------------------------
  28. // Purpose:
  29. // Input : *pobj -
  30. // Output : CMapClass *
  31. //-----------------------------------------------------------------------------
  32. CMapClass *CMapGroup::CopyFrom(CMapClass *pobj, bool bUpdateDependencies)
  33. {
  34. return(CMapClass::CopyFrom(pobj, bUpdateDependencies));
  35. }
  36. //-----------------------------------------------------------------------------
  37. // Purpose:
  38. // Output : CMapClass *
  39. //-----------------------------------------------------------------------------
  40. CMapClass *CMapGroup::Copy(bool bUpdateDependencies)
  41. {
  42. CMapGroup *pNew = new CMapGroup;
  43. return(pNew->CopyFrom(this, bUpdateDependencies));
  44. }
  45. //-----------------------------------------------------------------------------
  46. // Purpose: Returns a string describing this group.
  47. //-----------------------------------------------------------------------------
  48. const char* CMapGroup::GetDescription(void)
  49. {
  50. static char szBuf[128];
  51. sprintf(szBuf, "group of %d objects", m_Children.Count());
  52. return(szBuf);
  53. }
  54. void CMapGroup::SetLogicalPosition( const Vector2D &vecPosition )
  55. {
  56. if ( ( m_vecLogicalPosition.x != COORD_NOTINIT )
  57. && ( m_vecLogicalPosition.y != COORD_NOTINIT )
  58. && ( vecPosition != m_vecLogicalPosition ) )
  59. {
  60. Vector2D vecDelta = vecPosition - m_vecLogicalPosition;
  61. FOR_EACH_OBJ( m_Children, pos )
  62. {
  63. CMapClass *pobj = m_Children[pos];
  64. // update logical bounds
  65. pobj->SetLogicalPosition( pobj->GetLogicalPosition() + vecDelta );
  66. }
  67. }
  68. m_vecLogicalPosition = vecPosition;
  69. }
  70. const Vector2D& CMapGroup::GetLogicalPosition()
  71. {
  72. return m_vecLogicalPosition;
  73. }
  74. void CMapGroup::GetRenderLogicalBox( Vector2D &mins, Vector2D &maxs )
  75. {
  76. mins.Init( COORD_NOTINIT, COORD_NOTINIT );
  77. maxs.Init( -COORD_NOTINIT, -COORD_NOTINIT );
  78. FOR_EACH_OBJ( m_Children, pos )
  79. {
  80. CMapClass *pobj = m_Children[pos];
  81. // update logical bounds
  82. Vector2D logicalMins,logicalMaxs;
  83. pobj->GetRenderLogicalBox( logicalMins, logicalMaxs );
  84. mins = mins.Min(logicalMins);
  85. maxs = maxs.Max(logicalMaxs);
  86. }
  87. }
  88. //-----------------------------------------------------------------------------
  89. // Purpose:
  90. // Input : *pFile -
  91. // Output : ChunkFileResult_t
  92. //-----------------------------------------------------------------------------
  93. ChunkFileResult_t CMapGroup::LoadVMF(CChunkFile *pFile)
  94. {
  95. CChunkHandlerMap Handlers;
  96. Handlers.AddHandler("editor", (ChunkHandler_t)CMapClass::LoadEditorCallback, this);
  97. pFile->PushHandlers(&Handlers);
  98. ChunkFileResult_t eResult = pFile->ReadChunk((KeyHandler_t)CMapClass::LoadEditorKeyCallback, this);
  99. pFile->PopHandlers();
  100. return(eResult);
  101. }
  102. //-----------------------------------------------------------------------------
  103. // Purpose:
  104. // Input : *pFile -
  105. // Output : ChunkFileResult_t
  106. //-----------------------------------------------------------------------------
  107. ChunkFileResult_t CMapGroup::SaveVMF(CChunkFile *pFile, CSaveInfo *pSaveInfo)
  108. {
  109. //
  110. // Check rules before saving this object.
  111. //
  112. if (!pSaveInfo->ShouldSaveObject(this))
  113. {
  114. return(ChunkFile_Ok);
  115. }
  116. ChunkFileResult_t eResult = pFile->BeginChunk("group");
  117. //
  118. // Save the group's ID.
  119. //
  120. if (eResult == ChunkFile_Ok)
  121. {
  122. eResult = pFile->WriteKeyValueInt("id", GetID());
  123. }
  124. if (eResult == ChunkFile_Ok)
  125. {
  126. eResult = CMapClass::SaveVMF(pFile, pSaveInfo);
  127. }
  128. if (eResult == ChunkFile_Ok)
  129. {
  130. eResult = pFile->EndChunk();
  131. }
  132. return(eResult);
  133. }
  134. //-----------------------------------------------------------------------------
  135. // Purpose: Groups don't accept visgroups themselves, they
  136. // Input : *pVisGroup -
  137. //-----------------------------------------------------------------------------
  138. void CMapGroup::AddVisGroup(CVisGroup *pVisGroup)
  139. {
  140. FOR_EACH_OBJ( m_Children, pos )
  141. {
  142. m_Children[pos]->AddVisGroup( pVisGroup );
  143. }
  144. }