guiNavEditorCtrl.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2014 Daniel Buckmaster
  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 "torqueConfig.h"
  23. #ifdef TORQUE_TOOLS
  24. #include "platform/platform.h"
  25. #include "guiNavEditorCtrl.h"
  26. #include "duDebugDrawTorque.h"
  27. #include "console/engineAPI.h"
  28. #include "console/consoleTypes.h"
  29. #include "math/util/frustum.h"
  30. #include "math/mathUtils.h"
  31. #include "gfx/primBuilder.h"
  32. #include "gfx/gfxDrawUtil.h"
  33. #include "scene/sceneRenderState.h"
  34. #include "scene/sceneManager.h"
  35. #include "gui/core/guiCanvas.h"
  36. #include "gui/buttons/guiButtonCtrl.h"
  37. #include "gui/worldEditor/undoActions.h"
  38. #include "T3D/gameBase/gameConnection.h"
  39. IMPLEMENT_CONOBJECT(GuiNavEditorCtrl);
  40. ConsoleDocClass(GuiNavEditorCtrl,
  41. "@brief GUI tool that makes up the Navigation Editor\n\n"
  42. "Editor use only.\n\n"
  43. "@internal"
  44. );
  45. // Each of the mode names directly correlates with the Nav Editor's tool palette.
  46. const String GuiNavEditorCtrl::mSelectMode = "SelectMode";
  47. const String GuiNavEditorCtrl::mLinkMode = "LinkMode";
  48. const String GuiNavEditorCtrl::mCoverMode = "CoverMode";
  49. const String GuiNavEditorCtrl::mTileMode = "TileMode";
  50. const String GuiNavEditorCtrl::mTestMode = "TestMode";
  51. GuiNavEditorCtrl::GuiNavEditorCtrl()
  52. {
  53. mMode = mSelectMode;
  54. mIsDirty = false;
  55. mStartDragMousePoint = InvalidMousePoint;
  56. mMesh = NULL;
  57. mCurTile = mTile = -1;
  58. mPlayer = mCurPlayer = NULL;
  59. mSpawnClass = mSpawnDatablock = "";
  60. mLinkStart = Point3F::Max;
  61. mLink = mCurLink = -1;
  62. }
  63. GuiNavEditorCtrl::~GuiNavEditorCtrl()
  64. {
  65. }
  66. void GuiNavEditorUndoAction::undo()
  67. {
  68. }
  69. bool GuiNavEditorCtrl::onAdd()
  70. {
  71. if(!Parent::onAdd())
  72. return false;
  73. GFXStateBlockDesc desc;
  74. desc.fillMode = GFXFillSolid;
  75. desc.setBlend(false);
  76. desc.setZReadWrite(false, false);
  77. desc.setCullMode(GFXCullNone);
  78. mZDisableSB = GFX->createStateBlock(desc);
  79. desc.setZReadWrite(true, true);
  80. mZEnableSB = GFX->createStateBlock(desc);
  81. SceneManager::getPreRenderSignal().notify(this, &GuiNavEditorCtrl::_prepRenderImage);
  82. return true;
  83. }
  84. void GuiNavEditorCtrl::initPersistFields()
  85. {
  86. docsURL;
  87. addField("isDirty", TypeBool, Offset(mIsDirty, GuiNavEditorCtrl));
  88. addField("spawnClass", TypeRealString, Offset(mSpawnClass, GuiNavEditorCtrl),
  89. "Class of object to spawn in test mode.");
  90. addField("spawnDatablock", TypeRealString, Offset(mSpawnDatablock, GuiNavEditorCtrl),
  91. "Datablock to give new objects in test mode.");
  92. Parent::initPersistFields();
  93. }
  94. void GuiNavEditorCtrl::onSleep()
  95. {
  96. Parent::onSleep();
  97. //mMode = mSelectMode;
  98. }
  99. void GuiNavEditorCtrl::selectMesh(NavMesh *mesh)
  100. {
  101. mesh->setSelected(true);
  102. mMesh = mesh;
  103. }
  104. DefineEngineMethod(GuiNavEditorCtrl, selectMesh, void, (S32 id),,
  105. "@brief Select a NavMesh object.")
  106. {
  107. NavMesh *obj;
  108. if(Sim::findObject(id, obj))
  109. object->selectMesh(obj);
  110. }
  111. S32 GuiNavEditorCtrl::getMeshId()
  112. {
  113. return mMesh.isNull() ? 0 : mMesh->getId();
  114. }
  115. DefineEngineMethod(GuiNavEditorCtrl, getMesh, S32, (),,
  116. "@brief Select a NavMesh object.")
  117. {
  118. return object->getMeshId();
  119. }
  120. S32 GuiNavEditorCtrl::getPlayerId()
  121. {
  122. return mPlayer.isNull() ? 0 : mPlayer->getId();
  123. }
  124. DefineEngineMethod(GuiNavEditorCtrl, getPlayer, S32, (),,
  125. "@brief Select a NavMesh object.")
  126. {
  127. return object->getPlayerId();
  128. }
  129. void GuiNavEditorCtrl::deselect()
  130. {
  131. if(!mMesh.isNull())
  132. mMesh->setSelected(false);
  133. mMesh = NULL;
  134. mPlayer = mCurPlayer = NULL;
  135. mCurTile = mTile = -1;
  136. mLinkStart = Point3F::Max;
  137. mLink = mCurLink = -1;
  138. }
  139. DefineEngineMethod(GuiNavEditorCtrl, deselect, void, (),,
  140. "@brief Deselect whatever is currently selected in the editor.")
  141. {
  142. object->deselect();
  143. }
  144. void GuiNavEditorCtrl::deleteLink()
  145. {
  146. if(!mMesh.isNull() && mLink != -1)
  147. {
  148. mMesh->selectLink(mLink, false);
  149. mMesh->deleteLink(mLink);
  150. mLink = -1;
  151. Con::executef(this, "onLinkDeselected");
  152. }
  153. }
  154. DefineEngineMethod(GuiNavEditorCtrl, deleteLink, void, (),,
  155. "@brief Delete the currently selected link.")
  156. {
  157. object->deleteLink();
  158. }
  159. void GuiNavEditorCtrl::setLinkFlags(const LinkData &d)
  160. {
  161. if(mMode == mLinkMode && !mMesh.isNull() && mLink != -1)
  162. {
  163. mMesh->setLinkFlags(mLink, d);
  164. }
  165. }
  166. DefineEngineMethod(GuiNavEditorCtrl, setLinkFlags, void, (U32 flags),,
  167. "@Brief Set jump and drop properties of the selected link.")
  168. {
  169. object->setLinkFlags(LinkData(flags));
  170. }
  171. void GuiNavEditorCtrl::buildTile()
  172. {
  173. if(!mMesh.isNull() && mTile != -1)
  174. mMesh->buildTile(mTile);
  175. }
  176. DefineEngineMethod(GuiNavEditorCtrl, buildTile, void, (),,
  177. "@brief Build the currently selected tile.")
  178. {
  179. object->buildTile();
  180. }
  181. void GuiNavEditorCtrl::spawnPlayer(const Point3F &pos)
  182. {
  183. SceneObject *obj = (SceneObject*)Sim::spawnObject(mSpawnClass, mSpawnDatablock);
  184. if(obj)
  185. {
  186. MatrixF mat(true);
  187. mat.setPosition(pos);
  188. obj->setTransform(mat);
  189. SimObject* cleanup = Sim::findObject("MissionCleanup");
  190. if(cleanup)
  191. {
  192. SimGroup* missionCleanup = dynamic_cast<SimGroup*>(cleanup);
  193. missionCleanup->addObject(obj);
  194. }
  195. mPlayer = static_cast<AIPlayer*>(obj);
  196. Con::executef(this, "onPlayerSelected", Con::getIntArg(mPlayer->mLinkTypes.getFlags()));
  197. }
  198. }
  199. DefineEngineMethod(GuiNavEditorCtrl, spawnPlayer, void, (),,
  200. "@brief Spawn an AIPlayer at the centre of the screen.")
  201. {
  202. Point3F c;
  203. if(object->get3DCentre(c))
  204. object->spawnPlayer(c);
  205. }
  206. void GuiNavEditorCtrl::get3DCursor(GuiCursor *&cursor,
  207. bool &visible,
  208. const Gui3DMouseEvent &event_)
  209. {
  210. //cursor = mAddNodeCursor;
  211. //visible = false;
  212. cursor = NULL;
  213. visible = false;
  214. GuiCanvas *root = getRoot();
  215. if(!root)
  216. return;
  217. S32 currCursor = PlatformCursorController::curArrow;
  218. if(root->mCursorChanged == currCursor)
  219. return;
  220. PlatformWindow *window = root->getPlatformWindow();
  221. PlatformCursorController *controller = window->getCursorController();
  222. // We've already changed the cursor,
  223. // so set it back before we change it again.
  224. if(root->mCursorChanged != -1)
  225. controller->popCursor();
  226. // Now change the cursor shape
  227. controller->pushCursor(currCursor);
  228. root->mCursorChanged = currCursor;
  229. }
  230. bool GuiNavEditorCtrl::get3DCentre(Point3F &pos)
  231. {
  232. Point3F screen, start, end;
  233. screen.set(getExtent().x / 2, getExtent().y / 2, 0);
  234. unproject(screen, &start);
  235. screen.z = 1.0f;
  236. unproject(screen, &end);
  237. RayInfo ri;
  238. if(gServerContainer.castRay(start, end, StaticObjectType, &ri))
  239. {
  240. pos = ri.point;
  241. return true;
  242. }
  243. return false;
  244. }
  245. void GuiNavEditorCtrl::on3DMouseDown(const Gui3DMouseEvent & event)
  246. {
  247. mGizmo->on3DMouseDown(event);
  248. if(!isFirstResponder())
  249. setFirstResponder();
  250. mouseLock();
  251. // Construct a LineSegment from the camera position to 1000 meters away in
  252. // the direction clicked.
  253. // If that segment hits the terrain, truncate the ray to only be that length.
  254. Point3F startPnt = event.pos;
  255. Point3F endPnt = event.pos + event.vec * 1000.0f;
  256. RayInfo ri;
  257. U8 keys = Input::getModifierKeys();
  258. bool shift = keys & SI_LSHIFT;
  259. bool ctrl = keys & SI_LCTRL;
  260. if(mMode == mLinkMode && !mMesh.isNull())
  261. {
  262. if(gServerContainer.castRay(startPnt, endPnt, StaticObjectType, &ri))
  263. {
  264. U32 link = mMesh->getLink(ri.point);
  265. if(link != -1)
  266. {
  267. if(mLink != -1)
  268. mMesh->selectLink(mLink, false);
  269. mMesh->selectLink(link, true, false);
  270. mLink = link;
  271. LinkData d = mMesh->getLinkFlags(mLink);
  272. Con::executef(this, "onLinkSelected", Con::getIntArg(d.getFlags()));
  273. }
  274. else
  275. {
  276. if(mLink != -1)
  277. {
  278. mMesh->selectLink(mLink, false);
  279. mLink = -1;
  280. Con::executef(this, "onLinkDeselected");
  281. }
  282. else
  283. {
  284. if(mLinkStart != Point3F::Max)
  285. {
  286. mMesh->addLink(mLinkStart, ri.point);
  287. if(!shift)
  288. mLinkStart = Point3F::Max;
  289. }
  290. else
  291. {
  292. mLinkStart = ri.point;
  293. }
  294. }
  295. }
  296. }
  297. else
  298. {
  299. mMesh->selectLink(mLink, false);
  300. mLink = -1;
  301. Con::executef(this, "onLinkDeselected");
  302. }
  303. }
  304. if(mMode == mTileMode && !mMesh.isNull())
  305. {
  306. if(gServerContainer.castRay(startPnt, endPnt, StaticShapeObjectType, &ri))
  307. {
  308. mTile = mMesh->getTile(ri.point);
  309. dd.clear();
  310. mMesh->renderTileData(dd, mTile);
  311. }
  312. }
  313. if(mMode == mTestMode)
  314. {
  315. // Spawn new character
  316. if(ctrl)
  317. {
  318. if(gServerContainer.castRay(startPnt, endPnt, StaticObjectType, &ri))
  319. spawnPlayer(ri.point);
  320. }
  321. // Deselect character
  322. else if(shift)
  323. {
  324. mPlayer = NULL;
  325. Con::executef(this, "onPlayerDeselected");
  326. }
  327. // Select/move character
  328. else
  329. {
  330. if(gServerContainer.castRay(startPnt, endPnt, PlayerObjectType, &ri))
  331. {
  332. if(dynamic_cast<AIPlayer*>(ri.object))
  333. {
  334. mPlayer = dynamic_cast<AIPlayer*>(ri.object);
  335. Con::executef(this, "onPlayerSelected", Con::getIntArg(mPlayer->mLinkTypes.getFlags()));
  336. }
  337. }
  338. else if(!mPlayer.isNull() && gServerContainer.castRay(startPnt, endPnt, StaticObjectType, &ri))
  339. mPlayer->setPathDestination(ri.point);
  340. }
  341. }
  342. }
  343. void GuiNavEditorCtrl::on3DMouseUp(const Gui3DMouseEvent & event)
  344. {
  345. // Keep the Gizmo up to date.
  346. mGizmo->on3DMouseUp(event);
  347. mouseUnlock();
  348. }
  349. void GuiNavEditorCtrl::on3DMouseMove(const Gui3DMouseEvent & event)
  350. {
  351. //if(mSelRiver != NULL && mSelNode != -1)
  352. //mGizmo->on3DMouseMove(event);
  353. Point3F startPnt = event.pos;
  354. Point3F endPnt = event.pos + event.vec * 1000.0f;
  355. RayInfo ri;
  356. if(mMode == mLinkMode && !mMesh.isNull())
  357. {
  358. if(gServerContainer.castRay(startPnt, endPnt, StaticObjectType, &ri))
  359. {
  360. U32 link = mMesh->getLink(ri.point);
  361. if(link != -1)
  362. {
  363. if(link != mLink)
  364. {
  365. if(mCurLink != -1)
  366. mMesh->selectLink(mCurLink, false);
  367. mMesh->selectLink(link, true, true);
  368. }
  369. mCurLink = link;
  370. }
  371. else
  372. {
  373. if(mCurLink != mLink)
  374. mMesh->selectLink(mCurLink, false);
  375. mCurLink = -1;
  376. }
  377. }
  378. else
  379. {
  380. mMesh->selectLink(mCurLink, false);
  381. mCurLink = -1;
  382. }
  383. }
  384. // Select a tile from our current NavMesh.
  385. if(mMode == mTileMode && !mMesh.isNull())
  386. {
  387. if(gServerContainer.castRay(startPnt, endPnt, StaticObjectType, &ri))
  388. mCurTile = mMesh->getTile(ri.point);
  389. else
  390. mCurTile = -1;
  391. }
  392. if(mMode == mTestMode)
  393. {
  394. if(gServerContainer.castRay(startPnt, endPnt, PlayerObjectType, &ri))
  395. mCurPlayer = dynamic_cast<AIPlayer*>(ri.object);
  396. else
  397. mCurPlayer = NULL;
  398. }
  399. }
  400. void GuiNavEditorCtrl::on3DMouseDragged(const Gui3DMouseEvent & event)
  401. {
  402. mGizmo->on3DMouseDragged(event);
  403. if(mGizmo->isDirty())
  404. {
  405. Point3F scale = mGizmo->getScale();
  406. const MatrixF &mat = mGizmo->getTransform();
  407. VectorF normal;
  408. mat.getColumn(2, &normal);
  409. //mSelRiver->setNode(pos, scale.x, scale.z, normal, mSelNode);
  410. mIsDirty = true;
  411. }
  412. }
  413. void GuiNavEditorCtrl::on3DMouseEnter(const Gui3DMouseEvent & event)
  414. {
  415. }
  416. void GuiNavEditorCtrl::on3DMouseLeave(const Gui3DMouseEvent & event)
  417. {
  418. }
  419. void GuiNavEditorCtrl::updateGuiInfo()
  420. {
  421. }
  422. void GuiNavEditorCtrl::onRender(Point2I offset, const RectI &updateRect)
  423. {
  424. PROFILE_SCOPE(GuiNavEditorCtrl_OnRender);
  425. Parent::onRender(offset, updateRect);
  426. return;
  427. }
  428. static void renderBoxOutline(const Box3F &box, const ColorI &col)
  429. {
  430. if(box != Box3F::Invalid)
  431. {
  432. GFXStateBlockDesc desc;
  433. desc.setCullMode(GFXCullNone);
  434. desc.setFillModeSolid();
  435. desc.setZReadWrite(true, false);
  436. desc.setBlend(true);
  437. GFX->getDrawUtil()->drawCube(desc, box, ColorI(col, 20));
  438. desc.setFillModeWireframe();
  439. desc.setBlend(false);
  440. GFX->getDrawUtil()->drawCube(desc, box, ColorI(col, 255));
  441. }
  442. }
  443. void GuiNavEditorCtrl::renderScene(const RectI & updateRect)
  444. {
  445. GFX->setStateBlock(mZDisableSB);
  446. // get the projected size...
  447. GameConnection* connection = GameConnection::getConnectionToServer();
  448. if(!connection)
  449. return;
  450. // Grab the camera's transform
  451. MatrixF mat;
  452. connection->getControlCameraTransform(0, &mat);
  453. // Get the camera position
  454. Point3F camPos;
  455. mat.getColumn(3,&camPos);
  456. if(mMode == mLinkMode)
  457. {
  458. if(mLinkStart != Point3F::Max)
  459. {
  460. GFXStateBlockDesc desc;
  461. desc.setBlend(false);
  462. desc.setZReadWrite(true ,true);
  463. MatrixF linkMat(true);
  464. linkMat.setPosition(mLinkStart);
  465. Point3F scale(0.8f, 0.8f, 0.8f);
  466. GFX->getDrawUtil()->drawTransform(desc, linkMat, &scale);
  467. }
  468. }
  469. if(mMode == mTileMode && !mMesh.isNull())
  470. {
  471. renderBoxOutline(mMesh->getTileBox(mCurTile), ColorI::BLUE);
  472. renderBoxOutline(mMesh->getTileBox(mTile), ColorI::GREEN);
  473. if(Con::getBoolVariable("$Nav::Editor::renderVoxels", false)) dd.renderGroup(0);
  474. if(Con::getBoolVariable("$Nav::Editor::renderInput", false))
  475. {
  476. dd.depthMask(false);
  477. dd.renderGroup(1);
  478. dd.depthMask(true);
  479. }
  480. }
  481. if(mMode == mTestMode)
  482. {
  483. if(!mCurPlayer.isNull())
  484. renderBoxOutline(mCurPlayer->getWorldBox(), ColorI::BLUE);
  485. if(!mPlayer.isNull())
  486. renderBoxOutline(mPlayer->getWorldBox(), ColorI::GREEN);
  487. }
  488. duDebugDrawTorque d;
  489. if(!mMesh.isNull())
  490. mMesh->renderLinks(d);
  491. d.render();
  492. // Now draw all the 2d stuff!
  493. GFX->setClipRect(updateRect);
  494. }
  495. bool GuiNavEditorCtrl::getStaticPos(const Gui3DMouseEvent & event, Point3F &tpos)
  496. {
  497. // Find clicked point on the terrain
  498. Point3F startPnt = event.pos;
  499. Point3F endPnt = event.pos + event.vec * 1000.0f;
  500. RayInfo ri;
  501. bool hit;
  502. hit = gServerContainer.castRay(startPnt, endPnt, StaticShapeObjectType, &ri);
  503. tpos = ri.point;
  504. return hit;
  505. }
  506. void GuiNavEditorCtrl::setMode(String mode, bool sourceShortcut = false)
  507. {
  508. mMode = mode;
  509. Con::executef(this, "onModeSet", mode);
  510. if(sourceShortcut)
  511. Con::executef(this, "paletteSync", mode);
  512. }
  513. void GuiNavEditorCtrl::submitUndo(const UTF8 *name)
  514. {
  515. // Grab the mission editor undo manager.
  516. UndoManager *undoMan = NULL;
  517. if(!Sim::findObject("EUndoManager", undoMan))
  518. {
  519. Con::errorf("GuiNavEditorCtrl::submitUndo() - EUndoManager not found!");
  520. return;
  521. }
  522. // Setup the action.
  523. GuiNavEditorUndoAction *action = new GuiNavEditorUndoAction(name);
  524. action->mNavEditor = this;
  525. undoMan->addAction(action);
  526. }
  527. void GuiNavEditorCtrl::_prepRenderImage(SceneManager* sceneGraph, const SceneRenderState* state)
  528. {
  529. /*if(isAwake() && River::smEditorOpen && mSelRiver)
  530. {
  531. ObjectRenderInst *ri = state->getRenderPass()->allocInst<ObjectRenderInst>();
  532. ri->type = RenderPassManager::RIT_Editor;
  533. ri->renderDelegate.bind(this, &GuiNavEditorCtrl::_renderSelectedRiver);
  534. ri->defaultKey = 100;
  535. state->getRenderPass()->addInst(ri);
  536. }*/
  537. }
  538. DefineEngineMethod(GuiNavEditorCtrl, getMode, const char*, (), , "")
  539. {
  540. return object->getMode();
  541. }
  542. DefineEngineMethod(GuiNavEditorCtrl, setMode, void, (String mode),, "setMode(String mode)")
  543. {
  544. object->setMode(mode);
  545. }
  546. #endif