guiRoadEditorCtrl.cpp 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109
  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. mIsDirty = false;
  73. mMaterialName = StringTable->insert("DefaultDecalRoadMaterial");
  74. }
  75. GuiRoadEditorCtrl::~GuiRoadEditorCtrl()
  76. {
  77. // nothing to do
  78. }
  79. void GuiRoadEditorUndoAction::undo()
  80. {
  81. DecalRoad *road = NULL;
  82. if ( !Sim::findObject( mObjId, road ) )
  83. return;
  84. // Temporarily save the roads current data.
  85. String materialName = road->mMaterialName;
  86. F32 textureLength = road->mTextureLength;
  87. F32 breakAngle = road->mBreakAngle;
  88. F32 segmentsPerBatch = road->mSegmentsPerBatch;
  89. Vector<RoadNode> nodes;
  90. nodes.merge( road->mNodes );
  91. // Restore the Road properties saved in the UndoAction
  92. road->mMaterialName = materialName;
  93. road->mBreakAngle = breakAngle;
  94. road->mSegmentsPerBatch = segmentsPerBatch;
  95. road->mTextureLength = textureLength;
  96. road->inspectPostApply();
  97. // Restore the Nodes saved in the UndoAction
  98. road->mNodes.clear();
  99. for ( U32 i = 0; i < mNodes.size(); i++ )
  100. {
  101. road->_addNode( mNodes[i].point, mNodes[i].width );
  102. }
  103. // Regenerate the road
  104. road->regenerate();
  105. // If applicable set the selected road and node
  106. mRoadEditor->mSelRoad = road;
  107. mRoadEditor->mSelNode = -1;
  108. // Now save the previous Road data in this UndoAction
  109. // since an undo action must become a redo action and vice-versa
  110. mMaterialName = materialName;
  111. mBreakAngle = breakAngle;
  112. mSegmentsPerBatch = segmentsPerBatch;
  113. mTextureLength = textureLength;
  114. mNodes.clear();
  115. mNodes.merge( nodes );
  116. }
  117. bool GuiRoadEditorCtrl::onAdd()
  118. {
  119. if( !Parent::onAdd() )
  120. return false;
  121. mRoadSet = DecalRoad::getServerSet();
  122. GFXStateBlockDesc desc;
  123. desc.setCullMode( GFXCullNone );
  124. desc.setBlend(false);
  125. desc.setZReadWrite( false, false );
  126. mZDisableSB = GFX->createStateBlock(desc);
  127. return true;
  128. }
  129. void GuiRoadEditorCtrl::initPersistFields()
  130. {
  131. addField( "DefaultWidth", TypeF32, Offset( mDefaultWidth, GuiRoadEditorCtrl ) );
  132. addField( "HoverSplineColor", TypeColorI, Offset( mHoverSplineColor, GuiRoadEditorCtrl ) );
  133. addField( "SelectedSplineColor", TypeColorI, Offset( mSelectedSplineColor, GuiRoadEditorCtrl ) );
  134. addField( "HoverNodeColor", TypeColorI, Offset( mHoverNodeColor, GuiRoadEditorCtrl ) );
  135. addField( "isDirty", TypeBool, Offset( mIsDirty, GuiRoadEditorCtrl ) );
  136. addField( "materialName", TypeString, Offset( mMaterialName, GuiRoadEditorCtrl ),
  137. "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. newRoad->mMaterialName = mMaterialName;
  340. newRoad->registerObject();
  341. // Add to scene
  342. Scene* scene = Scene::getRootScene();
  343. if ( !scene )
  344. Con::errorf( "GuiDecalRoadEditorCtrl - could not find scene to add new DecalRoad" );
  345. else
  346. scene->addObject( newRoad );
  347. newRoad->insertNode( tPos, mDefaultWidth, 0 );
  348. U32 newNode = newRoad->insertNode( tPos, mDefaultWidth, 1 );
  349. // Always add to the end of the road, the first node is the start.
  350. mAddNodeIdx = U32_MAX;
  351. setSelectedRoad( newRoad );
  352. setSelectedNode( newNode );
  353. mMode = mAddNodeMode;
  354. // Disable the hover node while in addNodeMode, we
  355. // don't want some random node enlarged.
  356. mHoverNode = -1;
  357. // Grab the mission editor undo manager.
  358. UndoManager *undoMan = NULL;
  359. if ( !Sim::findObject( "EUndoManager", undoMan ) )
  360. {
  361. Con::errorf( "GuiRoadEditorCtrl::on3DMouseDown() - EUndoManager not found!" );
  362. return;
  363. }
  364. // Create the UndoAction.
  365. MECreateUndoAction *action = new MECreateUndoAction("Create Road");
  366. action->addObject( newRoad );
  367. // Submit it.
  368. undoMan->addAction( action );
  369. //send a callback to script after were done here if one exists
  370. if ( isMethod( "onRoadCreation" ) )
  371. Con::executef( this, "onRoadCreation" );
  372. return;
  373. }
  374. else if ( mMode == mAddNodeMode )
  375. {
  376. // Oops the road got deleted, maybe from an undo action?
  377. // Back to NormalMode.
  378. if ( mSelRoad )
  379. {
  380. // A double-click on a node in Normal mode means set AddNode mode.
  381. if ( clickedNodeIdx == 0 )
  382. {
  383. submitUndo( "Add Node" );
  384. mAddNodeIdx = clickedNodeIdx;
  385. mMode = mAddNodeMode;
  386. mSelNode = mSelRoad->insertNode( tPos, mDefaultWidth, mAddNodeIdx );
  387. mIsDirty = true;
  388. setSelectedNode( mSelNode );
  389. return;
  390. }
  391. else
  392. {
  393. if( clickedRoadPtr && clickedNodeIdx == clickedRoadPtr->mNodes.size() - 1 )
  394. {
  395. submitUndo( "Add Node" );
  396. mAddNodeIdx = U32_MAX;
  397. mMode = mAddNodeMode;
  398. mSelNode = mSelRoad->addNode( tPos, mDefaultWidth );
  399. mIsDirty = true;
  400. setSelectedNode( mSelNode );
  401. return;
  402. }
  403. else
  404. {
  405. submitUndo( "Insert Node" );
  406. // A single-click on empty space while in
  407. // AddNode mode means insert / add a node.
  408. //submitUndo( "Add Node" );
  409. //F32 width = mSelRoad->getNodeWidth( mSelNode );
  410. U32 newNode = mSelRoad->insertNode( tPos, mDefaultWidth, mAddNodeIdx);
  411. mIsDirty = true;
  412. setSelectedNode( newNode );
  413. return;
  414. }
  415. }
  416. }
  417. }
  418. else if ( mMode == mInsertPointMode && mSelRoad != NULL)
  419. {
  420. if ( clickedRoadPtr == mSelRoad )
  421. {
  422. F32 w0 = mSelRoad->getNodeWidth( insertNodeIdx );
  423. F32 w1 = mSelRoad->getNodeWidth( insertNodeIdx + 1 );
  424. F32 width = ( w0 + w1 ) * 0.5f;
  425. submitUndo( "Insert Node" );
  426. U32 newNode = mSelRoad->insertNode( tPos, width, insertNodeIdx + 1);
  427. mIsDirty = true;
  428. setSelectedNode( newNode );
  429. return;
  430. }
  431. }
  432. else if ( mMode == mRemovePointMode && mSelRoad != NULL)
  433. {
  434. if ( nodeClicked && clickedRoadPtr == mSelRoad )
  435. {
  436. setSelectedNode( clickedNodeIdx );
  437. deleteSelectedNode();
  438. return;
  439. }
  440. }
  441. else if ( mMode == mMovePointMode )
  442. {
  443. if ( nodeClicked && clickedRoadPtr == mSelRoad )
  444. {
  445. setSelectedNode( clickedNodeIdx );
  446. return;
  447. }
  448. }
  449. else if ( mMode == mScalePointMode )
  450. {
  451. if ( nodeClicked && clickedRoadPtr == mSelRoad )
  452. {
  453. setSelectedNode( clickedNodeIdx );
  454. return;
  455. }
  456. }
  457. }
  458. void GuiRoadEditorCtrl::on3DRightMouseDown(const Gui3DMouseEvent & event)
  459. {
  460. //mIsPanning = true;
  461. }
  462. void GuiRoadEditorCtrl::on3DRightMouseUp(const Gui3DMouseEvent & event)
  463. {
  464. //mIsPanning = false;
  465. }
  466. void GuiRoadEditorCtrl::on3DMouseUp(const Gui3DMouseEvent & event)
  467. {
  468. mStartWidth = -1.0f;
  469. mSavedDrag = false;
  470. mouseUnlock();
  471. }
  472. void GuiRoadEditorCtrl::on3DMouseMove(const Gui3DMouseEvent & event)
  473. {
  474. if ( mSelRoad != NULL && mMode == mAddNodeMode )
  475. {
  476. Point3F startPnt = event.pos;
  477. Point3F endPnt = event.pos + event.vec * 1000.0f;
  478. RayInfo ri;
  479. if ( gServerContainer.castRay(startPnt, endPnt, TerrainObjectType, &ri) )
  480. {
  481. mSelRoad->setNodePosition( mSelNode, ri.point );
  482. mIsDirty = true;
  483. }
  484. return;
  485. }
  486. // Is cursor hovering over a road?
  487. if ( mMode == mSelectRoadMode )
  488. {
  489. mHoverRoad = NULL;
  490. Point3F startPnt = event.pos;
  491. Point3F endPnt = event.pos + event.vec * 1000.0f;
  492. RayInfo ri;
  493. if ( gServerContainer.castRay(startPnt, endPnt, TerrainObjectType, &ri) )
  494. {
  495. DecalRoad *pRoad = NULL;
  496. for ( SimSetIterator iter(mRoadSet); *iter; ++iter )
  497. {
  498. pRoad = static_cast<DecalRoad*>( *iter );
  499. if ( pRoad->containsPoint( ri.point ) )
  500. {
  501. mHoverRoad = pRoad;
  502. break;
  503. }
  504. }
  505. }
  506. }
  507. // Is cursor hovering over a RoadNode?
  508. if ( mHoverRoad )
  509. {
  510. DecalRoad *pRoad = mHoverRoad;
  511. S32 hoverNodeIdx = -1;
  512. F32 hoverNodeDist = F32_MAX;
  513. for ( U32 i = 0; i < pRoad->mNodes.size(); i++ )
  514. {
  515. const Point3F &nodePos = pRoad->mNodes[i].point;
  516. Point3F screenPos;
  517. project( nodePos, &screenPos );
  518. RectI rect( Point2I((S32)screenPos.x,(S32)screenPos.y) - mNodeHalfSize, mNodeHalfSize * 2 );
  519. if ( rect.pointInRect( event.mousePoint ) && screenPos.z < hoverNodeDist )
  520. {
  521. hoverNodeDist = screenPos.z;
  522. hoverNodeIdx = i;
  523. }
  524. }
  525. mHoverNode = hoverNodeIdx;
  526. }
  527. }
  528. void GuiRoadEditorCtrl::on3DMouseDragged(const Gui3DMouseEvent & event)
  529. {
  530. // Drags are only used to transform nodes
  531. if ( !mSelRoad || mSelNode == -1 ||
  532. ( mMode != mMovePointMode && mMode != mScalePointMode ) )
  533. return;
  534. if ( !mSavedDrag )
  535. {
  536. submitUndo( "Modify Node" );
  537. mSavedDrag = true;
  538. }
  539. if ( mMode == mScalePointMode )
  540. {
  541. Point3F tPos;
  542. if ( !getTerrainPos( event, tPos ) )
  543. return;
  544. if ( mStartWidth == -1.0f )
  545. {
  546. mStartWidth = mSelRoad->mNodes[mSelNode].width;
  547. mStartX = event.mousePoint.x;
  548. mStartWorld = tPos;
  549. }
  550. S32 deltaScreenX = event.mousePoint.x - mStartX;
  551. F32 worldDist = ( event.pos - mStartWorld ).len();
  552. F32 deltaWorldX = ( deltaScreenX * worldDist ) / getWorldToScreenScale().y;
  553. F32 width = mStartWidth + deltaWorldX;
  554. mSelRoad->setNodeWidth( mSelNode, width );
  555. mIsDirty = true;
  556. }
  557. else if( mMode == mMovePointMode )
  558. {
  559. Point3F tPos;
  560. if ( !getTerrainPos( event, tPos ) )
  561. return;
  562. mSelRoad->setNodePosition( mSelNode, tPos );
  563. mIsDirty = true;
  564. }
  565. Con::executef( this, "onNodeModified", Con::getIntArg(mSelNode) );
  566. }
  567. void GuiRoadEditorCtrl::on3DMouseEnter(const Gui3DMouseEvent & event)
  568. {
  569. // nothing to do
  570. }
  571. void GuiRoadEditorCtrl::on3DMouseLeave(const Gui3DMouseEvent & event)
  572. {
  573. // nothing to do
  574. }
  575. bool GuiRoadEditorCtrl::onKeyDown(const GuiEvent& event)
  576. {
  577. if( event.keyCode == KEY_RETURN && mMode == mAddNodeMode )
  578. {
  579. // Delete the node attached to the cursor.
  580. deleteSelectedNode();
  581. mMode = mAddRoadMode;
  582. return true;
  583. }
  584. return false;
  585. }
  586. void GuiRoadEditorCtrl::updateGuiInfo()
  587. {
  588. // nothing to do
  589. }
  590. void GuiRoadEditorCtrl::onRender( Point2I offset, const RectI &updateRect )
  591. {
  592. PROFILE_SCOPE( GuiRoadEditorCtrl_OnRender );
  593. Parent::onRender( offset, updateRect );
  594. return;
  595. }
  596. void GuiRoadEditorCtrl::renderScene(const RectI & updateRect)
  597. {
  598. GFX->setStateBlock( mZDisableSB );
  599. // Draw the spline based from the client-side road
  600. // because the serverside spline is not actually reliable...
  601. // Can be incorrect if the DecalRoad is before the TerrainBlock
  602. // in the scene.
  603. if ( mHoverRoad && mHoverRoad != mSelRoad )
  604. {
  605. DecalRoad *pRoad = (DecalRoad*)mHoverRoad->getClientObject();
  606. if ( pRoad )
  607. _drawRoadSpline( pRoad, mHoverSplineColor );
  608. }
  609. if ( mSelRoad )
  610. {
  611. DecalRoad *pRoad = (DecalRoad*)mSelRoad->getClientObject();
  612. if ( pRoad )
  613. _drawRoadSpline( pRoad, mSelectedSplineColor );
  614. }
  615. }
  616. void GuiRoadEditorCtrl::renderGui( Point2I offset, const RectI &updateRect )
  617. {
  618. // Draw Control nodes for selected and highlighted roads
  619. if ( mHoverRoad )
  620. _drawRoadControlNodes( mHoverRoad, mHoverSplineColor );
  621. if ( mSelRoad )
  622. _drawRoadControlNodes( mSelRoad, mSelectedSplineColor );
  623. Parent::renderGui(offset, updateRect);
  624. }
  625. void GuiRoadEditorCtrl::_drawRoadSpline( DecalRoad *road, const ColorI &color )
  626. {
  627. if ( road->mEdges.size() <= 1 )
  628. return;
  629. GFXTransformSaver saver;
  630. if ( DecalRoad::smShowSpline )
  631. {
  632. // Render the center-line
  633. PrimBuild::color( color );
  634. PrimBuild::begin( GFXLineStrip, road->mEdges.size() );
  635. for ( U32 i = 0; i < road->mEdges.size(); i++ )
  636. {
  637. PrimBuild::vertex3fv( road->mEdges[i].p1 );
  638. }
  639. PrimBuild::end();
  640. }
  641. if ( DecalRoad::smWireframe )
  642. {
  643. // Left-side line
  644. PrimBuild::color3i( 100, 100, 100 );
  645. PrimBuild::begin( GFXLineStrip, road->mEdges.size() );
  646. for ( U32 i = 0; i < road->mEdges.size(); i++ )
  647. {
  648. PrimBuild::vertex3fv( road->mEdges[i].p0 );
  649. }
  650. PrimBuild::end();
  651. // Right-side line
  652. PrimBuild::begin( GFXLineStrip, road->mEdges.size() );
  653. for ( U32 i = 0; i < road->mEdges.size(); i++ )
  654. {
  655. PrimBuild::vertex3fv( road->mEdges[i].p2 );
  656. }
  657. PrimBuild::end();
  658. // Cross-sections
  659. PrimBuild::begin( GFXLineList, road->mEdges.size() * 2 );
  660. for ( U32 i = 0; i < road->mEdges.size(); i++ )
  661. {
  662. PrimBuild::vertex3fv( road->mEdges[i].p0 );
  663. PrimBuild::vertex3fv( road->mEdges[i].p2 );
  664. }
  665. PrimBuild::end();
  666. }
  667. }
  668. void GuiRoadEditorCtrl::_drawRoadControlNodes( DecalRoad *road, const ColorI &color )
  669. {
  670. if ( !DecalRoad::smShowSpline )
  671. return;
  672. RectI bounds = getBounds();
  673. GFXDrawUtil *drawer = GFX->getDrawUtil();
  674. bool isSelected = ( road == mSelRoad );
  675. bool isHighlighted = ( road == mHoverRoad );
  676. for ( U32 i = 0; i < road->mNodes.size(); i++ )
  677. {
  678. if ( false && isSelected && mSelNode == i )
  679. continue;
  680. const Point3F &wpos = road->mNodes[i].point;
  681. Point3F spos;
  682. project( wpos, &spos );
  683. if ( spos.z > 1.0f )
  684. continue;
  685. Point2I posi;
  686. posi.x = spos.x;
  687. posi.y = spos.y;
  688. if ( !bounds.pointInRect( posi ) )
  689. continue;
  690. ColorI theColor = color;
  691. Point2I nodeHalfSize = mNodeHalfSize;
  692. if ( isHighlighted && mHoverNode == i )
  693. nodeHalfSize += Point2I(2,2);
  694. if ( isSelected )
  695. {
  696. if ( mSelNode == i )
  697. {
  698. theColor.set(0,0,255);
  699. }
  700. else if ( i == 0 )
  701. {
  702. theColor.set(0,255,0);
  703. }
  704. else if ( i == road->mNodes.size() - 1 )
  705. {
  706. theColor.set(255,0,0);
  707. }
  708. }
  709. drawer->drawRectFill( posi - nodeHalfSize, posi + nodeHalfSize, theColor );
  710. }
  711. }
  712. bool GuiRoadEditorCtrl::getTerrainPos( const Gui3DMouseEvent & event, Point3F &tpos )
  713. {
  714. // Find clicked point on the terrain
  715. Point3F startPnt = event.pos;
  716. Point3F endPnt = event.pos + event.vec * 10000.0f;
  717. RayInfo ri;
  718. bool hit;
  719. hit = gServerContainer.castRay(startPnt, endPnt, TerrainObjectType, &ri);
  720. tpos = ri.point;
  721. return hit;
  722. }
  723. void GuiRoadEditorCtrl::deleteSelectedNode()
  724. {
  725. if ( !mSelRoad || mSelNode == -1 )
  726. return;
  727. // If the road has only two nodes remaining,
  728. // delete the whole road.
  729. if ( mSelRoad->mNodes.size() <= 2 )
  730. {
  731. deleteSelectedRoad();
  732. }
  733. else
  734. {
  735. // Only submit undo if we weren't in AddMode
  736. if ( mMode != mAddNodeMode )
  737. submitUndo( "Delete Node" );
  738. // Delete the SelectedNode of the SelectedRoad
  739. mSelRoad->deleteNode(mSelNode);
  740. mIsDirty = true;
  741. // We deleted the Node but not the Road (it has nodes left)
  742. // so decrement the currently selected node.
  743. if ( mSelRoad->mNodes.size() <= mSelNode )
  744. mSelNode--;
  745. }
  746. }
  747. void GuiRoadEditorCtrl::deleteSelectedRoad( bool undoAble )
  748. {
  749. AssertFatal( mSelRoad != NULL, "GuiRoadEditorCtrl::deleteSelectedRoad() - No road IS selected" );
  750. // Not undo-able? Just delete it.
  751. if ( !undoAble )
  752. {
  753. DecalRoad *lastRoad = mSelRoad;
  754. setSelectedRoad(NULL);
  755. lastRoad->deleteObject();
  756. mIsDirty = true;
  757. return;
  758. }
  759. // Grab the mission editor undo manager.
  760. UndoManager *undoMan = NULL;
  761. if ( !Sim::findObject( "EUndoManager", undoMan ) )
  762. {
  763. // Couldn't find it? Well just delete the road.
  764. Con::errorf( "GuiRoadEditorCtrl::on3DMouseDown() - EUndoManager not found!" );
  765. return;
  766. }
  767. else
  768. {
  769. DecalRoad *lastRoad = mSelRoad;
  770. setSelectedRoad(NULL);
  771. // Create the UndoAction.
  772. MEDeleteUndoAction *action = new MEDeleteUndoAction("Deleted Road");
  773. action->deleteObject( lastRoad );
  774. mIsDirty = true;
  775. // Submit it.
  776. undoMan->addAction( action );
  777. }
  778. }
  779. void GuiRoadEditorCtrl::setMode( String mode, bool sourceShortcut = false )
  780. {
  781. mMode = mode;
  782. if( sourceShortcut )
  783. Con::executef( this, "paletteSync", mode );
  784. }
  785. void GuiRoadEditorCtrl::setSelectedRoad( DecalRoad *road )
  786. {
  787. mSelRoad = road;
  788. if ( road != NULL )
  789. Con::executef( this, "onRoadSelected", road->getIdString() );
  790. else
  791. Con::executef( this, "onRoadSelected" );
  792. if ( mSelRoad != road )
  793. setSelectedNode(-1);
  794. }
  795. void GuiRoadEditorCtrl::setNodeWidth( F32 width )
  796. {
  797. if ( mSelRoad && mSelNode != -1 )
  798. {
  799. mSelRoad->setNodeWidth( mSelNode, width );
  800. mIsDirty = true;
  801. }
  802. }
  803. F32 GuiRoadEditorCtrl::getNodeWidth()
  804. {
  805. if ( mSelRoad && mSelNode != -1 )
  806. return mSelRoad->getNodeWidth( mSelNode );
  807. return 0.0f;
  808. }
  809. void GuiRoadEditorCtrl::setNodePosition(const Point3F& pos)
  810. {
  811. if ( mSelRoad && mSelNode != -1 )
  812. {
  813. mSelRoad->setNodePosition( mSelNode, pos );
  814. mIsDirty = true;
  815. }
  816. }
  817. Point3F GuiRoadEditorCtrl::getNodePosition()
  818. {
  819. if ( mSelRoad && mSelNode != -1 )
  820. return mSelRoad->getNodePosition( mSelNode );
  821. return Point3F( 0, 0, 0 );
  822. }
  823. void GuiRoadEditorCtrl::setSelectedNode( S32 node )
  824. {
  825. //if ( mSelNode == node )
  826. // return;
  827. mSelNode = node;
  828. if ( mSelNode != -1 && mSelRoad != NULL )
  829. Con::executef( this, "onNodeSelected", Con::getIntArg(mSelNode), Con::getFloatArg(mSelRoad->mNodes[mSelNode].width) );
  830. else
  831. Con::executef( this, "onNodeSelected", Con::getIntArg(-1) );
  832. }
  833. void GuiRoadEditorCtrl::submitUndo( const UTF8 *name )
  834. {
  835. // Grab the mission editor undo manager.
  836. UndoManager *undoMan = NULL;
  837. if ( !Sim::findObject( "EUndoManager", undoMan ) )
  838. {
  839. Con::errorf( "GuiRoadEditorCtrl::submitUndo() - EUndoManager not found!" );
  840. return;
  841. }
  842. // Setup the action.
  843. GuiRoadEditorUndoAction *action = new GuiRoadEditorUndoAction( name );
  844. action->mObjId = mSelRoad->getId();
  845. action->mBreakAngle = mSelRoad->mBreakAngle;
  846. action->mMaterialName = mSelRoad->mMaterialName;
  847. action->mSegmentsPerBatch = mSelRoad->mSegmentsPerBatch;
  848. action->mTextureLength = mSelRoad->mTextureLength;
  849. action->mRoadEditor = this;
  850. for( U32 i = 0; i < mSelRoad->mNodes.size(); i++ )
  851. {
  852. action->mNodes.push_back( mSelRoad->mNodes[i] );
  853. }
  854. undoMan->addAction( action );
  855. }
  856. DefineEngineMethod( GuiRoadEditorCtrl, deleteNode, void, (), , "deleteNode()" )
  857. {
  858. object->deleteSelectedNode();
  859. }
  860. DefineEngineMethod( GuiRoadEditorCtrl, getMode, const char*, (), , "" )
  861. {
  862. return object->getMode();
  863. }
  864. DefineEngineMethod( GuiRoadEditorCtrl, setMode, void, ( const char * mode ), , "setMode( String mode )" )
  865. {
  866. String newMode = ( mode );
  867. object->setMode( newMode );
  868. }
  869. DefineEngineMethod( GuiRoadEditorCtrl, getNodeWidth, F32, (), , "" )
  870. {
  871. return object->getNodeWidth();
  872. }
  873. DefineEngineMethod( GuiRoadEditorCtrl, setNodeWidth, void, ( F32 width ), , "" )
  874. {
  875. object->setNodeWidth( width );
  876. }
  877. DefineEngineMethod( GuiRoadEditorCtrl, getNodePosition, Point3F, (), , "" )
  878. {
  879. return object->getNodePosition();
  880. }
  881. DefineEngineMethod( GuiRoadEditorCtrl, setNodePosition, void, ( Point3F pos ), , "" )
  882. {
  883. object->setNodePosition( pos );
  884. }
  885. DefineEngineMethod( GuiRoadEditorCtrl, setSelectedRoad, void, ( const char * pathRoad ), (""), "" )
  886. {
  887. if (dStrcmp( pathRoad,"")==0 )
  888. object->setSelectedRoad(NULL);
  889. else
  890. {
  891. DecalRoad *road = NULL;
  892. if ( Sim::findObject( pathRoad, road ) )
  893. object->setSelectedRoad(road);
  894. }
  895. }
  896. DefineEngineMethod( GuiRoadEditorCtrl, getSelectedRoad, S32, (), , "" )
  897. {
  898. DecalRoad *road = object->getSelectedRoad();
  899. if ( road )
  900. return road->getId();
  901. return NULL;
  902. }
  903. DefineEngineMethod( GuiRoadEditorCtrl, getSelectedNode, S32, (), , "" )
  904. {
  905. return object->getSelectedNode();
  906. }
  907. DefineEngineMethod( GuiRoadEditorCtrl, deleteRoad, void, (), , "" )
  908. {
  909. object->deleteSelectedRoad();
  910. }