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