guiNavEditorCtrl.cpp 8.6 KB

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