editTSCtrl.cpp 41 KB

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