guiRoadEditorCtrl.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110
  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. mMaterialName = StringTable->insert("DefaultDecalRoadMaterial");
  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 materialName = road->mMaterialName;
  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->mMaterialName = materialName;
  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. mMaterialName = materialName;
  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. addField( "materialName", TypeString, Offset( mMaterialName, GuiRoadEditorCtrl ),
  138. "Default Material used by the Road Editor on road creation." );
  139. //addField( "MoveNodeCursor", TYPEID< SimObject >(), Offset( mMoveNodeCursor, GuiRoadEditorCtrl) );
  140. //addField( "AddNodeCursor", TYPEID< SimObject >(), Offset( mAddNodeCursor, GuiRoadEditorCtrl) );
  141. //addField( "InsertNodeCursor", TYPEID< SimObject >(), Offset( mInsertNodeCursor, GuiRoadEditorCtrl) );
  142. //addField( "ResizeNodeCursor", TYPEID< SimObject >(), Offset( mResizeNodeCursor, GuiRoadEditorCtrl) );
  143. Parent::initPersistFields();
  144. }
  145. void GuiRoadEditorCtrl::onSleep()
  146. {
  147. Parent::onSleep();
  148. mMode = mSelectRoadMode;
  149. mHoverNode = -1;
  150. mHoverRoad = NULL;
  151. setSelectedNode(-1);
  152. }
  153. void GuiRoadEditorCtrl::get3DCursor( GuiCursor *&cursor,
  154. bool &visible,
  155. const Gui3DMouseEvent &event_ )
  156. {
  157. //cursor = mAddNodeCursor;
  158. //visible = false;
  159. cursor = NULL;
  160. visible = false;
  161. GuiCanvas *root = getRoot();
  162. if ( !root )
  163. return;
  164. S32 currCursor = PlatformCursorController::curArrow;
  165. if ( root->mCursorChanged == currCursor )
  166. return;
  167. PlatformWindow *window = root->getPlatformWindow();
  168. PlatformCursorController *controller = window->getCursorController();
  169. // We've already changed the cursor,
  170. // so set it back before we change it again.
  171. if( root->mCursorChanged != -1)
  172. controller->popCursor();
  173. // Now change the cursor shape
  174. controller->pushCursor(currCursor);
  175. root->mCursorChanged = currCursor;
  176. }
  177. void GuiRoadEditorCtrl::on3DMouseDown(const Gui3DMouseEvent & event)
  178. {
  179. if ( !isFirstResponder() )
  180. setFirstResponder();
  181. // Get the clicked terrain position.
  182. Point3F tPos;
  183. if ( !getTerrainPos( event, tPos ) )
  184. return;
  185. mouseLock();
  186. // Find any road / node at the clicked position.
  187. // TODO: handle overlapping roads/nodes somehow, cycle through them.
  188. DecalRoad *roadPtr = NULL;
  189. S32 closestNodeIdx = -1;
  190. F32 closestDist = F32_MAX;
  191. DecalRoad *closestNodeRoad = NULL;
  192. // First, find the closest node in any road to the clicked position.
  193. for ( SimSetIterator iter(mRoadSet); *iter; ++iter )
  194. {
  195. roadPtr = static_cast<DecalRoad*>( *iter );
  196. U32 idx;
  197. if ( roadPtr->getClosestNode( tPos, idx ) )
  198. {
  199. Point3F nodePos = roadPtr->getNodePosition(idx);
  200. F32 dist = ( nodePos - tPos ).len();
  201. if ( dist < closestDist )
  202. {
  203. closestNodeIdx = idx;
  204. closestDist = dist;
  205. closestNodeRoad = roadPtr;
  206. }
  207. }
  208. }
  209. //
  210. // Second, determine if the screen-space node rectangle
  211. // contains the clicked position.
  212. bool nodeClicked = false;
  213. S32 clickedNodeIdx = -1;
  214. if ( closestNodeIdx != -1 )
  215. {
  216. Point3F nodePos = closestNodeRoad->getNodePosition( closestNodeIdx );
  217. Point3F temp;
  218. project( nodePos, &temp );
  219. Point2I screenPos( temp.x, temp.y );
  220. RectI nodeRect( screenPos - mNodeHalfSize, mNodeHalfSize * 2 );
  221. nodeClicked = nodeRect.pointInRect( event.mousePoint );
  222. if ( nodeClicked )
  223. clickedNodeIdx = closestNodeIdx;
  224. }
  225. //
  226. // Determine the clickedRoad
  227. //
  228. DecalRoad *clickedRoadPtr = NULL;
  229. U32 insertNodeIdx = 0;
  230. if ( nodeClicked && (mSelRoad == NULL || closestNodeRoad == mSelRoad) )
  231. {
  232. // If a node was clicked, the owning road is always
  233. // considered the clicked road.
  234. clickedRoadPtr = closestNodeRoad;
  235. }
  236. else
  237. {
  238. // check the selected road first
  239. if ( mSelRoad != NULL && mSelRoad->containsPoint( tPos, &insertNodeIdx ) )
  240. {
  241. clickedRoadPtr = mSelRoad;
  242. nodeClicked = false;
  243. clickedNodeIdx = -1;
  244. }
  245. else
  246. {
  247. // Otherwise, we must ask each road if it contains
  248. // the clicked pos.
  249. for ( SimSetIterator iter(mRoadSet); *iter; ++iter )
  250. {
  251. roadPtr = static_cast<DecalRoad*>( *iter );
  252. if ( roadPtr->containsPoint( tPos, &insertNodeIdx ) )
  253. {
  254. clickedRoadPtr = roadPtr;
  255. break;
  256. }
  257. }
  258. }
  259. }
  260. // shortcuts
  261. bool dblClick = ( event.mouseClickCount > 1 );
  262. if( dblClick )
  263. {
  264. if( mMode == mSelectRoadMode )
  265. {
  266. setMode( mAddRoadMode, true );
  267. return;
  268. }
  269. if( mMode == mAddNodeMode )
  270. {
  271. // Delete the node attached to the cursor.
  272. deleteSelectedNode();
  273. mMode = mAddRoadMode;
  274. return;
  275. }
  276. }
  277. //this check is here in order to bounce back from deleting a whole road with ctrl+z
  278. //this check places the editor back into addroadmode
  279. if ( mMode == mAddNodeMode )
  280. {
  281. if ( !mSelRoad )
  282. mMode = mAddRoadMode;
  283. }
  284. if ( mMode == mSelectRoadMode )
  285. {
  286. // Did not click on a road or a node.
  287. if ( !clickedRoadPtr )
  288. {
  289. setSelectedRoad( NULL );
  290. setSelectedNode( -1 );
  291. return;
  292. }
  293. // Clicked on a road that wasn't the currently selected road.
  294. if ( clickedRoadPtr != mSelRoad )
  295. {
  296. setSelectedRoad( clickedRoadPtr );
  297. setSelectedNode( -1 );
  298. return;
  299. }
  300. // Clicked on a node in the currently selected road that wasn't
  301. // the currently selected node.
  302. if ( nodeClicked )
  303. {
  304. setSelectedNode( clickedNodeIdx );
  305. return;
  306. }
  307. // Clicked a position on the currently selected road
  308. // that did not contain a node.
  309. //U32 newNode = clickedRoadPtr->insertNode( tPos, mDefaultWidth, insertNodeIdx );
  310. //setSelectedNode( newNode );
  311. }
  312. else if ( mMode == mAddRoadMode )
  313. {
  314. if ( nodeClicked && clickedRoadPtr )
  315. {
  316. // A double-click on a node in Normal mode means set AddNode mode.
  317. if ( clickedNodeIdx == 0 )
  318. {
  319. setSelectedRoad( clickedRoadPtr );
  320. setSelectedNode( clickedNodeIdx );
  321. mAddNodeIdx = clickedNodeIdx;
  322. mMode = mAddNodeMode;
  323. mSelNode = mSelRoad->insertNode( tPos, mDefaultWidth, mAddNodeIdx );
  324. mIsDirty = true;
  325. return;
  326. }
  327. else if ( clickedNodeIdx == clickedRoadPtr->mNodes.size() - 1 )
  328. {
  329. setSelectedRoad( clickedRoadPtr );
  330. setSelectedNode( clickedNodeIdx );
  331. mAddNodeIdx = U32_MAX;
  332. mMode = mAddNodeMode;
  333. mSelNode = mSelRoad->addNode( tPos, mDefaultWidth );
  334. mIsDirty = true;
  335. setSelectedNode( mSelNode );
  336. return;
  337. }
  338. }
  339. DecalRoad *newRoad = new DecalRoad;
  340. newRoad->mMaterialName = mMaterialName;
  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->mMaterialName = mSelRoad->mMaterialName;
  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 NULL;
  903. }
  904. DefineEngineMethod( GuiRoadEditorCtrl, getSelectedNode, S32, (), , "" )
  905. {
  906. return object->getSelectedNode();
  907. }
  908. DefineEngineMethod( GuiRoadEditorCtrl, deleteRoad, void, (), , "" )
  909. {
  910. object->deleteSelectedRoad();
  911. }