forked from nillerusr/source-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathToolManager.cpp
305 lines (250 loc) · 7.92 KB
/
ToolManager.cpp
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
// TODO: add an autoregistration system for tools a la LINK_ENTITY_TO_CLASS
//=============================================================================//
#include "stdafx.h"
#include "MapDoc.h"
#include "MainFrm.h"
#include "MapView2D.h" // FIXME: for MapView2D::updTool
#include "ToolAxisHandle.h"
#include "ToolDecal.h"
#include "ToolDisplace.h"
#include "ToolManager.h"
#include "ToolMagnify.h"
#include "ToolMaterial.h"
#include "ToolPickFace.h"
#include "ToolPickAngles.h"
#include "ToolPickEntity.h"
#include "ToolPointHandle.h"
#include "ToolSphere.h"
#include "ToolSweptHull.h"
#include "ToolBlock.h"
#include "ToolCamera.h"
#include "ToolClipper.h"
#include "ToolCordon.h"
#include "ToolEntity.h"
#include "ToolMorph.h"
#include "ToolOverlay.h"
#include "ToolSelection.h"
#include "ToolMagnify.h"
#include "ToolMaterial.h"
#include "ChunkFile.h"
// memdbgon must be the last include file in a .cpp file!!!
#include <tier0/memdbgon.h>
static CToolManager s_DummyToolmanager;
CToolManager* ToolManager()
{
CMapDoc *pDoc = CMapDoc::GetActiveMapDoc();
if ( pDoc )
return pDoc->GetTools();
return &s_DummyToolmanager;
}
//-----------------------------------------------------------------------------
// Purpose: Prepares the tool manager for use.
// Output : Returns true on success, false on failure.
//-----------------------------------------------------------------------------
bool CToolManager::Init( CMapDoc *pDocument )
{
// add default tools
//
// Create the tools that are held by the tool manager and add them
// to the internal tools list.
//
RemoveAllTools();
m_pDocument = pDocument;
AddTool( new CToolDisplace );
AddTool( new CToolMagnify );
AddTool( new CToolDecal );
AddTool( new CToolMaterial );
AddTool( new CToolAxisHandle );
AddTool( new CToolPointHandle );
AddTool( new CToolSphere );
AddTool( new CToolPickAngles );
AddTool( new CToolPickEntity );
AddTool( new CToolPickFace );
AddTool( new CToolSweptPlayerHull );
AddTool( new Selection3D );
AddTool( new CToolBlock );
AddTool( new CToolEntity );
AddTool( new Camera3D );
AddTool( new Morph3D );
AddTool( new Clipper3D );
AddTool( new Cordon3D );
AddTool( new CToolOverlay );
return true;
}
//-----------------------------------------------------------------------------
// Purpose: Shuts down the tool manager - called on app exit.
//-----------------------------------------------------------------------------
void CToolManager::Shutdown()
{
m_pActiveTool = NULL;
m_pDocument = NULL;
RemoveAllTools();
}
//-----------------------------------------------------------------------------
// Purpose: Constructor. Allocates the tools.
//-----------------------------------------------------------------------------
CToolManager::CToolManager()
{
m_pActiveTool = NULL;
m_pDocument = NULL;
}
//-----------------------------------------------------------------------------
// Purpose: Destructor. Deletes the tools.
//-----------------------------------------------------------------------------
CToolManager::~CToolManager()
{
Shutdown();
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CToolManager::AddTool(CBaseTool *pTool)
{
if ( GetToolForID( pTool->GetToolID() ) )
{
Assert( !pTool );
Msg("CToolManager::AddTool: Tool %i already registered.\n", pTool->GetToolID());
return;
}
pTool->Init( m_pDocument );
m_Tools.AddToTail(pTool);
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
CBaseTool *CToolManager::GetActiveTool()
{
return m_pActiveTool;
}
//-----------------------------------------------------------------------------
// Purpose: Returns a tool pointer for a given tool ID, NULL if there is no
// corresponding tool.
//-----------------------------------------------------------------------------
CBaseTool *CToolManager::GetToolForID(ToolID_t eToolID)
{
int nToolCount = GetToolCount();
for (int i = 0; i < nToolCount; i++)
{
CBaseTool *pTool = GetTool(i);
if (pTool->GetToolID() == eToolID)
{
return pTool;
}
}
return NULL;
}
//-----------------------------------------------------------------------------
// Purpose: Returns the ID of the active tool.
//-----------------------------------------------------------------------------
ToolID_t CToolManager::GetActiveToolID()
{
if ( m_pActiveTool )
return m_pActiveTool->GetToolID();
else
return TOOL_NONE;
}
//-----------------------------------------------------------------------------
// Purpose: Pushes a new tool onto the tool stack and activates it. The active
// tool will be deactivated and reactivated when PopTool is called.
//-----------------------------------------------------------------------------
void CToolManager::PushTool(ToolID_t eToolID)
{
//
// Add the new tool to the top of the tool stack.
//
if (eToolID != GetActiveToolID())
{
m_ToolIDStack.AddToHead(GetActiveToolID());
}
SetTool(eToolID);
}
//-----------------------------------------------------------------------------
// Purpose: Restores the active tool to what it was when PushTool was called.
// If the stack is underflowed somehow, the pointer is restored.
//-----------------------------------------------------------------------------
void CToolManager::PopTool()
{
int nCount = m_ToolIDStack.Count();
if (nCount > 0)
{
ToolID_t eNewTool = m_ToolIDStack.Element(0);
m_ToolIDStack.Remove(0);
SetTool(eNewTool);
}
}
//-----------------------------------------------------------------------------
// Purpose: Sets the current active tool by ID.
// Input : iTool - ID of the tool to activate.
//-----------------------------------------------------------------------------
void CToolManager::SetTool(ToolID_t eNewTool)
{
CBaseTool *pNewTool = GetToolForID(eNewTool);
CBaseTool *pOldTool = m_pActiveTool;
// Check to see that we can deactive the current tool
if ( pOldTool && (pOldTool != pNewTool) )
{
// Deactivate the current tool unless we are just 'reactivating' it.
if( !pOldTool->CanDeactivate() )
return;
}
// set active tool to new tool already so old tool can peek whats coming next
m_pActiveTool = pNewTool;
// deactivate the old tool if different.
if ( pOldTool && (pOldTool != pNewTool) )
{
pOldTool->Deactivate();
}
// always activate the new tool
if ( pNewTool )
{
pNewTool->Activate();
}
// FIXME: When we start up, we end up here before the main window is created because
// CFaceEditDispPage::OnSetActive() calls SetTool(TOOL_FACEEDIT_DISP). This
// behavior is rather nonsensical during startup.
CMainFrame *pwndMain = GetMainWnd();
if (pwndMain != NULL)
{
pwndMain->m_ObjectBar.UpdateListForTool(eNewTool);
}
if ( m_pDocument )
m_pDocument->UpdateAllViews( MAPVIEW_UPDATE_TOOL );
}
ChunkFileResult_t CToolManager::SaveVMF(CChunkFile *pFile, CSaveInfo *pSaveInfo)
{
for (int i=0;i<m_Tools.Count(); i++)
{
if ( m_Tools[i]->GetVMFChunkName() != NULL )
{
m_Tools[i]->SaveVMF( pFile, pSaveInfo );
}
}
return ChunkFile_Ok;
}
ChunkFileResult_t CToolManager::LoadCallback(CChunkFile *pFile, CBaseTool *pTool)
{
return pTool->LoadVMF( pFile );
}
void CToolManager::AddToolHandlers( CChunkHandlerMap *pHandlersMap )
{
for (int i=0;i<m_Tools.Count(); i++)
{
if ( m_Tools[i]->GetVMFChunkName() != NULL )
{
pHandlersMap->AddHandler( m_Tools[i]->GetVMFChunkName(), (ChunkHandler_t)LoadCallback, m_Tools[i] );
}
}
}
//-----------------------------------------------------------------------------
// Purpose: Removes all the document-created tools from the tools list.
//-----------------------------------------------------------------------------
void CToolManager::RemoveAllTools()
{
m_pActiveTool = NULL;
m_Tools.PurgeAndDeleteElements();
m_ToolIDStack.RemoveAll();
}