summaryrefslogtreecommitdiff
path: root/hammer/ToolPointHandle.cpp
blob: c7a52b158a1274744c6848e82bbc676f2bee6b86 (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
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
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: 
//
//=============================================================================//

#include "stdafx.h"
#include "History.h"
#include "MainFrm.h"			// FIXME: For ObjectProperties
#include "MapDoc.h"
#include "MapView2D.h"
#include "MapPointHandle.h"
#include "PopupMenus.h"
#include "Render2D.h"
#include "StatusBarIDs.h"		// For SetStatusText
#include "ToolManager.h"
#include "ToolPointHandle.h"
#include "Selection.h"

// memdbgon must be the last include file in a .cpp file!!!
#include <tier0/memdbgon.h>


class CToolPointHandleMsgWnd : public CWnd
{
	public:

		bool Create(void);
		void PreMenu2D(CToolPointHandle *pTool, CMapView2D *pView);

	protected:

		//{{AFX_MSG_MAP(CToolPointHandleMsgWnd)
		afx_msg void OnCenter();
		//}}AFX_MSG
	
		DECLARE_MESSAGE_MAP()

	private:

		CToolPointHandle *m_pToolPointHandle;
		CMapView2D *m_pView2D;
};


static CToolPointHandleMsgWnd s_wndToolMessage;
static const char *g_pszClassName = "ValveEditor_PointHandleToolWnd";


BEGIN_MESSAGE_MAP(CToolPointHandleMsgWnd, CWnd)
	//{{AFX_MSG_MAP(CToolPointHandleMsgWnd)
	ON_COMMAND(ID_CENTER_ON_ENTITY, OnCenter)
	//}}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 CToolPointHandleMsgWnd::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 tool to this window before activating the context menu.
//-----------------------------------------------------------------------------
void CToolPointHandleMsgWnd::PreMenu2D(CToolPointHandle *pToolPointHandle, CMapView2D *pView)
{
	Assert(pToolPointHandle != NULL);
	m_pToolPointHandle = pToolPointHandle;
	m_pView2D = pView;
}


//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CToolPointHandleMsgWnd::OnCenter()
{
	if (m_pToolPointHandle)
	{
		m_pToolPointHandle->CenterOnParent(m_pView2D);
	}
}


//-----------------------------------------------------------------------------
// Purpose: Constructor.
//-----------------------------------------------------------------------------
CToolPointHandle::CToolPointHandle(void)
{
	m_pPoint = NULL;
}


//-----------------------------------------------------------------------------
// Purpose: Attaches the point to the tool for manipulation.
//-----------------------------------------------------------------------------
void CToolPointHandle::Attach(CMapPointHandle *pPoint)
{
	m_pPoint = pPoint;
}


//-----------------------------------------------------------------------------
// Purpose: Handles left button down events in the 2D view.
// Input  : Per CWnd::OnLButtonDown.
// Output : Returns true if the message was handled, false if not.
//-----------------------------------------------------------------------------
bool CToolPointHandle::OnLMouseDown2D(CMapView2D *pView, UINT nFlags, const Vector2D &VPoint)
{
	//
	// Activate this tool and start dragging the axis endpoint.
	//
	ToolManager()->PushTool(TOOL_POINT_HANDLE);
	pView->SetCapture();

	GetHistory()->MarkUndoPosition( m_pDocument->GetSelection()->GetList(), "Modify Origin");
	GetHistory()->Keep(m_pPoint);
	return true;
}


//-----------------------------------------------------------------------------
// Purpose: Handles left button up events in the 2D view.
// Input  : Per CWnd::OnLButtonUp.
// Output : Returns true if the message was handled, false if not.
//-----------------------------------------------------------------------------
bool CToolPointHandle::OnLMouseUp2D(CMapView2D *pView, UINT nFlags, const Vector2D &VPoint)
{
	m_pPoint->UpdateOrigin(m_pPoint->m_Origin);

	ToolManager()->PopTool();
	ReleaseCapture();

	m_pDocument->UpdateAllViews( MAPVIEW_UPDATE_TOOL );
	
	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 CToolPointHandle::OnMouseMove2D(CMapView2D *pView, UINT nFlags, const Vector2D &vPoint)
{
	//
	// Make sure the point is visible.
	//
	pView->ToolScrollToPoint( vPoint );

	//
	// Snap the point to half the grid size. Do this so that we can always center
	// the origin even on odd-width objects.
	//
	Vector vecWorld;
	pView->ClientToWorld(vecWorld, vPoint);

	m_pDocument->Snap(vecWorld, constrainHalfSnap);

	//
	// Move to the snapped position.
	//
	m_pPoint->m_Origin[pView->axHorz] = vecWorld[pView->axHorz];
	m_pPoint->m_Origin[pView->axVert] = vecWorld[pView->axVert];
	
	//
	// Update the status bar and the views.
	//
	char szBuf[128];
	sprintf(szBuf, " @%.0f, %.0f ", m_pPoint->m_Origin[pView->axHorz], m_pPoint->m_Origin[pView->axVert]);
	SetStatusText(SBI_COORDS, szBuf);

	m_pDocument->UpdateAllViews( MAPVIEW_UPDATE_TOOL );

	return true;
}


//-----------------------------------------------------------------------------
// Purpose: Renders the tool in the 2D view.
// Input  : pRender - The interface to use for rendering.
//-----------------------------------------------------------------------------
void CToolPointHandle::RenderTool2D(CRender2D *pRender)
{
	SelectionState_t eState = m_pPoint->SetSelectionState(SELECT_MODIFY);
	m_pPoint->Render2D(pRender);
	m_pPoint->SetSelectionState(eState);
}


//-----------------------------------------------------------------------------
// Purpose: 
// Input  : *pView - 
//			point - 
// Output : Returns true on success, false on failure.
//-----------------------------------------------------------------------------
bool CToolPointHandle::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, IDM_POPUP_POINT_HANDLE));

		// Create the window that handles menu messages.
		s_wndToolMessage.Create();
	}

	if (!pView->PointInClientRect(vPoint) )
	{
		return 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: 
//-----------------------------------------------------------------------------
void CToolPointHandle::CenterOnParent(CMapView *pView)
{
	if (m_pPoint)
	{
		GetHistory()->MarkUndoPosition(m_pDocument->GetSelection()->GetList(), "Center Origin");
		GetHistory()->Keep(m_pPoint);

		CMapClass *pParent = m_pPoint->GetParent();

		Vector vecCenter;
		pParent->GetBoundsCenter(vecCenter);
		m_pPoint->UpdateOrigin(vecCenter);
		m_pDocument->UpdateAllViews( MAPVIEW_UPDATE_TOOL );
	}
}