guiRoadEditorCtrl.cpp 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111
  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 "environment/editors/guiRoadEditorCtrl.h"
  24. #include "console/consoleTypes.h"
  25. #include "console/engineAPI.h"
  26. #include "scene/sceneManager.h"
  27. #include "collision/collision.h"
  28. #include "math/util/frustum.h"
  29. #include "gfx/gfxPrimitiveBuffer.h"
  30. #include "gfx/gfxTextureHandle.h"
  31. #include "gfx/gfxTransformSaver.h"
  32. #include "gfx/gfxDrawUtil.h"
  33. #include "gfx/primBuilder.h"
  34. #include "T3D/gameBase/gameConnection.h"
  35. #include "gui/core/guiCanvas.h"
  36. #include "gui/buttons/guiButtonCtrl.h"
  37. #include "gui/worldEditor/undoActions.h"
  38. #include "materials/materialDefinition.h"
  39. #include "T3D/Scene.h"
  40. IMPLEMENT_CONOBJECT(GuiRoadEditorCtrl);
  41. ConsoleDocClass( GuiRoadEditorCtrl,
  42. "@brief GUI tool that makes up the Decal Road Editor\n\n"
  43. "Editor use only.\n\n"
  44. "@internal"
  45. );
  46. GuiRoadEditorCtrl::GuiRoadEditorCtrl()
  47. {
  48. // Each of the mode names directly correlates with the River Editor's
  49. // tool palette
  50. mSelectRoadMode = "RoadEditorSelectMode";
  51. mAddRoadMode = "RoadEditorAddRoadMode";
  52. mMovePointMode = "RoadEditorMoveMode";
  53. mScalePointMode = "RoadEditorScaleMode";
  54. mAddNodeMode = "RoadEditorAddNodeMode";
  55. mInsertPointMode = "RoadEditorInsertPointMode";
  56. mRemovePointMode = "RoadEditorRemovePointMode";
  57. mMode = mSelectRoadMode;
  58. mRoadSet = NULL;
  59. mSelNode = -1;
  60. mHoverNode = -1;
  61. mSelRoad = NULL;
  62. mHoverRoad = NULL;
  63. mAddNodeIdx = 0;
  64. mDefaultWidth = 10.0f;
  65. mInsertIdx = -1;
  66. mStartWidth = -1.0f;
  67. mStartX = 0;
  68. mNodeHalfSize.set(4,4);
  69. mHoverSplineColor.set( 255,0,0,255 );
  70. mSelectedSplineColor.set( 0,255,0,255 );
  71. mHoverNodeColor.set( 255,255,255,255 );
  72. mSavedDrag = false;
  73. mIsDirty = false;
  74. mMaterialAssetId = Con::getVariable("$DecalRoadEditor::defaultMaterialAsset");
  75. }
  76. GuiRoadEditorCtrl::~GuiRoadEditorCtrl()
  77. {
  78. // nothing to do
  79. }
  80. void GuiRoadEditorUndoAction::undo()
  81. {
  82. DecalRoad *road = NULL;
  83. if ( !Sim::findObject( mObjId, road ) )
  84. return;
  85. // Temporarily save the roads current data.
  86. String materialAssetId = road->mMaterialAssetId;
  87. F32 textureLength = road->mTextureLength;
  88. F32 breakAngle = road->mBreakAngle;
  89. F32 segmentsPerBatch = road->mSegmentsPerBatch;
  90. Vector<RoadNode> nodes;
  91. nodes.merge( road->mNodes );
  92. // Restore the Road properties saved in the UndoAction
  93. road->_setMaterial(materialAssetId);
  94. road->mBreakAngle = breakAngle;
  95. road->mSegmentsPerBatch = segmentsPerBatch;
  96. road->mTextureLength = textureLength;
  97. road->inspectPostApply();
  98. // Restore the Nodes saved in the UndoAction
  99. road->mNodes.clear();
  100. for ( U32 i = 0; i < mNodes.size(); i++ )
  101. {
  102. road->_addNode( mNodes[i].point, mNodes[i].width );
  103. }
  104. // Regenerate the road
  105. road->regenerate();
  106. // If applicable set the selected road and node
  107. mRoadEditor->mSelRoad = road;
  108. mRoadEditor->mSelNode = -1;
  109. // Now save the previous Road data in this UndoAction
  110. // since an undo action must become a redo action and vice-versa
  111. mMaterialAssetId = materialAssetId;
  112. mBreakAngle = breakAngle;
  113. mSegmentsPerBatch = segmentsPerBatch;
  114. mTextureLength = textureLength;
  115. mNodes.clear();
  116. mNodes.merge( nodes );
  117. }
  118. bool GuiRoadEditorCtrl::onAdd()
  119. {
  120. if( !Parent::onAdd() )
  121. return false;
  122. mRoadSet = DecalRoad::getServerSet();
  123. GFXStateBlockDesc desc;
  124. desc.setCullMode( GFXCullNone );
  125. desc.setBlend(false);
  126. desc.setZReadWrite( false, false );
  127. mZDisableSB = GFX->createStateBlock(desc);
  128. return true;
  129. }
  130. void GuiRoadEditorCtrl::initPersistFields()
  131. {
  132. addField( "DefaultWidth", TypeF32, Offset( mDefaultWidth, GuiRoadEditorCtrl ) );
  133. addField( "HoverSplineColor", TypeColorI, Offset( mHoverSplineColor, GuiRoadEditorCtrl ) );
  134. addField( "SelectedSplineColor", TypeColorI, Offset( mSelectedSplineColor, GuiRoadEditorCtrl ) );
  135. addField( "HoverNodeColor", TypeColorI, Offset( mHoverNodeColor, GuiRoadEditorCtrl ) );
  136. addField( "isDirty", TypeBool, Offset( mIsDirty, GuiRoadEditorCtrl ) );
  137. INITPERSISTFIELD_MATERIALASSET(Material, GuiRoadEditorCtrl, "Default Material used by the Road Editor on road creation.");
  138. //addField( "MoveNodeCursor", TYPEID< SimObject >(), Offset( mMoveNodeCursor, GuiRoadEditorCtrl) );
  139. //addField( "AddNodeCursor", TYPEID< SimObject >(), Offset( mAddNodeCursor, GuiRoadEditorCtrl) );
  140. //addField( "InsertNodeCursor", TYPEID< SimObject >(), Offset( mInsertNodeCursor, GuiRoadEditorCtrl) );
  141. //addField( "ResizeNodeCursor", TYPEID< SimObject >(), Offset( mResizeNodeCursor, GuiRoadEditorCtrl) );
  142. Parent::initPersistFields();
  143. }
  144. void GuiRoadEditorCtrl::onSleep()
  145. {
  146. Parent::onSleep();
  147. mMode = mSelectRoadMode;
  148. mHoverNode = -1;
  149. mHoverRoad = NULL;
  150. setSelectedNode(-1);
  151. }
  152. void GuiRoadEditorCtrl::get3DCursor( GuiCursor *&cursor,
  153. bool &visible,
  154. const Gui3DMouseEvent &event_ )
  155. {
  156. //cursor = mAddNodeCursor;
  157. //visible = false;
  158. cursor = NULL;
  159. visible = false;
  160. GuiCanvas *root = getRoot();
  161. if ( !root )
  162. return;
  163. S32 currCursor = PlatformCursorController::curArrow;
  164. if ( root->mCursorChanged == currCursor )
  165. return;
  166. PlatformWindow *window = root->getPlatformWindow();
  167. PlatformCursorController *controller = window->getCursorController();
  168. // We've already changed the cursor,
  169. // so set it back before we change it again.
  170. if( root->mCursorChanged != -1)
  171. controller->popCursor();
  172. // Now change the cursor shape
  173. controller->pushCursor(currCursor);
  174. root->mCursorChanged = currCursor;
  175. }
  176. void GuiRoadEditorCtrl::on3DMouseDown(const Gui3DMouseEvent & event)
  177. {
  178. if ( !isFirstResponder() )
  179. setFirstResponder();
  180. // Get the clicked terrain position.
  181. Point3F tPos;
  182. if ( !getTerrainPos( event, tPos ) )
  183. return;
  184. mouseLock();
  185. // Find any road / node at the clicked position.
  186. // TODO: handle overlapping roads/nodes somehow, cycle through them.
  187. DecalRoad *roadPtr = NULL;
  188. S32 closestNodeIdx = -1;
  189. F32 closestDist = F32_MAX;
  190. DecalRoad *closestNodeRoad = NULL;
  191. // First, find the closest node in any road to the clicked position.
  192. for ( SimSetIterator iter(mRoadSet); *iter; ++iter )
  193. {
  194. roadPtr = static_cast<DecalRoad*>( *iter );
  195. U32 idx;
  196. if ( roadPtr->getClosestNode( tPos, idx ) )
  197. {
  198. Point3F nodePos = roadPtr->getNodePosition(idx);
  199. F32 dist = ( nodePos - tPos ).len();
  200. if ( dist < closestDist )
  201. {
  202. closestNodeIdx = idx;
  203. closestDist = dist;
  204. closestNodeRoad = roadPtr;
  205. }
  206. }
  207. }
  208. //
  209. // Second, determine if the screen-space node rectangle
  210. // contains the clicked position.
  211. bool nodeClicked = false;
  212. S32 clickedNodeIdx = -1;
  213. if ( closestNodeIdx != -1 )
  214. {
  215. Point3F nodePos = closestNodeRoad->getNodePosition( closestNodeIdx );
  216. Point3F temp;
  217. project( nodePos, &temp );
  218. Point2I screenPos( temp.x, temp.y );
  219. RectI nodeRect( screenPos - mNodeHalfSize, mNodeHalfSize * 2 );
  220. nodeClicked = nodeRect.pointInRect( event.mousePoint );
  221. if ( nodeClicked )
  222. clickedNodeIdx = closestNodeIdx;
  223. }
  224. //
  225. // Determine the clickedRoad
  226. //
  227. DecalRoad *clickedRoadPtr = NULL;
  228. U32 insertNodeIdx = 0;
  229. if ( nodeClicked && (mSelRoad == NULL || closestNodeRoad == mSelRoad) )
  230. {
  231. // If a node was clicked, the owning road is always
  232. // considered the clicked road.
  233. clickedRoadPtr = closestNodeRoad;
  234. }
  235. else
  236. {
  237. // check the selected road first
  238. if ( mSelRoad != NULL && mSelRoad->containsPoint( tPos, &insertNodeIdx ) )
  239. {
  240. clickedRoadPtr = mSelRoad;
  241. nodeClicked = false;
  242. clickedNodeIdx = -1;
  243. }
  244. else
  245. {
  246. // Otherwise, we must ask each road if it contains
  247. // the clicked pos.
  248. for ( SimSetIterator iter(mRoadSet); *iter; ++iter )
  249. {
  250. roadPtr = static_cast<DecalRoad*>( *iter );
  251. if ( roadPtr->containsPoint( tPos, &insertNodeIdx ) )
  252. {
  253. clickedRoadPtr = roadPtr;
  254. break;
  255. }
  256. }
  257. }
  258. }
  259. // shortcuts
  260. bool dblClick = ( event.mouseClickCount > 1 );
  261. if( dblClick )
  262. {
  263. if( mMode == mSelectRoadMode )
  264. {
  265. setMode( mAddRoadMode, true );
  266. return;
  267. }
  268. if( mMode == mAddNodeMode )
  269. {
  270. // Delete the node attached to the cursor.
  271. deleteSelectedNode();
  272. mMode = mAddRoadMode;
  273. return;
  274. }
  275. }
  276. //this check is here in order to bounce back from deleting a whole road with ctrl+z
  277. //this check places the editor back into addroadmode
  278. if ( mMode == mAddNodeMode )
  279. {
  280. if ( !mSelRoad )
  281. mMode = mAddRoadMode;
  282. }
  283. if ( mMode == mSelectRoadMode )
  284. {
  285. // Did not click on a road or a node.
  286. if ( !clickedRoadPtr )
  287. {
  288. setSelectedRoad( NULL );
  289. setSelectedNode( -1 );
  290. return;
  291. }
  292. // Clicked on a road that wasn't the currently selected road.
  293. if ( clickedRoadPtr != mSelRoad )
  294. {
  295. setSelectedRoad( clickedRoadPtr );
  296. setSelectedNode( -1 );
  297. return;
  298. }
  299. // Clicked on a node in the currently selected road that wasn't
  300. // the currently selected node.
  301. if ( nodeClicked )
  302. {
  303. setSelectedNode( clickedNodeIdx );
  304. return;
  305. }
  306. // Clicked a position on the currently selected road
  307. // that did not contain a node.
  308. //U32 newNode = clickedRoadPtr->insertNode( tPos, mDefaultWidth, insertNodeIdx );
  309. //setSelectedNode( newNode );
  310. }
  311. else if ( mMode == mAddRoadMode )
  312. {
  313. if ( nodeClicked && clickedRoadPtr )
  314. {
  315. // A double-click on a node in Normal mode means set AddNode mode.
  316. if ( clickedNodeIdx == 0 )
  317. {
  318. setSelectedRoad( clickedRoadPtr );
  319. setSelectedNode( clickedNodeIdx );
  320. mAddNodeIdx = clickedNodeIdx;
  321. mMode = mAddNodeMode;
  322. mSelNode = mSelRoad->insertNode( tPos, mDefaultWidth, mAddNodeIdx );
  323. mIsDirty = true;
  324. return;
  325. }
  326. else if ( clickedNodeIdx == clickedRoadPtr->mNodes.size() - 1 )
  327. {
  328. setSelectedRoad( clickedRoadPtr );
  329. setSelectedNode( clickedNodeIdx );
  330. mAddNodeIdx = U32_MAX;
  331. mMode = mAddNodeMode;
  332. mSelNode = mSelRoad->addNode( tPos, mDefaultWidth );
  333. mIsDirty = true;
  334. setSelectedNode( mSelNode );
  335. return;
  336. }
  337. }
  338. DecalRoad *newRoad = new DecalRoad;
  339. if (mMaterialAsset.notNull())
  340. newRoad->_setMaterial(mMaterialAssetId);
  341. newRoad->registerObject();
  342. // Add to scene
  343. Scene* scene = Scene::getRootScene();
  344. if ( !scene )
  345. Con::errorf( "GuiDecalRoadEditorCtrl - could not find scene to add new DecalRoad" );
  346. else
  347. scene->addObject( newRoad );
  348. newRoad->insertNode( tPos, mDefaultWidth, 0 );
  349. U32 newNode = newRoad->insertNode( tPos, mDefaultWidth, 1 );
  350. // Always add to the end of the road, the first node is the start.
  351. mAddNodeIdx = U32_MAX;
  352. setSelectedRoad( newRoad );
  353. setSelectedNode( newNode );
  354. mMode = mAddNodeMode;
  355. // Disable the hover node while in addNodeMode, we
  356. // don't want some random node enlarged.
  357. mHoverNode = -1;
  358. // Grab the mission editor undo manager.
  359. UndoManager *undoMan = NULL;
  360. if ( !Sim::findObject( "EUndoManager", undoMan ) )
  361. {
  362. Con::errorf( "GuiRoadEditorCtrl::on3DMouseDown() - EUndoManager not found!" );
  363. return;
  364. }
  365. // Create the UndoAction.
  366. MECreateUndoAction *action = new MECreateUndoAction("Create Road");
  367. action->addObject( newRoad );
  368. // Submit it.
  369. undoMan->addAction( action );
  370. //send a callback to script after were done here if one exists
  371. if ( isMethod( "onRoadCreation" ) )
  372. Con::executef( this, "onRoadCreation" );
  373. return;
  374. }
  375. else if ( mMode == mAddNodeMode )
  376. {
  377. // Oops the road got deleted, maybe from an undo action?
  378. // Back to NormalMode.
  379. if ( mSelRoad )
  380. {
  381. // A double-click on a node in Normal mode means set AddNode mode.
  382. if ( clickedNodeIdx == 0 )
  383. {
  384. submitUndo( "Add Node" );
  385. mAddNodeIdx = clickedNodeIdx;
  386. mMode = mAddNodeMode;
  387. mSelNode = mSelRoad->insertNode( tPos, mDefaultWidth, mAddNodeIdx );
  388. mIsDirty = true;
  389. setSelectedNode( mSelNode );
  390. return;
  391. }
  392. else
  393. {
  394. if( clickedRoadPtr && clickedNodeIdx == clickedRoadPtr->mNodes.size() - 1 )
  395. {
  396. submitUndo( "Add Node" );
  397. mAddNodeIdx = U32_MAX;
  398. mMode = mAddNodeMode;
  399. mSelNode = mSelRoad->addNode( tPos, mDefaultWidth );
  400. mIsDirty = true;
  401. setSelectedNode( mSelNode );
  402. return;
  403. }
  404. else
  405. {
  406. submitUndo( "Insert Node" );
  407. // A single-click on empty space while in
  408. // AddNode mode means insert / add a node.
  409. //submitUndo( "Add Node" );
  410. //F32 width = mSelRoad->getNodeWidth( mSelNode );
  411. U32 newNode = mSelRoad->insertNode( tPos, mDefaultWidth, mAddNodeIdx);
  412. mIsDirty = true;
  413. setSelectedNode( newNode );
  414. return;
  415. }
  416. }
  417. }
  418. }
  419. else if ( mMode == mInsertPointMode && mSelRoad != NULL)
  420. {
  421. if ( clickedRoadPtr == mSelRoad )
  422. {
  423. F32 w0 = mSelRoad->getNodeWidth( insertNodeIdx );
  424. F32 w1 = mSelRoad->getNodeWidth( insertNodeIdx + 1 );
  425. F32 width = ( w0 + w1 ) * 0.5f;
  426. submitUndo( "Insert Node" );
  427. U32 newNode = mSelRoad->insertNode( tPos, width, insertNodeIdx + 1);
  428. mIsDirty = true;
  429. setSelectedNode( newNode );
  430. return;
  431. }
  432. }
  433. else if ( mMode == mRemovePointMode && mSelRoad != NULL)
  434. {
  435. if ( nodeClicked && clickedRoadPtr == mSelRoad )
  436. {
  437. setSelectedNode( clickedNodeIdx );
  438. deleteSelectedNode();
  439. return;
  440. }
  441. }
  442. else if ( mMode == mMovePointMode )
  443. {
  444. if ( nodeClicked && clickedRoadPtr == mSelRoad )
  445. {
  446. setSelectedNode( clickedNodeIdx );
  447. return;
  448. }
  449. }
  450. else if ( mMode == mScalePointMode )
  451. {
  452. if ( nodeClicked && clickedRoadPtr == mSelRoad )
  453. {
  454. setSelectedNode( clickedNodeIdx );
  455. return;
  456. }
  457. }
  458. }
  459. void GuiRoadEditorCtrl::on3DRightMouseDown(const Gui3DMouseEvent & event)
  460. {
  461. //mIsPanning = true;
  462. }
  463. void GuiRoadEditorCtrl::on3DRightMouseUp(const Gui3DMouseEvent & event)
  464. {
  465. //mIsPanning = false;
  466. }
  467. void GuiRoadEditorCtrl::on3DMouseUp(const Gui3DMouseEvent & event)
  468. {
  469. mStartWidth = -1.0f;
  470. mSavedDrag = false;
  471. mouseUnlock();
  472. }
  473. void GuiRoadEditorCtrl::on3DMouseMove(const Gui3DMouseEvent & event)
  474. {
  475. if ( mSelRoad != NULL && mMode == mAddNodeMode )
  476. {
  477. Point3F startPnt = event.pos;
  478. Point3F endPnt = event.pos + event.vec * 1000.0f;
  479. RayInfo ri;
  480. if ( gServerContainer.castRay(startPnt, endPnt, TerrainObjectType, &ri) )
  481. {
  482. mSelRoad->setNodePosition( mSelNode, ri.point );
  483. mIsDirty = true;
  484. }
  485. return;
  486. }
  487. // Is cursor hovering over a road?
  488. if ( mMode == mSelectRoadMode )
  489. {
  490. mHoverRoad = NULL;
  491. Point3F startPnt = event.pos;
  492. Point3F endPnt = event.pos + event.vec * 1000.0f;
  493. RayInfo ri;
  494. if ( gServerContainer.castRay(startPnt, endPnt, TerrainObjectType, &ri) )
  495. {
  496. DecalRoad *pRoad = NULL;
  497. for ( SimSetIterator iter(mRoadSet); *iter; ++iter )
  498. {
  499. pRoad = static_cast<DecalRoad*>( *iter );
  500. if ( pRoad->containsPoint( ri.point ) )
  501. {
  502. mHoverRoad = pRoad;
  503. break;
  504. }
  505. }
  506. }
  507. }
  508. // Is cursor hovering over a RoadNode?
  509. if ( mHoverRoad )
  510. {
  511. DecalRoad *pRoad = mHoverRoad;
  512. S32 hoverNodeIdx = -1;
  513. F32 hoverNodeDist = F32_MAX;
  514. for ( U32 i = 0; i < pRoad->mNodes.size(); i++ )
  515. {
  516. const Point3F &nodePos = pRoad->mNodes[i].point;
  517. Point3F screenPos;
  518. project( nodePos, &screenPos );
  519. RectI rect( Point2I((S32)screenPos.x,(S32)screenPos.y) - mNodeHalfSize, mNodeHalfSize * 2 );
  520. if ( rect.pointInRect( event.mousePoint ) && screenPos.z < hoverNodeDist )
  521. {
  522. hoverNodeDist = screenPos.z;
  523. hoverNodeIdx = i;
  524. }
  525. }
  526. mHoverNode = hoverNodeIdx;
  527. }
  528. }
  529. void GuiRoadEditorCtrl::on3DMouseDragged(const Gui3DMouseEvent & event)
  530. {
  531. // Drags are only used to transform nodes
  532. if ( !mSelRoad || mSelNode == -1 ||
  533. ( mMode != mMovePointMode && mMode != mScalePointMode ) )
  534. return;
  535. if ( !mSavedDrag )
  536. {
  537. submitUndo( "Modify Node" );
  538. mSavedDrag = true;
  539. }
  540. if ( mMode == mScalePointMode )
  541. {
  542. Point3F tPos;
  543. if ( !getTerrainPos( event, tPos ) )
  544. return;
  545. if ( mStartWidth == -1.0f )
  546. {
  547. mStartWidth = mSelRoad->mNodes[mSelNode].width;
  548. mStartX = event.mousePoint.x;
  549. mStartWorld = tPos;
  550. }
  551. S32 deltaScreenX = event.mousePoint.x - mStartX;
  552. F32 worldDist = ( event.pos - mStartWorld ).len();
  553. F32 deltaWorldX = ( deltaScreenX * worldDist ) / getWorldToScreenScale().y;
  554. F32 width = mStartWidth + deltaWorldX;
  555. mSelRoad->setNodeWidth( mSelNode, width );
  556. mIsDirty = true;
  557. }
  558. else if( mMode == mMovePointMode )
  559. {
  560. Point3F tPos;
  561. if ( !getTerrainPos( event, tPos ) )
  562. return;
  563. mSelRoad->setNodePosition( mSelNode, tPos );
  564. mIsDirty = true;
  565. }
  566. Con::executef( this, "onNodeModified", Con::getIntArg(mSelNode) );
  567. }
  568. void GuiRoadEditorCtrl::on3DMouseEnter(const Gui3DMouseEvent & event)
  569. {
  570. // nothing to do
  571. }
  572. void GuiRoadEditorCtrl::on3DMouseLeave(const Gui3DMouseEvent & event)
  573. {
  574. // nothing to do
  575. }
  576. bool GuiRoadEditorCtrl::onKeyDown(const GuiEvent& event)
  577. {
  578. if( event.keyCode == KEY_RETURN && mMode == mAddNodeMode )
  579. {
  580. // Delete the node attached to the cursor.
  581. deleteSelectedNode();
  582. mMode = mAddRoadMode;
  583. return true;
  584. }
  585. return false;
  586. }
  587. void GuiRoadEditorCtrl::updateGuiInfo()
  588. {
  589. // nothing to do
  590. }
  591. void GuiRoadEditorCtrl::onRender( Point2I offset, const RectI &updateRect )
  592. {
  593. PROFILE_SCOPE( GuiRoadEditorCtrl_OnRender );
  594. Parent::onRender( offset, updateRect );
  595. return;
  596. }
  597. void GuiRoadEditorCtrl::renderScene(const RectI & updateRect)
  598. {
  599. GFX->setStateBlock( mZDisableSB );
  600. // Draw the spline based from the client-side road
  601. // because the serverside spline is not actually reliable...
  602. // Can be incorrect if the DecalRoad is before the TerrainBlock
  603. // in the scene.
  604. if ( mHoverRoad && mHoverRoad != mSelRoad )
  605. {
  606. DecalRoad *pRoad = (DecalRoad*)mHoverRoad->getClientObject();
  607. if ( pRoad )
  608. _drawRoadSpline( pRoad, mHoverSplineColor );
  609. }
  610. if ( mSelRoad )
  611. {
  612. DecalRoad *pRoad = (DecalRoad*)mSelRoad->getClientObject();
  613. if ( pRoad )
  614. _drawRoadSpline( pRoad, mSelectedSplineColor );
  615. }
  616. }
  617. void GuiRoadEditorCtrl::renderGui( Point2I offset, const RectI &updateRect )
  618. {
  619. // Draw Control nodes for selected and highlighted roads
  620. if ( mHoverRoad )
  621. _drawRoadControlNodes( mHoverRoad, mHoverSplineColor );
  622. if ( mSelRoad )
  623. _drawRoadControlNodes( mSelRoad, mSelectedSplineColor );
  624. Parent::renderGui(offset, updateRect);
  625. }
  626. void GuiRoadEditorCtrl::_drawRoadSpline( DecalRoad *road, const ColorI &color )
  627. {
  628. if ( road->mEdges.size() <= 1 )
  629. return;
  630. GFXTransformSaver saver;
  631. if ( DecalRoad::smShowSpline )
  632. {
  633. // Render the center-line
  634. PrimBuild::color( color );
  635. PrimBuild::begin( GFXLineStrip, road->mEdges.size() );
  636. for ( U32 i = 0; i < road->mEdges.size(); i++ )
  637. {
  638. PrimBuild::vertex3fv( road->mEdges[i].p1 );
  639. }
  640. PrimBuild::end();
  641. }
  642. if ( DecalRoad::smWireframe )
  643. {
  644. // Left-side line
  645. PrimBuild::color3i( 100, 100, 100 );
  646. PrimBuild::begin( GFXLineStrip, road->mEdges.size() );
  647. for ( U32 i = 0; i < road->mEdges.size(); i++ )
  648. {
  649. PrimBuild::vertex3fv( road->mEdges[i].p0 );
  650. }
  651. PrimBuild::end();
  652. // Right-side line
  653. PrimBuild::begin( GFXLineStrip, road->mEdges.size() );
  654. for ( U32 i = 0; i < road->mEdges.size(); i++ )
  655. {
  656. PrimBuild::vertex3fv( road->mEdges[i].p2 );
  657. }
  658. PrimBuild::end();
  659. // Cross-sections
  660. PrimBuild::begin( GFXLineList, road->mEdges.size() * 2 );
  661. for ( U32 i = 0; i < road->mEdges.size(); i++ )
  662. {
  663. PrimBuild::vertex3fv( road->mEdges[i].p0 );
  664. PrimBuild::vertex3fv( road->mEdges[i].p2 );
  665. }
  666. PrimBuild::end();
  667. }
  668. }
  669. void GuiRoadEditorCtrl::_drawRoadControlNodes( DecalRoad *road, const ColorI &color )
  670. {
  671. if ( !DecalRoad::smShowSpline )
  672. return;
  673. RectI bounds = getBounds();
  674. GFXDrawUtil *drawer = GFX->getDrawUtil();
  675. bool isSelected = ( road == mSelRoad );
  676. bool isHighlighted = ( road == mHoverRoad );
  677. for ( U32 i = 0; i < road->mNodes.size(); i++ )
  678. {
  679. if ( false && isSelected && mSelNode == i )
  680. continue;
  681. const Point3F &wpos = road->mNodes[i].point;
  682. Point3F spos;
  683. project( wpos, &spos );
  684. if ( spos.z > 1.0f )
  685. continue;
  686. Point2I posi;
  687. posi.x = spos.x;
  688. posi.y = spos.y;
  689. if ( !bounds.pointInRect( posi ) )
  690. continue;
  691. ColorI theColor = color;
  692. Point2I nodeHalfSize = mNodeHalfSize;
  693. if ( isHighlighted && mHoverNode == i )
  694. nodeHalfSize += Point2I(2,2);
  695. if ( isSelected )
  696. {
  697. if ( mSelNode == i )
  698. {
  699. theColor.set(0,0,255);
  700. }
  701. else if ( i == 0 )
  702. {
  703. theColor.set(0,255,0);
  704. }
  705. else if ( i == road->mNodes.size() - 1 )
  706. {
  707. theColor.set(255,0,0);
  708. }
  709. }
  710. drawer->drawRectFill( posi - nodeHalfSize, posi + nodeHalfSize, theColor );
  711. }
  712. }
  713. bool GuiRoadEditorCtrl::getTerrainPos( const Gui3DMouseEvent & event, Point3F &tpos )
  714. {
  715. // Find clicked point on the terrain
  716. Point3F startPnt = event.pos;
  717. Point3F endPnt = event.pos + event.vec * 10000.0f;
  718. RayInfo ri;
  719. bool hit;
  720. hit = gServerContainer.castRay(startPnt, endPnt, TerrainObjectType, &ri);
  721. tpos = ri.point;
  722. return hit;
  723. }
  724. void GuiRoadEditorCtrl::deleteSelectedNode()
  725. {
  726. if ( !mSelRoad || mSelNode == -1 )
  727. return;
  728. // If the road has only two nodes remaining,
  729. // delete the whole road.
  730. if ( mSelRoad->mNodes.size() <= 2 )
  731. {
  732. deleteSelectedRoad();
  733. }
  734. else
  735. {
  736. // Only submit undo if we weren't in AddMode
  737. if ( mMode != mAddNodeMode )
  738. submitUndo( "Delete Node" );
  739. // Delete the SelectedNode of the SelectedRoad
  740. mSelRoad->deleteNode(mSelNode);
  741. mIsDirty = true;
  742. // We deleted the Node but not the Road (it has nodes left)
  743. // so decrement the currently selected node.
  744. if ( mSelRoad->mNodes.size() <= mSelNode )
  745. mSelNode--;
  746. }
  747. }
  748. void GuiRoadEditorCtrl::deleteSelectedRoad( bool undoAble )
  749. {
  750. AssertFatal( mSelRoad != NULL, "GuiRoadEditorCtrl::deleteSelectedRoad() - No road IS selected" );
  751. // Not undo-able? Just delete it.
  752. if ( !undoAble )
  753. {
  754. DecalRoad *lastRoad = mSelRoad;
  755. setSelectedRoad(NULL);
  756. lastRoad->deleteObject();
  757. mIsDirty = true;
  758. return;
  759. }
  760. // Grab the mission editor undo manager.
  761. UndoManager *undoMan = NULL;
  762. if ( !Sim::findObject( "EUndoManager", undoMan ) )
  763. {
  764. // Couldn't find it? Well just delete the road.
  765. Con::errorf( "GuiRoadEditorCtrl::on3DMouseDown() - EUndoManager not found!" );
  766. return;
  767. }
  768. else
  769. {
  770. DecalRoad *lastRoad = mSelRoad;
  771. setSelectedRoad(NULL);
  772. // Create the UndoAction.
  773. MEDeleteUndoAction *action = new MEDeleteUndoAction("Deleted Road");
  774. action->deleteObject( lastRoad );
  775. mIsDirty = true;
  776. // Submit it.
  777. undoMan->addAction( action );
  778. }
  779. }
  780. void GuiRoadEditorCtrl::setMode( String mode, bool sourceShortcut = false )
  781. {
  782. mMode = mode;
  783. if( sourceShortcut )
  784. Con::executef( this, "paletteSync", mode );
  785. }
  786. void GuiRoadEditorCtrl::setSelectedRoad( DecalRoad *road )
  787. {
  788. mSelRoad = road;
  789. if ( road != NULL )
  790. Con::executef( this, "onRoadSelected", road->getIdString() );
  791. else
  792. Con::executef( this, "onRoadSelected" );
  793. if ( mSelRoad != road )
  794. setSelectedNode(-1);
  795. }
  796. void GuiRoadEditorCtrl::setNodeWidth( F32 width )
  797. {
  798. if ( mSelRoad && mSelNode != -1 )
  799. {
  800. mSelRoad->setNodeWidth( mSelNode, width );
  801. mIsDirty = true;
  802. }
  803. }
  804. F32 GuiRoadEditorCtrl::getNodeWidth()
  805. {
  806. if ( mSelRoad && mSelNode != -1 )
  807. return mSelRoad->getNodeWidth( mSelNode );
  808. return 0.0f;
  809. }
  810. void GuiRoadEditorCtrl::setNodePosition(const Point3F& pos)
  811. {
  812. if ( mSelRoad && mSelNode != -1 )
  813. {
  814. mSelRoad->setNodePosition( mSelNode, pos );
  815. mIsDirty = true;
  816. }
  817. }
  818. Point3F GuiRoadEditorCtrl::getNodePosition()
  819. {
  820. if ( mSelRoad && mSelNode != -1 )
  821. return mSelRoad->getNodePosition( mSelNode );
  822. return Point3F( 0, 0, 0 );
  823. }
  824. void GuiRoadEditorCtrl::setSelectedNode( S32 node )
  825. {
  826. //if ( mSelNode == node )
  827. // return;
  828. mSelNode = node;
  829. if ( mSelNode != -1 && mSelRoad != NULL )
  830. Con::executef( this, "onNodeSelected", Con::getIntArg(mSelNode), Con::getFloatArg(mSelRoad->mNodes[mSelNode].width) );
  831. else
  832. Con::executef( this, "onNodeSelected", Con::getIntArg(-1) );
  833. }
  834. void GuiRoadEditorCtrl::submitUndo( const UTF8 *name )
  835. {
  836. // Grab the mission editor undo manager.
  837. UndoManager *undoMan = NULL;
  838. if ( !Sim::findObject( "EUndoManager", undoMan ) )
  839. {
  840. Con::errorf( "GuiRoadEditorCtrl::submitUndo() - EUndoManager not found!" );
  841. return;
  842. }
  843. // Setup the action.
  844. GuiRoadEditorUndoAction *action = new GuiRoadEditorUndoAction( name );
  845. action->mObjId = mSelRoad->getId();
  846. action->mBreakAngle = mSelRoad->mBreakAngle;
  847. action->mMaterialAssetId = mSelRoad->mMaterialAssetId;
  848. action->mSegmentsPerBatch = mSelRoad->mSegmentsPerBatch;
  849. action->mTextureLength = mSelRoad->mTextureLength;
  850. action->mRoadEditor = this;
  851. for( U32 i = 0; i < mSelRoad->mNodes.size(); i++ )
  852. {
  853. action->mNodes.push_back( mSelRoad->mNodes[i] );
  854. }
  855. undoMan->addAction( action );
  856. }
  857. DefineEngineMethod( GuiRoadEditorCtrl, deleteNode, void, (), , "deleteNode()" )
  858. {
  859. object->deleteSelectedNode();
  860. }
  861. DefineEngineMethod( GuiRoadEditorCtrl, getMode, const char*, (), , "" )
  862. {
  863. return object->getMode();
  864. }
  865. DefineEngineMethod( GuiRoadEditorCtrl, setMode, void, ( const char * mode ), , "setMode( String mode )" )
  866. {
  867. String newMode = ( mode );
  868. object->setMode( newMode );
  869. }
  870. DefineEngineMethod( GuiRoadEditorCtrl, getNodeWidth, F32, (), , "" )
  871. {
  872. return object->getNodeWidth();
  873. }
  874. DefineEngineMethod( GuiRoadEditorCtrl, setNodeWidth, void, ( F32 width ), , "" )
  875. {
  876. object->setNodeWidth( width );
  877. }
  878. DefineEngineMethod( GuiRoadEditorCtrl, getNodePosition, Point3F, (), , "" )
  879. {
  880. return object->getNodePosition();
  881. }
  882. DefineEngineMethod( GuiRoadEditorCtrl, setNodePosition, void, ( Point3F pos ), , "" )
  883. {
  884. object->setNodePosition( pos );
  885. }
  886. DefineEngineMethod( GuiRoadEditorCtrl, setSelectedRoad, void, ( const char * pathRoad ), (""), "" )
  887. {
  888. if (String::compare( pathRoad,"")==0 )
  889. object->setSelectedRoad(NULL);
  890. else
  891. {
  892. DecalRoad *road = NULL;
  893. if ( Sim::findObject( pathRoad, road ) )
  894. object->setSelectedRoad(road);
  895. }
  896. }
  897. DefineEngineMethod( GuiRoadEditorCtrl, getSelectedRoad, S32, (), , "" )
  898. {
  899. DecalRoad *road = object->getSelectedRoad();
  900. if ( road )
  901. return road->getId();
  902. return 0;
  903. }
  904. DefineEngineMethod( GuiRoadEditorCtrl, getSelectedNode, S32, (), , "" )
  905. {
  906. return object->getSelectedNode();
  907. }
  908. DefineEngineMethod( GuiRoadEditorCtrl, deleteRoad, void, (), , "" )
  909. {
  910. object->deleteSelectedRoad();
  911. }