forestSelectionTool.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platform/platform.h"
  23. #include "forest/editor/forestSelectionTool.h"
  24. #include "forest/forest.h"
  25. #include "forest/editor/forestUndo.h"
  26. #include "forest/editor/forestEditorCtrl.h"
  27. #include "gui/worldEditor/editTSCtrl.h"
  28. #include "gui/worldEditor/gizmo.h"
  29. #include "console/consoleTypes.h"
  30. #include "console/engineAPI.h"
  31. #include "core/util/tVector.h"
  32. #include "core/util/safeDelete.h"
  33. #include "gfx/gfxDrawUtil.h"
  34. #include "gui/worldEditor/worldEditor.h"
  35. #include "math/mMatrix.h"
  36. template <>
  37. MatrixF Selection<ForestItem>::getOrientation()
  38. {
  39. if ( size() == 1 )
  40. return first().getTransform();
  41. return MatrixF::Identity;
  42. }
  43. template <>
  44. Point3F Selection<ForestItem>::getOrigin()
  45. {
  46. Point3F centroid( Point3F::Zero );
  47. if ( empty() )
  48. return centroid;
  49. Selection<ForestItem>::iterator itr = begin();
  50. for (; itr != end(); ++itr)
  51. {
  52. const MatrixF &mat = itr->getTransform();
  53. Point3F wPos;
  54. mat.getColumn( 3, &wPos );
  55. centroid += wPos;
  56. }
  57. centroid /= (F32)size();
  58. return centroid;
  59. }
  60. template <>
  61. Point3F Selection<ForestItem>::getScale()
  62. {
  63. if ( size() == 1 )
  64. return Point3F( first().getScale() );
  65. return Point3F::One;
  66. }
  67. void ForestItemSelection::offsetObject( ForestItem &object, const Point3F &delta )
  68. {
  69. if ( !mData )
  70. return;
  71. MatrixF newMat( object.getTransform() );
  72. newMat.displace( delta );
  73. object = mData->updateItem( object.getKey(), object.getPosition(), object.getData(), newMat, object.getScale() );
  74. }
  75. void ForestItemSelection::rotateObject( ForestItem &object, const EulerF &delta, const Point3F &origin )
  76. {
  77. if ( !mData )
  78. return;
  79. MatrixF mat = object.getTransform();
  80. Point3F pos;
  81. mat.getColumn( 3, &pos );
  82. MatrixF wMat( mat );
  83. wMat.inverse();
  84. // get offset in obj space
  85. Point3F offset = pos - origin;
  86. if ( size() == 1 )
  87. {
  88. wMat.mulV(offset);
  89. MatrixF transform(EulerF::Zero, -offset);
  90. transform.mul(MatrixF(delta));
  91. transform.mul(MatrixF(EulerF::Zero, offset));
  92. mat.mul(transform);
  93. }
  94. else
  95. {
  96. MatrixF transform( delta );
  97. Point3F wOffset;
  98. transform.mulV( offset, &wOffset );
  99. wMat.mulV( offset );
  100. transform.set( EulerF::Zero, -offset );
  101. mat.setColumn( 3, Point3F::Zero );
  102. wMat.setColumn( 3, Point3F::Zero );
  103. transform.mul( wMat );
  104. transform.mul( MatrixF(delta) );
  105. transform.mul( mat );
  106. mat.mul( transform );
  107. mat.normalize();
  108. mat.setColumn( 3, wOffset + origin );
  109. }
  110. object = mData->updateItem( object.getKey(), object.getPosition(), object.getData(), mat, object.getScale() );
  111. }
  112. void ForestItemSelection::scaleObject( ForestItem &object, const Point3F &delta )
  113. {
  114. if ( !mData )
  115. return;
  116. object = mData->updateItem( object.getKey(), object.getPosition(), object.getData(), object.getTransform(), delta.z );
  117. }
  118. IMPLEMENT_CONOBJECT( ForestSelectionTool );
  119. ConsoleDocClass( ForestSelectionTool,
  120. "@brief Specialized selection tool for picking individual trees in a forest.\n\n"
  121. "Editor use only.\n\n"
  122. "@internal"
  123. );
  124. ForestSelectionTool::ForestSelectionTool()
  125. : Parent(),
  126. mCurrAction( NULL ),
  127. mGizmo( NULL ),
  128. mGizmoProfile( NULL )
  129. {
  130. mBounds = Box3F::Invalid;
  131. mDragRectColor.set(255,255,0);
  132. mMouseDown = false;
  133. mDragSelect = false;
  134. }
  135. ForestSelectionTool::~ForestSelectionTool()
  136. {
  137. SAFE_DELETE( mCurrAction );
  138. }
  139. void ForestSelectionTool::setParentEditor( ForestEditorCtrl *editor )
  140. {
  141. mEditor = editor;
  142. mGizmo = editor->getGizmo();
  143. mGizmoProfile = mGizmo->getProfile();
  144. }
  145. void ForestSelectionTool::_selectItem( const ForestItem &item )
  146. {
  147. // Make sure its not already selected.
  148. for ( U32 i=0; i < mSelection.size(); i++ )
  149. {
  150. if ( mSelection[i].getKey() == item.getKey() )
  151. return;
  152. }
  153. mSelection.push_back( item );
  154. mBounds.intersect( item.getWorldBox() );
  155. }
  156. void ForestSelectionTool::deleteSelection()
  157. {
  158. if (!mEditor)
  159. return;
  160. ForestDeleteUndoAction *action = new ForestDeleteUndoAction( mForest->getData(), mEditor );
  161. for ( U32 i=0; i < mSelection.size(); i++ )
  162. {
  163. const ForestItem &item = mSelection[i];
  164. action->removeItem( item );
  165. }
  166. clearSelection();
  167. _submitUndo( action );
  168. }
  169. void ForestSelectionTool::clearSelection()
  170. {
  171. mSelection.clear();
  172. mBounds = Box3F::Invalid;
  173. }
  174. void ForestSelectionTool::cutSelection()
  175. {
  176. }
  177. void ForestSelectionTool::copySelection()
  178. {
  179. }
  180. void ForestSelectionTool::pasteSelection()
  181. {
  182. }
  183. void ForestSelectionTool::setActiveForest( Forest *forest )
  184. {
  185. mForest = forest;
  186. if ( forest )
  187. mSelection.setForestData( forest->getData() );
  188. else
  189. mSelection.setForestData( NULL );
  190. }
  191. void ForestSelectionTool::on3DMouseDown( const Gui3DMouseEvent &evt )
  192. {
  193. mMouseDown = true;
  194. mMouseDragged = false;
  195. mUsingGizmo = !mSelection.empty() && mGizmo->getSelection() != Gizmo::None;
  196. if ( mUsingGizmo )
  197. {
  198. mGizmo->on3DMouseDown( evt );
  199. return;
  200. }
  201. mDragSelection.clear();
  202. mDragRect.set( Point2I(evt.mousePoint), Point2I(0,0) );
  203. mDragStart = evt.mousePoint;
  204. const bool multiSel = evt.modifier & SI_CTRL;
  205. if ( !multiSel )
  206. clearSelection();
  207. if ( mHoverItem.isValid() )
  208. _selectItem( mHoverItem );
  209. // This should never happen... it should have been
  210. // submitted and nulled in on3DMouseUp()!
  211. //
  212. // Yeah, unless you had a breakpoint there and on3DMouseUp never fired,
  213. // in which case this is really annoying.
  214. //
  215. //AssertFatal( !mCurrAction, "ForestSelectionTool::on3DMouseDown() - Dangling undo action!" );
  216. }
  217. void ForestSelectionTool::on3DMouseMove( const Gui3DMouseEvent &evt )
  218. {
  219. // Invalidate the hover item first... we're gonna find a new one.
  220. mHoverItem.makeInvalid();
  221. if ( !mForest )
  222. return;
  223. if ( !mSelection.empty() )
  224. mGizmo->on3DMouseMove( evt );
  225. RayInfo ri;
  226. ri.userData = new ForestItem;
  227. Point3F startPnt = evt.pos;
  228. Point3F endPnt = evt.pos + evt.vec * 1000.0f;
  229. if ( mForest->castRayRendered( startPnt, endPnt, &ri ) )
  230. mHoverItem = (*(ForestItem*)ri.userData);
  231. delete static_cast<ForestItem*>(ri.userData);
  232. }
  233. void ForestSelectionTool::on3DMouseDragged( const Gui3DMouseEvent &evt )
  234. {
  235. mHoverItem.makeInvalid();
  236. if ( mUsingGizmo )
  237. {
  238. mGizmo->on3DMouseDragged( evt );
  239. const Point3F &deltaRot = mGizmo->getDeltaRot();
  240. const Point3F &deltaPos = mGizmo->getOffset();
  241. const Point3F &deltaScale = mGizmo->getDeltaScale();
  242. if ( deltaRot.isZero() && deltaPos.isZero() && deltaScale.isZero() )
  243. return;
  244. // Store the current item states!
  245. if ( !mCurrAction )
  246. {
  247. mCurrAction = new ForestUpdateAction( mForest->getData(), mEditor );
  248. for ( U32 i=0; i < mSelection.size(); i++ )
  249. {
  250. const ForestItem &item = mSelection[i];
  251. mCurrAction->saveItem( item );
  252. }
  253. }
  254. switch ( mGizmo->getMode() )
  255. {
  256. case MoveMode:
  257. mSelection.offset( deltaPos ); break;
  258. case RotateMode:
  259. mSelection.rotate( deltaRot ); break;
  260. case ScaleMode:
  261. mSelection.scale( mGizmo->getScale() ); break;
  262. default: ;
  263. }
  264. return;
  265. }
  266. mDragSelect = true;
  267. mHoverItem.makeInvalid();
  268. // Doing a drag selection.
  269. if ( mDragSelect )
  270. {
  271. // build the drag selection on the renderScene method - make sure no neg extent!
  272. mDragRect.point.x = (evt.mousePoint.x < mDragStart.x) ? evt.mousePoint.x : mDragStart.x;
  273. mDragRect.extent.x = (evt.mousePoint.x > mDragStart.x) ? evt.mousePoint.x - mDragStart.x : mDragStart.x - evt.mousePoint.x;
  274. mDragRect.point.y = (evt.mousePoint.y < mDragStart.y) ? evt.mousePoint.y : mDragStart.y;
  275. mDragRect.extent.y = (evt.mousePoint.y > mDragStart.y) ? evt.mousePoint.y - mDragStart.y : mDragStart.y - evt.mousePoint.y;
  276. return;
  277. }
  278. }
  279. void ForestSelectionTool::on3DMouseUp( const Gui3DMouseEvent &evt )
  280. {
  281. mGizmo->on3DMouseUp( evt );
  282. mMouseDown = false;
  283. // If we have an undo action then we must have
  284. // moved, rotated, or scaled something.
  285. if ( mCurrAction )
  286. {
  287. _submitUndo( mCurrAction );
  288. mCurrAction = NULL;
  289. return;
  290. }
  291. // If we were performing a drag select, finalize it now.
  292. if ( mDragSelect )
  293. {
  294. mDragSelect = false;
  295. clearSelection();
  296. for ( S32 i = 0; i < mDragSelection.size(); i++ )
  297. _selectItem( mDragSelection[i] );
  298. mDragSelection.clear();
  299. return;
  300. }
  301. }
  302. void ForestSelectionTool::onRender3D()
  303. {
  304. GFXDrawUtil *drawUtil = GFX->getDrawUtil();
  305. ColorI color( 255, 255, 255, 255 );
  306. MatrixF treeMat;
  307. GFXStateBlockDesc desc;
  308. desc.setBlend( true );
  309. desc.setZReadWrite( true, false );
  310. if ( mHoverItem.isValid() )
  311. {
  312. treeMat = mHoverItem.getTransform();
  313. drawUtil->drawObjectBox( desc, mHoverItem.getSize(), mHoverItem.getWorldBox().getCenter(), treeMat, color );
  314. }
  315. if ( !mSelection.empty() )
  316. {
  317. for ( U32 i = 0; i < mSelection.size(); i++ )
  318. {
  319. const ForestItem &item = mSelection[i];
  320. treeMat = item.getTransform();
  321. drawUtil->drawObjectBox( desc, item.getSize(), item.getWorldBox().getCenter(), treeMat, color );
  322. }
  323. mGizmo->set( mSelection.getOrientation(), mSelection.getOrigin(), mSelection.getScale() );
  324. mGizmo->renderGizmo( mEditor->getLastCameraQuery().cameraMatrix, mEditor->getLastCameraQuery().fov );
  325. }
  326. }
  327. static Frustum gDragFrustum;
  328. void ForestSelectionTool::onRender2D()
  329. {
  330. // Draw drag selection rect.
  331. if ( mDragSelect && mDragRect.extent.x > 1 && mDragRect.extent.y > 1 )
  332. GFX->getDrawUtil()->drawRect( mDragRect, mDragRectColor );
  333. // update what is in the selection
  334. if ( mDragSelect )
  335. mDragSelection.clear();
  336. // Determine selected objects based on the drag box touching
  337. // a mesh if a drag operation has begun.
  338. if ( mDragSelect && mDragRect.extent.x > 1 && mDragRect.extent.y > 1 )
  339. {
  340. // Build the drag frustum based on the rect
  341. F32 wwidth;
  342. F32 wheight;
  343. F32 aspectRatio = F32(mEditor->getWidth()) / F32(mEditor->getHeight());
  344. const CameraQuery &lastCameraQuery = mEditor->getLastCameraQuery();
  345. if(!lastCameraQuery.ortho)
  346. {
  347. wheight = lastCameraQuery.nearPlane * mTan(lastCameraQuery.fov / 2);
  348. wwidth = aspectRatio * wheight;
  349. }
  350. else
  351. {
  352. wheight = lastCameraQuery.fov;
  353. wwidth = aspectRatio * wheight;
  354. }
  355. F32 hscale = wwidth * 2 / F32(mEditor->getWidth());
  356. F32 vscale = wheight * 2 / F32(mEditor->getHeight());
  357. Point2I editorPosition = mEditor->getPosition();
  358. F32 left = (mDragRect.point.x - editorPosition.x) * hscale - wwidth;
  359. F32 right = (mDragRect.point.x - editorPosition.x + mDragRect.extent.x) * hscale - wwidth;
  360. F32 top = wheight - vscale * (mDragRect.point.y - editorPosition.y);
  361. F32 bottom = wheight - vscale * (mDragRect.point.y - editorPosition.y + mDragRect.extent.y);
  362. gDragFrustum.set(lastCameraQuery.ortho, left, right, top, bottom, lastCameraQuery.nearPlane, lastCameraQuery.farPlane, lastCameraQuery.cameraMatrix );
  363. mForest->getData()->getItems( gDragFrustum, &mDragSelection );
  364. }
  365. }
  366. bool ForestSelectionTool::updateGuiInfo()
  367. {
  368. SimObject *statusbar;
  369. if ( !Sim::findObject( "EditorGuiStatusBar", statusbar ) )
  370. return false;
  371. String text( "Forest Editor." );
  372. GizmoMode mode = mGizmoProfile->mode;
  373. if ( mMouseDown && mGizmo->getSelection() != Gizmo::None )
  374. {
  375. Point3F delta;
  376. String qualifier;
  377. if ( mode == RotateMode )
  378. delta = mGizmo->getDeltaTotalRot();
  379. else if ( mode == MoveMode )
  380. delta = mGizmo->getTotalOffset();
  381. else if ( mode == ScaleMode )
  382. delta = mGizmo->getDeltaTotalScale();
  383. if ( mGizmo->getAlignment() == Object && mode != ScaleMode )
  384. {
  385. mSelection.getOrientation().mulV( delta );
  386. }
  387. if ( mIsZero( delta.x, 0.0001f ) )
  388. delta.x = 0.0f;
  389. if ( mIsZero( delta.y, 0.0001f ) )
  390. delta.y = 0.0f;
  391. if ( mIsZero( delta.z, 0.0001f ) )
  392. delta.z = 0.0f;
  393. if ( mode == RotateMode )
  394. {
  395. delta.x = mRadToDeg( delta.x );
  396. delta.y = mRadToDeg( delta.y );
  397. delta.z = mRadToDeg( delta.z );
  398. text = String::ToString( "Delta angle ( x: %4.2f, y: %4.2f, z: %4.2f ).", delta.x, delta.y, delta.z );
  399. }
  400. else if ( mode == MoveMode )
  401. text = String::ToString( "Delta position ( x: %4.2f, y: %4.2f, z: %4.2f ).", delta.x, delta.y, delta.z );
  402. else if ( mode == ScaleMode )
  403. text = String::ToString( "Delta scale ( x: %4.2f, y: %4.2f, z: %4.2f ).", delta.x, delta.y, delta.z );
  404. }
  405. else
  406. {
  407. if ( mode == MoveMode )
  408. text = "Move selection. SHIFT while dragging duplicates objects.";
  409. else if ( mode == RotateMode )
  410. text = "Rotate selection.";
  411. else if ( mode == ScaleMode )
  412. text = "Scale selection.";
  413. }
  414. Con::executef( statusbar, "setInfo", text.c_str() );
  415. Con::executef( statusbar, "setSelectionObjectsByCount", mSelection.size() );
  416. return true;
  417. }
  418. void ForestSelectionTool::updateGizmo()
  419. {
  420. mGizmoProfile->restoreDefaultState();
  421. const GizmoMode &mode = mGizmoProfile->mode;
  422. if ( mode == ScaleMode )
  423. {
  424. mGizmoProfile->flags &= ~GizmoProfile::PlanarHandlesOn;
  425. mGizmoProfile->allAxesScaleUniform = true;
  426. }
  427. }
  428. void ForestSelectionTool::onUndoAction()
  429. {
  430. ForestData *data = mForest->getData();
  431. // Remove items from our selection that no longer exist.
  432. for ( S32 i = 0; i < mSelection.size(); i++ )
  433. {
  434. const ForestItem &item = data->findItem( mSelection[i].getKey(), mSelection[i].getPosition() );
  435. if ( item == ForestItem::Invalid )
  436. {
  437. mSelection.erase_fast( i );
  438. i--;
  439. }
  440. else
  441. mSelection[i] = item;
  442. }
  443. // Recalculate our selection bounds.
  444. mBounds = Box3F::Invalid;
  445. for ( S32 i = 0; i < mSelection.size(); i++ )
  446. mBounds.intersect( mSelection[i].getWorldBox() );
  447. }
  448. DefineConsoleMethod( ForestSelectionTool, getSelectionCount, S32, (), , "" )
  449. {
  450. return object->getSelectionCount();
  451. }
  452. DefineConsoleMethod( ForestSelectionTool, deleteSelection, void, (), , "" )
  453. {
  454. object->deleteSelection();
  455. }
  456. DefineConsoleMethod( ForestSelectionTool, clearSelection, void, (), , "" )
  457. {
  458. object->clearSelection();
  459. }
  460. DefineConsoleMethod( ForestSelectionTool, cutSelection, void, (), , "" )
  461. {
  462. object->cutSelection();
  463. }
  464. DefineConsoleMethod( ForestSelectionTool, copySelection, void, (), , "" )
  465. {
  466. object->copySelection();
  467. }
  468. DefineConsoleMethod( ForestSelectionTool, pasteSelection, void, (), , "" )
  469. {
  470. object->pasteSelection();
  471. }