editTSCtrl.cpp 41 KB

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