forestSelectionTool.cpp 16 KB

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