Counter Strike : Global Offensive Source Code
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.

269 lines
7.6 KiB

  1. //========= Copyright � 1996-2005, 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->SetModifiedFlag();
  119. m_pDocument->UpdateAllViews( MAPVIEW_UPDATE_TOOL );
  120. return true;
  121. }
  122. //-----------------------------------------------------------------------------
  123. // Purpose: Handles mouse move events in the 2D view.
  124. // Input : Per CWnd::OnMouseMove.
  125. // Output : Returns true if the message was handled, false if not.
  126. //-----------------------------------------------------------------------------
  127. bool CToolPointHandle::OnMouseMove2D(CMapView2D *pView, UINT nFlags, const Vector2D &vPoint)
  128. {
  129. //
  130. // Make sure the point is visible.
  131. //
  132. pView->ToolScrollToPoint( vPoint );
  133. //
  134. // Snap the point to half the grid size. Do this so that we can always center
  135. // the origin even on odd-width objects.
  136. //
  137. Vector vecWorld;
  138. pView->ClientToWorld(vecWorld, vPoint);
  139. m_pDocument->Snap(vecWorld, constrainHalfSnap);
  140. //
  141. // Move to the snapped position.
  142. //
  143. m_pPoint->m_Origin[pView->axHorz] = vecWorld[pView->axHorz];
  144. m_pPoint->m_Origin[pView->axVert] = vecWorld[pView->axVert];
  145. //
  146. // Update the status bar and the views.
  147. //
  148. char szBuf[128];
  149. sprintf(szBuf, " @%.0f, %.0f ", m_pPoint->m_Origin[pView->axHorz], m_pPoint->m_Origin[pView->axVert]);
  150. SetStatusText(SBI_COORDS, szBuf);
  151. m_pDocument->UpdateAllViews( MAPVIEW_UPDATE_TOOL );
  152. return true;
  153. }
  154. //-----------------------------------------------------------------------------
  155. // Purpose: Renders the tool in the 2D view.
  156. // Input : pRender - The interface to use for rendering.
  157. //-----------------------------------------------------------------------------
  158. void CToolPointHandle::RenderTool2D(CRender2D *pRender)
  159. {
  160. SelectionState_t eState = m_pPoint->SetSelectionState(SELECT_MODIFY);
  161. m_pPoint->Render2D(pRender);
  162. m_pPoint->SetSelectionState(eState);
  163. }
  164. //-----------------------------------------------------------------------------
  165. // Purpose:
  166. // Input : *pView -
  167. // point -
  168. // Output : Returns true on success, false on failure.
  169. //-----------------------------------------------------------------------------
  170. bool CToolPointHandle::OnContextMenu2D(CMapView2D *pView, UINT nFlags, const Vector2D &vPoint)
  171. {
  172. static CMenu menu, menuCreate;
  173. static bool bInit = false;
  174. if (!bInit)
  175. {
  176. bInit = true;
  177. // Create the menu.
  178. menu.LoadMenu(IDR_POPUPS);
  179. menuCreate.Attach(::GetSubMenu(menu.m_hMenu, IDM_POPUP_POINT_HANDLE));
  180. // Create the window that handles menu messages.
  181. s_wndToolMessage.Create();
  182. }
  183. if (!pView->PointInClientRect(vPoint) )
  184. {
  185. return false;
  186. }
  187. CPoint ptScreen( vPoint.x,vPoint.y);
  188. pView->ClientToScreen(&ptScreen);
  189. s_wndToolMessage.PreMenu2D(this, pView);
  190. menuCreate.TrackPopupMenu(TPM_LEFTBUTTON | TPM_RIGHTBUTTON | TPM_LEFTALIGN, ptScreen.x, ptScreen.y, &s_wndToolMessage);
  191. return true;
  192. }
  193. //-----------------------------------------------------------------------------
  194. // Purpose:
  195. //-----------------------------------------------------------------------------
  196. void CToolPointHandle::CenterOnParent(CMapView *pView)
  197. {
  198. if (m_pPoint)
  199. {
  200. GetHistory()->MarkUndoPosition(m_pDocument->GetSelection()->GetList(), "Center Origin");
  201. GetHistory()->Keep(m_pPoint);
  202. CMapClass *pParent = m_pPoint->GetParent();
  203. Vector vecCenter;
  204. pParent->GetBoundsCenter(vecCenter);
  205. m_pPoint->UpdateOrigin(vecCenter);
  206. m_pDocument->SetModifiedFlag();
  207. m_pDocument->UpdateAllViews( MAPVIEW_UPDATE_TOOL );
  208. }
  209. }