editTSCtrl.cpp 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395
  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->fov = mOrthoFOV;
  947. }
  948. smCamMatrix = query->cameraMatrix;
  949. smCamMatrix.getColumn(3,&smCamPos);
  950. smCamOrtho = query->ortho;
  951. smCamNearPlane = query->nearPlane;
  952. return true;
  953. }
  954. return false;
  955. }
  956. //------------------------------------------------------------------------------
  957. DefineEngineMethod( EditTSCtrl, getDisplayType, S32, (),, "" )
  958. {
  959. return object->getDisplayType();
  960. }
  961. DefineEngineMethod( EditTSCtrl, setDisplayType, void, ( S32 displayType ),, "" )
  962. {
  963. object->setDisplayType( displayType );
  964. }
  965. DefineEngineMethod( EditTSCtrl, getOrthoFOV, F32, (),,
  966. "Return the FOV for orthographic views." )
  967. {
  968. return object->getOrthoFOV();
  969. }
  970. DefineEngineMethod( EditTSCtrl, setOrthoFOV, void, ( F32 fov ),,
  971. "Set the FOV for to use for orthographic views." )
  972. {
  973. object->setOrthoFOV( fov );
  974. }
  975. DefineEngineMethod( EditTSCtrl, renderBox, void, ( Point3F pos, Point3F size ),, "" )
  976. {
  977. if( !object->mConsoleRendering || !object->mConsoleFillColor.alpha )
  978. return;
  979. GFXStateBlockDesc desc;
  980. desc.setBlend( true );
  981. Box3F box;
  982. box.set( size );
  983. box.setCenter( pos );
  984. MatrixF camera = GFX->getWorldMatrix();
  985. camera.inverse();
  986. if( box.isContained( camera.getPosition() ) )
  987. desc.setCullMode( GFXCullNone );
  988. GFX->getDrawUtil()->drawCube( desc, size, pos, object->mConsoleFillColor );
  989. }
  990. DefineEngineMethod(EditTSCtrl, renderSphere, void, ( Point3F pos, F32 radius, S32 sphereLevel ), ( 0 ), "" )
  991. {
  992. if ( !object->mConsoleRendering || !object->mConsoleFillColor.alpha )
  993. return;
  994. // TODO: We need to support subdivision levels in GFXDrawUtil!
  995. if ( sphereLevel <= 0 )
  996. sphereLevel = object->mConsoleSphereLevel;
  997. GFXStateBlockDesc desc;
  998. desc.setBlend( true );
  999. MatrixF camera = GFX->getWorldMatrix();
  1000. camera.inverse();
  1001. SphereF sphere( pos, radius );
  1002. if( sphere.isContained( camera.getPosition() ) )
  1003. desc.setCullMode( GFXCullNone );
  1004. GFX->getDrawUtil()->drawSphere( desc, radius, pos, object->mConsoleFillColor );
  1005. }
  1006. DefineEngineMethod( EditTSCtrl, renderCircle, void, ( Point3F pos, Point3F normal, F32 radius, S32 segments ), ( 0 ), "" )
  1007. {
  1008. if(!object->mConsoleRendering)
  1009. return;
  1010. if(!object->mConsoleFrameColor.alpha && !object->mConsoleFillColor.alpha)
  1011. return;
  1012. if ( segments <= 0 )
  1013. segments = object->mConsoleCircleSegments;
  1014. normal.normalizeSafe();
  1015. AngAxisF aa;
  1016. F32 dotUp = mDot( normal, Point3F(0,0,1) );
  1017. if ( dotUp == 1.0f )
  1018. aa.set( Point3F(0,0,1), 0.0f ); // normal is 0,0,1
  1019. else if ( dotUp == -1.0f )
  1020. aa.set( Point3F(1,0,0), M_PI_F ); // normal is 0,0,-1
  1021. else
  1022. {
  1023. mCross( normal, Point3F(0,0,1), &aa.axis );
  1024. aa.axis.normalizeSafe();
  1025. aa.angle = mAcos( mClampF( dotUp, -1.f, 1.f ) );
  1026. }
  1027. MatrixF mat;
  1028. aa.setMatrix(&mat);
  1029. F32 step = M_2PI / segments;
  1030. F32 angle = 0.f;
  1031. Vector<Point3F> points(segments);
  1032. for(U32 i = 0; i < segments; i++)
  1033. {
  1034. Point3F pnt(mCos(angle), mSin(angle), 0.f);
  1035. mat.mulP(pnt);
  1036. pnt *= radius;
  1037. pnt += pos;
  1038. points.push_front(pnt);
  1039. angle += step;
  1040. }
  1041. GFX->setStateBlock(object->mBlendSB);
  1042. // framed
  1043. if(object->mConsoleFrameColor.alpha)
  1044. {
  1045. // TODO: Set GFX line width (when it exists) to the value of 'object->mConsoleLineWidth'
  1046. PrimBuild::color( object->mConsoleFrameColor );
  1047. PrimBuild::begin( GFXLineStrip, points.size() + 1 );
  1048. for( S32 i = 0; i < points.size(); i++ )
  1049. PrimBuild::vertex3fv( points[i] );
  1050. // GFX does not have a LineLoop primitive, so connect the last line
  1051. if( points.size() > 0 )
  1052. PrimBuild::vertex3fv( points[0] );
  1053. PrimBuild::end();
  1054. // TODO: Reset GFX line width here
  1055. }
  1056. // filled
  1057. if(object->mConsoleFillColor.alpha)
  1058. {
  1059. PrimBuild::color( object->mConsoleFillColor );
  1060. PrimBuild::begin( GFXTriangleFan, points.size() + 2 );
  1061. // Center point
  1062. PrimBuild::vertex3fv( pos );
  1063. // Edge verts
  1064. for( S32 i = 0; i < points.size(); i++ )
  1065. PrimBuild::vertex3fv( points[i] );
  1066. PrimBuild::vertex3fv( points[0] );
  1067. PrimBuild::end();
  1068. }
  1069. }
  1070. DefineEngineMethod( EditTSCtrl, renderTriangle, void, ( Point3F a, Point3F b, Point3F c ),, "" )
  1071. {
  1072. if(!object->mConsoleRendering)
  1073. return;
  1074. if(!object->mConsoleFrameColor.alpha && !object->mConsoleFillColor.alpha)
  1075. return;
  1076. const Point3F* pnts[3] = { &a, &b, &c };
  1077. GFX->setStateBlock(object->mBlendSB);
  1078. // frame
  1079. if( object->mConsoleFrameColor.alpha )
  1080. {
  1081. PrimBuild::color( object->mConsoleFrameColor );
  1082. // TODO: Set GFX line width (when it exists) to the value of 'object->mConsoleLineWidth'
  1083. PrimBuild::begin( GFXLineStrip, 4 );
  1084. PrimBuild::vertex3fv( *pnts[0] );
  1085. PrimBuild::vertex3fv( *pnts[1] );
  1086. PrimBuild::vertex3fv( *pnts[2] );
  1087. PrimBuild::vertex3fv( *pnts[0] );
  1088. PrimBuild::end();
  1089. // TODO: Reset GFX line width here
  1090. }
  1091. // fill
  1092. if( object->mConsoleFillColor.alpha )
  1093. {
  1094. PrimBuild::color( object->mConsoleFillColor );
  1095. PrimBuild::begin( GFXTriangleList, 3 );
  1096. PrimBuild::vertex3fv( *pnts[0] );
  1097. PrimBuild::vertex3fv( *pnts[1] );
  1098. PrimBuild::vertex3fv( *pnts[2] );
  1099. PrimBuild::end();
  1100. }
  1101. }
  1102. DefineEngineMethod( EditTSCtrl, renderLine, void, ( Point3F start, Point3F end, F32 lineWidth ), ( 0 ), "" )
  1103. {
  1104. if ( !object->mConsoleRendering || !object->mConsoleFrameColor.alpha )
  1105. return;
  1106. // TODO: We don't support 3d lines with width... fix this!
  1107. if ( lineWidth <= 0 )
  1108. lineWidth = object->mConsoleLineWidth;
  1109. GFX->getDrawUtil()->drawLine( start, end, object->mConsoleFrameColor );
  1110. }
  1111. DefineEngineMethod( EditTSCtrl, getGizmo, S32, (),, "" )
  1112. {
  1113. return object->getGizmo()->getId();
  1114. }
  1115. DefineEngineMethod( EditTSCtrl, isMiddleMouseDown, bool, (),, "" )
  1116. {
  1117. return object->isMiddleMouseDown();
  1118. }