guiNavEditorCtrl.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  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. #include "T3D/AI/AIController.h"
  40. IMPLEMENT_CONOBJECT(GuiNavEditorCtrl);
  41. ConsoleDocClass(GuiNavEditorCtrl,
  42. "@brief GUI tool that makes up the Navigation Editor\n\n"
  43. "Editor use only.\n\n"
  44. "@internal"
  45. );
  46. // Each of the mode names directly correlates with the Nav Editor's tool palette.
  47. const String GuiNavEditorCtrl::mSelectMode = "SelectMode";
  48. const String GuiNavEditorCtrl::mLinkMode = "LinkMode";
  49. const String GuiNavEditorCtrl::mCoverMode = "CoverMode";
  50. const String GuiNavEditorCtrl::mTestMode = "TestMode";
  51. GuiNavEditorCtrl::GuiNavEditorCtrl()
  52. {
  53. mMode = mSelectMode;
  54. mIsDirty = false;
  55. mStartDragMousePoint = InvalidMousePoint;
  56. mMesh = NULL;
  57. mPlayer = mCurPlayer = NULL;
  58. mSpawnClass = mSpawnDatablock = "";
  59. }
  60. GuiNavEditorCtrl::~GuiNavEditorCtrl()
  61. {
  62. }
  63. void GuiNavEditorUndoAction::undo()
  64. {
  65. }
  66. bool GuiNavEditorCtrl::onAdd()
  67. {
  68. if(!Parent::onAdd())
  69. return false;
  70. GFXStateBlockDesc desc;
  71. desc.fillMode = GFXFillSolid;
  72. desc.setBlend(false);
  73. desc.setZReadWrite(false, false);
  74. desc.setCullMode(GFXCullNone);
  75. mZDisableSB = GFX->createStateBlock(desc);
  76. desc.setZReadWrite(true, true);
  77. mZEnableSB = GFX->createStateBlock(desc);
  78. SceneManager::getPreRenderSignal().notify(this, &GuiNavEditorCtrl::_prepRenderImage);
  79. return true;
  80. }
  81. void GuiNavEditorCtrl::initPersistFields()
  82. {
  83. docsURL;
  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. if (mTool)
  101. mTool->setActiveNavMesh(mMesh);
  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. }
  135. DefineEngineMethod(GuiNavEditorCtrl, deselect, void, (),,
  136. "@brief Deselect whatever is currently selected in the editor.")
  137. {
  138. object->deselect();
  139. }
  140. void GuiNavEditorCtrl::spawnPlayer(const Point3F &pos)
  141. {
  142. SceneObject *obj = (SceneObject*)Sim::spawnObject(mSpawnClass, mSpawnDatablock);
  143. if(obj)
  144. {
  145. MatrixF mat(true);
  146. mat.setPosition(pos);
  147. obj->setTransform(mat);
  148. SimObject* cleanup = Sim::findObject("MissionCleanup");
  149. if(cleanup)
  150. {
  151. SimGroup* missionCleanup = dynamic_cast<SimGroup*>(cleanup);
  152. missionCleanup->addObject(obj);
  153. }
  154. mPlayer = obj;
  155. #ifdef TORQUE_NAVIGATION_ENABLED
  156. AIPlayer* asAIPlayer = dynamic_cast<AIPlayer*>(obj);
  157. if (asAIPlayer) //try direct
  158. {
  159. Con::executef(this, "onPlayerSelected", Con::getIntArg(asAIPlayer->mLinkTypes.getFlags()));
  160. }
  161. else
  162. {
  163. ShapeBase* sbo = dynamic_cast<ShapeBase*>(obj);
  164. if (sbo->getAIController())
  165. {
  166. if (sbo->getAIController()->mControllerData)
  167. Con::executef(this, "onPlayerSelected", Con::getIntArg(sbo->getAIController()->mControllerData->mLinkTypes.getFlags()));
  168. }
  169. else
  170. {
  171. #endif
  172. Con::executef(this, "onPlayerSelected");
  173. #ifdef TORQUE_NAVIGATION_ENABLED
  174. }
  175. }
  176. #endif
  177. }
  178. }
  179. DefineEngineMethod(GuiNavEditorCtrl, spawnPlayer, void, (),,
  180. "@brief Spawn an AIPlayer at the centre of the screen.")
  181. {
  182. Point3F c;
  183. if(object->get3DCentre(c))
  184. object->spawnPlayer(c);
  185. }
  186. void GuiNavEditorCtrl::get3DCursor(GuiCursor *&cursor,
  187. bool &visible,
  188. const Gui3DMouseEvent &event_)
  189. {
  190. //cursor = mAddNodeCursor;
  191. //visible = false;
  192. cursor = NULL;
  193. visible = false;
  194. GuiCanvas *root = getRoot();
  195. if(!root)
  196. return;
  197. S32 currCursor = PlatformCursorController::curArrow;
  198. if(root->mCursorChanged == currCursor)
  199. return;
  200. PlatformWindow *window = root->getPlatformWindow();
  201. PlatformCursorController *controller = window->getCursorController();
  202. // We've already changed the cursor,
  203. // so set it back before we change it again.
  204. if(root->mCursorChanged != -1)
  205. controller->popCursor();
  206. // Now change the cursor shape
  207. controller->pushCursor(currCursor);
  208. root->mCursorChanged = currCursor;
  209. }
  210. bool GuiNavEditorCtrl::get3DCentre(Point3F &pos)
  211. {
  212. Point3F screen, start, end;
  213. screen.set(getExtent().x / 2, getExtent().y / 2, 0);
  214. unproject(screen, &start);
  215. screen.z = 1.0f;
  216. unproject(screen, &end);
  217. RayInfo ri;
  218. if(gServerContainer.castRay(start, end, StaticObjectType, &ri))
  219. {
  220. pos = ri.point;
  221. return true;
  222. }
  223. return false;
  224. }
  225. void GuiNavEditorCtrl::on3DMouseDown(const Gui3DMouseEvent & event)
  226. {
  227. if (!mMesh)
  228. return;
  229. mGizmo->on3DMouseDown(event);
  230. if (mTool)
  231. mTool->on3DMouseDown(event);
  232. mouseLock();
  233. return;
  234. // Construct a LineSegment from the camera position to 1000 meters away in
  235. // the direction clicked.
  236. // If that segment hits the terrain, truncate the ray to only be that length.
  237. Point3F startPnt = event.pos;
  238. Point3F endPnt = event.pos + event.vec * 1000.0f;
  239. RayInfo ri;
  240. U8 keys = Input::getModifierKeys();
  241. bool shift = keys & SI_LSHIFT;
  242. bool ctrl = keys & SI_LCTRL;
  243. if(mMode == mTestMode)
  244. {
  245. // Spawn new character
  246. if(ctrl)
  247. {
  248. if(gServerContainer.castRay(startPnt, endPnt, StaticObjectType, &ri))
  249. spawnPlayer(ri.point);
  250. }
  251. // Deselect character
  252. else if(shift)
  253. {
  254. mPlayer = NULL;
  255. Con::executef(this, "onPlayerDeselected");
  256. }
  257. // Select/move character
  258. else
  259. {
  260. if(gServerContainer.castRay(startPnt, endPnt, PlayerObjectType | VehicleObjectType, &ri))
  261. {
  262. if(ri.object)
  263. {
  264. mPlayer = ri.object;
  265. #ifdef TORQUE_NAVIGATION_ENABLED
  266. AIPlayer* asAIPlayer = dynamic_cast<AIPlayer*>(mPlayer.getPointer());
  267. if (asAIPlayer) //try direct
  268. {
  269. Con::executef(this, "onPlayerSelected", Con::getIntArg(asAIPlayer->mLinkTypes.getFlags()));
  270. }
  271. else
  272. {
  273. ShapeBase* sbo = dynamic_cast<ShapeBase*>(mPlayer.getPointer());
  274. if (sbo->getAIController())
  275. {
  276. if (sbo->getAIController()->mControllerData)
  277. Con::executef(this, "onPlayerSelected", Con::getIntArg(sbo->getAIController()->mControllerData->mLinkTypes.getFlags()));
  278. }
  279. else
  280. {
  281. #endif
  282. Con::executef(this, "onPlayerSelected");
  283. }
  284. #ifdef TORQUE_NAVIGATION_ENABLED
  285. }
  286. }
  287. #endif
  288. }
  289. else if (!mPlayer.isNull() && gServerContainer.castRay(startPnt, endPnt, StaticObjectType, &ri))
  290. {
  291. AIPlayer* asAIPlayer = dynamic_cast<AIPlayer*>(mPlayer.getPointer());
  292. if (asAIPlayer) //try direct
  293. {
  294. #ifdef TORQUE_NAVIGATION_ENABLED
  295. asAIPlayer->setPathDestination(ri.point);
  296. #else
  297. asAIPlayer->setMoveDestination(ri.point,false);
  298. #endif
  299. }
  300. else
  301. {
  302. ShapeBase* sbo = dynamic_cast<ShapeBase*>(mPlayer.getPointer());
  303. if (sbo->getAIController())
  304. {
  305. if (sbo->getAIController()->mControllerData)
  306. sbo->getAIController()->getNav()->setPathDestination(ri.point, true);
  307. }
  308. }
  309. }
  310. }
  311. }
  312. }
  313. void GuiNavEditorCtrl::on3DMouseUp(const Gui3DMouseEvent & event)
  314. {
  315. if (!mMesh)
  316. return;
  317. // Keep the Gizmo up to date.
  318. mGizmo->on3DMouseUp(event);
  319. if (mTool)
  320. mTool->on3DMouseUp(event);
  321. mouseUnlock();
  322. }
  323. void GuiNavEditorCtrl::on3DMouseMove(const Gui3DMouseEvent & event)
  324. {
  325. if (!mMesh)
  326. return;
  327. if (mTool)
  328. mTool->on3DMouseMove(event);
  329. return;
  330. //if(mSelRiver != NULL && mSelNode != -1)
  331. //mGizmo->on3DMouseMove(event);
  332. Point3F startPnt = event.pos;
  333. Point3F endPnt = event.pos + event.vec * 1000.0f;
  334. RayInfo ri;
  335. if(mMode == mTestMode)
  336. {
  337. if(gServerContainer.castRay(startPnt, endPnt, PlayerObjectType | VehicleObjectType, &ri))
  338. mCurPlayer = ri.object;
  339. else
  340. mCurPlayer = NULL;
  341. }
  342. }
  343. void GuiNavEditorCtrl::on3DMouseDragged(const Gui3DMouseEvent & event)
  344. {
  345. mGizmo->on3DMouseDragged(event);
  346. if(mGizmo->isDirty())
  347. {
  348. Point3F scale = mGizmo->getScale();
  349. const MatrixF &mat = mGizmo->getTransform();
  350. VectorF normal;
  351. mat.getColumn(2, &normal);
  352. //mSelRiver->setNode(pos, scale.x, scale.z, normal, mSelNode);
  353. mIsDirty = true;
  354. }
  355. }
  356. void GuiNavEditorCtrl::on3DMouseEnter(const Gui3DMouseEvent & event)
  357. {
  358. }
  359. void GuiNavEditorCtrl::on3DMouseLeave(const Gui3DMouseEvent & event)
  360. {
  361. }
  362. void GuiNavEditorCtrl::updateGuiInfo()
  363. {
  364. if (mTool)
  365. {
  366. if (mTool->updateGuiInfo())
  367. return;
  368. }
  369. }
  370. void GuiNavEditorCtrl::onRender(Point2I offset, const RectI &updateRect)
  371. {
  372. PROFILE_SCOPE(GuiNavEditorCtrl_OnRender);
  373. Parent::onRender(offset, updateRect);
  374. return;
  375. }
  376. static void renderBoxOutline(const Box3F &box, const ColorI &col)
  377. {
  378. if(box != Box3F::Invalid)
  379. {
  380. GFXStateBlockDesc desc;
  381. desc.setCullMode(GFXCullNone);
  382. desc.setFillModeSolid();
  383. desc.setZReadWrite(true, false);
  384. desc.setBlend(true);
  385. GFX->getDrawUtil()->drawCube(desc, box, ColorI(col, 20));
  386. desc.setFillModeWireframe();
  387. desc.setBlend(false);
  388. GFX->getDrawUtil()->drawCube(desc, box, ColorI(col, 255));
  389. }
  390. }
  391. void GuiNavEditorCtrl::renderScene(const RectI & updateRect)
  392. {
  393. GFX->setStateBlock(mZDisableSB);
  394. // get the projected size...
  395. GameConnection* connection = GameConnection::getConnectionToServer();
  396. if(!connection)
  397. return;
  398. // Grab the camera's transform
  399. MatrixF mat;
  400. connection->getControlCameraTransform(0, &mat);
  401. // Get the camera position
  402. Point3F camPos;
  403. mat.getColumn(3,&camPos);
  404. if (mTool)
  405. mTool->onRender3D();
  406. if(mMode == mTestMode)
  407. {
  408. if(!mCurPlayer.isNull())
  409. renderBoxOutline(mCurPlayer->getWorldBox(), ColorI::BLUE);
  410. if(!mPlayer.isNull())
  411. renderBoxOutline(mPlayer->getWorldBox(), ColorI::GREEN);
  412. }
  413. duDebugDrawTorque d;
  414. if(!mMesh.isNull())
  415. mMesh->renderLinks(d);
  416. // Now draw all the 2d stuff!
  417. GFX->setClipRect(updateRect);
  418. }
  419. bool GuiNavEditorCtrl::getStaticPos(const Gui3DMouseEvent & event, Point3F &tpos)
  420. {
  421. // Find clicked point on the terrain
  422. Point3F startPnt = event.pos;
  423. Point3F endPnt = event.pos + event.vec * 1000.0f;
  424. RayInfo ri;
  425. bool hit;
  426. hit = gServerContainer.castRay(startPnt, endPnt, StaticShapeObjectType, &ri);
  427. tpos = ri.point;
  428. return hit;
  429. }
  430. void GuiNavEditorCtrl::setMode(String mode, bool sourceShortcut = false)
  431. {
  432. mMode = mode;
  433. Con::executef(this, "onModeSet", mode);
  434. if(sourceShortcut)
  435. Con::executef(this, "paletteSync", mode);
  436. }
  437. void GuiNavEditorCtrl::submitUndo(const UTF8 *name)
  438. {
  439. // Grab the mission editor undo manager.
  440. UndoManager *undoMan = NULL;
  441. if(!Sim::findObject("EUndoManager", undoMan))
  442. {
  443. Con::errorf("GuiNavEditorCtrl::submitUndo() - EUndoManager not found!");
  444. return;
  445. }
  446. // Setup the action.
  447. GuiNavEditorUndoAction *action = new GuiNavEditorUndoAction(name);
  448. action->mNavEditor = this;
  449. undoMan->addAction(action);
  450. }
  451. void GuiNavEditorCtrl::_prepRenderImage(SceneManager* sceneGraph, const SceneRenderState* state)
  452. {
  453. /*if(isAwake() && River::smEditorOpen && mSelRiver)
  454. {
  455. ObjectRenderInst *ri = state->getRenderPass()->allocInst<ObjectRenderInst>();
  456. ri->type = RenderPassManager::RIT_Editor;
  457. ri->renderDelegate.bind(this, &GuiNavEditorCtrl::_renderSelectedRiver);
  458. ri->defaultKey = 100;
  459. state->getRenderPass()->addInst(ri);
  460. }*/
  461. }
  462. void GuiNavEditorCtrl::setActiveTool(NavMeshTool* tool)
  463. {
  464. if (mTool)
  465. {
  466. mTool->onDeactivated();
  467. }
  468. mTool = tool;
  469. if (mTool)
  470. {
  471. mTool->setActiveNavMesh(mMesh);
  472. mTool->onActivated(mLastEvent);
  473. }
  474. }
  475. void GuiNavEditorCtrl::setDrawMode(S32 id)
  476. {
  477. if (mMesh.isNull())
  478. return;
  479. mMesh->setDrawMode((NavMesh::DrawMode)id);
  480. }
  481. DefineEngineMethod(GuiNavEditorCtrl, setDrawMode, void, (S32 id), ,
  482. "@brief Deselect whatever is currently selected in the editor.")
  483. {
  484. object->setDrawMode(id);
  485. }
  486. DefineEngineMethod(GuiNavEditorCtrl, setActiveTool, void, (const char* toolName), , "( NavMeshTool tool )")
  487. {
  488. NavMeshTool* tool = dynamic_cast<NavMeshTool*>(Sim::findObject(toolName));
  489. object->setActiveTool(tool);
  490. }
  491. DefineEngineMethod(GuiNavEditorCtrl, getMode, const char*, (), , "")
  492. {
  493. return object->getMode();
  494. }
  495. DefineEngineMethod(GuiNavEditorCtrl, setMode, void, (String mode),, "setMode(String mode)")
  496. {
  497. object->setMode(mode);
  498. }
  499. #endif