guiRoadEditorCtrl.cpp 29 KB

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