editTSCtrl.cpp 40 KB

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