guiRoadEditorCtrl.cpp 29 KB

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