Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

265 lines
7.5 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================//
  6. #include "stdafx.h"
  7. #include "History.h"
  8. #include "MainFrm.h" // FIXME: For ObjectProperties
  9. #include "MapDoc.h"
  10. #include "MapView2D.h"
  11. #include "MapPointHandle.h"
  12. #include "PopupMenus.h"
  13. #include "Render2D.h"
  14. #include "StatusBarIDs.h" // For SetStatusText
  15. #include "ToolManager.h"
  16. #include "ToolPointHandle.h"
  17. #include "Selection.h"
  18. // memdbgon must be the last include file in a .cpp file!!!
  19. #include <tier0/memdbgon.h>
  20. class CToolPointHandleMsgWnd : public CWnd
  21. {
  22. public:
  23. bool Create(void);
  24. void PreMenu2D(CToolPointHandle *pTool, CMapView2D *pView);
  25. protected:
  26. //{{AFX_MSG_MAP(CToolPointHandleMsgWnd)
  27. afx_msg void OnCenter();
  28. //}}AFX_MSG
  29. DECLARE_MESSAGE_MAP()
  30. private:
  31. CToolPointHandle *m_pToolPointHandle;
  32. CMapView2D *m_pView2D;
  33. };
  34. static CToolPointHandleMsgWnd s_wndToolMessage;
  35. static const char *g_pszClassName = "ValveEditor_PointHandleToolWnd";
  36. BEGIN_MESSAGE_MAP(CToolPointHandleMsgWnd, CWnd)
  37. //{{AFX_MSG_MAP(CToolPointHandleMsgWnd)
  38. ON_COMMAND(ID_CENTER_ON_ENTITY, OnCenter)
  39. //}}AFX_MSG_MAP
  40. END_MESSAGE_MAP()
  41. //-----------------------------------------------------------------------------
  42. // Purpose: Creates the hidden window that receives context menu commands for the
  43. // block tool.
  44. // Output : Returns true on success, false on failure.
  45. //-----------------------------------------------------------------------------
  46. bool CToolPointHandleMsgWnd::Create(void)
  47. {
  48. WNDCLASS wndcls;
  49. memset(&wndcls, 0, sizeof(WNDCLASS));
  50. wndcls.lpfnWndProc = AfxWndProc;
  51. wndcls.hInstance = AfxGetInstanceHandle();
  52. wndcls.lpszClassName = g_pszClassName;
  53. if (!AfxRegisterClass(&wndcls))
  54. {
  55. return(false);
  56. }
  57. return(CWnd::CreateEx(0, g_pszClassName, g_pszClassName, 0, CRect(0, 0, 10, 10), NULL, 0) == TRUE);
  58. }
  59. //-----------------------------------------------------------------------------
  60. // Purpose: Attaches the tool to this window before activating the context menu.
  61. //-----------------------------------------------------------------------------
  62. void CToolPointHandleMsgWnd::PreMenu2D(CToolPointHandle *pToolPointHandle, CMapView2D *pView)
  63. {
  64. Assert(pToolPointHandle != NULL);
  65. m_pToolPointHandle = pToolPointHandle;
  66. m_pView2D = pView;
  67. }
  68. //-----------------------------------------------------------------------------
  69. // Purpose:
  70. //-----------------------------------------------------------------------------
  71. void CToolPointHandleMsgWnd::OnCenter()
  72. {
  73. if (m_pToolPointHandle)
  74. {
  75. m_pToolPointHandle->CenterOnParent(m_pView2D);
  76. }
  77. }
  78. //-----------------------------------------------------------------------------
  79. // Purpose: Constructor.
  80. //-----------------------------------------------------------------------------
  81. CToolPointHandle::CToolPointHandle(void)
  82. {
  83. m_pPoint = NULL;
  84. }
  85. //-----------------------------------------------------------------------------
  86. // Purpose: Attaches the point to the tool for manipulation.
  87. //-----------------------------------------------------------------------------
  88. void CToolPointHandle::Attach(CMapPointHandle *pPoint)
  89. {
  90. m_pPoint = pPoint;
  91. }
  92. //-----------------------------------------------------------------------------
  93. // Purpose: Handles left button down events in the 2D view.
  94. // Input : Per CWnd::OnLButtonDown.
  95. // Output : Returns true if the message was handled, false if not.
  96. //-----------------------------------------------------------------------------
  97. bool CToolPointHandle::OnLMouseDown2D(CMapView2D *pView, UINT nFlags, const Vector2D &VPoint)
  98. {
  99. //
  100. // Activate this tool and start dragging the axis endpoint.
  101. //
  102. ToolManager()->PushTool(TOOL_POINT_HANDLE);
  103. pView->SetCapture();
  104. GetHistory()->MarkUndoPosition( m_pDocument->GetSelection()->GetList(), "Modify Origin");
  105. GetHistory()->Keep(m_pPoint);
  106. return true;
  107. }
  108. //-----------------------------------------------------------------------------
  109. // Purpose: Handles left button up events in the 2D view.
  110. // Input : Per CWnd::OnLButtonUp.
  111. // Output : Returns true if the message was handled, false if not.
  112. //-----------------------------------------------------------------------------
  113. bool CToolPointHandle::OnLMouseUp2D(CMapView2D *pView, UINT nFlags, const Vector2D &VPoint)
  114. {
  115. m_pPoint->UpdateOrigin(m_pPoint->m_Origin);
  116. ToolManager()->PopTool();
  117. ReleaseCapture();
  118. m_pDocument->UpdateAllViews( MAPVIEW_UPDATE_TOOL );
  119. return true;
  120. }
  121. //-----------------------------------------------------------------------------
  122. // Purpose: Handles mouse move events in the 2D view.
  123. // Input : Per CWnd::OnMouseMove.
  124. // Output : Returns true if the message was handled, false if not.
  125. //-----------------------------------------------------------------------------
  126. bool CToolPointHandle::OnMouseMove2D(CMapView2D *pView, UINT nFlags, const Vector2D &vPoint)
  127. {
  128. //
  129. // Make sure the point is visible.
  130. //
  131. pView->ToolScrollToPoint( vPoint );
  132. //
  133. // Snap the point to half the grid size. Do this so that we can always center
  134. // the origin even on odd-width objects.
  135. //
  136. Vector vecWorld;
  137. pView->ClientToWorld(vecWorld, vPoint);
  138. m_pDocument->Snap(vecWorld, constrainHalfSnap);
  139. //
  140. // Move to the snapped position.
  141. //
  142. m_pPoint->m_Origin[pView->axHorz] = vecWorld[pView->axHorz];
  143. m_pPoint->m_Origin[pView->axVert] = vecWorld[pView->axVert];
  144. //
  145. // Update the status bar and the views.
  146. //
  147. char szBuf[128];
  148. sprintf(szBuf, " @%.0f, %.0f ", m_pPoint->m_Origin[pView->axHorz], m_pPoint->m_Origin[pView->axVert]);
  149. SetStatusText(SBI_COORDS, szBuf);
  150. m_pDocument->UpdateAllViews( MAPVIEW_UPDATE_TOOL );
  151. return true;
  152. }
  153. //-----------------------------------------------------------------------------
  154. // Purpose: Renders the tool in the 2D view.
  155. // Input : pRender - The interface to use for rendering.
  156. //-----------------------------------------------------------------------------
  157. void CToolPointHandle::RenderTool2D(CRender2D *pRender)
  158. {
  159. SelectionState_t eState = m_pPoint->SetSelectionState(SELECT_MODIFY);
  160. m_pPoint->Render2D(pRender);
  161. m_pPoint->SetSelectionState(eState);
  162. }
  163. //-----------------------------------------------------------------------------
  164. // Purpose:
  165. // Input : *pView -
  166. // point -
  167. // Output : Returns true on success, false on failure.
  168. //-----------------------------------------------------------------------------
  169. bool CToolPointHandle::OnContextMenu2D(CMapView2D *pView, UINT nFlags, const Vector2D &vPoint)
  170. {
  171. static CMenu menu, menuCreate;
  172. static bool bInit = false;
  173. if (!bInit)
  174. {
  175. bInit = true;
  176. // Create the menu.
  177. menu.LoadMenu(IDR_POPUPS);
  178. menuCreate.Attach(::GetSubMenu(menu.m_hMenu, IDM_POPUP_POINT_HANDLE));
  179. // Create the window that handles menu messages.
  180. s_wndToolMessage.Create();
  181. }
  182. if (!pView->PointInClientRect(vPoint) )
  183. {
  184. return false;
  185. }
  186. CPoint ptScreen( vPoint.x,vPoint.y);
  187. pView->ClientToScreen(&ptScreen);
  188. s_wndToolMessage.PreMenu2D(this, pView);
  189. menuCreate.TrackPopupMenu(TPM_LEFTBUTTON | TPM_RIGHTBUTTON | TPM_LEFTALIGN, ptScreen.x, ptScreen.y, &s_wndToolMessage);
  190. return true;
  191. }
  192. //-----------------------------------------------------------------------------
  193. // Purpose:
  194. //-----------------------------------------------------------------------------
  195. void CToolPointHandle::CenterOnParent(CMapView *pView)
  196. {
  197. if (m_pPoint)
  198. {
  199. GetHistory()->MarkUndoPosition(m_pDocument->GetSelection()->GetList(), "Center Origin");
  200. GetHistory()->Keep(m_pPoint);
  201. CMapClass *pParent = m_pPoint->GetParent();
  202. Vector vecCenter;
  203. pParent->GetBoundsCenter(vecCenter);
  204. m_pPoint->UpdateOrigin(vecCenter);
  205. m_pDocument->UpdateAllViews( MAPVIEW_UPDATE_TOOL );
  206. }
  207. }