guiNavEditorCtrl.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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. }
  58. GuiNavEditorCtrl::~GuiNavEditorCtrl()
  59. {
  60. }
  61. void GuiNavEditorUndoAction::undo()
  62. {
  63. }
  64. bool GuiNavEditorCtrl::onAdd()
  65. {
  66. if(!Parent::onAdd())
  67. return false;
  68. GFXStateBlockDesc desc;
  69. desc.fillMode = GFXFillSolid;
  70. desc.setBlend(false);
  71. desc.setZReadWrite(false, false);
  72. desc.setCullMode(GFXCullNone);
  73. mZDisableSB = GFX->createStateBlock(desc);
  74. desc.setZReadWrite(true, true);
  75. mZEnableSB = GFX->createStateBlock(desc);
  76. SceneManager::getPreRenderSignal().notify(this, &GuiNavEditorCtrl::_prepRenderImage);
  77. return true;
  78. }
  79. void GuiNavEditorCtrl::initPersistFields()
  80. {
  81. docsURL;
  82. addField("isDirty", TypeBool, Offset(mIsDirty, GuiNavEditorCtrl));
  83. Parent::initPersistFields();
  84. }
  85. void GuiNavEditorCtrl::onSleep()
  86. {
  87. Parent::onSleep();
  88. //mMode = mSelectMode;
  89. }
  90. void GuiNavEditorCtrl::selectMesh(NavMesh *mesh)
  91. {
  92. mesh->setSelected(true);
  93. mMesh = mesh;
  94. if (mTool)
  95. mTool->setActiveNavMesh(mMesh);
  96. }
  97. DefineEngineMethod(GuiNavEditorCtrl, selectMesh, void, (S32 id),,
  98. "@brief Select a NavMesh object.")
  99. {
  100. NavMesh *obj;
  101. if(Sim::findObject(id, obj))
  102. object->selectMesh(obj);
  103. }
  104. S32 GuiNavEditorCtrl::getMeshId()
  105. {
  106. return mMesh.isNull() ? 0 : mMesh->getId();
  107. }
  108. DefineEngineMethod(GuiNavEditorCtrl, getMesh, S32, (),,
  109. "@brief Select a NavMesh object.")
  110. {
  111. return object->getMeshId();
  112. }
  113. void GuiNavEditorCtrl::get3DCursor(GuiCursor *&cursor,
  114. bool &visible,
  115. const Gui3DMouseEvent &event_)
  116. {
  117. //cursor = mAddNodeCursor;
  118. //visible = false;
  119. cursor = NULL;
  120. visible = false;
  121. GuiCanvas *root = getRoot();
  122. if(!root)
  123. return;
  124. S32 currCursor = PlatformCursorController::curArrow;
  125. if(root->mCursorChanged == currCursor)
  126. return;
  127. PlatformWindow *window = root->getPlatformWindow();
  128. PlatformCursorController *controller = window->getCursorController();
  129. // We've already changed the cursor,
  130. // so set it back before we change it again.
  131. if(root->mCursorChanged != -1)
  132. controller->popCursor();
  133. // Now change the cursor shape
  134. controller->pushCursor(currCursor);
  135. root->mCursorChanged = currCursor;
  136. }
  137. bool GuiNavEditorCtrl::get3DCentre(Point3F &pos)
  138. {
  139. Point3F screen, start, end;
  140. screen.set(getExtent().x / 2, getExtent().y / 2, 0);
  141. unproject(screen, &start);
  142. screen.z = 1.0f;
  143. unproject(screen, &end);
  144. RayInfo ri;
  145. if(gServerContainer.castRay(start, end, StaticObjectType, &ri))
  146. {
  147. pos = ri.point;
  148. return true;
  149. }
  150. return false;
  151. }
  152. void GuiNavEditorCtrl::on3DMouseDown(const Gui3DMouseEvent & event)
  153. {
  154. if (!mMesh)
  155. return;
  156. mGizmo->on3DMouseDown(event);
  157. if (mTool)
  158. mTool->on3DMouseDown(event);
  159. mouseLock();
  160. return;
  161. }
  162. void GuiNavEditorCtrl::on3DMouseUp(const Gui3DMouseEvent & event)
  163. {
  164. if (!mMesh)
  165. return;
  166. // Keep the Gizmo up to date.
  167. mGizmo->on3DMouseUp(event);
  168. if (mTool)
  169. mTool->on3DMouseUp(event);
  170. mouseUnlock();
  171. }
  172. void GuiNavEditorCtrl::on3DMouseMove(const Gui3DMouseEvent & event)
  173. {
  174. if (!mMesh)
  175. return;
  176. if (mTool)
  177. mTool->on3DMouseMove(event);
  178. return;
  179. }
  180. void GuiNavEditorCtrl::on3DMouseDragged(const Gui3DMouseEvent & event)
  181. {
  182. mGizmo->on3DMouseDragged(event);
  183. if(mGizmo->isDirty())
  184. {
  185. Point3F scale = mGizmo->getScale();
  186. const MatrixF &mat = mGizmo->getTransform();
  187. VectorF normal;
  188. mat.getColumn(2, &normal);
  189. //mSelRiver->setNode(pos, scale.x, scale.z, normal, mSelNode);
  190. mIsDirty = true;
  191. }
  192. }
  193. void GuiNavEditorCtrl::on3DMouseEnter(const Gui3DMouseEvent & event)
  194. {
  195. }
  196. void GuiNavEditorCtrl::on3DMouseLeave(const Gui3DMouseEvent & event)
  197. {
  198. }
  199. void GuiNavEditorCtrl::updateGuiInfo()
  200. {
  201. if (mTool)
  202. {
  203. if (mTool->updateGuiInfo())
  204. return;
  205. }
  206. }
  207. void GuiNavEditorCtrl::onRender(Point2I offset, const RectI &updateRect)
  208. {
  209. PROFILE_SCOPE(GuiNavEditorCtrl_OnRender);
  210. Parent::onRender(offset, updateRect);
  211. return;
  212. }
  213. void GuiNavEditorCtrl::renderScene(const RectI & updateRect)
  214. {
  215. GFX->setStateBlock(mZDisableSB);
  216. // get the projected size...
  217. GameConnection* connection = GameConnection::getConnectionToServer();
  218. if(!connection)
  219. return;
  220. // Grab the camera's transform
  221. MatrixF mat;
  222. connection->getControlCameraTransform(0, &mat);
  223. // Get the camera position
  224. Point3F camPos;
  225. mat.getColumn(3,&camPos);
  226. if (mTool)
  227. mTool->onRender3D();
  228. duDebugDrawTorque d;
  229. if(!mMesh.isNull())
  230. mMesh->renderLinks(d);
  231. // Now draw all the 2d stuff!
  232. GFX->setClipRect(updateRect);
  233. }
  234. bool GuiNavEditorCtrl::getStaticPos(const Gui3DMouseEvent & event, Point3F &tpos)
  235. {
  236. // Find clicked point on the terrain
  237. Point3F startPnt = event.pos;
  238. Point3F endPnt = event.pos + event.vec * 1000.0f;
  239. RayInfo ri;
  240. bool hit;
  241. hit = gServerContainer.castRay(startPnt, endPnt, StaticShapeObjectType, &ri);
  242. tpos = ri.point;
  243. return hit;
  244. }
  245. void GuiNavEditorCtrl::setMode(String mode, bool sourceShortcut = false)
  246. {
  247. mMode = mode;
  248. Con::executef(this, "onModeSet", mode);
  249. if(sourceShortcut)
  250. Con::executef(this, "paletteSync", mode);
  251. }
  252. void GuiNavEditorCtrl::submitUndo(const UTF8 *name)
  253. {
  254. // Grab the mission editor undo manager.
  255. UndoManager *undoMan = NULL;
  256. if(!Sim::findObject("EUndoManager", undoMan))
  257. {
  258. Con::errorf("GuiNavEditorCtrl::submitUndo() - EUndoManager not found!");
  259. return;
  260. }
  261. // Setup the action.
  262. GuiNavEditorUndoAction *action = new GuiNavEditorUndoAction(name);
  263. action->mNavEditor = this;
  264. undoMan->addAction(action);
  265. }
  266. void GuiNavEditorCtrl::_prepRenderImage(SceneManager* sceneGraph, const SceneRenderState* state)
  267. {
  268. /*if(isAwake() && River::smEditorOpen && mSelRiver)
  269. {
  270. ObjectRenderInst *ri = state->getRenderPass()->allocInst<ObjectRenderInst>();
  271. ri->type = RenderPassManager::RIT_Editor;
  272. ri->renderDelegate.bind(this, &GuiNavEditorCtrl::_renderSelectedRiver);
  273. ri->defaultKey = 100;
  274. state->getRenderPass()->addInst(ri);
  275. }*/
  276. }
  277. void GuiNavEditorCtrl::setActiveTool(NavMeshTool* tool)
  278. {
  279. if (mTool)
  280. {
  281. mTool->onDeactivated();
  282. }
  283. mTool = tool;
  284. if (mTool)
  285. {
  286. mTool->setActiveNavMesh(mMesh);
  287. mTool->onActivated(mLastEvent);
  288. }
  289. }
  290. void GuiNavEditorCtrl::setDrawMode(S32 id)
  291. {
  292. if (mMesh.isNull())
  293. return;
  294. mMesh->setDrawMode((NavMesh::DrawMode)id);
  295. }
  296. DefineEngineMethod(GuiNavEditorCtrl, setDrawMode, void, (S32 id), ,
  297. "@brief Deselect whatever is currently selected in the editor.")
  298. {
  299. object->setDrawMode(id);
  300. }
  301. DefineEngineMethod(GuiNavEditorCtrl, setActiveTool, void, (const char* toolName), , "( NavMeshTool tool )")
  302. {
  303. NavMeshTool* tool = dynamic_cast<NavMeshTool*>(Sim::findObject(toolName));
  304. object->setActiveTool(tool);
  305. }
  306. DefineEngineMethod(GuiNavEditorCtrl, getMode, const char*, (), , "")
  307. {
  308. return object->getMode();
  309. }
  310. DefineEngineMethod(GuiNavEditorCtrl, setMode, void, (String mode),, "setMode(String mode)")
  311. {
  312. object->setMode(mode);
  313. }
  314. #endif