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.

1175 lines
27 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include "stdafx.h"
  8. #include "Box3D.h"
  9. #include "GlobalFunctions.h"
  10. #include "MainFrm.h" // dvs: hack for tools migration code
  11. #include "MapDoc.h"
  12. #include "MapView2D.h"
  13. #include "Options.h"
  14. #include "Render2D.h"
  15. #include "Render3D.h"
  16. #include "RenderUtils.h"
  17. #include "resource.h"
  18. #include "StatusBarIDs.h"
  19. #include "hammer_mathlib.h"
  20. #include "vgui/Cursor.h"
  21. #include "HammerVGui.h"
  22. #include <VGuiMatSurface/IMatSystemSurface.h>
  23. #include "camera.h"
  24. // memdbgon must be the last include file in a .cpp file!!!
  25. #include <tier0/memdbgon.h>
  26. #pragma warning(disable:4244)
  27. extern float g_MAX_MAP_COORD; // dvs: move these into Globals.h!!
  28. extern float g_MIN_MAP_COORD; // dvs: move these into Globals.h!!
  29. WorldUnits_t Box3D::m_eWorldUnits = Units_None;
  30. //-----------------------------------------------------------------------------
  31. // Purpose:
  32. //-----------------------------------------------------------------------------
  33. Box3D::Box3D(void)
  34. {
  35. SetEmpty();
  36. SetDrawFlags(0);
  37. m_TranslateMode = modeScale;
  38. m_vTranslationFixPoint.Init();
  39. m_TranslateHandle.Init();
  40. m_bEnableHandles = true;
  41. SetDrawColors(Options.colors.clrToolHandle, Options.colors.clrToolBlock);
  42. }
  43. void Box3D::SetEmpty()
  44. {
  45. Tool3D::SetEmpty();
  46. ResetBounds();
  47. if ( m_pDocument )
  48. {
  49. m_pDocument->UpdateAllViews( MAPVIEW_UPDATE_TOOL );
  50. }
  51. }
  52. //-----------------------------------------------------------------------------
  53. // Purpose:
  54. // Input : pView -
  55. // pt - Point in client coordinates.
  56. // bValidOnly -
  57. // Output :
  58. //-----------------------------------------------------------------------------
  59. int Box3D::HitTest(CMapView *pView, const Vector2D &ptClient, bool bTestHandles)
  60. {
  61. bool bHit = false;
  62. if ( pView->HitTest( ptClient, bmins, bmaxs ) )
  63. {
  64. // The point is inside the main rect.
  65. m_LastHitTestHandle.Init();
  66. bHit = true;
  67. }
  68. if ( !m_bEnableHandles || !bTestHandles )
  69. {
  70. // Handles are turned off, so we don't need to do any more testing.
  71. // Return whether we hit the main rect or not.
  72. return bHit;
  73. }
  74. // check if we hit a handle
  75. Vector handles[3*3*3];
  76. int numHandles = GetVisibleHandles( handles, pView, m_TranslateMode );
  77. Vector vOffset(HANDLE_OFFSET,HANDLE_OFFSET,HANDLE_OFFSET);
  78. if ( pView->IsOrthographic() )
  79. {
  80. vOffset /= pView->GetCamera()->GetZoom();
  81. }
  82. else
  83. {
  84. vOffset.Init();
  85. }
  86. Vector vCenter = (bmins+bmaxs)/2;
  87. Vector vDelta = (bmaxs + vOffset) - vCenter;
  88. for ( int i = 0; i<numHandles; i++ )
  89. {
  90. Vector pos = vCenter + vDelta * handles[i];
  91. if ( HitRect( pView, ptClient, pos, HANDLE_RADIUS ) )
  92. {
  93. // remember handle found
  94. m_LastHitTestHandle = handles[i];
  95. bHit = true;
  96. break;
  97. }
  98. }
  99. return bHit;
  100. }
  101. //-----------------------------------------------------------------------------
  102. // Purpose: Set the cursor based on the hit test results and current translate mode.
  103. // Input : eHandleHit - The handle that the cursor is over.
  104. // eTransformMode - The current transform mode of the tool - scale, rotate, or shear.
  105. //-----------------------------------------------------------------------------
  106. unsigned long Box3D::UpdateCursor(CMapView *pView, const Vector &vHandleHit, TransformMode_t eTransformMode)
  107. {
  108. if ( eTransformMode == modeMove || vHandleHit.IsZero() )
  109. return vgui::dc_sizeall;
  110. if ( eTransformMode == modeNone )
  111. return vgui::dc_arrow;
  112. if (eTransformMode == modeRotate)
  113. return g_pMatSystemSurface->CreateCursorFromFile("Resource/rotate.cur");
  114. // cursor icon depends on handle and map view :
  115. Vector2D ptOrigin; pView->WorldToClient( ptOrigin, Vector(0,0,0) );
  116. Vector2D ptHit; pView->WorldToClient( ptHit, vHandleHit );
  117. Vector2D pt; pt.x = ptHit.x - ptOrigin.x; pt.y = ptHit.y - ptOrigin.y;
  118. if (eTransformMode == modeScale)
  119. {
  120. if ( pt.x > 0 )
  121. {
  122. if ( pt.y > 0 )
  123. return vgui::dc_sizenwse;
  124. else if ( pt.y < 0 )
  125. return vgui::dc_sizenesw;
  126. else
  127. return vgui::dc_sizewe;
  128. }
  129. else if ( pt.x < 0 )
  130. {
  131. if ( pt.y > 0 )
  132. return vgui::dc_sizenesw;
  133. else if ( pt.y < 0 )
  134. return vgui::dc_sizenwse;
  135. else
  136. return vgui::dc_sizewe;
  137. }
  138. else // pt.x == 0
  139. {
  140. if ( pt.y != 0 )
  141. return vgui::dc_sizens;
  142. else
  143. return vgui::dc_sizeall;
  144. }
  145. }
  146. else if (eTransformMode == modeShear)
  147. {
  148. if ( pt.x == 0 )
  149. return vgui::dc_sizewe;
  150. else
  151. return vgui::dc_sizens;
  152. }
  153. return vgui::dc_none;
  154. }
  155. //-----------------------------------------------------------------------------
  156. // Purpose:
  157. // Input : bEnable -
  158. //-----------------------------------------------------------------------------
  159. void Box3D::EnableHandles(bool bEnable)
  160. {
  161. m_bEnableHandles = bEnable;
  162. }
  163. //-----------------------------------------------------------------------------
  164. // Purpose: Finds the corner nearest to a given point in world coordinates.
  165. // Output : Returns the corner in world coordinates (axThird is always 0).
  166. //-----------------------------------------------------------------------------
  167. const Vector Box3D::NearestCorner( const Vector2D &vPoint, CMapView *pView, const Vector *pCustomHandleBox )
  168. {
  169. Vector vHandles[3*3*3];
  170. float fBestDist = 999999.9f;
  171. Vector vBestCorner(0,0,0);
  172. int nFace = -1;
  173. Vector start,end,pos;
  174. pView->BuildRay( vPoint, start,end );
  175. float dist = IntersectionLineAABBox( bmins, bmaxs, start, end, nFace );
  176. if ( dist < 0 )
  177. return vBestCorner;
  178. // get point where we hit the bbox
  179. pos = end-start; VectorNormalize( pos );
  180. pos = start + pos*dist;
  181. // mode rotate has only corner handles
  182. int nNumHandles = GetVisibleHandles( vHandles, pView, modeRotate );
  183. for ( int i=0; i<nNumHandles; i++ )
  184. {
  185. Vector vecCorner;
  186. HandleToWorld( vecCorner, vHandles[i], pCustomHandleBox );
  187. float distance = VectorLength( vecCorner - pos );
  188. if ( distance < fBestDist )
  189. {
  190. fBestDist = distance;
  191. vBestCorner = vecCorner;
  192. }
  193. }
  194. return vBestCorner;
  195. }
  196. //-----------------------------------------------------------------------------
  197. // Purpose:
  198. // Input : pt -
  199. // ptForceMoveRef -
  200. // Output : Returns TRUE if pt hits a handle or is in box area, FALSE otherwise.
  201. //-----------------------------------------------------------------------------
  202. void Box3D::StartTranslation(
  203. CMapView *pView,
  204. const Vector2D &vPoint,
  205. const Vector &vHandleOrigin,
  206. const Vector *pRefPoint,
  207. const Vector *pCustomHandleBox )
  208. {
  209. if ( vHandleOrigin.IsZero() )
  210. {
  211. // we hit the main body, switch to move translation then
  212. m_LastTranslateMode = m_TranslateMode;
  213. m_TranslateMode = modeMove;
  214. }
  215. m_TranslateHandle = vHandleOrigin;
  216. m_bPreventOverlap = true;
  217. if ( pRefPoint )
  218. {
  219. // transformation reference point was given
  220. m_vTranslationFixPoint = *pRefPoint;
  221. }
  222. else
  223. {
  224. // build reference point based on mode & handle
  225. if (m_TranslateMode == modeRotate)
  226. {
  227. // user center of object for rotation
  228. m_vTranslationFixPoint = (bmins + bmaxs) / 2;
  229. }
  230. else if (m_TranslateMode == modeMove)
  231. {
  232. // chose nearest corner to
  233. m_vTranslationFixPoint = NearestCorner( vPoint, pView, pCustomHandleBox );
  234. }
  235. else
  236. {
  237. // find opposite point to handle
  238. m_vTranslationFixPoint.Init();
  239. for ( int i=0; i<3; i++ )
  240. {
  241. float handle = m_TranslateHandle[i];
  242. if ( handle > 0 )
  243. {
  244. m_vTranslationFixPoint[i] = bmins[i];
  245. }
  246. else if ( handle < 0 )
  247. {
  248. m_vTranslationFixPoint[i] = bmaxs[i];
  249. }
  250. }
  251. }
  252. }
  253. // get axis normals from picked face
  254. Vector v1,v2,v3,vOrigin;
  255. // if no valid translation handle, cull against BBox
  256. if ( m_TranslateMode == modeMove )
  257. {
  258. int nFace;
  259. pView->BuildRay( vPoint, v1, v2 );
  260. IntersectionLineAABBox( bmins, bmaxs, v1, v2, nFace );
  261. if ( nFace >= 0 )
  262. {
  263. // get axis & normals of face we hit
  264. GetAxisFromFace( nFace, v1, v2, v3 );
  265. }
  266. else
  267. {
  268. pView->GetBestTransformPlane( v1,v2,v3 );
  269. }
  270. vOrigin = m_vTranslationFixPoint;
  271. }
  272. else
  273. {
  274. pView->GetBestTransformPlane( v1,v2,v3 );
  275. HandleToWorld( vOrigin, m_TranslateHandle );
  276. }
  277. // set temp transformation plane
  278. SetTransformationPlane(vOrigin, v1, v2, v3 );
  279. // align translation plane to world origin
  280. ProjectOnTranslationPlane( vec3_origin, vOrigin, 0 );
  281. // set transformation plane
  282. SetTransformationPlane(vOrigin, v1, v2, v3 );
  283. Tool3D::StartTranslation( pView, vPoint, false );
  284. m_TransformMatrix.Identity();
  285. }
  286. //-----------------------------------------------------------------------------
  287. // Purpose:
  288. // Input : pszBuf -
  289. //-----------------------------------------------------------------------------
  290. void Box3D::GetStatusString(char *pszBuf)
  291. {
  292. *pszBuf = '\0';
  293. Vector mins(0,0,0);
  294. Vector maxs(0,0,0);
  295. if ( IsValidBox() )
  296. {
  297. mins = bmins;
  298. maxs = bmaxs;
  299. }
  300. if ( IsTranslating() )
  301. {
  302. TranslateBox( mins, maxs );
  303. }
  304. Vector size = maxs - mins;
  305. Vector center = ( maxs + mins ) * 0.5f;
  306. if ( !IsTranslating() || m_TranslateMode == modeScale || m_TranslateMode == modeMove )
  307. {
  308. if (!IsEmpty())
  309. {
  310. if ( IsTranslating() && m_TranslateMode == modeMove )
  311. {
  312. center = m_vTranslationFixPoint;
  313. TranslatePoint( center );
  314. }
  315. switch (m_eWorldUnits)
  316. {
  317. case Units_None:
  318. {
  319. sprintf(pszBuf, " %dw %dl %dh @(%.0f %.0f %.0f)",
  320. (int)fabs(size.x), (int)fabs(size.y), (int)fabs(size.z),
  321. center.x,center.y,center.z );
  322. break;
  323. }
  324. case Units_Inches:
  325. {
  326. sprintf(pszBuf, " %d\"w %d\"l %d\"h", (int)fabs(size.x), (int)fabs(size.y), (int)fabs(size.z));
  327. break;
  328. }
  329. case Units_Feet_Inches:
  330. {
  331. int nFeetWide = (int)fabs(size.x) / 12;
  332. int nInchesWide = (int)fabs(size.x) % 12;
  333. int nFeetLong = (int)fabs(size.y) / 12;
  334. int nInchesLong = (int)fabs(size.y) % 12;
  335. int nFeetHigh = (int)fabs(size.z) / 12;
  336. int nInchesHigh = (int)fabs(size.z) % 12;
  337. sprintf(pszBuf, " %d' %d\"w %d' %d\"l %d' %d\"h", nFeetWide, nInchesWide, nFeetLong, nInchesLong, nFeetHigh, nInchesHigh);
  338. break;
  339. }
  340. }
  341. }
  342. }
  343. else if ( m_TranslateMode == modeShear )
  344. {
  345. sprintf(pszBuf, " shear: %d %d %d ", (int)m_vTranslation.x, (int)m_vTranslation.y, (int)m_vTranslation.z );
  346. }
  347. else if ( m_TranslateMode == modeRotate )
  348. {
  349. int rotAxis = GetTransformationAxis();
  350. if ( rotAxis != -1 )
  351. {
  352. sprintf(pszBuf, " %.2f%c", m_vTranslation[abs(rotAxis+2)%3], 0xF8);
  353. }
  354. else
  355. {
  356. sprintf(pszBuf, " %.2f %.2f %.2f%c", m_vTranslation.x, m_vTranslation.y, m_vTranslation.z, 0xF8);
  357. }
  358. }
  359. else
  360. {
  361. Assert( 0 );
  362. }
  363. }
  364. //-----------------------------------------------------------------------------
  365. // Purpose:
  366. //-----------------------------------------------------------------------------
  367. void Box3D::UpdateStatusBar()
  368. {
  369. char szBuf[MAX_PATH];
  370. GetStatusString(szBuf);
  371. SetStatusText(SBI_SIZE, szBuf);
  372. }
  373. int Box3D::GetVisibleHandles( Vector *handles, CMapView *pView, int nMode )
  374. {
  375. bool bCorners, bEdges, bFaces;
  376. bool bIs2D = pView->IsOrthographic();
  377. Vector vViewAxis = pView->GetViewAxis();
  378. Vector vViewPoint; pView->GetCamera()->GetViewPoint( vViewPoint );
  379. if ( bIs2D )
  380. {
  381. bCorners = false;
  382. bEdges = nMode == modeRotate || nMode == modeScale;
  383. bFaces = nMode == modeShear || nMode == modeScale;
  384. }
  385. else
  386. {
  387. bCorners = nMode == modeRotate || nMode == modeScale;
  388. bEdges = nMode == modeScale;
  389. bFaces = nMode == modeShear;
  390. }
  391. if ( !bCorners && !bEdges && !bFaces )
  392. return 0;
  393. int count = 0;
  394. for ( int x = -1; x < 2; x++ )
  395. {
  396. if ( bIs2D && (x != 0) && (fabs(vViewAxis.x) == 1) )
  397. continue;
  398. for ( int y = -1; y < 2; y++ )
  399. {
  400. if ( bIs2D && (y != 0) && (fabs(vViewAxis.y) == 1) )
  401. continue;
  402. for ( int z = -1; z<2; z++)
  403. {
  404. if ( bIs2D && (z != 0) && (fabs(vViewAxis.z) == 1) )
  405. continue;
  406. int n = abs(x) + abs(y) + abs(z);
  407. if ( n == 0 )
  408. {
  409. // don't add center as handle
  410. continue;
  411. }
  412. else if ( n == 1 )
  413. {
  414. if ( !bFaces )
  415. continue;
  416. }
  417. else if ( n == 2 )
  418. {
  419. if ( !bEdges )
  420. continue;
  421. }
  422. else
  423. {
  424. if ( !bCorners )
  425. continue;
  426. }
  427. if ( !bIs2D )
  428. {
  429. Vector vHandle; HandleToWorld( vHandle, Vector(x,y,z) );
  430. Vector vDelta = vHandle - vViewPoint;
  431. float fDistance = VectorLength( vDelta );
  432. int nFace;
  433. vDelta /= fDistance; // normalize
  434. if ( DotProduct(vDelta,vViewAxis) < 0 )
  435. continue;
  436. float fIntersection = IntersectionLineAABBox( bmins, bmaxs, vViewPoint, vViewPoint+vDelta*99999, nFace );
  437. if ( fIntersection >= 0 && fIntersection*1.01 < fDistance )
  438. continue;
  439. }
  440. // add handle as visible
  441. handles[count] = Vector(x,y,z);
  442. count++;
  443. }
  444. }
  445. }
  446. return count;
  447. }
  448. //-----------------------------------------------------------------------------
  449. // Purpose:
  450. // Input : ptWorld - point to update with in world coordinates
  451. // uConstraints -
  452. // dragSize -
  453. // Output :
  454. //-----------------------------------------------------------------------------
  455. bool Box3D::UpdateTranslation(const Vector &vUpdate, UINT uConstraints)
  456. {
  457. if (m_TranslateMode == modeNone)
  458. {
  459. return false;
  460. }
  461. else if ( m_TranslateMode == modeRotate )
  462. {
  463. Vector vCenter; ProjectOnTranslationPlane( m_vTranslationFixPoint, vCenter );
  464. Vector vStart; HandleToWorld( vStart, m_TranslateHandle );
  465. Vector v1 = vStart-vCenter; VectorNormalize( v1 );
  466. Vector v2 = (vStart+vUpdate)-vCenter; VectorNormalize( v2 );
  467. float volume = DotProduct( m_vPlaneNormal, CrossProduct( v1, v2) );
  468. float angle = RAD2DEG( acos( DotProduct( v1,v2) ) );
  469. if (uConstraints & constrainSnap)
  470. {
  471. angle += 7.5;
  472. angle -= fmod(double(angle), double(15.0));
  473. }
  474. else
  475. {
  476. angle += 0.25;
  477. angle -= fmod(double(angle), double(.5));
  478. }
  479. if ( volume < 0 )
  480. angle = -angle;
  481. if ( fabs(m_vPlaneNormal.x) == 1 )
  482. m_vTranslation.z = (m_vPlaneNormal.x>0)?angle:-angle;
  483. else if ( fabs(m_vPlaneNormal.y) == 1 )
  484. m_vTranslation.x = (m_vPlaneNormal.y>0)?angle:-angle;
  485. else if ( fabs(m_vPlaneNormal.z) == 1 )
  486. m_vTranslation.y = (m_vPlaneNormal.z>0)?angle:-angle;
  487. }
  488. else
  489. {
  490. if ( vUpdate == m_vTranslation )
  491. return false; // no change
  492. m_vTranslation = vUpdate;
  493. // restrict translation, snap to grid, prevent overlap etc
  494. // make sure reference point snaps if enabled
  495. if ( uConstraints )
  496. {
  497. // project back on projection plane
  498. Vector pos;
  499. if ( m_TranslateMode == modeMove )
  500. {
  501. // when moving opbject make sure reference point is on grid
  502. pos = m_vTranslationFixPoint;
  503. }
  504. else
  505. {
  506. // otherwise translated handle should be on grid
  507. HandleToWorld( pos, m_TranslateHandle);
  508. }
  509. ProjectOnTranslationPlane( pos + m_vTranslation, m_vTranslation, uConstraints );
  510. m_vTranslation -= pos;
  511. }
  512. if ( m_TranslateMode == modeScale )
  513. {
  514. for ( int i=0; i<3; i++ )
  515. {
  516. float handle = m_TranslateHandle[i];
  517. if ( handle > 0 )
  518. {
  519. float newMaxs = bmaxs[i] + m_vTranslation[i];
  520. if( m_bPreventOverlap && newMaxs <= bmins[i] )
  521. {
  522. m_vTranslation[i] = bmins[i] - bmaxs[i] + 1;
  523. }
  524. }
  525. else if ( handle < 0 )
  526. {
  527. float newMins = bmins[i] + m_vTranslation[i];
  528. if( m_bPreventOverlap && newMins >= bmaxs[i] )
  529. {
  530. m_vTranslation[i] = bmaxs[i] - bmins[i] - 1;
  531. }
  532. }
  533. }
  534. }
  535. }
  536. UpdateTransformMatrix();
  537. m_pDocument->UpdateAllViews( MAPVIEW_UPDATE_TOOL );
  538. return true;
  539. }
  540. //-----------------------------------------------------------------------------
  541. // Purpose:
  542. // Input : dwHandleColor -
  543. // dwBoxColor -
  544. //-----------------------------------------------------------------------------
  545. void Box3D::SetDrawColors(COLORREF dwHandleColor, COLORREF dwBoxColor)
  546. {
  547. if (dwHandleColor != 0xffffffff)
  548. {
  549. m_clrHandle = dwHandleColor;
  550. }
  551. if (dwBoxColor != 0xffffffff)
  552. {
  553. m_clrBox = dwBoxColor;
  554. }
  555. }
  556. //-----------------------------------------------------------------------------
  557. // Purpose:
  558. // Input : *pt -
  559. //-----------------------------------------------------------------------------
  560. void Box3D::TranslatePoint(Vector& pt)
  561. {
  562. TransformPoint( m_TransformMatrix, pt );
  563. }
  564. const VMatrix& Box3D::GetTransformMatrix()
  565. {
  566. return m_TransformMatrix;
  567. }
  568. void Box3D::UpdateTransformMatrix()
  569. {
  570. m_TransformMatrix.Identity();
  571. if ( m_TranslateMode == modeNone )
  572. {
  573. return;
  574. }
  575. else if ( m_TranslateMode == modeMove )
  576. {
  577. m_TransformMatrix.SetTranslation( m_vTranslation );
  578. return;
  579. }
  580. else if ( m_TranslateMode == modeScale )
  581. {
  582. Vector vScale( 1,1,1);
  583. Vector vMove(0,0,0);
  584. Vector vSize = bmaxs-bmins;
  585. for ( int i=0; i<3; i++ )
  586. {
  587. float handle = m_TranslateHandle[i];
  588. if ( vSize[i] == 0 )
  589. continue;
  590. if ( handle > 0 )
  591. {
  592. vScale[i] = (m_vTranslation[i]+vSize[i]) / vSize[i];
  593. vMove[i] = m_vTranslation[i] / 2;
  594. }
  595. else if ( handle < 0 )
  596. {
  597. vScale[i] = (-m_vTranslation[i]+vSize[i]) / vSize[i];
  598. vMove[i] = m_vTranslation[i] / 2;
  599. }
  600. }
  601. m_TransformMatrix = m_TransformMatrix.Scale( vScale );
  602. m_TransformMatrix.SetTranslation( vMove );
  603. }
  604. else if ( m_TranslateMode == modeShear )
  605. {
  606. Vector vSize = bmaxs-bmins;
  607. int axisS = -1; // shear axis that wont change
  608. int axisA = -1; // first shear axis
  609. int axisB = -1; // second shear axis
  610. for ( int i=0; i<3; i++ )
  611. {
  612. float handle = m_TranslateHandle[i];
  613. if ( handle > 0 )
  614. {
  615. Assert( axisS == -1);
  616. axisS = i;
  617. }
  618. else if ( handle < 0 )
  619. {
  620. Assert( axisS == -1);
  621. axisS = i;
  622. vSize *= -1;
  623. }
  624. else
  625. {
  626. if ( axisA == -1 )
  627. axisA = i;
  628. else
  629. axisB = i;
  630. }
  631. }
  632. Assert( (axisA!=-1) && (axisB!=-1) && (axisS!=-1) );
  633. m_TransformMatrix.m[axisA][axisS] = (m_vTranslation[axisA])/(vSize[axisS]);
  634. m_TransformMatrix.m[axisB][axisS] = (m_vTranslation[axisB])/(vSize[axisS]);
  635. }
  636. else if ( m_TranslateMode == modeRotate )
  637. {
  638. QAngle angle = *(QAngle*)&m_vTranslation; // buuuhhh
  639. m_TransformMatrix.SetupMatrixOrgAngles( vec3_origin, angle );
  640. }
  641. // apply m_vTranslationFixPoint offset
  642. Vector offset;
  643. m_TransformMatrix.V3Mul( m_vTranslationFixPoint, offset );
  644. offset = m_vTranslationFixPoint - offset;
  645. m_TransformMatrix.m[0][3] += offset[0];
  646. m_TransformMatrix.m[1][3] += offset[1];
  647. m_TransformMatrix.m[2][3] += offset[2];
  648. }
  649. void Box3D::TranslateBox(Vector& mins, Vector& maxs)
  650. {
  651. if ( m_TranslateMode == modeNone )
  652. {
  653. return;
  654. }
  655. if ( m_TranslateMode == modeMove )
  656. {
  657. mins += m_vTranslation;
  658. maxs += m_vTranslation;
  659. }
  660. else if ( m_TranslateMode == modeScale )
  661. {
  662. for ( int i=0; i<3; i++ )
  663. {
  664. float handle = m_TranslateHandle[i];
  665. if ( handle > 0 )
  666. {
  667. maxs[i] += m_vTranslation[i];
  668. }
  669. else if ( handle < 0 )
  670. {
  671. mins[i] += m_vTranslation[i];
  672. }
  673. }
  674. }
  675. else if ( m_TranslateMode == modeShear )
  676. {
  677. TranslatePoint( mins );
  678. TranslatePoint( maxs );
  679. }
  680. else if ( m_TranslateMode == modeRotate )
  681. {
  682. TranslatePoint( mins );
  683. TranslatePoint( maxs );
  684. }
  685. NormalizeBox( mins, maxs );
  686. }
  687. //-----------------------------------------------------------------------------
  688. // Purpose:
  689. // Input : bSave -
  690. //-----------------------------------------------------------------------------
  691. void Box3D::FinishTranslation(bool bSave)
  692. {
  693. if( bSave )
  694. {
  695. Vector newMins = bmins;
  696. Vector newMaxs = bmaxs;
  697. TranslateBox( newMins, newMaxs );
  698. LimitBox( newMins, newMaxs, g_MAX_MAP_COORD );
  699. SetBounds( newMins, newMaxs );
  700. m_bEmpty = false;
  701. }
  702. // if we are finished with moving the selection, switch back to the
  703. // original translation mode
  704. if ( m_TranslateMode == modeMove )
  705. {
  706. m_TranslateMode = m_LastTranslateMode;
  707. }
  708. Tool3D::FinishTranslation(bSave);
  709. }
  710. //-----------------------------------------------------------------------------
  711. // Purpose:
  712. //-----------------------------------------------------------------------------
  713. void Box3D::ToggleTranslateMode(void)
  714. {
  715. if( m_TranslateMode == modeMove )
  716. {
  717. m_TranslateMode = modeScale;
  718. }
  719. else if( m_TranslateMode == modeScale )
  720. {
  721. m_TranslateMode = modeRotate;
  722. }
  723. else if( m_TranslateMode == modeRotate )
  724. {
  725. m_TranslateMode = modeShear;
  726. }
  727. else if( m_TranslateMode == modeShear )
  728. {
  729. m_TranslateMode = modeScale; // don't go back to move mode
  730. }
  731. }
  732. //-----------------------------------------------------------------------------
  733. // Purpose:
  734. // Input : dwFlags -
  735. //-----------------------------------------------------------------------------
  736. void Box3D::SetDrawFlags(DWORD dwFlags)
  737. {
  738. m_dwDrawFlags = dwFlags;
  739. }
  740. void Box3D::RenderHandles2D(CRender2D *pRender, const Vector &mins, const Vector &maxs)
  741. {
  742. Vector handles[3*3*3];
  743. int numHandles = GetVisibleHandles( handles, pRender->GetView(), m_TranslateMode );
  744. if ( numHandles == 0 )
  745. return;
  746. pRender->SetHandleColor( GetRValue(m_clrHandle), GetGValue(m_clrHandle), GetBValue(m_clrHandle) );
  747. if ( m_TranslateMode == modeRotate )
  748. {
  749. pRender->SetHandleStyle( HANDLE_RADIUS, CRender::HANDLE_CIRCLE );
  750. }
  751. else
  752. {
  753. pRender->SetHandleStyle( HANDLE_RADIUS, CRender::HANDLE_SQUARE );
  754. }
  755. Vector vCenter = (mins+maxs)/2;
  756. Vector vDelta = maxs - vCenter;
  757. Vector2D vOffset;
  758. bool bPopMode = pRender->BeginClientSpace();
  759. for ( int i=0; i<numHandles; i++)
  760. {
  761. pRender->TransformNormal( vOffset, handles[i] );
  762. vOffset.x = fsign(vOffset.x);
  763. vOffset.y = fsign(vOffset.y);
  764. vOffset*=HANDLE_OFFSET;
  765. Vector pos = vCenter + vDelta * handles[i];
  766. pRender->DrawHandle( pos, &vOffset );
  767. }
  768. if ( bPopMode )
  769. pRender->EndClientSpace();
  770. }
  771. void Box3D::RenderHandles3D(CRender3D *pRender, const Vector &mins, const Vector &maxs)
  772. {
  773. Vector handles[3*3*3];
  774. int numHandles = GetVisibleHandles( handles, pRender->GetView(), m_TranslateMode );
  775. if ( numHandles == 0 )
  776. return;
  777. Vector vCenter = (mins+maxs)/2;
  778. Vector vDelta = maxs - vCenter;
  779. if ( m_TranslateMode == modeRotate )
  780. {
  781. pRender->SetHandleStyle( HANDLE_RADIUS, CRender::HANDLE_CIRCLE );
  782. }
  783. else
  784. {
  785. pRender->SetHandleStyle( HANDLE_RADIUS, CRender::HANDLE_SQUARE );
  786. }
  787. pRender->SetHandleColor( GetRValue(m_clrHandle), GetGValue(m_clrHandle), GetBValue(m_clrHandle) );
  788. pRender->PushRenderMode( RENDER_MODE_FLAT_NOZ );
  789. bool bPopMode = pRender->BeginClientSpace();
  790. for ( int i=0; i<numHandles; i++)
  791. {
  792. Vector pos = vCenter + vDelta * handles[i];
  793. pRender->DrawHandle( pos );
  794. }
  795. if ( bPopMode )
  796. pRender->EndClientSpace();
  797. pRender->PopRenderMode();
  798. }
  799. void Box3D::HandleToWorld( Vector &vWorld, const Vector &vHandle, const Vector *pCustomHandleBox)
  800. {
  801. Vector vCenter, vDelta;
  802. if ( pCustomHandleBox )
  803. {
  804. vCenter = (pCustomHandleBox[0] + pCustomHandleBox[1]) / 2;
  805. vDelta = pCustomHandleBox[1] - vCenter;
  806. }
  807. else
  808. {
  809. vCenter = (bmins+bmaxs)/2;
  810. vDelta = bmaxs - vCenter;
  811. }
  812. vWorld = vCenter + (vDelta * vHandle);
  813. }
  814. //-----------------------------------------------------------------------------
  815. // Purpose:
  816. // Input : *pDC -
  817. // bounds -
  818. //-----------------------------------------------------------------------------
  819. void Box3D::RenderTool2D(CRender2D *pRender)
  820. {
  821. Vector mins = bmins;
  822. Vector maxs = bmaxs;
  823. CMapView2D *pView = (CMapView2D*)pRender->GetView();
  824. Assert( pRender );
  825. if ( IsTranslating() )
  826. {
  827. TranslateBox( mins, maxs );
  828. }
  829. else if ( IsEmpty() )
  830. {
  831. return;
  832. }
  833. if ( m_dwDrawFlags & boundstext)
  834. {
  835. DrawBoundsText(pRender, mins, maxs, DBT_TOP | DBT_LEFT);
  836. }
  837. if ( IsTranslating() )
  838. {
  839. pRender->PushRenderMode( RENDER_MODE_DOTTED );
  840. pRender->SetDrawColor( GetRValue(Options.colors.clrToolDrag), GetGValue(Options.colors.clrToolDrag), GetBValue(Options.colors.clrToolDrag) );
  841. }
  842. else if (!(m_dwDrawFlags & thicklines))
  843. {
  844. pRender->PushRenderMode( RENDER_MODE_DOTTED );
  845. pRender->SetDrawColor( GetRValue(m_clrBox), GetGValue(m_clrBox), GetBValue(m_clrBox) );
  846. }
  847. else
  848. {
  849. pRender->PushRenderMode( RENDER_MODE_FLAT_NOZ );
  850. pRender->SetDrawColor( GetRValue(m_clrBox), GetGValue(m_clrBox), GetBValue(m_clrBox) );
  851. }
  852. // render bounds
  853. if ( !IsTranslating() || m_TranslateMode == modeScale || m_TranslateMode == modeMove )
  854. {
  855. // draw simple rectangle
  856. pRender->DrawRectangle( mins, maxs, false, 0 );
  857. }
  858. else
  859. {
  860. // during rotation or shearing, draw transformed bounding box
  861. Vector v[4];
  862. // init all points to center
  863. v[0] = v[1] = v[2] = v[3] = (bmins+bmaxs) / 2;
  864. int axis = pView->axHorz;
  865. v[0][axis] = v[1][axis] = bmins[axis];
  866. v[2][axis] = v[3][axis] = bmaxs[axis];
  867. axis = pView->axVert;
  868. v[1][axis] = v[2][axis] = bmins[axis];
  869. v[0][axis] = v[3][axis] = bmaxs[axis];
  870. for ( int i=0; i<4; i++)
  871. {
  872. TranslatePoint( v[i] );
  873. }
  874. pRender->DrawLine( v[0], v[1] );
  875. pRender->DrawLine( v[1], v[2] );
  876. pRender->DrawLine( v[2], v[3] );
  877. pRender->DrawLine( v[3], v[0] );
  878. }
  879. pRender->PopRenderMode();
  880. // draw a cross for translation origin in move or rotation mode
  881. if ( IsTranslating() )
  882. {
  883. if ( m_TranslateMode == modeMove || m_TranslateMode == modeRotate )
  884. {
  885. Vector vec = m_vTranslationFixPoint;
  886. if ( m_TranslateMode == modeMove )
  887. {
  888. TranslatePoint( vec );
  889. }
  890. // draw 'X'
  891. pRender->SetHandleStyle( 7, CRender::HANDLE_CROSS );
  892. pRender->SetHandleColor( GetRValue(Options.colors.clrToolDrag), GetGValue(Options.colors.clrToolDrag), GetBValue(Options.colors.clrToolDrag) );
  893. pRender->DrawHandle( vec );
  894. }
  895. }
  896. else if ( m_bEnableHandles )
  897. {
  898. RenderHandles2D( pRender, mins, maxs );
  899. }
  900. }
  901. //-----------------------------------------------------------------------------
  902. // Purpose: Renders this region as a wireframe box.
  903. // Input : pRender - 3D Renderer.
  904. //-----------------------------------------------------------------------------
  905. void Box3D::RenderTool3D(CRender3D *pRender)
  906. {
  907. if ( IsTranslating() )
  908. {
  909. VMatrix matrix = GetTransformMatrix();
  910. pRender->BeginLocalTransfrom( matrix );
  911. }
  912. else if (IsEmpty())
  913. {
  914. return;
  915. }
  916. pRender->PushRenderMode( RENDER_MODE_FLAT );
  917. pRender->SetDrawColor( GetRValue(m_clrBox), GetGValue(m_clrBox), GetBValue(m_clrBox) );
  918. pRender->DrawBox( bmins, bmaxs );
  919. pRender->PopRenderMode();
  920. if ( IsTranslating() )
  921. {
  922. pRender->EndLocalTransfrom();
  923. if ( m_TranslateMode == modeMove || m_TranslateMode == modeRotate )
  924. {
  925. Vector vec = m_vTranslationFixPoint;
  926. if ( m_TranslateMode == modeMove )
  927. {
  928. TranslatePoint( vec );
  929. }
  930. // draw 'X'
  931. pRender->PushRenderMode( RENDER_MODE_FLAT_NOZ );
  932. pRender->SetHandleStyle( 7, CRender::HANDLE_CROSS );
  933. pRender->SetHandleColor( GetRValue(Options.colors.clrToolDrag), GetGValue(Options.colors.clrToolDrag), GetBValue(Options.colors.clrToolDrag) );
  934. pRender->DrawHandle( vec );
  935. pRender->PopRenderMode();
  936. }
  937. }
  938. else if ( m_bEnableHandles )
  939. {
  940. RenderHandles3D( pRender, bmins, bmaxs );
  941. };
  942. }
  943. //-----------------------------------------------------------------------------
  944. // Purpose:
  945. // Input : *vecStart -
  946. // *mins -
  947. // *maxs -
  948. //-----------------------------------------------------------------------------
  949. void Box3D::StartNew( CMapView *pView, const Vector2D &vPoint, const Vector &vecStart, const Vector &vecSize )
  950. {
  951. //Setup our info
  952. m_TranslateMode = modeScale;
  953. m_TranslateHandle = Vector( 1, 1, 1 );
  954. bmins = vecStart;
  955. bmaxs = vecStart+vecSize;
  956. NormalizeBox( bmins, bmaxs );
  957. StartTranslation( pView, vPoint, Vector( 1, 1, 1 ) );
  958. m_bPreventOverlap = false;
  959. m_bEmpty = false;
  960. }