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.

184 lines
5.3 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //=============================================================================//
  8. #include "actbusydoc.h"
  9. #include "datamodel/dmelement.h"
  10. #include "actbusytool.h"
  11. //-----------------------------------------------------------------------------
  12. // Constructor
  13. //-----------------------------------------------------------------------------
  14. CActBusyDoc::CActBusyDoc( IActBusyDocCallback *pCallback ) : m_pCallback( pCallback )
  15. {
  16. m_hRoot = NULL;
  17. m_pFileName[0] = 0;
  18. m_bDirty = false;
  19. g_pDataModel->InstallNotificationCallback( this );
  20. }
  21. CActBusyDoc::~CActBusyDoc()
  22. {
  23. g_pDataModel->RemoveNotificationCallback( this );
  24. }
  25. //-----------------------------------------------------------------------------
  26. // Inherited from INotifyUI
  27. //-----------------------------------------------------------------------------
  28. void CActBusyDoc::NotifyDataChanged( const char *pReason, int nNotifySource, int nNotifyFlags )
  29. {
  30. OnDataChanged( pReason, nNotifySource, nNotifyFlags );
  31. }
  32. //-----------------------------------------------------------------------------
  33. // Gets the file name
  34. //-----------------------------------------------------------------------------
  35. const char *CActBusyDoc::GetFileName()
  36. {
  37. return m_pFileName;
  38. }
  39. void CActBusyDoc::SetFileName( const char *pFileName )
  40. {
  41. Q_strncpy( m_pFileName, pFileName, sizeof( m_pFileName ) );
  42. SetDirty( true );
  43. }
  44. //-----------------------------------------------------------------------------
  45. // Dirty bits
  46. //-----------------------------------------------------------------------------
  47. void CActBusyDoc::SetDirty( bool bDirty )
  48. {
  49. m_bDirty = bDirty;
  50. }
  51. bool CActBusyDoc::IsDirty() const
  52. {
  53. return m_bDirty;
  54. }
  55. //-----------------------------------------------------------------------------
  56. // Creates a new act busy
  57. //-----------------------------------------------------------------------------
  58. void CActBusyDoc::CreateNew()
  59. {
  60. Assert( !m_hRoot.Get() );
  61. // This is not undoable
  62. CDisableUndoScopeGuard guard;
  63. Q_strncpy( m_pFileName, "untitled", sizeof( m_pFileName ) );
  64. DmFileId_t fileid = g_pDataModel->FindOrCreateFileId( m_pFileName );
  65. // Create the main element
  66. m_hRoot = g_pDataModel->CreateElement( "DmElement", "ActBusyList", fileid );
  67. if ( m_hRoot == DMELEMENT_HANDLE_INVALID )
  68. return;
  69. g_pDataModel->SetFileRoot( fileid, m_hRoot );
  70. // Each act busy list needs to have an editortype associated with it so it displays nicely in editors
  71. m_hRoot->SetValue( "editorType", "actBusyList" );
  72. m_hRoot->AddAttribute( "children", AT_ELEMENT_ARRAY );
  73. SetDirty( false );
  74. }
  75. //-----------------------------------------------------------------------------
  76. // Saves/loads from file
  77. //-----------------------------------------------------------------------------
  78. bool CActBusyDoc::LoadFromFile( const char *pFileName )
  79. {
  80. Assert( !m_hRoot.Get() );
  81. SetDirty( false );
  82. m_hRoot = NULL;
  83. Q_strncpy( m_pFileName, pFileName, sizeof( m_pFileName ) );
  84. if ( !m_pFileName[0] )
  85. return false;
  86. // This is not undoable
  87. CDisableUndoScopeGuard guard;
  88. CDmElement *root = NULL;
  89. g_pDataModel->RestoreFromFile( m_pFileName, NULL, "actbusy", &root );
  90. m_hRoot = root;
  91. OnDataChanged( "CActBusyDoc::LoadFromFile", NOTIFY_SOURCE_APPLICATION, NOTIFY_CHANGE_TOPOLOGICAL );
  92. SetDirty( false );
  93. return true;
  94. }
  95. void CActBusyDoc::SaveToFile( )
  96. {
  97. if ( m_hRoot.Get() && m_pFileName && m_pFileName[0] )
  98. {
  99. g_pDataModel->SaveToFile( m_pFileName, NULL, "keyvalues", "actbusy", m_hRoot );
  100. }
  101. SetDirty( false );
  102. }
  103. //-----------------------------------------------------------------------------
  104. // Creates a new act busy
  105. //-----------------------------------------------------------------------------
  106. void CActBusyDoc::CreateActBusy()
  107. {
  108. CDmElement *pRoot = GetRootObject();
  109. if ( !pRoot )
  110. return;
  111. // This is undoable
  112. CAppUndoScopeGuard guard( NOTIFY_SETDIRTYFLAG, "Add ActBusy", "Add ActBusy" );
  113. DmFileId_t fileid = g_pDataModel->FindOrCreateFileId( m_pFileName );
  114. // Create the main element
  115. CDmeHandle<CDmElement> hActBusy = g_pDataModel->CreateElement( "DmElement", "ActBusy", fileid );
  116. if ( hActBusy == DMELEMENT_HANDLE_INVALID )
  117. return;
  118. hActBusy->SetValue( "editorType", "actBusy" );
  119. hActBusy->SetValue( "busy_anim", "" );
  120. hActBusy->SetValue( "entry_anim", "" );
  121. hActBusy->SetValue( "exit_anim", "" );
  122. hActBusy->SetValue( "busy_sequence", "" );
  123. hActBusy->SetValue( "entry_sequence", "" );
  124. hActBusy->SetValue( "exit_sequence", "" );
  125. hActBusy->SetValue( "min_time", 0.0f );
  126. hActBusy->SetValue( "max_time", 0.0f );
  127. hActBusy->SetValue( "interrupts", "BA_INT_NONE" );
  128. CDmrElementArray<> children( pRoot, "children" );
  129. children.AddToTail( hActBusy );
  130. }
  131. //-----------------------------------------------------------------------------
  132. // Returns the root object
  133. //-----------------------------------------------------------------------------
  134. CDmElement *CActBusyDoc::GetRootObject()
  135. {
  136. return m_hRoot;
  137. }
  138. //-----------------------------------------------------------------------------
  139. // Called when data changes
  140. //-----------------------------------------------------------------------------
  141. void CActBusyDoc::OnDataChanged( const char *pReason, int nNotifySource, int nNotifyFlags )
  142. {
  143. SetDirty( nNotifyFlags & NOTIFY_SETDIRTYFLAG ? true : false );
  144. m_pCallback->OnDocChanged( pReason, nNotifySource, nNotifyFlags );
  145. }