editTSCtrl.cpp 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  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 "gui/worldEditor/editTSCtrl.h"
  24. #include "console/consoleTypes.h"
  25. #include "console/engineAPI.h"
  26. #include "T3D/gameBase/gameConnection.h"
  27. #include "gui/worldEditor/editor.h"
  28. #include "gui/core/guiCanvas.h"
  29. #include "terrain/terrData.h"
  30. #include "T3D/missionArea.h"
  31. #include "gfx/primBuilder.h"
  32. #include "gfx/gfxDrawUtil.h"
  33. #include "gfx/gfxTransformSaver.h"
  34. #include "gfx/gfxDebugEvent.h"
  35. #include "scene/sceneManager.h"
  36. #include "scene/sceneRenderState.h"
  37. #include "renderInstance/renderBinManager.h"
  38. IMPLEMENT_CONOBJECT(EditTSCtrl);
  39. ConsoleDocClass( EditTSCtrl,
  40. "@brief 3D view control used specifically by Torque 3D's editors.\n\n"
  41. "For Torque 3D editors only, not for actual game development\n\n"
  42. "@ingroup Editors\n"
  43. "@internal"
  44. );
  45. Point3F EditTSCtrl::smCamPos;
  46. MatrixF EditTSCtrl::smCamMatrix;
  47. bool EditTSCtrl::smCamOrtho = false;
  48. F32 EditTSCtrl::smCamNearPlane;
  49. F32 EditTSCtrl::smCamFOV = 75.0f;
  50. F32 EditTSCtrl::smVisibleDistanceScale = 1.0f;
  51. U32 EditTSCtrl::smSceneBoundsMask = EnvironmentObjectType | TerrainObjectType | WaterObjectType | CameraObjectType;
  52. Point3F EditTSCtrl::smMinSceneBounds = Point3F(500.0f, 500.0f, 500.0f);
  53. EditTSCtrl::EditTSCtrl()
  54. {
  55. mGizmoProfile = NULL;
  56. mGizmo = NULL;
  57. mRenderMissionArea = true;
  58. mMissionAreaFillColor.set(255,0,0,20);
  59. mMissionAreaFrameColor.set(255,0,0,128);
  60. mMissionAreaHeightAdjust = 5.0f;
  61. mConsoleFrameColor.set(255,0,0,255);
  62. mConsoleFillColor.set(255,0,0,120);
  63. mConsoleSphereLevel = 1;
  64. mConsoleCircleSegments = 32;
  65. mConsoleLineWidth = 1;
  66. mRightMousePassThru = true;
  67. mMiddleMousePassThru = true;
  68. mConsoleRendering = false;
  69. mDisplayType = DisplayTypePerspective;
  70. mOrthoFOV = 50.0f;
  71. mOrthoCamTrans.set(0.0f, 0.0f, 0.0f);
  72. mIsoCamAngle = mDegToRad(45.0f);
  73. mIsoCamRot = EulerF(0, 0, 0);
  74. mRenderGridPlane = true;
  75. mGridPlaneOriginColor = ColorI(255, 255, 255, 100);
  76. mGridPlaneColor = ColorI(102, 102, 102, 100);
  77. mGridPlaneMinorTickColor = ColorI(51, 51, 51, 100);
  78. mGridPlaneMinorTicks = 9;
  79. mGridPlaneSize = 1.0f;
  80. mGridPlaneSizePixelBias = 10.0f;
  81. mLastMousePos.set(0, 0);
  82. mAllowBorderMove = false;
  83. mMouseMoveBorder = 20;
  84. mMouseMoveSpeed = 0.1f;
  85. mLastBorderMoveTime = 0;
  86. mLeftMouseDown = false;
  87. mRightMouseDown = false;
  88. mMiddleMouseDown = false;
  89. mMiddleMouseTriggered = false;
  90. mMouseLeft = false;
  91. mBlendSB = NULL;
  92. }
  93. EditTSCtrl::~EditTSCtrl()
  94. {
  95. mBlendSB = NULL;
  96. }
  97. //------------------------------------------------------------------------------
  98. bool EditTSCtrl::onAdd()
  99. {
  100. if(!Parent::onAdd())
  101. return(false);
  102. // give all derived access to the fields
  103. setModStaticFields(true);
  104. GFXStateBlockDesc blenddesc;
  105. blenddesc.setBlend(true, GFXBlendSrcAlpha, GFXBlendInvSrcAlpha);
  106. mBlendSB = GFX->createStateBlock( blenddesc );
  107. if ( !mGizmoProfile )
  108. {
  109. Con::errorf( "EditTSCtrl::onadd - gizmoProfile was not assigned, cannot create control!" );
  110. return false;
  111. }
  112. mGizmo = new Gizmo();
  113. mGizmo->setProfile( mGizmoProfile );
  114. mGizmo->registerObject();
  115. return true;
  116. }
  117. void EditTSCtrl::onRemove()
  118. {
  119. Parent::onRemove();
  120. if ( mGizmo )
  121. mGizmo->deleteObject();
  122. }
  123. void EditTSCtrl::onRender(Point2I offset, const RectI &updateRect)
  124. {
  125. // Perform possible mouse border move...
  126. if(mAllowBorderMove && smCamOrtho && !mLeftMouseDown && !mRightMouseDown && !mMouseLeft)
  127. {
  128. Point2I ext = getExtent();
  129. U32 current = Platform::getRealMilliseconds();
  130. bool update = false;
  131. F32 movex = 0.0f;
  132. F32 movey = 0.0f;
  133. Point2I localMouse = globalToLocalCoord(mLastMousePos);
  134. if(localMouse.x <= mMouseMoveBorder || localMouse.x >= ext.x - mMouseMoveBorder)
  135. {
  136. if(mLastBorderMoveTime != 0)
  137. {
  138. U32 dt = current - mLastBorderMoveTime;
  139. if(localMouse.x <= mMouseMoveBorder)
  140. {
  141. movex = mMouseMoveSpeed * dt;
  142. }
  143. else
  144. {
  145. movex = -mMouseMoveSpeed * dt;
  146. }
  147. }
  148. update = true;
  149. }
  150. if(localMouse.y <= mMouseMoveBorder || localMouse.y >= ext.y - mMouseMoveBorder)
  151. {
  152. if(mLastBorderMoveTime != 0)
  153. {
  154. U32 dt = current - mLastBorderMoveTime;
  155. if(localMouse.y <= mMouseMoveBorder)
  156. {
  157. movey = mMouseMoveSpeed * dt;
  158. }
  159. else
  160. {
  161. movey = -mMouseMoveSpeed * dt;
  162. }
  163. }
  164. update = true;
  165. }
  166. if(update)
  167. {
  168. mLastBorderMoveTime = current;
  169. calcOrthoCamOffset(movex, movey);
  170. }
  171. else
  172. {
  173. mLastBorderMoveTime = 0;
  174. }
  175. }
  176. updateGuiInfo();
  177. Parent::onRender(offset, updateRect);
  178. }
  179. //------------------------------------------------------------------------------
  180. void EditTSCtrl::initPersistFields()
  181. {
  182. addGroup( "Grid" );
  183. addField( "gridSize", TypeF32, Offset( mGridPlaneSize, EditTSCtrl ) );
  184. addField( "gridColor", TypeColorI, Offset( mGridPlaneColor, EditTSCtrl ) );
  185. addField( "gridOriginColor", TypeColorI, Offset( mGridPlaneOriginColor, EditTSCtrl ) );
  186. addField( "gridMinorTickColor", TypeColorI, Offset( mGridPlaneMinorTickColor, EditTSCtrl ) );
  187. addField( "renderOrthoGrid", TypeBool, Offset( mRenderGridPlane, EditTSCtrl ),
  188. "Whether to render the grid in orthographic axial projections." );
  189. addField( "renderOrthoGridPixelBias", TypeF32, Offset( mGridPlaneSizePixelBias, EditTSCtrl ),
  190. "Grid patch pixel size below which to switch to coarser grid resolutions." );
  191. endGroup( "Grid" );
  192. addGroup("Mission Area");
  193. addField("renderMissionArea", TypeBool, Offset(mRenderMissionArea, EditTSCtrl));
  194. addField("missionAreaFillColor", TypeColorI, Offset(mMissionAreaFillColor, EditTSCtrl));
  195. addField("missionAreaFrameColor", TypeColorI, Offset(mMissionAreaFrameColor, EditTSCtrl));
  196. addField("missionAreaHeightAdjust", TypeF32, Offset(mMissionAreaHeightAdjust, EditTSCtrl),
  197. "How high above and below the terrain to render the mission area bounds." );
  198. endGroup("Mission Area");
  199. addGroup("BorderMovement");
  200. addField("allowBorderMove", TypeBool, Offset(mAllowBorderMove, EditTSCtrl));
  201. addField("borderMovePixelSize", TypeS32, Offset(mMouseMoveBorder, EditTSCtrl));
  202. addField("borderMoveSpeed", TypeF32, Offset(mMouseMoveSpeed, EditTSCtrl));
  203. endGroup("BorderMovement");
  204. addGroup("Misc");
  205. addField("consoleFrameColor", TypeColorI, Offset(mConsoleFrameColor, EditTSCtrl));
  206. addField("consoleFillColor", TypeColorI, Offset(mConsoleFillColor, EditTSCtrl));
  207. addField("consoleSphereLevel", TypeS32, Offset(mConsoleSphereLevel, EditTSCtrl));
  208. addField("consoleCircleSegments", TypeS32, Offset(mConsoleCircleSegments, EditTSCtrl));
  209. addField("consoleLineWidth", TypeS32, Offset(mConsoleLineWidth, EditTSCtrl));
  210. addField("gizmoProfile", TYPEID< GizmoProfile >(), Offset(mGizmoProfile, EditTSCtrl));
  211. endGroup("Misc");
  212. Parent::initPersistFields();
  213. }
  214. void EditTSCtrl::consoleInit()
  215. {
  216. Con::addVariable("pref::WorldEditor::visibleDistanceScale", TypeF32, &EditTSCtrl::smVisibleDistanceScale, "Scale factor for the visible render distance.\n"
  217. "@ingroup ");
  218. Con::addVariable("pref::WorldEditor::cameraFOV", TypeF32, &EditTSCtrl::smCamFOV, "Field of view for editor's perspective camera, in degrees.\n"
  219. "@ingroup ");
  220. Con::setIntVariable( "$EditTsCtrl::DisplayTypeTop", DisplayTypeTop);
  221. Con::setIntVariable( "$EditTsCtrl::DisplayTypeBottom", DisplayTypeBottom);
  222. Con::setIntVariable( "$EditTsCtrl::DisplayTypeFront", DisplayTypeFront);
  223. Con::setIntVariable( "$EditTsCtrl::DisplayTypeBack", DisplayTypeBack);
  224. Con::setIntVariable( "$EditTsCtrl::DisplayTypeLeft", DisplayTypeLeft);
  225. Con::setIntVariable( "$EditTsCtrl::DisplayTypeRight", DisplayTypeRight);
  226. Con::setIntVariable( "$EditTsCtrl::DisplayTypePerspective", DisplayTypePerspective);
  227. Con::setIntVariable( "$EditTsCtrl::DisplayTypeIsometric", DisplayTypeIsometric);
  228. }
  229. //------------------------------------------------------------------------------
  230. void EditTSCtrl::make3DMouseEvent(Gui3DMouseEvent & gui3DMouseEvent, const GuiEvent & event)
  231. {
  232. (GuiEvent&)(gui3DMouseEvent) = event;
  233. gui3DMouseEvent.mousePoint = event.mousePoint;
  234. if(!smCamOrtho)
  235. {
  236. // get the eye pos and the mouse vec from that...
  237. Point3F screenPoint((F32)gui3DMouseEvent.mousePoint.x, (F32)gui3DMouseEvent.mousePoint.y, 1.0f);
  238. Point3F wp;
  239. unproject(screenPoint, &wp);
  240. gui3DMouseEvent.pos = smCamPos;
  241. gui3DMouseEvent.vec = wp - smCamPos;
  242. gui3DMouseEvent.vec.normalizeSafe();
  243. }
  244. else
  245. {
  246. // get the eye pos and the mouse vec from that...
  247. Point3F screenPoint((F32)gui3DMouseEvent.mousePoint.x, (F32)gui3DMouseEvent.mousePoint.y, 0.0f);
  248. Point3F np, fp;
  249. unproject(screenPoint, &np);
  250. gui3DMouseEvent.pos = np;
  251. smCamMatrix.getColumn( 1, &(gui3DMouseEvent.vec) );
  252. }
  253. }
  254. //------------------------------------------------------------------------------
  255. TerrainBlock* EditTSCtrl::getActiveTerrain()
  256. {
  257. // Find a terrain block
  258. SimSet* scopeAlwaysSet = Sim::getGhostAlwaysSet();
  259. for(SimSet::iterator itr = scopeAlwaysSet->begin(); itr != scopeAlwaysSet->end(); itr++)
  260. {
  261. TerrainBlock* block = dynamic_cast<TerrainBlock*>(*itr);
  262. if( block )
  263. return block;
  264. }
  265. return NULL;
  266. }
  267. //------------------------------------------------------------------------------
  268. void EditTSCtrl::setDisplayType( S32 type )
  269. {
  270. mDisplayType = type;
  271. // Disable middle-mouse pass-thru in ortho views so we can
  272. // use the middle mouse button for navigation.
  273. mMiddleMousePassThru = !isOrthoDisplayType();
  274. if( mGizmo )
  275. {
  276. // Disable gizmo's grid plane in the isometric views since
  277. // they will render with the grid from EditTSCtrl. Also disable
  278. // the move grid as it doesn't make sense in ortho views.
  279. if( type != DisplayTypePerspective )
  280. {
  281. mGizmo->setGridPlaneEnabled( false );
  282. mGizmo->setMoveGridEnabled( false );
  283. }
  284. else
  285. {
  286. mGizmo->setGridPlaneEnabled( true );
  287. mGizmo->setMoveGridEnabled( true );
  288. }
  289. }
  290. }
  291. //------------------------------------------------------------------------------
  292. void EditTSCtrl::getCursor(GuiCursor *&cursor, bool &visible, const GuiEvent &event)
  293. {
  294. make3DMouseEvent(mLastEvent, event);
  295. get3DCursor(cursor, visible, mLastEvent);
  296. }
  297. void EditTSCtrl::get3DCursor(GuiCursor *&cursor, bool &visible, const Gui3DMouseEvent &event)
  298. {
  299. TORQUE_UNUSED(event);
  300. cursor = NULL;
  301. visible = false;
  302. }
  303. void EditTSCtrl::onMouseUp(const GuiEvent & event)
  304. {
  305. mLeftMouseDown = false;
  306. make3DMouseEvent(mLastEvent, event);
  307. on3DMouseUp(mLastEvent);
  308. }
  309. void EditTSCtrl::onMouseDown(const GuiEvent & event)
  310. {
  311. mLeftMouseDown = true;
  312. mLastBorderMoveTime = 0;
  313. make3DMouseEvent(mLastEvent, event);
  314. on3DMouseDown(mLastEvent);
  315. setFirstResponder();
  316. }
  317. void EditTSCtrl::onMouseMove(const GuiEvent & event)
  318. {
  319. make3DMouseEvent(mLastEvent, event);
  320. on3DMouseMove(mLastEvent);
  321. mLastMousePos = event.mousePoint;
  322. }
  323. void EditTSCtrl::onMouseDragged(const GuiEvent & event)
  324. {
  325. make3DMouseEvent(mLastEvent, event);
  326. on3DMouseDragged(mLastEvent);
  327. }
  328. void EditTSCtrl::onMouseEnter(const GuiEvent & event)
  329. {
  330. mMouseLeft = false;
  331. make3DMouseEvent(mLastEvent, event);
  332. on3DMouseEnter(mLastEvent);
  333. }
  334. void EditTSCtrl::onMouseLeave(const GuiEvent & event)
  335. {
  336. mMouseLeft = true;
  337. mLastBorderMoveTime = 0;
  338. make3DMouseEvent(mLastEvent, event);
  339. on3DMouseLeave(mLastEvent);
  340. }
  341. void EditTSCtrl::onRightMouseDown(const GuiEvent & event)
  342. {
  343. // always process the right mouse event first...
  344. mRightMouseDown = true;
  345. mLastBorderMoveTime = 0;
  346. make3DMouseEvent(mLastEvent, event);
  347. on3DRightMouseDown(mLastEvent);
  348. if(!mLeftMouseDown && mRightMousePassThru && mProfile->mCanKeyFocus)
  349. {
  350. GuiCanvas *pCanvas = getRoot();
  351. if( !pCanvas )
  352. return;
  353. PlatformWindow *pWindow = static_cast<GuiCanvas*>(getRoot())->getPlatformWindow();
  354. if( !pWindow )
  355. return;
  356. PlatformCursorController *pController = pWindow->getCursorController();
  357. if( !pController )
  358. return;
  359. // ok, gotta disable the mouse
  360. // script functions are lockMouse(true); Canvas.cursorOff();
  361. pWindow->setMouseLocked(true);
  362. pCanvas->setCursorON( false );
  363. if(mDisplayType != DisplayTypePerspective)
  364. {
  365. mouseLock();
  366. mLastMousePos = event.mousePoint;
  367. pCanvas->setForceMouseToGUI(true);
  368. mLastMouseClamping = pCanvas->getClampTorqueCursor();
  369. pCanvas->setClampTorqueCursor(false);
  370. }
  371. if(mDisplayType == DisplayTypeIsometric)
  372. {
  373. // Store the screen center point on the terrain for a possible rotation
  374. TerrainBlock* activeTerrain = getActiveTerrain();
  375. if( activeTerrain )
  376. {
  377. F32 extx, exty;
  378. if(event.modifier & SI_SHIFT)
  379. {
  380. extx = F32(event.mousePoint.x);
  381. exty = F32(event.mousePoint.y);
  382. }
  383. else
  384. {
  385. extx = getExtent().x * 0.5;
  386. exty = getExtent().y * 0.5;
  387. }
  388. Point3F sp(extx, exty, 0.0f); // Near plane projection
  389. Point3F start;
  390. unproject(sp, &start);
  391. Point3F end = start + mLastEvent.vec * 4000.0f;
  392. Point3F tStartPnt, tEndPnt;
  393. activeTerrain->getTransform().mulP(start, &tStartPnt);
  394. activeTerrain->getTransform().mulP(end, &tEndPnt);
  395. RayInfo info;
  396. bool result = activeTerrain->castRay(tStartPnt, tEndPnt, &info);
  397. if(result)
  398. {
  399. info.point.interpolate(start, end, info.t);
  400. mIsoCamRotCenter = info.point;
  401. }
  402. else
  403. {
  404. mIsoCamRotCenter = start;
  405. }
  406. }
  407. else
  408. {
  409. F32 extx = getExtent().x * 0.5;
  410. F32 exty = getExtent().y * 0.5;
  411. Point3F sp(extx, exty, 0.0f); // Near plane projection
  412. unproject(sp, &mIsoCamRotCenter);
  413. }
  414. }
  415. setFirstResponder();
  416. }
  417. }
  418. void EditTSCtrl::onRightMouseUp(const GuiEvent & event)
  419. {
  420. mRightMouseDown = false;
  421. make3DMouseEvent(mLastEvent, event);
  422. on3DRightMouseUp(mLastEvent);
  423. }
  424. void EditTSCtrl::onRightMouseDragged(const GuiEvent & event)
  425. {
  426. make3DMouseEvent(mLastEvent, event);
  427. on3DRightMouseDragged(mLastEvent);
  428. // Handle zoom of orthographic views.
  429. if( isOrthoDisplayType() )
  430. {
  431. orthoZoom( ( event.mousePoint.y - mLastMousePos.y ) * 0.5f );
  432. mLastMousePos = event.mousePoint;
  433. }
  434. }
  435. void EditTSCtrl::onMiddleMouseDown(const GuiEvent & event)
  436. {
  437. mMiddleMouseDown = true;
  438. mMiddleMouseTriggered = false;
  439. mLastBorderMoveTime = 0;
  440. if(!mLeftMouseDown && !mRightMouseDown && mMiddleMousePassThru && mProfile->mCanKeyFocus)
  441. {
  442. GuiCanvas *pCanvas = getRoot();
  443. if( !pCanvas )
  444. return;
  445. PlatformWindow *pWindow = static_cast<GuiCanvas*>(getRoot())->getPlatformWindow();
  446. if( !pWindow )
  447. return;
  448. PlatformCursorController *pController = pWindow->getCursorController();
  449. if( !pController )
  450. return;
  451. // ok, gotta disable the mouse
  452. // script functions are lockMouse(true); Canvas.cursorOff();
  453. pWindow->setMouseLocked(true);
  454. pCanvas->setCursorON( false );
  455. // Trigger 2 is used by the camera
  456. MoveManager::mTriggerCount[2]++;
  457. mMiddleMouseTriggered = true;
  458. setFirstResponder();
  459. }
  460. }
  461. void EditTSCtrl::onMiddleMouseUp(const GuiEvent & event)
  462. {
  463. // Trigger 2 is used by the camera
  464. if( mMiddleMouseTriggered )
  465. {
  466. MoveManager::mTriggerCount[2]++;
  467. mMiddleMouseTriggered = false;
  468. }
  469. mMiddleMouseDown = false;
  470. }
  471. void EditTSCtrl::onMiddleMouseDragged(const GuiEvent & event)
  472. {
  473. // Handle translation of orthographic views.
  474. if( isOrthoDisplayType() )
  475. {
  476. calcOrthoCamOffset((event.mousePoint.x - mLastMousePos.x), (event.mousePoint.y - mLastMousePos.y), event.modifier);
  477. mLastMousePos = event.mousePoint;
  478. }
  479. }
  480. bool EditTSCtrl::onMouseWheelUp( const GuiEvent &event )
  481. {
  482. // Looks like this should be zooming based on a factor of the GuiEvent.fval
  483. if( isOrthoDisplayType() && !event.modifier )
  484. {
  485. orthoZoom( -2.f );
  486. return true;
  487. }
  488. make3DMouseEvent(mLastEvent, event);
  489. on3DMouseWheelUp(mLastEvent);
  490. return false;
  491. }
  492. bool EditTSCtrl::onMouseWheelDown( const GuiEvent &event )
  493. {
  494. // Looks like this should be zooming based on a factor of the GuiEvent.fval
  495. if(mDisplayType != DisplayTypePerspective && !event.modifier)
  496. {
  497. orthoZoom( 2.f );
  498. return true;
  499. }
  500. make3DMouseEvent(mLastEvent, event);
  501. on3DMouseWheelDown(mLastEvent);
  502. return false;
  503. }
  504. bool EditTSCtrl::onInputEvent(const InputEventInfo & event)
  505. {
  506. if(mRightMousePassThru && event.deviceType == MouseDeviceType &&
  507. event.objInst == KEY_BUTTON1 && event.action == SI_BREAK)
  508. {
  509. // if the right mouse pass thru is enabled,
  510. // we want to reactivate mouse on a right mouse button up
  511. GuiCanvas *pCanvas = getRoot();
  512. if( !pCanvas )
  513. return false;
  514. PlatformWindow *pWindow = static_cast<GuiCanvas*>(getRoot())->getPlatformWindow();
  515. if( !pWindow )
  516. return false;
  517. PlatformCursorController *pController = pWindow->getCursorController();
  518. if( !pController )
  519. return false;
  520. pWindow->setMouseLocked(false);
  521. pCanvas->setCursorON( true );
  522. if(mDisplayType != DisplayTypePerspective)
  523. {
  524. mouseUnlock();
  525. pCanvas->setForceMouseToGUI(false);
  526. pCanvas->setClampTorqueCursor(mLastMouseClamping);
  527. }
  528. }
  529. if(mMiddleMousePassThru && event.deviceType == MouseDeviceType &&
  530. event.objInst == KEY_BUTTON2 && event.action == SI_BREAK)
  531. {
  532. // if the middle mouse pass thru is enabled,
  533. // we want to reactivate mouse on a middle mouse button up
  534. GuiCanvas *pCanvas = getRoot();
  535. if( !pCanvas )
  536. return false;
  537. PlatformWindow *pWindow = static_cast<GuiCanvas*>(getRoot())->getPlatformWindow();
  538. if( !pWindow )
  539. return false;
  540. PlatformCursorController *pController = pWindow->getCursorController();
  541. if( !pController )
  542. return false;
  543. pWindow->setMouseLocked(false);
  544. pCanvas->setCursorON( true );
  545. }
  546. // we return false so that the canvas can properly process the right mouse button up...
  547. return false;
  548. }
  549. //------------------------------------------------------------------------------
  550. void EditTSCtrl::orthoZoom( F32 steps )
  551. {
  552. //TODO: this really should be proportional
  553. mOrthoFOV += steps;
  554. if( mOrthoFOV < 1.0f )
  555. mOrthoFOV = 1.0f;
  556. }
  557. void EditTSCtrl::calcOrthoCamOffset(F32 mousex, F32 mousey, U8 modifier)
  558. {
  559. F32 camScale = 0.01f;
  560. switch(mDisplayType)
  561. {
  562. case DisplayTypeTop:
  563. mOrthoCamTrans.x -= mousex * mOrthoFOV * camScale;
  564. mOrthoCamTrans.y += mousey * mOrthoFOV * camScale;
  565. break;
  566. case DisplayTypeBottom:
  567. mOrthoCamTrans.x -= mousex * mOrthoFOV * camScale;
  568. mOrthoCamTrans.y -= mousey * mOrthoFOV * camScale;
  569. break;
  570. case DisplayTypeFront:
  571. mOrthoCamTrans.x += mousex * mOrthoFOV * camScale;
  572. mOrthoCamTrans.z += mousey * mOrthoFOV * camScale;
  573. break;
  574. case DisplayTypeBack:
  575. mOrthoCamTrans.x -= mousex * mOrthoFOV * camScale;
  576. mOrthoCamTrans.z += mousey * mOrthoFOV * camScale;
  577. break;
  578. case DisplayTypeLeft:
  579. mOrthoCamTrans.y += mousex * mOrthoFOV * camScale;
  580. mOrthoCamTrans.z += mousey * mOrthoFOV * camScale;
  581. break;
  582. case DisplayTypeRight:
  583. mOrthoCamTrans.y -= mousex * mOrthoFOV * camScale;
  584. mOrthoCamTrans.z += mousey * mOrthoFOV * camScale;
  585. break;
  586. case DisplayTypeIsometric:
  587. if(modifier & SI_PRIMARY_CTRL)
  588. {
  589. // NOTE: Maybe move the center of rotation code to right mouse down to avoid compound errors?
  590. F32 rot = mDegToRad(mousex);
  591. Point3F campos = (mRawCamPos + mOrthoCamTrans) - mIsoCamRotCenter;
  592. MatrixF mat(EulerF(0, 0, rot));
  593. mat.mulP(campos);
  594. mOrthoCamTrans = (campos + mIsoCamRotCenter) - mRawCamPos;
  595. mIsoCamRot.z += rot;
  596. }
  597. else
  598. {
  599. mOrthoCamTrans.x -= mousex * mOrthoFOV * camScale * mCos(mIsoCamRot.z) - mousey * mOrthoFOV * camScale * mSin(mIsoCamRot.z);
  600. mOrthoCamTrans.y += mousex * mOrthoFOV * camScale * mSin(mIsoCamRot.z) + mousey * mOrthoFOV * camScale * mCos(mIsoCamRot.z);
  601. }
  602. break;
  603. }
  604. }
  605. void EditTSCtrl::updateGizmo()
  606. {
  607. mGizmoProfile->restoreDefaultState();
  608. }
  609. //------------------------------------------------------------------------------
  610. void EditTSCtrl::renderWorld(const RectI & updateRect)
  611. {
  612. // Make sure that whatever the editor does, it doesn't
  613. // dirty the GFX transform state.
  614. GFXTransformSaver saver;
  615. updateGizmo();
  616. gClientSceneGraph->setDisplayTargetResolution(getExtent());
  617. // Use a render instance to do editor 3D scene
  618. // rendering after HDR is processed and while the depth
  619. // buffer is still intact.
  620. RenderPassManager *rpm = gClientSceneGraph->getDefaultRenderPass();
  621. ObjectRenderInst *inst = rpm->allocInst<ObjectRenderInst>();
  622. inst->type = RenderPassManager::RIT_Editor;
  623. inst->renderDelegate.bind(this, &EditTSCtrl::_renderScene);
  624. rpm->addInst(inst);
  625. if( mDisplayType == DisplayTypePerspective )
  626. gClientSceneGraph->renderScene( SPT_Diffuse );
  627. else
  628. {
  629. // If we are in an orthographic mode, do a special render
  630. // with AL, fog, and PostFX disabled.
  631. FogData savedFogData = gClientSceneGraph->getFogData();
  632. gClientSceneGraph->setFogData( FogData() );
  633. SceneRenderState renderState
  634. (
  635. gClientSceneGraph,
  636. SPT_Diffuse
  637. );
  638. gClientSceneGraph->renderScene( &renderState );
  639. gClientSceneGraph->setFogData( savedFogData );
  640. }
  641. }
  642. void EditTSCtrl::_renderScene( ObjectRenderInst*, SceneRenderState *state, BaseMatInstance* )
  643. {
  644. GFXTransformSaver saver;
  645. // render through console callbacks
  646. SimSet * missionGroup = static_cast<SimSet*>(Sim::findObject("MissionGroup"));
  647. if(missionGroup)
  648. {
  649. mConsoleRendering = true;
  650. // [ rene, 27-Jan-10 ] This calls onEditorRender on the server objects instead
  651. // of on the client objects which seems a bit questionable to me.
  652. for(SimSetIterator itr(missionGroup); *itr; ++itr)
  653. {
  654. SceneObject* object = dynamic_cast< SceneObject* >( *itr );
  655. if( object && object->isRenderEnabled() && !object->isHidden() )
  656. {
  657. char buf[2][16];
  658. dSprintf(buf[0], 16, object->isSelected() ? "true" : "false");
  659. dSprintf(buf[1], 16, object->isExpanded() ? "true" : "false");
  660. Con::executef( object, "onEditorRender", getIdString(), buf[0], buf[1] );
  661. }
  662. }
  663. mConsoleRendering = false;
  664. }
  665. // render the mission area...
  666. renderMissionArea();
  667. // Draw the grid
  668. if ( mRenderGridPlane )
  669. renderGrid();
  670. // render the editor stuff
  671. renderScene(mSaveViewport);
  672. // Draw the camera axis
  673. GFX->setClipRect(mSaveViewport);
  674. GFX->setStateBlock(mDefaultGuiSB);
  675. renderCameraAxis();
  676. }
  677. void EditTSCtrl::renderMissionArea()
  678. {
  679. MissionArea* obj = MissionArea::getServerObject();
  680. if ( !obj )
  681. return;
  682. if ( !mRenderMissionArea && !obj->isSelected() )
  683. return;
  684. GFXDEBUGEVENT_SCOPE( Editor_renderMissionArea, ColorI::WHITE );
  685. F32 minHeight = 0.0f;
  686. F32 maxHeight = 0.0f;
  687. TerrainBlock* terrain = getActiveTerrain();
  688. if ( terrain )
  689. {
  690. terrain->getMinMaxHeight( &minHeight, &maxHeight );
  691. Point3F pos = terrain->getPosition();
  692. maxHeight += pos.z + mMissionAreaHeightAdjust;
  693. minHeight += pos.z - mMissionAreaHeightAdjust;
  694. }
  695. const RectI& area = obj->getArea();
  696. Box3F areaBox( area.point.x,
  697. area.point.y,
  698. minHeight,
  699. area.point.x + area.extent.x,
  700. area.point.y + area.extent.y,
  701. maxHeight );
  702. GFXDrawUtil* drawer = GFX->getDrawUtil();
  703. GFXStateBlockDesc desc;
  704. desc.setCullMode( GFXCullNone );
  705. desc.setBlend( true );
  706. desc.setZReadWrite( false, false );
  707. desc.setFillModeSolid();
  708. drawer->drawCube( desc, areaBox, mMissionAreaFillColor );
  709. desc.setFillModeWireframe();
  710. drawer->drawCube( desc, areaBox, mMissionAreaFrameColor );
  711. }
  712. void EditTSCtrl::renderCameraAxis()
  713. {
  714. GFXDEBUGEVENT_SCOPE( Editor_renderCameraAxis, ColorI::WHITE );
  715. static MatrixF sRotMat(EulerF( (M_PI_F / -2.0f), 0.0f, 0.0f));
  716. MatrixF camMat = mLastCameraQuery.cameraMatrix;
  717. camMat.mul(sRotMat);
  718. camMat.inverse();
  719. MatrixF axis;
  720. axis.setColumn(0, Point3F(1, 0, 0));
  721. axis.setColumn(1, Point3F(0, 0, 1));
  722. axis.setColumn(2, Point3F(0, -1, 0));
  723. axis.mul(camMat);
  724. Point3F forwardVec, upVec, rightVec;
  725. axis.getColumn( 2, &forwardVec );
  726. axis.getColumn( 1, &upVec );
  727. axis.getColumn( 0, &rightVec );
  728. Point2I pos = getPosition();
  729. F32 offsetx = pos.x + 20.0;
  730. F32 offsety = pos.y + getExtent().y - 42.0; // Take the status bar into account
  731. F32 scale = 15.0;
  732. // Generate correct drawing order
  733. ColorI c1(255,0,0);
  734. ColorI c2(0,255,0);
  735. ColorI c3(0,0,255);
  736. ColorI tc;
  737. Point3F *p1, *p2, *p3, *tp;
  738. p1 = &rightVec;
  739. p2 = &upVec;
  740. p3 = &forwardVec;
  741. if(p3->y > p2->y)
  742. {
  743. tp = p2; tc = c2;
  744. p2 = p3; c2 = c3;
  745. p3 = tp; c3 = tc;
  746. }
  747. if(p2->y > p1->y)
  748. {
  749. tp = p1; tc = c1;
  750. p1 = p2; c1 = c2;
  751. p2 = tp; c2 = tc;
  752. }
  753. PrimBuild::begin( GFXLineList, 6 );
  754. //*** Axis 1
  755. PrimBuild::color(c1);
  756. PrimBuild::vertex3f(offsetx, offsety, 0);
  757. PrimBuild::vertex3f(offsetx+p1->x*scale, offsety-p1->z*scale, 0);
  758. //*** Axis 2
  759. PrimBuild::color(c2);
  760. PrimBuild::vertex3f(offsetx, offsety, 0);
  761. PrimBuild::vertex3f(offsetx+p2->x*scale, offsety-p2->z*scale, 0);
  762. //*** Axis 3
  763. PrimBuild::color(c3);
  764. PrimBuild::vertex3f(offsetx, offsety, 0);
  765. PrimBuild::vertex3f(offsetx+p3->x*scale, offsety-p3->z*scale, 0);
  766. PrimBuild::end();
  767. }
  768. void EditTSCtrl::renderGrid()
  769. {
  770. if( !isOrthoDisplayType() )
  771. return;
  772. GFXDEBUGEVENT_SCOPE( Editor_renderGrid, ColorI::WHITE );
  773. // Calculate the displayed grid size based on view
  774. F32 drawnGridSize = mGridPlaneSize;
  775. F32 gridPixelSize = projectRadius(1.0f, mGridPlaneSize);
  776. if(gridPixelSize < mGridPlaneSizePixelBias)
  777. {
  778. U32 counter = 1;
  779. while(gridPixelSize < mGridPlaneSizePixelBias)
  780. {
  781. drawnGridSize = mGridPlaneSize * counter * 10.0f;
  782. gridPixelSize = projectRadius(1.0f, drawnGridSize);
  783. ++counter;
  784. // No infinite loops here
  785. if(counter > 1000)
  786. break;
  787. }
  788. }
  789. F32 minorTickSize = 0;
  790. F32 gridSize = drawnGridSize;
  791. U32 minorTickMax = mGridPlaneMinorTicks + 1;
  792. if(minorTickMax > 0)
  793. {
  794. minorTickSize = drawnGridSize;
  795. gridSize = drawnGridSize * minorTickMax;
  796. }
  797. // Build the view-based origin
  798. VectorF dir;
  799. smCamMatrix.getColumn( 1, &dir );
  800. Point3F gridPlanePos = smCamPos + dir;
  801. Point2F size(mOrthoWidth + 2 * gridSize, mOrthoHeight + 2 * gridSize);
  802. GFXStateBlockDesc desc;
  803. desc.setBlend( true );
  804. desc.setZReadWrite( true, false );
  805. GFXDrawUtil::Plane plane = GFXDrawUtil::PlaneXY;
  806. switch( getDisplayType() )
  807. {
  808. case DisplayTypeTop:
  809. case DisplayTypeBottom:
  810. plane = GFXDrawUtil::PlaneXY;
  811. break;
  812. case DisplayTypeLeft:
  813. case DisplayTypeRight:
  814. plane = GFXDrawUtil::PlaneYZ;
  815. break;
  816. case DisplayTypeFront:
  817. case DisplayTypeBack:
  818. plane = GFXDrawUtil::PlaneXZ;
  819. break;
  820. default:
  821. break;
  822. }
  823. GFX->getDrawUtil()->drawPlaneGrid( desc, gridPlanePos, size, Point2F( minorTickSize, minorTickSize ), mGridPlaneMinorTickColor, plane );
  824. GFX->getDrawUtil()->drawPlaneGrid( desc, gridPlanePos, size, Point2F( gridSize, gridSize ), mGridPlaneColor, plane );
  825. }
  826. static void sceneBoundsCalcCallback(SceneObject* obj, void *key)
  827. {
  828. // Early out for those objects that slipped through the mask check
  829. // because they belong to more than one type.
  830. if((obj->getTypeMask() & EditTSCtrl::smSceneBoundsMask) != 0)
  831. return;
  832. if(obj->isGlobalBounds())
  833. return;
  834. Box3F* bounds = (Box3F*)key;
  835. Point3F min = obj->getWorldBox().minExtents;
  836. Point3F max = obj->getWorldBox().maxExtents;
  837. #if 0 // Console messages in render loop lead to massive spam.
  838. if (min.x <= -5000.0f || min.y <= -5000.0f || min.z <= -5000.0f ||
  839. min.x >= 5000.0f || min.y >= 5000.0f || min.z >= 5000.0f)
  840. Con::errorf("SceneObject %d (%s : %s) has a bounds that could cause problems with a non-perspective view", obj->getId(), obj->getClassName(), obj->getName());
  841. #endif
  842. bounds->minExtents.setMin(min);
  843. bounds->minExtents.setMin(max);
  844. bounds->maxExtents.setMax(min);
  845. bounds->maxExtents.setMax(max);
  846. }
  847. bool EditTSCtrl::getCameraTransform(MatrixF* cameraMatrix)
  848. {
  849. GameConnection* connection = dynamic_cast<GameConnection *>(NetConnection::getConnectionToServer());
  850. return (connection && connection->getControlCameraTransform(0.032f, cameraMatrix));
  851. }
  852. void EditTSCtrl::computeSceneBounds(Box3F& bounds)
  853. {
  854. bounds.minExtents.set(1e10, 1e10, 1e10);
  855. bounds.maxExtents.set(-1e10, -1e10, -1e10);
  856. // Calculate the scene bounds
  857. gClientContainer.findObjects(~(smSceneBoundsMask), sceneBoundsCalcCallback, &bounds);
  858. }
  859. bool EditTSCtrl::processCameraQuery(CameraQuery * query)
  860. {
  861. if(mDisplayType == DisplayTypePerspective)
  862. {
  863. query->ortho = false;
  864. }
  865. else
  866. {
  867. query->ortho = true;
  868. }
  869. if (getCameraTransform(&query->cameraMatrix))
  870. {
  871. query->farPlane = gClientSceneGraph->getVisibleDistance() * smVisibleDistanceScale;
  872. query->nearPlane = gClientSceneGraph->getNearClip();
  873. query->fov = mDegToRad(smCamFOV);
  874. if(query->ortho)
  875. {
  876. MatrixF camRot(true);
  877. const F32 camBuffer = 1.0f;
  878. Point3F camPos = query->cameraMatrix.getPosition();
  879. F32 isocamplanedist = 0.0f;
  880. if(mDisplayType == DisplayTypeIsometric)
  881. {
  882. const RectI& vp = GFX->getViewport();
  883. isocamplanedist = 0.25 * vp.extent.y * mSin(mIsoCamAngle);
  884. }
  885. // Calculate the scene bounds
  886. Box3F sceneBounds;
  887. computeSceneBounds(sceneBounds);
  888. if(!sceneBounds.isValidBox())
  889. {
  890. sceneBounds.maxExtents = camPos + smMinSceneBounds;
  891. sceneBounds.minExtents = camPos - smMinSceneBounds;
  892. }
  893. else
  894. {
  895. query->farPlane = getMax(smMinSceneBounds.x * 2.0f, (sceneBounds.maxExtents - sceneBounds.minExtents).len() + camBuffer * 2.0f + isocamplanedist);
  896. }
  897. mRawCamPos = camPos;
  898. camPos += mOrthoCamTrans;
  899. switch(mDisplayType)
  900. {
  901. case DisplayTypeTop:
  902. camRot.setColumn(0, Point3F(1.0, 0.0, 0.0));
  903. camRot.setColumn(1, Point3F(0.0, 0.0, -1.0));
  904. camRot.setColumn(2, Point3F(0.0, 1.0, 0.0));
  905. camPos.z = getMax(camPos.z + smMinSceneBounds.z, sceneBounds.maxExtents.z + camBuffer);
  906. break;
  907. case DisplayTypeBottom:
  908. camRot.setColumn(0, Point3F(1.0, 0.0, 0.0));
  909. camRot.setColumn(1, Point3F(0.0, 0.0, 1.0));
  910. camRot.setColumn(2, Point3F(0.0, -1.0, 0.0));
  911. camPos.z = getMin(camPos.z - smMinSceneBounds.z, sceneBounds.minExtents.z - camBuffer);
  912. break;
  913. case DisplayTypeFront:
  914. camRot.setColumn(0, Point3F(-1.0, 0.0, 0.0));
  915. camRot.setColumn(1, Point3F( 0.0, -1.0, 0.0));
  916. camRot.setColumn(2, Point3F( 0.0, 0.0, 1.0));
  917. camPos.y = getMax(camPos.y + smMinSceneBounds.y, sceneBounds.maxExtents.y + camBuffer);
  918. break;
  919. case DisplayTypeBack:
  920. camRot.setColumn(0, Point3F(1.0, 0.0, 0.0));
  921. camRot.setColumn(1, Point3F(0.0, 1.0, 0.0));
  922. camRot.setColumn(2, Point3F(0.0, 0.0, 1.0));
  923. camPos.y = getMin(camPos.y - smMinSceneBounds.y, sceneBounds.minExtents.y - camBuffer);
  924. break;
  925. case DisplayTypeLeft:
  926. camRot.setColumn(0, Point3F( 0.0, -1.0, 0.0));
  927. camRot.setColumn(1, Point3F( 1.0, 0.0, 0.0));
  928. camRot.setColumn(2, Point3F( 0.0, 0.0, 1.0));
  929. camPos.x = getMin(camPos.x - smMinSceneBounds.x, sceneBounds.minExtents.x - camBuffer);
  930. break;
  931. case DisplayTypeRight:
  932. camRot.setColumn(0, Point3F( 0.0, 1.0, 0.0));
  933. camRot.setColumn(1, Point3F(-1.0, 0.0, 0.0));
  934. camRot.setColumn(2, Point3F( 0.0, 0.0, 1.0));
  935. camPos.x = getMax(camPos.x + smMinSceneBounds.x, sceneBounds.maxExtents.x + camBuffer);
  936. break;
  937. case DisplayTypeIsometric:
  938. camPos.z = sceneBounds.maxExtents.z + camBuffer + isocamplanedist;
  939. MatrixF angle(EulerF(mIsoCamAngle, 0, 0));
  940. MatrixF rot(mIsoCamRot);
  941. camRot.mul(rot, angle);
  942. break;
  943. }
  944. query->cameraMatrix = camRot;
  945. query->cameraMatrix.setPosition(camPos);
  946. query->headMatrix = query->cameraMatrix;
  947. query->fov = mOrthoFOV;
  948. }
  949. smCamMatrix = query->cameraMatrix;
  950. smCamMatrix.getColumn(3,&smCamPos);
  951. smCamOrtho = query->ortho;
  952. smCamNearPlane = query->nearPlane;
  953. return true;
  954. }
  955. return false;
  956. }
  957. //------------------------------------------------------------------------------
  958. DefineEngineMethod( EditTSCtrl, getDisplayType, S32, (),, "" )
  959. {
  960. return object->getDisplayType();
  961. }
  962. DefineEngineMethod( EditTSCtrl, setDisplayType, void, ( S32 displayType ),, "" )
  963. {
  964. object->setDisplayType( displayType );
  965. }
  966. DefineEngineMethod( EditTSCtrl, getOrthoFOV, F32, (),,
  967. "Return the FOV for orthographic views." )
  968. {
  969. return object->getOrthoFOV();
  970. }
  971. DefineEngineMethod( EditTSCtrl, setOrthoFOV, void, ( F32 fov ),,
  972. "Set the FOV for to use for orthographic views." )
  973. {
  974. object->setOrthoFOV( fov );
  975. }
  976. DefineEngineMethod( EditTSCtrl, renderBox, void, ( Point3F pos, Point3F size ),, "" )
  977. {
  978. if( !object->mConsoleRendering || !object->mConsoleFillColor.alpha )
  979. return;
  980. GFXStateBlockDesc desc;
  981. desc.setBlend( true );
  982. Box3F box;
  983. box.set( size );
  984. box.setCenter( pos );
  985. MatrixF camera = GFX->getWorldMatrix();
  986. camera.inverse();
  987. if( box.isContained( camera.getPosition() ) )
  988. desc.setCullMode( GFXCullNone );
  989. GFX->getDrawUtil()->drawCube( desc, size, pos, object->mConsoleFillColor );
  990. }
  991. DefineEngineMethod(EditTSCtrl, renderSphere, void, ( Point3F pos, F32 radius, S32 sphereLevel ), ( 0 ), "" )
  992. {
  993. if ( !object->mConsoleRendering || !object->mConsoleFillColor.alpha )
  994. return;
  995. // TODO: We need to support subdivision levels in GFXDrawUtil!
  996. if ( sphereLevel <= 0 )
  997. sphereLevel = object->mConsoleSphereLevel;
  998. GFXStateBlockDesc desc;
  999. desc.setBlend( true );
  1000. MatrixF camera = GFX->getWorldMatrix();
  1001. camera.inverse();
  1002. SphereF sphere( pos, radius );
  1003. if( sphere.isContained( camera.getPosition() ) )
  1004. desc.setCullMode( GFXCullNone );
  1005. GFX->getDrawUtil()->drawSphere( desc, radius, pos, object->mConsoleFillColor );
  1006. }
  1007. DefineEngineMethod( EditTSCtrl, renderCircle, void, ( Point3F pos, Point3F normal, F32 radius, S32 segments ), ( 0 ), "" )
  1008. {
  1009. if(!object->mConsoleRendering)
  1010. return;
  1011. if(!object->mConsoleFrameColor.alpha && !object->mConsoleFillColor.alpha)
  1012. return;
  1013. if ( segments <= 0 )
  1014. segments = object->mConsoleCircleSegments;
  1015. normal.normalizeSafe();
  1016. AngAxisF aa;
  1017. F32 dotUp = mDot( normal, Point3F(0,0,1) );
  1018. if ( dotUp == 1.0f )
  1019. aa.set( Point3F(0,0,1), 0.0f ); // normal is 0,0,1
  1020. else if ( dotUp == -1.0f )
  1021. aa.set( Point3F(1,0,0), M_PI_F ); // normal is 0,0,-1
  1022. else
  1023. {
  1024. mCross( normal, Point3F(0,0,1), &aa.axis );
  1025. aa.axis.normalizeSafe();
  1026. aa.angle = mAcos( mClampF( dotUp, -1.f, 1.f ) );
  1027. }
  1028. MatrixF mat;
  1029. aa.setMatrix(&mat);
  1030. F32 step = M_2PI / segments;
  1031. F32 angle = 0.f;
  1032. Vector<Point3F> points(segments);
  1033. for(U32 i = 0; i < segments; i++)
  1034. {
  1035. Point3F pnt(mCos(angle), mSin(angle), 0.f);
  1036. mat.mulP(pnt);
  1037. pnt *= radius;
  1038. pnt += pos;
  1039. points.push_front(pnt);
  1040. angle += step;
  1041. }
  1042. GFX->setStateBlock(object->mBlendSB);
  1043. // framed
  1044. if(object->mConsoleFrameColor.alpha)
  1045. {
  1046. // TODO: Set GFX line width (when it exists) to the value of 'object->mConsoleLineWidth'
  1047. PrimBuild::color( object->mConsoleFrameColor );
  1048. PrimBuild::begin( GFXLineStrip, points.size() + 1 );
  1049. for( S32 i = 0; i < points.size(); i++ )
  1050. PrimBuild::vertex3fv( points[i] );
  1051. // GFX does not have a LineLoop primitive, so connect the last line
  1052. if( points.size() > 0 )
  1053. PrimBuild::vertex3fv( points[0] );
  1054. PrimBuild::end();
  1055. // TODO: Reset GFX line width here
  1056. }
  1057. // filled
  1058. if(object->mConsoleFillColor.alpha)
  1059. {
  1060. PrimBuild::color( object->mConsoleFillColor );
  1061. PrimBuild::begin( GFXTriangleStrip, points.size() + 2 );
  1062. // Center point
  1063. PrimBuild::vertex3fv( pos );
  1064. // Edge verts
  1065. for( S32 i = 0; i < points.size(); i++ )
  1066. PrimBuild::vertex3fv( points[i] );
  1067. PrimBuild::vertex3fv( points[0] );
  1068. PrimBuild::end();
  1069. }
  1070. }
  1071. DefineEngineMethod( EditTSCtrl, renderTriangle, void, ( Point3F a, Point3F b, Point3F c ),, "" )
  1072. {
  1073. if(!object->mConsoleRendering)
  1074. return;
  1075. if(!object->mConsoleFrameColor.alpha && !object->mConsoleFillColor.alpha)
  1076. return;
  1077. const Point3F* pnts[3] = { &a, &b, &c };
  1078. GFX->setStateBlock(object->mBlendSB);
  1079. // frame
  1080. if( object->mConsoleFrameColor.alpha )
  1081. {
  1082. PrimBuild::color( object->mConsoleFrameColor );
  1083. // TODO: Set GFX line width (when it exists) to the value of 'object->mConsoleLineWidth'
  1084. PrimBuild::begin( GFXLineStrip, 4 );
  1085. PrimBuild::vertex3fv( *pnts[0] );
  1086. PrimBuild::vertex3fv( *pnts[1] );
  1087. PrimBuild::vertex3fv( *pnts[2] );
  1088. PrimBuild::vertex3fv( *pnts[0] );
  1089. PrimBuild::end();
  1090. // TODO: Reset GFX line width here
  1091. }
  1092. // fill
  1093. if( object->mConsoleFillColor.alpha )
  1094. {
  1095. PrimBuild::color( object->mConsoleFillColor );
  1096. PrimBuild::begin( GFXTriangleList, 3 );
  1097. PrimBuild::vertex3fv( *pnts[0] );
  1098. PrimBuild::vertex3fv( *pnts[1] );
  1099. PrimBuild::vertex3fv( *pnts[2] );
  1100. PrimBuild::end();
  1101. }
  1102. }
  1103. DefineEngineMethod( EditTSCtrl, renderLine, void, ( Point3F start, Point3F end, F32 lineWidth ), ( 0 ), "" )
  1104. {
  1105. if ( !object->mConsoleRendering || !object->mConsoleFrameColor.alpha )
  1106. return;
  1107. // TODO: We don't support 3d lines with width... fix this!
  1108. if ( lineWidth <= 0 )
  1109. lineWidth = object->mConsoleLineWidth;
  1110. GFX->getDrawUtil()->drawLine( start, end, object->mConsoleFrameColor );
  1111. }
  1112. DefineEngineMethod( EditTSCtrl, getGizmo, S32, (),, "" )
  1113. {
  1114. return object->getGizmo()->getId();
  1115. }
  1116. DefineEngineMethod( EditTSCtrl, isMiddleMouseDown, bool, (),, "" )
  1117. {
  1118. return object->isMiddleMouseDown();
  1119. }