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.

211 lines
4.8 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: Defines the interface to the Undo system.
  4. //
  5. //=============================================================================//
  6. #ifndef HISTORY_H
  7. #define HISTORY_H
  8. #ifdef _WIN32
  9. #pragma once
  10. #endif
  11. #include "MapClass.h" // For CMapObjectList
  12. class CMapClass;
  13. class CMapDoc;
  14. class CHistory;
  15. //
  16. // Holds undo information for a single object, due to a single operation. Held by a CHistoryTrack.
  17. //
  18. class CTrackEntry
  19. {
  20. public:
  21. enum TrackType_t
  22. {
  23. ttNone = -1,
  24. ttCopy,
  25. ttDelete,
  26. ttCreate,
  27. };
  28. CTrackEntry();
  29. CTrackEntry(TrackType_t eType, ...);
  30. ~CTrackEntry();
  31. void Undo(CHistory *Opposite);
  32. void DispatchUndoNotify(void);
  33. void SetKeptChildren(bool bSet);
  34. inline int GetSize(void) { return(m_nDataSize); }
  35. void OnRemoveVisGroup(CVisGroup *pGroup);
  36. bool m_bAutoDestruct;
  37. protected:
  38. size_t m_nDataSize;
  39. TrackType_t m_eType; // What type of event this entry can undo.
  40. //
  41. // Based on the event type, one of these structs will be filled out:
  42. //
  43. union
  44. {
  45. struct
  46. {
  47. CMapClass *pCurrent; // Pointer to the object as it currently exists in the world.
  48. CMapClass *pKeptObject; // Pointer to a copy of the object at the time it was kept.
  49. } m_Copy;
  50. struct
  51. {
  52. CMapClass *pDeleted; // Pointer to the object that was deleted from the world.
  53. CMapClass *pKeptParent; // Pointer to the object's parent at the time of deletion.
  54. } m_Delete;
  55. struct
  56. {
  57. CMapClass *pCreated; // Pointer to the object that was created and added to the world.
  58. } m_Create;
  59. };
  60. bool m_bKeptChildren;
  61. bool m_bUndone; // Set to true after this entry is undone.
  62. };
  63. //
  64. // Tracks all the objects changed by a single operation, such as "Nudge Objects" or "Delete". Each
  65. // track contains one track entry per object affected by the operation.
  66. //
  67. class CHistoryTrack
  68. {
  69. public:
  70. CHistoryTrack(CHistory *pParent, const CMapObjectList *pSelection);
  71. ~CHistoryTrack();
  72. void Keep(CMapClass *pObject, bool bKeepChildren);
  73. void KeepForDestruction(CMapClass *pObject);
  74. void KeepNew(CMapClass *pObject);
  75. void Undo();
  76. void SetName(LPCTSTR pszName) { if(pszName) strcpy(szName, pszName); }
  77. void OnRemoveVisGroup(CVisGroup *pGroup);
  78. private:
  79. BOOL CheckObjectFlag(CMapClass *pObject, int iFlag);
  80. CUtlVector<CTrackEntry> Data;
  81. CHistory *Parent;
  82. DWORD dwID; // id of this tracker..
  83. char szName[128];
  84. CMapObjectList Selected;
  85. bool m_bAutoDestruct;
  86. size_t uDataSize; // approx
  87. friend class CHistory;
  88. };
  89. class CHistory
  90. {
  91. public:
  92. CHistory();
  93. ~CHistory();
  94. static void SetHistory(CHistory *pHistory);
  95. void SetOpposite(BOOL bUndo, CHistory*);
  96. inline void SetDocument(CMapDoc *pDoc);
  97. inline CMapDoc *GetDocument(void);
  98. // mark undo position:
  99. void MarkUndoPosition(const CMapObjectList* pSelection = NULL, LPCTSTR pszName = NULL, BOOL = FALSE);
  100. //
  101. // Keep this object so we can undo changes to it:
  102. //
  103. void Keep(CMapClass *pObject);
  104. void KeepNoChildren(CMapClass *pObject);
  105. void Keep(const CMapObjectList *pList);
  106. //
  107. // Store this pointer for destruction if it cycles off the undo stack:
  108. //
  109. void KeepForDestruction(CMapClass *pObject);
  110. //
  111. // Store this object for destruction if 'undone':
  112. //
  113. void KeepNew(CMapClass *pObject, bool bKeepChildren = true);
  114. void KeepNew(const CMapObjectList *pList, bool bKeepChildren = true);
  115. void Undo(CMapObjectList *pNewSelection);
  116. BOOL IsUndoable(); // anything to undo?
  117. void OnRemoveVisGroup(CVisGroup *pVisGroup);
  118. // returns current name
  119. LPCTSTR GetCurTrackName() { return CurTrack ? CurTrack->szName : ""; }
  120. // total override:
  121. void SetActive(BOOL bActive);
  122. BOOL IsActive() { return m_bActive; }
  123. // temporary shutdown/resume:
  124. inline void Pause() { bPaused = TRUE; }
  125. inline void Resume() { if(bPaused == TRUE) bPaused = FALSE; }
  126. inline BOOL IsPaused() { return bPaused || !IsActive(); }
  127. private:
  128. CHistoryTrack *CurTrack;
  129. CUtlVector<CHistoryTrack*> Tracks;
  130. CMapDoc *m_pDoc; // Associated document.
  131. // opposite tracker:
  132. CHistory *Opposite;
  133. BOOL bUndo; // is this the undo tracker?
  134. BOOL bPaused;
  135. size_t uDataSize;
  136. BOOL m_bActive; // veto control
  137. friend class CHistoryTrack;
  138. };
  139. //-----------------------------------------------------------------------------
  140. // Purpose: Sets the document that this undo history belongs to.
  141. //-----------------------------------------------------------------------------
  142. void CHistory::SetDocument(CMapDoc *pDoc)
  143. {
  144. m_pDoc = pDoc;
  145. }
  146. //-----------------------------------------------------------------------------
  147. // Purpose: Returns the document that this undo history belongs to.
  148. //-----------------------------------------------------------------------------
  149. CMapDoc *CHistory::GetDocument(void)
  150. {
  151. return(m_pDoc);
  152. }
  153. CHistory *GetHistory();
  154. #endif // HISTORY_H