guiNavEditorCtrl.cpp 17 KB

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