guiNavEditorCtrl.cpp 17 KB

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