-
Notifications
You must be signed in to change notification settings - Fork 98
/
ToolCordon.cpp
400 lines (323 loc) · 11.1 KB
/
ToolCordon.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
//========= Copyright © 1996-2005, Valve Corporation, All rights reserved. ============//
//
// Purpose: Implements the cordon tool. The cordon tool defines a rectangular
// volume that acts as a visibility filter. Only objects that intersect
// the cordon are rendered in the views. When saving the MAP file while
// the cordon tool is active, only brushes that intersect the cordon
// bounds are saved. The cordon box is replaced by brushes in order to
// seal the map.
//
//=============================================================================//
#include "stdafx.h"
#include "ChunkFile.h"
#include "ToolCordon.h"
#include "History.h"
#include "GlobalFunctions.h"
#include "MainFrm.h"
#include "MapDoc.h"
#include "MapDefs.h"
#include "MapSolid.h"
#include "MapView2D.h"
#include "MapView3D.h"
#include "MapWorld.h"
#include "render2d.h"
#include "StatusBarIDs.h"
#include "ToolManager.h"
#include "Options.h"
#include "WorldSize.h"
#include "vgui/Cursor.h"
// memdbgon must be the last include file in a .cpp file!!!
#include <tier0/memdbgon.h>
Vector Cordon3D::m_vecLastMins; // Last mins & maxs the user dragged out with this tool;
Vector Cordon3D::m_vecLastMaxs; // used to fill in the third axis when starting a new box.
//-----------------------------------------------------------------------------
// Purpose: Constructor.
//-----------------------------------------------------------------------------
Cordon3D::Cordon3D(void)
{
SetDrawColors(RGB(0, 255, 255), RGB(255, 0, 0));
m_vecLastMins.Init( COORD_NOTINIT, COORD_NOTINIT, COORD_NOTINIT );
m_vecLastMaxs.Init( COORD_NOTINIT, COORD_NOTINIT, COORD_NOTINIT );
}
//-----------------------------------------------------------------------------
// Purpose: Called when the tool is activated.
// Input : eOldTool - The ID of the previously active tool.
//-----------------------------------------------------------------------------
void Cordon3D::OnActivate()
{
RefreshToolState();
}
//-----------------------------------------------------------------------------
// Fetch data from the document and update our internal state.
//-----------------------------------------------------------------------------
void Cordon3D::RefreshToolState()
{
Vector mins( COORD_NOTINIT, COORD_NOTINIT, COORD_NOTINIT );
Vector maxs( COORD_NOTINIT, COORD_NOTINIT, COORD_NOTINIT );
if ( m_pDocument->Cordon_GetCount() > 0 )
{
m_pDocument->Cordon_GetEditCordon( mins, maxs );
m_vecLastMins = mins;
m_vecLastMaxs = maxs;
}
SetBounds( mins, maxs );
m_bEmpty = !IsValidBox();
EnableHandles( true );
}
//-----------------------------------------------------------------------------
// Return true of the boxes intersect (but not if they just touch).
//-----------------------------------------------------------------------------
inline bool BoxesIntersect2D( const Vector2D &vBox1Min, const Vector2D &vBox1Max, const Vector2D &vBox2Min, const Vector2D &vBox2Max )
{
return ( vBox1Min.x < vBox2Max.x ) && ( vBox1Max.x > vBox2Min.x ) &&
( vBox1Min.y < vBox2Max.y ) && ( vBox1Max.y > vBox2Min.y );
}
//-----------------------------------------------------------------------------
// Purpose: Handles left mouse button down events in the 2D view.
// Input : Per CWnd::OnLButtonDown.
// Output : Returns true if the message was handled, false if not.
//-----------------------------------------------------------------------------
bool Cordon3D::OnLMouseDown2D(CMapView2D *pView, UINT nFlags, const Vector2D &vPoint)
{
Tool3D::OnLMouseDown2D(pView, nFlags, vPoint);
Vector vecWorld;
pView->ClientToWorld(vecWorld, vPoint);
unsigned int uConstraints = GetConstraints( nFlags );
if ( HitTest(pView, vPoint, true) )
{
StartTranslation( pView, vPoint, m_LastHitTestHandle );
}
else
{
//
// Test against all active cordons
//
CMapDoc *pDoc = pView->GetMapDoc();
if ( !pDoc )
return true;
// Make a little box around the cursor to test against.
const int iSelUnits = 2;
Vector2D selMins( vPoint.x - iSelUnits, vPoint.y - iSelUnits );
Vector2D selMaxs( vPoint.x + iSelUnits, vPoint.y + iSelUnits );
for ( int i = 0; i < pDoc->Cordon_GetCount(); i++ )
{
Cordon_t *cordon = pDoc->Cordon_GetCordon( i );
if ( !cordon->m_bActive )
continue;
for ( int j = 0; j < cordon->m_Boxes.Count(); j++ )
{
BoundBox *box = &cordon->m_Boxes[j];
Vector2D vecClientMins;
Vector2D vecClientMaxs;
pView->WorldToClient( vecClientMins, box->bmins );
pView->WorldToClient( vecClientMaxs, box->bmaxs );
// 2D projection can flip Y
NormalizeBox( vecClientMins, vecClientMaxs );
if ( BoxesIntersect2D( vecClientMins, vecClientMaxs, selMins, selMaxs ) )
{
pDoc->Cordon_SelectCordonForEditing( cordon, box, SELECT_CORDON_FROM_TOOL );
RefreshToolState();
return true;
}
}
}
// getvisiblepoint fills in any coord that's still set to COORD_NOTINIT:
vecWorld[pView->axThird] = m_vecLastMins[pView->axThird];
m_pDocument->GetBestVisiblePoint(vecWorld);
// snap starting position to grid
if ( uConstraints & constrainSnap )
m_pDocument->Snap(vecWorld,uConstraints);
// Create a new box
// Add it to the current edit cordon
// Set the edit cordon to current edit cordon and the new box
Cordon_t *cordon = m_pDocument->Cordon_GetSelectedCordonForEditing();
BoundBox *box = NULL;
if ( !cordon )
{
// No cordon, we need a new one.
cordon = m_pDocument->Cordon_CreateNewCordon( DEFAULT_CORDON_NAME, &box );
}
else
{
// Just add a box to the current cordon.
box = m_pDocument->Cordon_AddBox( cordon );
}
box->bmins = box->bmaxs = vecWorld;
Vector vecSize( 0, 0, 0 );
if ( ( m_vecLastMins[pView->axThird] == COORD_NOTINIT ) || ( m_vecLastMaxs[pView->axThird] == COORD_NOTINIT ) )
{
vecSize[pView->axThird] = pDoc->GetGridSpacing();
}
else
{
vecSize[pView->axThird] = m_vecLastMaxs[pView->axThird] - m_vecLastMins[pView->axThird];
}
StartNew( pView, vPoint, vecWorld, vecSize );
m_pDocument->Cordon_SelectCordonForEditing( cordon, box, SELECT_CORDON_FROM_TOOL );
if ( pDoc->Cordon_IsCordoning() )
{
pDoc->UpdateVisibilityAll();
}
pDoc->SetModifiedFlag( true );
}
return true;
}
//-----------------------------------------------------------------------------
// Purpose: Handles left mouse button up events in the 2D view.
// Input : Per CWnd::OnLButtonUp.
// Output : Returns true if the message was handled, false if not.
//-----------------------------------------------------------------------------
bool Cordon3D::OnLMouseUp2D(CMapView2D *pView, UINT nFlags, const Vector2D &vPoint)
{
bool bShift = ( nFlags & MK_SHIFT ) != 0;
Tool3D::OnLMouseUp2D(pView, nFlags, vPoint) ;
if ( IsTranslating() )
{
if ( bShift )
{
}
FinishTranslation( true );
if ( bShift )
{
// Clone the selected cordon
Cordon_t *cordon = m_pDocument->Cordon_GetSelectedCordonForEditing();
BoundBox *box = m_pDocument->Cordon_AddBox( cordon );
box->bmins = bmins;
box->bmaxs = bmaxs;
m_pDocument->Cordon_SelectCordonForEditing( cordon, box, SELECT_CORDON_FROM_TOOL );
RefreshToolState();
}
else
{
m_pDocument->Cordon_SetEditCordon( bmins, bmaxs );
// Remember these bounds for the next time we start dragging out a new cordon.
m_vecLastMins = bmins;
m_vecLastMaxs = bmaxs;
}
}
m_pDocument->UpdateStatusbar();
return true;
}
//-----------------------------------------------------------------------------
// Purpose: Handles mouse move events in the 2D view.
// Input : Per CWnd::OnMouseMove.
// Output : Returns true if the message was handled, false if not.
//-----------------------------------------------------------------------------
bool Cordon3D::OnMouseMove2D(CMapView2D *pView, UINT nFlags, const Vector2D &vPoint)
{
vgui::HCursor hCursor = vgui::dc_arrow;
Tool3D::OnMouseMove2D(pView, nFlags, vPoint) ;
unsigned int uConstraints = GetConstraints( nFlags );
// Convert to world coords.
Vector vecWorld;
pView->ClientToWorld(vecWorld, vPoint);
// Update status bar position display.
//
char szBuf[128];
m_pDocument->Snap(vecWorld,uConstraints);
sprintf(szBuf, " @%.0f, %.0f ", vecWorld[pView->axHorz], vecWorld[pView->axVert]);
SetStatusText(SBI_COORDS, szBuf);
if ( IsTranslating() )
{
// cursor is cross here
Tool3D::UpdateTranslation( pView, vPoint, uConstraints );
hCursor = vgui::dc_none;
}
else if ( HitTest(pView, vPoint, true) )
{
hCursor = UpdateCursor( pView, m_LastHitTestHandle, m_TranslateMode );
}
if ( hCursor != vgui::dc_none )
pView->SetCursor( hCursor );
return true;
}
//-----------------------------------------------------------------------------
// Purpose: Handles the escape key in the 2D or 3D views.
//-----------------------------------------------------------------------------
void Cordon3D::OnEscape(void)
{
if ( IsTranslating() )
{
FinishTranslation( false );
}
else
{
m_pDocument->GetTools()->SetTool(TOOL_POINTER);
}
}
//-----------------------------------------------------------------------------
// Deletes the currently selected cordon in response to the user hitting DELETE
//-----------------------------------------------------------------------------
void Cordon3D::OnDelete()
{
BoundBox *pBox = NULL;
Cordon_t *pCordon = m_pDocument->Cordon_GetSelectedCordonForEditing( &pBox );
if ( !pCordon || !pBox )
return;
if ( !pBox || ( pCordon->m_Boxes.Count() <= 1 ) )
{
m_pDocument->Cordon_RemoveCordon( pCordon );
}
else
{
m_pDocument->Cordon_RemoveBox( pCordon, pBox );
}
m_pDocument->UpdateVisibilityAll();
m_pDocument->SetModifiedFlag( true );
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
bool Cordon3D::OnKeyDown2D(CMapView2D *pView, UINT nChar, UINT nRepCnt, UINT nFlags)
{
if (nChar == VK_ESCAPE)
{
OnEscape();
return true;
}
if ( nChar == VK_DELETE )
{
OnDelete();
}
return false;
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
bool Cordon3D::OnKeyDown3D(CMapView3D *pView, UINT nChar, UINT nRepCnt, UINT nFlags)
{
if (nChar == VK_ESCAPE)
{
OnEscape();
return true;
}
if ( nChar == VK_DELETE )
{
OnDelete();
}
return false;
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
void Cordon3D::RenderTool2D( CRender2D *pRender )
{
pRender->PushRenderMode( RENDER_MODE_DOTTED );
pRender->SetDrawColor( 255, 255, 0 );
// If cordoning is active, the document's rendering code handles drawing the cordons.
if ( !m_pDocument->Cordon_IsCordoning() )
{
int nCount = m_pDocument->Cordon_GetCount();
for ( int i = 0; i < nCount; i++ )
{
Cordon_t *pCordon = m_pDocument->Cordon_GetCordon( i );
if ( pCordon->m_bActive )
{
for ( int j = 0; j < pCordon->m_Boxes.Count(); j++ )
{
// draw simple rectangle
pRender->DrawRectangle( pCordon->m_Boxes[j].bmins, pCordon->m_Boxes[j].bmaxs, false, 0 );
}
}
}
}
pRender->PopRenderMode();
BaseClass::RenderTool2D( pRender );
}