forked from nillerusr/source-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathToolBlock.cpp
517 lines (411 loc) · 12.4 KB
/
ToolBlock.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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
// $NoKeywords: $
//=============================================================================//
#include "stdafx.h"
#include "History.h"
#include "MainFrm.h"
#include "MapDefs.h"
#include "MapDoc.h"
#include "MapView2D.h"
#include "MapView3D.h"
#include "Options.h"
#include "StatusBarIDs.h"
#include "ToolBlock.h"
#include "ToolManager.h"
#include "vgui/Cursor.h"
#include "Selection.h"
class CToolBlockMessageWnd : public CWnd
{
public:
bool Create(void);
void PreMenu2D(CToolBlock *pTool, CMapView2D *pView);
protected:
//{{AFX_MSG_MAP(CToolBlockMessageWnd)
afx_msg void OnCreateObject();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
private:
CToolBlock *m_pToolBlock;
CMapView2D *m_pView2D;
};
static CToolBlockMessageWnd s_wndToolMessage;
static const char *g_pszClassName = "ValveEditor_BlockToolWnd";
BEGIN_MESSAGE_MAP(CToolBlockMessageWnd, CWnd)
//{{AFX_MSG_MAP(CToolMessageWnd)
ON_COMMAND(ID_CREATEOBJECT, OnCreateObject)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
//-----------------------------------------------------------------------------
// Purpose: Creates the hidden window that receives context menu commands for the
// block tool.
// Output : Returns true on success, false on failure.
//-----------------------------------------------------------------------------
bool CToolBlockMessageWnd::Create(void)
{
WNDCLASS wndcls;
memset(&wndcls, 0, sizeof(WNDCLASS));
wndcls.lpfnWndProc = AfxWndProc;
wndcls.hInstance = AfxGetInstanceHandle();
wndcls.lpszClassName = g_pszClassName;
if (!AfxRegisterClass(&wndcls))
{
return(false);
}
return(CWnd::CreateEx(0, g_pszClassName, g_pszClassName, 0, CRect(0, 0, 10, 10), NULL, 0) == TRUE);
}
//-----------------------------------------------------------------------------
// Purpose: Attaches the block tool to this window before activating the context
// menu.
//-----------------------------------------------------------------------------
void CToolBlockMessageWnd::PreMenu2D(CToolBlock *pToolBlock, CMapView2D *pView)
{
Assert(pToolBlock != NULL);
m_pToolBlock = pToolBlock;
m_pView2D = pView;
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CToolBlockMessageWnd::OnCreateObject()
{
m_pToolBlock->CreateMapObject(m_pView2D);
}
//-----------------------------------------------------------------------------
// Purpose: Constructor.
//-----------------------------------------------------------------------------
CToolBlock::CToolBlock(void)
{
// We always show our dimensions when we render.
SetDrawFlags(GetDrawFlags() | Box3D::boundstext);
SetDrawColors(Options.colors.clrToolHandle, Options.colors.clrToolBlock);
}
//-----------------------------------------------------------------------------
// Purpose: Destructor.
//-----------------------------------------------------------------------------
CToolBlock::~CToolBlock(void)
{
}
//-----------------------------------------------------------------------------
// Purpose: Handles key down events in the 2D view.
// Input : Per CWnd::OnKeyDown.
// Output : Returns true if the message was handled, false if not.
//-----------------------------------------------------------------------------
bool CToolBlock::OnKeyDown2D(CMapView2D *pView, UINT nChar, UINT nRepCnt, UINT nFlags)
{
switch (nChar)
{
case VK_RETURN:
{
if ( !IsEmpty() )
{
CreateMapObject(pView);
}
return true;
}
case VK_ESCAPE:
{
OnEscape();
return true;
}
}
return false;
}
//-----------------------------------------------------------------------------
// Purpose: Handles context menu events in the 2D view.
// Input : Per CWnd::OnContextMenu.
// Output : Returns true if the message was handled, false if not.
//-----------------------------------------------------------------------------
bool CToolBlock::OnContextMenu2D(CMapView2D *pView, UINT nFlags, const Vector2D &vPoint)
{
static CMenu menu, menuCreate;
static bool bInit = false;
if (!bInit)
{
bInit = true;
// Create the menu.
menu.LoadMenu(IDR_POPUPS);
menuCreate.Attach(::GetSubMenu(menu.m_hMenu, 1));
// Create the window that handles menu messages.
s_wndToolMessage.Create();
}
if ( !pView->PointInClientRect(vPoint) )
{
return false;
}
if ( !IsEmpty() )
{
if ( HitTest(pView, vPoint, false) )
{
CPoint ptScreen( vPoint.x,vPoint.y);
pView->ClientToScreen(&ptScreen);
s_wndToolMessage.PreMenu2D(this, pView);
menuCreate.TrackPopupMenu(TPM_LEFTBUTTON | TPM_RIGHTBUTTON | TPM_LEFTALIGN, ptScreen.x, ptScreen.y, &s_wndToolMessage);
}
}
return true;
}
//-----------------------------------------------------------------------------
// 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 CToolBlock::OnLMouseDown3D(CMapView3D *pView, UINT nFlags, const Vector2D &vPoint)
{
Tool3D::OnLMouseDown3D(pView, nFlags, vPoint);
//
// If we have a box, see if they clicked on any part of it.
//
if ( !IsEmpty() )
{
if ( HitTest( pView, vPoint, true ) )
{
// just update current block
StartTranslation( pView, vPoint, m_LastHitTestHandle );
return true;
}
}
return true;
}
//-----------------------------------------------------------------------------
// 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 CToolBlock::OnLMouseDown2D(CMapView2D *pView, UINT nFlags, const Vector2D &vPoint)
{
Tool3D::OnLMouseDown2D(pView, nFlags, vPoint);
// If we have a box, see if they clicked on any part of it.
if ( !IsEmpty() )
{
if ( HitTest( pView, vPoint, true ) )
{
// just update current block
StartTranslation( pView, vPoint, m_LastHitTestHandle );
return 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 CToolBlock::OnLMouseUp2D(CMapView2D *pView, UINT nFlags, const Vector2D &vPoint)
{
Tool3D::OnLMouseUp2D(pView, nFlags, vPoint);
if (IsTranslating())
{
FinishTranslation(true);
}
m_pDocument->UpdateStatusbar();
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 CToolBlock::OnLMouseUp3D(CMapView3D *pView, UINT nFlags, const Vector2D &vPoint)
{
Tool3D::OnLMouseUp3D(pView, nFlags, vPoint);
if (IsTranslating())
{
FinishTranslation(true);
}
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 CToolBlock::OnMouseMove2D(CMapView2D *pView, UINT nFlags, const Vector2D &vPoint)
{
vgui::HCursor hCursor = vgui::dc_arrow;
// Snap it to the grid.
unsigned int uConstraints = GetConstraints( nFlags );
Tool3D::OnMouseMove2D(pView, nFlags, vPoint);
//
//
// Convert to world coords.
//
Vector vecWorld;
pView->ClientToWorld(vecWorld, vPoint);
//
// Update status bar position display.
//
char szBuf[128];
if ( uConstraints & constrainSnap )
m_pDocument->Snap(vecWorld,uConstraints);
sprintf(szBuf, " @%.0f, %.0f ", vecWorld[pView->axHorz], vecWorld[pView->axVert]);
SetStatusText(SBI_COORDS, szBuf);
if ( IsTranslating() )
{
Tool3D::UpdateTranslation( pView, vPoint, uConstraints);
hCursor = vgui::dc_none;
}
else if ( m_bMouseDragged[MOUSE_LEFT] )
{
// Starting a new box. Build a starting point for the drag.
pView->SetCursor( "Resource/block.cur" );
Vector vecStart;
pView->ClientToWorld(vecStart, m_vMouseStart[MOUSE_LEFT] );
// Snap it to the grid.
if ( uConstraints & constrainSnap )
m_pDocument->Snap( vecStart, uConstraints);
// Start the new box with the extents of the last selected thing.
m_pDocument->GetSelection()->GetLastValidBounds(bmins, bmaxs);
vecStart[pView->axThird] = bmins[pView->axThird];
StartNew(pView, vPoint, vecStart, pView->GetViewAxis() * (bmaxs-bmins) );
}
else if ( !IsEmpty() )
{
// If the cursor is on a handle, set it to a cross.
if ( HitTest(pView, vPoint, true) )
{
hCursor = UpdateCursor( pView, m_LastHitTestHandle, m_TranslateMode );
}
}
if ( hCursor != vgui::dc_none )
pView->SetCursor( hCursor );
return true;
}
bool CToolBlock::OnMouseMove3D(CMapView3D *pView, UINT nFlags, const Vector2D &vPoint)
{
Tool3D::OnMouseMove3D(pView, nFlags, vPoint);
if ( IsTranslating() )
{
unsigned int uConstraints = GetConstraints( nFlags );
// If they are dragging with a valid handle, update the views.
Tool3D::UpdateTranslation( pView, vPoint, uConstraints );
}
return true;
}
//-----------------------------------------------------------------------------
// Purpose: Handles key down events in the 3D view.
// Input : Per CWnd::OnKeyDown.
// Output : Returns true if the message was handled, false if not.
//-----------------------------------------------------------------------------
bool CToolBlock::OnKeyDown3D(CMapView3D *pView, UINT nChar, UINT nRepCnt, UINT nFlags)
{
switch (nChar)
{
case VK_RETURN:
{
if ( !IsEmpty() )
{
CreateMapObject(pView);
}
return true;
}
case VK_ESCAPE:
{
OnEscape();
return true;
}
}
return false;
}
//-----------------------------------------------------------------------------
// Purpose: Handles the escape key in the 2D or 3D views.
//-----------------------------------------------------------------------------
void CToolBlock::OnEscape(void)
{
//
// If we have nothing blocked, go back to the pointer tool.
//
if ( IsEmpty() )
{
ToolManager()->SetTool(TOOL_POINTER);
}
//
// We're blocking out a region, so clear it.
//
else
{
SetEmpty();
}
}
//-----------------------------------------------------------------------------
// Purpose: Creates a solid in the given view.
//-----------------------------------------------------------------------------
void CToolBlock::CreateMapObject(CMapView *pView)
{
CMapWorld *pWorld = m_pDocument->GetMapWorld();
if (!(bmaxs[0] - bmins[0]) || !(bmaxs[1] - bmins[1]) || !(bmaxs[2] - bmins[2]))
{
AfxMessageBox("The box is empty.");
SetEmpty();
return;
}
BoundBox NewBox = *this;
if (Options.view2d.bOrientPrimitives)
{
switch (pView->GetDrawType())
{
case VIEW2D_XY:
{
break;
}
case VIEW2D_YZ:
{
NewBox.Rotate90(AXIS_Y);
break;
}
case VIEW2D_XZ:
{
NewBox.Rotate90(AXIS_X);
break;
}
}
}
// Create the object within the given bounding box.
CMapClass *pObject = GetMainWnd()->m_ObjectBar.CreateInBox(&NewBox, pView);
if (pObject == NULL)
{
SetEmpty();
return;
}
m_pDocument->ExpandObjectKeywords(pObject, pWorld);
GetHistory()->MarkUndoPosition(m_pDocument->GetSelection()->GetList(), "New Object");
m_pDocument->AddObjectToWorld(pObject);
//
// Do we need to rotate this object based on the view we created it in?
//
if (Options.view2d.bOrientPrimitives)
{
Vector center;
pObject->GetBoundsCenter( center );
QAngle angles(0, 0, 0);
switch (pView->GetDrawType())
{
case VIEW2D_XY:
{
break;
}
case VIEW2D_YZ:
{
angles[1] = 90.f;
pObject->TransRotate(center, angles);
break;
}
case VIEW2D_XZ:
{
angles[0] = 90.f;
pObject->TransRotate(center, angles);
break;
}
}
}
GetHistory()->KeepNew(pObject);
// Select the new object.
m_pDocument->SelectObject(pObject, scClear|scSelect|scSaveChanges);
m_pDocument->SetModifiedFlag();
SetEmpty();
ResetBounds();
}