summaryrefslogtreecommitdiff
path: root/tools/actbusy/actbusydoc.cpp
blob: 43116b47c7458ffeeee5f69d94267426279a97a2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: 
//
// $NoKeywords: $
//
//=============================================================================//

#include "actbusydoc.h"
#include "datamodel/dmelement.h"
#include "actbusytool.h"


//-----------------------------------------------------------------------------
// Constructor
//-----------------------------------------------------------------------------
CActBusyDoc::CActBusyDoc( IActBusyDocCallback *pCallback ) : m_pCallback( pCallback )
{
	m_hRoot = NULL;
	m_pFileName[0] = 0;
	m_bDirty = false;
	g_pDataModel->InstallNotificationCallback( this );
}

CActBusyDoc::~CActBusyDoc()
{
	g_pDataModel->RemoveNotificationCallback( this );
}


//-----------------------------------------------------------------------------
// Inherited from INotifyUI
//-----------------------------------------------------------------------------
void CActBusyDoc::NotifyDataChanged( const char *pReason, int nNotifySource, int nNotifyFlags )
{
	OnDataChanged( pReason, nNotifySource, nNotifyFlags );
}

	
//-----------------------------------------------------------------------------
// Gets the file name
//-----------------------------------------------------------------------------
const char *CActBusyDoc::GetFileName()
{
	return m_pFileName;
}

void CActBusyDoc::SetFileName( const char *pFileName )
{
	Q_strncpy( m_pFileName, pFileName, sizeof( m_pFileName ) );
	SetDirty( true );
}


//-----------------------------------------------------------------------------
// Dirty bits
//-----------------------------------------------------------------------------
void CActBusyDoc::SetDirty( bool bDirty )
{
	m_bDirty = bDirty;
}

bool CActBusyDoc::IsDirty() const
{
	return m_bDirty;
}


//-----------------------------------------------------------------------------
// Creates a new act busy
//-----------------------------------------------------------------------------
void CActBusyDoc::CreateNew()
{
	Assert( !m_hRoot.Get() );

	// This is not undoable
	CDisableUndoScopeGuard guard;

	Q_strncpy( m_pFileName, "untitled", sizeof( m_pFileName ) );
	DmFileId_t fileid = g_pDataModel->FindOrCreateFileId( m_pFileName );

	// Create the main element
	m_hRoot = g_pDataModel->CreateElement( "DmElement", "ActBusyList", fileid );
	if ( m_hRoot == DMELEMENT_HANDLE_INVALID )
		return;

	g_pDataModel->SetFileRoot( fileid, m_hRoot );

	// Each act busy list needs to have an editortype associated with it so it displays nicely in editors
	m_hRoot->SetValue( "editorType", "actBusyList" );
	m_hRoot->AddAttribute( "children", AT_ELEMENT_ARRAY );

	SetDirty( false );
}


//-----------------------------------------------------------------------------
// Saves/loads from file
//-----------------------------------------------------------------------------
bool CActBusyDoc::LoadFromFile( const char *pFileName )
{
	Assert( !m_hRoot.Get() );

	SetDirty( false );
	m_hRoot = NULL;

	Q_strncpy( m_pFileName, pFileName, sizeof( m_pFileName ) );
	if ( !m_pFileName[0] )
		return false;

	// This is not undoable
	CDisableUndoScopeGuard guard;

	CDmElement *root = NULL;
	g_pDataModel->RestoreFromFile( m_pFileName, NULL, "actbusy", &root );
	m_hRoot = root;
	OnDataChanged( "CActBusyDoc::LoadFromFile", NOTIFY_SOURCE_APPLICATION, NOTIFY_CHANGE_TOPOLOGICAL );
	SetDirty( false );
	return true;
}

void CActBusyDoc::SaveToFile( )
{
	if ( m_hRoot.Get() && m_pFileName && m_pFileName[0] )
	{
		g_pDataModel->SaveToFile( m_pFileName, NULL, "keyvalues", "actbusy", m_hRoot );
	}

	SetDirty( false );
}


//-----------------------------------------------------------------------------
// Creates a new act busy
//-----------------------------------------------------------------------------
void CActBusyDoc::CreateActBusy()
{
	CDmElement *pRoot = GetRootObject();
	if ( !pRoot )
		return;

	// This is undoable
	CAppUndoScopeGuard guard( NOTIFY_SETDIRTYFLAG, "Add ActBusy", "Add ActBusy" );

	DmFileId_t fileid = g_pDataModel->FindOrCreateFileId( m_pFileName );

	// Create the main element
	CDmeHandle<CDmElement> hActBusy = g_pDataModel->CreateElement( "DmElement", "ActBusy", fileid );
	if ( hActBusy == DMELEMENT_HANDLE_INVALID )
		return;

	hActBusy->SetValue( "editorType", "actBusy" );
	hActBusy->SetValue( "busy_anim", "" );
	hActBusy->SetValue( "entry_anim", "" );
	hActBusy->SetValue( "exit_anim", "" );
	hActBusy->SetValue( "busy_sequence", "" );
	hActBusy->SetValue( "entry_sequence", "" );
	hActBusy->SetValue( "exit_sequence", "" );
	hActBusy->SetValue( "min_time", 0.0f );
	hActBusy->SetValue( "max_time", 0.0f );
	hActBusy->SetValue( "interrupts", "BA_INT_NONE" );

	CDmrElementArray<> children( pRoot, "children" );
	children.AddToTail( hActBusy );
}


//-----------------------------------------------------------------------------
// Returns the root object
//-----------------------------------------------------------------------------
CDmElement *CActBusyDoc::GetRootObject()
{
	return m_hRoot;
}

	
//-----------------------------------------------------------------------------
// Called when data changes
//-----------------------------------------------------------------------------
void CActBusyDoc::OnDataChanged( const char *pReason, int nNotifySource, int nNotifyFlags )
{
	SetDirty( nNotifyFlags & NOTIFY_SETDIRTYFLAG ? true : false );
	m_pCallback->OnDocChanged( pReason, nNotifySource, nNotifyFlags );
}