guiRoadEditorCtrl.cpp 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121
  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 );
  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. static const U32 bufSize = 256;
  878. char* returnBuffer = Con::getReturnBuffer(bufSize);
  879. dSprintf(returnBuffer, bufSize, "%f %f %f",
  880. object->getNodePosition().x, object->getNodePosition().y, object->getNodePosition().z);
  881. return returnBuffer;
  882. }
  883. ConsoleMethod( GuiRoadEditorCtrl, setNodePosition, void, 3, 3, "" )
  884. {
  885. Point3F pos;
  886. S32 count = dSscanf( argv[2], "%f %f %f",
  887. &pos.x, &pos.y, &pos.z);
  888. if ( (count != 3) )
  889. {
  890. Con::printf("Failed to parse node information \"px py pz\" from '%s'", argv[3]);
  891. return;
  892. }
  893. object->setNodePosition( pos );
  894. }
  895. ConsoleMethod( GuiRoadEditorCtrl, setSelectedRoad, void, 2, 3, "" )
  896. {
  897. if ( argc == 2 )
  898. object->setSelectedRoad(NULL);
  899. else
  900. {
  901. DecalRoad *road = NULL;
  902. if ( Sim::findObject( argv[2], road ) )
  903. object->setSelectedRoad(road);
  904. }
  905. }
  906. ConsoleMethod( GuiRoadEditorCtrl, getSelectedRoad, const char*, 2, 2, "" )
  907. {
  908. DecalRoad *road = object->getSelectedRoad();
  909. if ( road )
  910. return road->getIdString();
  911. return NULL;
  912. }
  913. ConsoleMethod( GuiRoadEditorCtrl, getSelectedNode, S32, 2, 2, "" )
  914. {
  915. return object->getSelectedNode();
  916. }
  917. ConsoleMethod( GuiRoadEditorCtrl, deleteRoad, void, 2, 2, "" )
  918. {
  919. object->deleteSelectedRoad();
  920. }