SceneWindow.cc 70 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 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 "graphics/dgl.h"
  23. #include "gui/guiTypes.h"
  24. #include "gui/guiCanvas.h"
  25. #include "console/console.h"
  26. #include "console/consoleTypes.h"
  27. #include "math/mMathFn.h"
  28. #include "2d/sceneobject/SceneObject.h"
  29. #include "2d/core/Utility.h"
  30. #include "2d/gui/SceneWindow.h"
  31. #ifndef _ASSET_MANAGER_H_
  32. #include "assets/assetManager.h"
  33. #endif
  34. // Script bindings.
  35. #include "SceneWindow_ScriptBinding.h"
  36. // Debug Profiling.
  37. #include "debug/profiler.h"
  38. //-----------------------------------------------------------------------------
  39. IMPLEMENT_CONOBJECT(SceneWindow);
  40. //-----------------------------------------------------------------------------
  41. static EnumTable::Enums interpolationModeLookup[] =
  42. {
  43. { SceneWindow::LINEAR, "LINEAR" },
  44. { SceneWindow::SIGMOID, "SIGMOID" },
  45. };
  46. static EnumTable interpolationModeTable(sizeof(interpolationModeLookup) / sizeof(EnumTable::Enums), &interpolationModeLookup[0]);
  47. //-----------------------------------------------------------------------------
  48. SceneWindow::CameraInterpolationMode SceneWindow::getInterpolationModeEnum(const char* label)
  49. {
  50. // Search for Mnemonic.
  51. for(U32 i = 0; i < (sizeof(interpolationModeLookup) / sizeof(EnumTable::Enums)); i++)
  52. if( dStricmp(interpolationModeLookup[i].label, label) == 0)
  53. return((CameraInterpolationMode)interpolationModeLookup[i].index);
  54. // Warn.
  55. Con::warnf( "SceneWindow::getInterpolationModeEnum() - Invalid interpolation mode '%s'.", label );
  56. return SceneWindow::INVALID_INTERPOLATION_MODE;
  57. }
  58. //-----------------------------------------------------------------------------
  59. SceneWindow::SceneWindow() : mpScene(NULL),
  60. mLockMouse(false),
  61. mWindowDirty(true),
  62. mRenderLayerMask(MASK_ALL),
  63. mRenderGroupMask(MASK_ALL),
  64. mBackgroundColor( "Black" ),
  65. mUseBackgroundColor(false),
  66. mCameraInterpolationMode(SIGMOID),
  67. mMaxQueueItems(64),
  68. mCameraTransitionTime(2.0f),
  69. mMovingCamera(false),
  70. mpMountedTo(NULL),
  71. mCameraMounted(false),
  72. mCameraShaking(false),
  73. mCameraShakeOffset(0.0f,0.0f),
  74. mViewLimitActive(false),
  75. mUseWindowInputEvents(true),
  76. mUseObjectInputEvents(false),
  77. mInputEventGroupMaskFilter(MASK_ALL),
  78. mInputEventLayerMaskFilter(MASK_ALL),
  79. mInputEventInvisibleFilter( true )
  80. {
  81. // Set Vector Associations.
  82. VECTOR_SET_ASSOCIATION( mCameraQueue );
  83. VECTOR_SET_ASSOCIATION( mInputEventQuery );
  84. VECTOR_SET_ASSOCIATION( mInputEventEntering );
  85. VECTOR_SET_ASSOCIATION( mInputEventLeaving );
  86. // Touch input event names.
  87. mInputEventDownName = StringTable->insert("onTouchDown");
  88. mInputEventUpName = StringTable->insert("onTouchUp");
  89. mInputEventMovedName = StringTable->insert("onTouchMoved");
  90. mInputEventDraggedName = StringTable->insert("onTouchDragged");
  91. mInputEventEnterName = StringTable->insert("onTouchEnter");
  92. mInputEventLeaveName = StringTable->insert("onTouchLeave");
  93. // Mouse input event names.
  94. mMouseEventRightMouseDownName = StringTable->insert("onRightMouseDown");
  95. mMouseEventRightMouseUpName = StringTable->insert("onRightMouseUp");
  96. mMouseEventRightMouseDraggedName= StringTable->insert("onRightMouseDragged");
  97. mMouseEventWheelUpName = StringTable->insert("onMouseWheelUp");
  98. mMouseEventWheelDownName = StringTable->insert("onMouseWheelDown");
  99. mMouseEventEnterName = StringTable->insert("onMouseEnter");
  100. mMouseEventLeaveName = StringTable->insert("onMouseLeave");
  101. // Turn-on Tick Processing.
  102. setProcessTicks( true );
  103. }
  104. //-----------------------------------------------------------------------------
  105. SceneWindow::~SceneWindow()
  106. {
  107. }
  108. //-----------------------------------------------------------------------------
  109. bool SceneWindow::onAdd()
  110. {
  111. if(!Parent::onAdd())
  112. return false;
  113. // Reset the camera position.
  114. setCameraPosition( Vector2::getZero() );
  115. // Reset the camera size.
  116. setCameraSize( Vector2( 100.0f, 75.0f ) );
  117. // Reset the camera zoom.
  118. setCameraZoom( 1.0f );
  119. // Zero Camera Time.
  120. zeroCameraTime();
  121. // Return Okay.
  122. return true;
  123. }
  124. //-----------------------------------------------------------------------------
  125. void SceneWindow::onRemove()
  126. {
  127. // Reset Scene.
  128. resetScene();
  129. // Call Parent.
  130. Parent::onRemove();
  131. }
  132. //-----------------------------------------------------------------------------
  133. void SceneWindow::initPersistFields()
  134. {
  135. // Call Parent.
  136. Parent::initPersistFields();
  137. // Add Fields.
  138. addField( "lockMouse", TypeBool, Offset(mLockMouse, SceneWindow) );
  139. addField( "UseWindowInputEvents", TypeBool, Offset(mUseWindowInputEvents, SceneWindow) );
  140. addField( "UseObjectInputEvents", TypeBool, Offset(mUseObjectInputEvents, SceneWindow) );
  141. // Background color.
  142. addField("UseBackgroundColor", TypeBool, Offset(mUseBackgroundColor, SceneWindow), &writeUseBackgroundColor, "" );
  143. addField("BackgroundColor", TypeColorF, Offset(mBackgroundColor, SceneWindow), &writeBackgroundColor, "" );
  144. }
  145. //-----------------------------------------------------------------------------
  146. void SceneWindow::setScene( Scene* pScene )
  147. {
  148. // Detach (if needed)
  149. resetScene();
  150. // Attach the Window.
  151. pScene->attachSceneWindow( this );
  152. // Set scene.
  153. mpScene = pScene;
  154. }
  155. //-----------------------------------------------------------------------------
  156. void SceneWindow::resetScene( void )
  157. {
  158. // Detach from scene (if attached).
  159. if ( getScene() )
  160. {
  161. getScene()->detachSceneWindow( this );
  162. }
  163. // Are we mounted to an object?
  164. if ( isCameraMounted() )
  165. {
  166. // Yes, so dismount object.
  167. dismount();
  168. }
  169. // Clear input event watched objects.
  170. mInputEventWatching.clear();
  171. // Reset scene.
  172. mpScene = NULL;
  173. }
  174. //-----------------------------------------------------------------------------
  175. void SceneWindow::setCameraPosition( const Vector2& position )
  176. {
  177. // Are we mounted to an object?
  178. if ( isCameraMounted() )
  179. {
  180. // Yes, so cannot use this command.
  181. Con::warnf("SceneWindow::setCameraPosition() - Cannot use this command when camera is mounted!");
  182. return;
  183. }
  184. // Stop Camera Move ( if any ).
  185. if ( mMovingCamera ) stopCameraMove();
  186. // Fetch the camera size.
  187. const Vector2 cameraSize = getCameraSize();
  188. // Set Camera Target.
  189. mCameraCurrent.mSourceArea = RectF( position.x - (cameraSize.x * 0.5f), position.y - (cameraSize.y * 0.5f), cameraSize.x, cameraSize.y );
  190. // Set Camera Target to Current.
  191. mCameraTarget = mCameraCurrent;
  192. }
  193. //-----------------------------------------------------------------------------
  194. void SceneWindow::setCameraSize( const Vector2& size )
  195. {
  196. // Stop Camera Move ( if any ).
  197. if ( mMovingCamera ) stopCameraMove();
  198. // Fetch the current position.
  199. const Vector2 position = getCameraPosition();
  200. // Set Camera Target.
  201. mCameraCurrent.mSourceArea = RectF( position.x - (size.x * 0.5f), position.y - (size.y * 0.5f), size.x, size.y );
  202. // Set Camera Target to Current.
  203. mCameraTarget = mCameraCurrent;
  204. }
  205. //-----------------------------------------------------------------------------
  206. void SceneWindow::setCameraArea( const RectF& cameraWindow )
  207. {
  208. // Are we mounted to an object?
  209. if ( isCameraMounted() )
  210. {
  211. // Yes, so cannot use this command.
  212. Con::warnf("SceneWindow::setCameraArea - Cannot use this command when camera is mounted!");
  213. return;
  214. }
  215. // Stop Camera Move ( if any ).
  216. if ( mMovingCamera ) stopCameraMove();
  217. // Set Camera Target.
  218. mCameraCurrent.mSourceArea = cameraWindow;
  219. mCameraCurrent.mCameraZoom = 1.0f;
  220. mCameraCurrent.mCameraAngle = 0.0f;
  221. // Set Camera Target to Current.
  222. mCameraTarget = mCameraCurrent;
  223. }
  224. //-----------------------------------------------------------------------------
  225. void SceneWindow::setCameraZoom( const F32 zoomFactor )
  226. {
  227. // Stop Camera Move ( if any ).
  228. if ( mMovingCamera ) stopCameraMove();
  229. // Set Camera Target.
  230. mCameraCurrent.mCameraZoom = getMax( zoomFactor, 0.000001f );
  231. // Set Camera Target to Current.
  232. mCameraTarget = mCameraCurrent;
  233. }
  234. //-----------------------------------------------------------------------------
  235. void SceneWindow::setCameraAngle( const F32 cameraAngle )
  236. {
  237. // Stop Camera Move ( if any ).
  238. if ( mMovingCamera ) stopCameraMove();
  239. // Set Camera Target.
  240. mCameraCurrent.mCameraAngle = mFmod( cameraAngle, b2_pi2 );
  241. // Set Camera Target to Current.
  242. mCameraTarget = mCameraCurrent;
  243. }
  244. //-----------------------------------------------------------------------------
  245. void SceneWindow::setTargetCameraPosition( const Vector2& position )
  246. {
  247. // Are we mounted to an object?
  248. if ( isCameraMounted() )
  249. {
  250. // Yes, so cannot use this command.
  251. Con::warnf("SceneWindow::setTargetCameraPosition - Cannot use this command when camera is mounted!");
  252. return;
  253. }
  254. // Stop Camera Move ( if any ).
  255. if ( mMovingCamera ) stopCameraMove();
  256. // Fetch the camera size.
  257. const Vector2 cameraSize = getTargetCameraSize();
  258. // Set Camera Target.
  259. mCameraTarget.mSourceArea = RectF( position.x - (cameraSize.x*0.5f), position.y - (cameraSize.y*0.5f), cameraSize.x, cameraSize.y );
  260. }
  261. //-----------------------------------------------------------------------------
  262. void SceneWindow::setTargetCameraSize( const Vector2& size )
  263. {
  264. // Stop Camera Move ( if any ).
  265. if ( mMovingCamera ) stopCameraMove();
  266. // Fetch the current position.
  267. const Vector2 position = getTargetCameraPosition();
  268. // Set Camera Target.
  269. mCameraTarget.mSourceArea = RectF( position.x - (size.x * 0.5f), position.y - (size.y * 0.5f), size.x, size.y );
  270. }
  271. //-----------------------------------------------------------------------------
  272. void SceneWindow::setTargetCameraArea( const RectF& cameraWindow )
  273. {
  274. // Are we mounted to an object?
  275. if ( isCameraMounted() )
  276. {
  277. // Yes, so cannot use this command.
  278. Con::warnf("SceneWindow::setTargetCameraArea - Cannot use this command when camera is mounted!");
  279. return;
  280. }
  281. // Stop Camera Move ( if any ).
  282. if ( mMovingCamera ) stopCameraMove();
  283. // Set Camera Target.
  284. mCameraTarget.mSourceArea = cameraWindow;
  285. }
  286. //-----------------------------------------------------------------------------
  287. void SceneWindow::setTargetCameraZoom( const F32 zoomFactor )
  288. {
  289. // Stop Camera Move ( if any ).
  290. if ( mMovingCamera ) stopCameraMove();
  291. // Set Camera Target.
  292. mCameraTarget.mCameraZoom = getMax( zoomFactor, 0.000001f );
  293. }
  294. //-----------------------------------------------------------------------------
  295. void SceneWindow::setTargetCameraAngle( const F32 cameraAngle )
  296. {
  297. // Stop Camera Move ( if any ).
  298. if ( mMovingCamera ) stopCameraMove();
  299. // Set Camera Target.
  300. mCameraTarget.mCameraAngle = mFmod( cameraAngle, b2_pi2 );
  301. }
  302. //-----------------------------------------------------------------------------
  303. void SceneWindow::setCameraInterpolationTime( const F32 interpolationTime )
  304. {
  305. // Set Interpolation Time.
  306. mCameraTransitionTime = interpolationTime;
  307. }
  308. //-----------------------------------------------------------------------------
  309. void SceneWindow::setCameraInterpolationMode( const CameraInterpolationMode interpolationMode )
  310. {
  311. // Set Interpolation Mode.
  312. mCameraInterpolationMode = interpolationMode;
  313. }
  314. //-----------------------------------------------------------------------------
  315. void SceneWindow::startCameraMove( const F32 interpolationTime )
  316. {
  317. // Are we mounted to an object and trying to move?
  318. if ( isCameraMounted() )
  319. {
  320. if ( ( mCameraCurrent.mSourceArea.point != mCameraTarget.mSourceArea.point ) ||
  321. ( mCameraCurrent.mSourceArea.extent != mCameraTarget.mSourceArea.extent ) )
  322. {
  323. // Yes, so cannot use this command.
  324. Con::warnf("SceneWindow::startCameraMove - Cannot use this command when camera is mounted!");
  325. return;
  326. }
  327. }
  328. // Stop move if we're at target already.
  329. if ( mCameraCurrent.mSourceArea.point == mCameraTarget.mSourceArea.point &&
  330. mCameraCurrent.mSourceArea.extent == mCameraTarget.mSourceArea.extent &&
  331. mIsEqual( mCameraCurrent.mCameraZoom, mCameraTarget.mCameraZoom ) &&
  332. mIsEqual( mCameraTarget.mCameraAngle, mCameraTarget.mCameraAngle ) )
  333. {
  334. // Reset Camera Move.
  335. mMovingCamera = false;
  336. // Return here.
  337. return;
  338. }
  339. else
  340. {
  341. // Stop Camera Move ( if any ).
  342. if ( mMovingCamera ) stopCameraMove();
  343. }
  344. // Set Camera Interpolation Time.
  345. setCameraInterpolationTime( interpolationTime );
  346. // Zero Camera Time.
  347. zeroCameraTime();
  348. // Set Source Camera.
  349. mCameraSource = mCameraCurrent;
  350. // Set Camera Move.
  351. mMovingCamera = true;
  352. // Complete camera move if interpolate time is zero.
  353. if ( mIsZero(mCameraTransitionTime) ) completeCameraMove();
  354. // Queue Current Camera.
  355. mCameraQueue.push_back( mCameraCurrent );
  356. // Clamp Queue Size.
  357. if ( mCameraQueue.size() > mMaxQueueItems ) mCameraQueue.pop_front();
  358. }
  359. //-----------------------------------------------------------------------------
  360. void SceneWindow::stopCameraMove( void )
  361. {
  362. // Quit if we're not moving.
  363. if ( !mMovingCamera ) return;
  364. // Reset Tick Camera Time.
  365. resetTickCameraTime();
  366. // Set target to Current.
  367. mCameraTarget = mCameraCurrent;
  368. // Reset Camera Move.
  369. mMovingCamera = false;
  370. }
  371. //-----------------------------------------------------------------------------
  372. void SceneWindow::completeCameraMove( void )
  373. {
  374. // Quit if we're not moving.
  375. if ( !mMovingCamera ) return;
  376. // Reset Tick Camera Time.
  377. resetTickCameraTime();
  378. // Move straight to target.
  379. mCameraCurrent = mCameraTarget;
  380. // Reset Camera Move.
  381. mMovingCamera = false;
  382. }
  383. //-----------------------------------------------------------------------------
  384. void SceneWindow::undoCameraMove( const F32 interpolationTime )
  385. {
  386. // Are we mounted to an object?
  387. if ( isCameraMounted() )
  388. {
  389. // Yes, so cannot use this command.
  390. Con::warnf("SceneWindow::undoCameraMove - Cannot use this command when camera is mounted!");
  391. return;
  392. }
  393. // Quit if we've got no queued targets.
  394. if ( mCameraQueue.size() == 0 ) return;
  395. // Stop Camera Move ( if any ).
  396. if ( mMovingCamera ) stopCameraMove();
  397. // Set Camera Interpolation Time.
  398. setCameraInterpolationTime( interpolationTime );
  399. // Zero Camera Time.
  400. zeroCameraTime();
  401. // Set Source Camera.
  402. mCameraSource = mCameraCurrent;
  403. // Set Camera Move.
  404. mMovingCamera = true;
  405. // Fetch Last Queued Camera Target.
  406. mCameraTarget = mCameraQueue.last();
  407. // Remove Last Target.
  408. mCameraQueue.pop_back();
  409. // Complete camera move if interpolate time is zero.
  410. if ( mIsZero(mCameraTransitionTime) ) completeCameraMove();
  411. }
  412. //-----------------------------------------------------------------------------
  413. void SceneWindow::updateCamera( void )
  414. {
  415. // Calculate Normalised Time.
  416. const F32 normCameraTime = mRenderCameraTime / mCameraTransitionTime;
  417. // Have we finished the interpolation?
  418. if ( mGreaterThanOrEqual(normCameraTime, 1.0f) )
  419. {
  420. // Yes, so complete camera move.
  421. completeCameraMove();
  422. // Finish here.
  423. return;
  424. }
  425. // Interpolate Camera Window/Zoom.
  426. mCameraCurrent.mSourceArea.point.x = interpolate( mCameraSource.mSourceArea.point.x, mCameraTarget.mSourceArea.point.x, normCameraTime );
  427. mCameraCurrent.mSourceArea.point.y = interpolate( mCameraSource.mSourceArea.point.y, mCameraTarget.mSourceArea.point.y, normCameraTime );
  428. mCameraCurrent.mSourceArea.extent.x = interpolate( mCameraSource.mSourceArea.extent.x, mCameraTarget.mSourceArea.extent.x, normCameraTime );
  429. mCameraCurrent.mSourceArea.extent.y = interpolate( mCameraSource.mSourceArea.extent.y, mCameraTarget.mSourceArea.extent.y, normCameraTime );
  430. mCameraCurrent.mCameraZoom = interpolate( mCameraSource.mCameraZoom, mCameraTarget.mCameraZoom, normCameraTime );
  431. mCameraCurrent.mCameraAngle = interpolate( mCameraSource.mCameraAngle, mCameraTarget.mCameraAngle, normCameraTime );
  432. }
  433. //-----------------------------------------------------------------------------
  434. F32 SceneWindow::interpolate( F32 from, F32 to, F32 delta )
  435. {
  436. // Linear.
  437. if ( mCameraInterpolationMode == LINEAR )
  438. return linearInterpolate( from, to, delta );
  439. // Sigmoid.
  440. else if ( mCameraInterpolationMode == SIGMOID )
  441. return sigmoidInterpolate( from, to, delta );
  442. // Hmmm...
  443. else
  444. return from;
  445. }
  446. //-----------------------------------------------------------------------------
  447. F32 SceneWindow::linearInterpolate( F32 from, F32 to, F32 delta )
  448. {
  449. // Clamp if we're over/under time.
  450. if ( delta <= 0.0f )
  451. return from;
  452. else if ( delta >= 1.0f )
  453. return to;
  454. // Calculate resultant interpolation.
  455. return ( from * ( 1.0f - delta ) ) + ( to * delta );
  456. }
  457. //-----------------------------------------------------------------------------
  458. F32 SceneWindow::sigmoidInterpolate( F32 from, F32 to, F32 delta )
  459. {
  460. // Range Expand/Clamp Delta to (-1 -> +1).
  461. delta = mClampF( (delta - 0.5f) * 2.0f, -1.0f, 1.0f );
  462. // Calculate interpolator value using sigmoid function.
  463. F32 sigmoid = mClampF ( 1.0f / (1.0f + mPow(2.718282f, -15.0f * delta)), 0.0f, 1.0f );
  464. // Calculate resultant interpolation.
  465. return ( from * ( 1.0f - sigmoid ) ) + ( to * sigmoid );
  466. }
  467. //-----------------------------------------------------------------------------
  468. void SceneWindow::startCameraShake( const F32 magnitude, const F32 time )
  469. {
  470. // Is the time zero?
  471. if ( mIsZero( time ) && mIsZero( magnitude ) )
  472. {
  473. // Yes, so simply stop the camera shaking.
  474. stopCameraShake();
  475. // Finish here.
  476. return;
  477. }
  478. // Set Current Shake.
  479. mCurrentShake = mFabs(magnitude);
  480. // Set Shake Life.
  481. mShakeLife = time;
  482. // Calculate Shake Ramp.
  483. mShakeRamp = mCurrentShake / mShakeLife;
  484. // Flag camera shaking.
  485. mCameraShaking = true;
  486. }
  487. //-----------------------------------------------------------------------------
  488. void SceneWindow::stopCameraShake( void )
  489. {
  490. // Flag camera not shaking.
  491. mCameraShaking = false;
  492. // Reset Shake Offset.
  493. mCameraShakeOffset.setZero();
  494. }
  495. //-----------------------------------------------------------------------------
  496. void SceneWindow::mount( SceneObject* pSceneObject, const Vector2& mountOffset, const F32 mountForce, const bool sendToMount, const bool mountAngle )
  497. {
  498. // Sanity!
  499. AssertFatal( pSceneObject != NULL, "Scene object cannot be NULL." );
  500. // Cannot mount if not in a scene.
  501. if ( !mpScene )
  502. {
  503. // Warn!
  504. Con::warnf("Cannot mount scene window (%d) to a scene object (%d) if scene window is not attached to a scene.", getId(), pSceneObject->getId() );
  505. return;
  506. }
  507. // Fetch objects' scene.
  508. const Scene* pScene = pSceneObject->getScene();
  509. // Scene object must be in a scene.
  510. if ( !pScene )
  511. {
  512. // Warn!
  513. Con::warnf("Cannot mount scene window (%d) to a scene object (%d) that is not in a scene.", getId(), pSceneObject->getId() );
  514. return;
  515. }
  516. // Scene object must be in same scene as the one the scene window is attached to.
  517. if ( pScene != mpScene )
  518. {
  519. // Warn!
  520. Con::warnf("Cannot mount scene window (%d) to a scene object (%d) that are not using the same scene.", getId(), pSceneObject->getId() );
  521. return;
  522. }
  523. // Are we mounted to an object?
  524. if ( isCameraMounted() )
  525. {
  526. // Yes, so dismount object.
  527. dismount();
  528. }
  529. else
  530. {
  531. // No, so stop any Camera Move.
  532. if ( mMovingCamera ) stopCameraMove();
  533. }
  534. // Set Mount Object Reference.
  535. mpMountedTo = pSceneObject;
  536. // Store Mount Offset.
  537. mMountOffset = mountOffset;
  538. // Set Mount Force.
  539. mMountForce = mountForce;
  540. // Set Mount Angle.
  541. mMountAngle = mountAngle;
  542. // Add Camera Mount Reference.
  543. pSceneObject->addCameraMountReference( this );
  544. // Flag Camera mounted.
  545. mCameraMounted = true;
  546. // Send directly to mount (if selected).
  547. if ( sendToMount )
  548. {
  549. // Fetch Mount Position.
  550. const Vector2& mountPos = mpMountedTo->getBody()->GetWorldPoint( mountOffset );
  551. // Calculate Window Half-Dimensions.
  552. const F32 halfWidth = mCameraCurrent.mSourceArea.len_x() * 0.5f;
  553. const F32 halfHeight = mCameraCurrent.mSourceArea.len_y() * 0.5f;
  554. // Set Current View to Object Position.
  555. mCameraCurrent.mSourceArea.point.set( mountPos.x - halfWidth, mountPos.y - halfHeight );
  556. }
  557. // Reset Tick Camera Position.
  558. resetTickCameraPosition();
  559. }
  560. //-----------------------------------------------------------------------------
  561. void SceneWindow::dismount( void )
  562. {
  563. // Nothing to do if we're not mounted!
  564. if (!isCameraMounted() )
  565. return;
  566. // Remove Camera Mount Reference.
  567. mpMountedTo->removeCameraMountReference();
  568. // Reset Camera Object Mount.
  569. mpMountedTo = NULL;
  570. // Flag Camera not mounted.
  571. mCameraMounted = false;
  572. // Reset Tick Camera Position.
  573. resetTickCameraPosition();
  574. }
  575. //-----------------------------------------------------------------------------
  576. void SceneWindow::setViewLimitOn( const Vector2& limitMin, const Vector2& limitMax )
  577. {
  578. // Activate View Limit.
  579. mViewLimitActive = true;
  580. // Set View Limit Min/Max.
  581. mViewLimitMin = limitMin;
  582. mViewLimitMax = limitMax;
  583. // Calculate Camera Area.
  584. mViewLimitArea = mViewLimitMax - mViewLimitMin;
  585. }
  586. //-----------------------------------------------------------------------------
  587. void SceneWindow::clampCameraViewLimit( void )
  588. {
  589. // Finish if the view-limit is not on or the camera is moving.
  590. if ( !mViewLimitActive || mMovingCamera )
  591. return;
  592. // Calculate Current Camera View.
  593. calculateCameraView( &mCameraCurrent );
  594. // Calculate the source and destination centres.
  595. const Point2F sourceCentre = mCameraCurrent.mSourceArea.centre();
  596. const Point2F destinationCentre = mCameraCurrent.mDestinationArea.centre();
  597. // Are the source and destination areas the same?
  598. if ( sourceCentre != destinationCentre )
  599. {
  600. // No, so adjust the source position to be at the same position as the destination i.e. don't change the source area.
  601. mCameraCurrent.mSourceArea.point += destinationCentre - sourceCentre;
  602. }
  603. }
  604. //-----------------------------------------------------------------------------
  605. void SceneWindow::setObjectInputEventFilter( const U32 groupMask, const U32 layerMask, const bool useInvisible )
  606. {
  607. // Set Object Mouse Event Filter.
  608. mInputEventGroupMaskFilter = groupMask;
  609. mInputEventLayerMaskFilter = layerMask;
  610. mInputEventInvisibleFilter = useInvisible;
  611. }
  612. //-----------------------------------------------------------------------------
  613. void SceneWindow::setObjectInputEventGroupFilter( const U32 groupMask )
  614. {
  615. mInputEventGroupMaskFilter = groupMask;
  616. // Clear existing watched input events.
  617. clearWatchedInputEvents();
  618. }
  619. //-----------------------------------------------------------------------------
  620. void SceneWindow::setObjectInputEventLayerFilter( const U32 layerMask )
  621. {
  622. mInputEventLayerMaskFilter = layerMask;
  623. // Clear existing watched input events.
  624. clearWatchedInputEvents();
  625. }
  626. //-----------------------------------------------------------------------------
  627. void SceneWindow::setObjectInputEventInvisibleFilter( const bool useInvisible )
  628. {
  629. mInputEventInvisibleFilter = useInvisible;
  630. // Clear existing watched input events.
  631. clearWatchedInputEvents();
  632. }
  633. //-----------------------------------------------------------------------------
  634. void SceneWindow::setMousePosition( const Vector2& mousePosition )
  635. {
  636. // Fetch Canvas.
  637. GuiCanvas* pCanvas = getRoot();
  638. // Canvas available?
  639. if ( pCanvas )
  640. {
  641. // Are we bound to a scene?
  642. if ( getScene() )
  643. {
  644. Vector2 windowMousePosition;
  645. // Yes, so convert window into scene coordinates...
  646. sceneToWindowPoint( mousePosition, windowMousePosition );
  647. // Copy into a compatible format for TGE.
  648. Point2I localWindowPosition( S32(windowMousePosition.x), S32(windowMousePosition.y) );
  649. // Set Cursor Position.
  650. pCanvas->setCursorPos( localToGlobalCoord(localWindowPosition) );
  651. }
  652. else
  653. {
  654. // No, so error.
  655. Con::warnf("SceneObject::setMousePosition() - No scene attached to window!");
  656. return;
  657. }
  658. }
  659. }
  660. //-----------------------------------------------------------------------------
  661. Vector2 SceneWindow::getMousePosition( void )
  662. {
  663. // Calculate Current Camera View.
  664. calculateCameraView( &mCameraCurrent );
  665. // Fetch Canvas.
  666. GuiCanvas* pCanvas = getRoot();
  667. // World Mouse Position.
  668. Vector2 worldMousePoint(0, 0);
  669. // Canvas available?
  670. if ( pCanvas )
  671. {
  672. // Yes, so fetch local GUI coordinates.
  673. const Vector2 localGuiPoint = globalToLocalCoord( pCanvas->getCursorPos() );
  674. // Are we bound to a scene?
  675. if ( getScene() )
  676. {
  677. // Yes, so convert window into scene coordinates...
  678. windowToScenePoint(localGuiPoint, worldMousePoint);
  679. }
  680. else
  681. {
  682. // No, so use window screen coordinates.
  683. worldMousePoint = localGuiPoint;
  684. }
  685. }
  686. #if 0
  687. else
  688. {
  689. // No, so warn
  690. Con::warnf("SceneWindow::getMousePosition() - Window not attached to canvas!" );
  691. }
  692. #endif
  693. // Return World Mouse Position.
  694. return worldMousePoint;
  695. }
  696. //-----------------------------------------------------------------------------
  697. void SceneWindow::windowToScenePoint( const Vector2& srcPoint, Vector2& dstPoint ) const
  698. {
  699. // Return Conversion.
  700. dstPoint.Set( (srcPoint.x * mCameraCurrent.mSceneWindowScale.x) + mCameraCurrent.mSceneMin.x, mCameraCurrent.mSceneMax.y - (srcPoint.y * mCameraCurrent.mSceneWindowScale.y) );
  701. }
  702. //-----------------------------------------------------------------------------
  703. void SceneWindow::sceneToWindowPoint( const Vector2& srcPoint, Vector2& dstPoint ) const
  704. {
  705. // Return Conversion.
  706. dstPoint.Set( (srcPoint.x - mCameraCurrent.mSceneMin.x) / mCameraCurrent.mSceneWindowScale.x, (mCameraCurrent.mSceneMax.y - srcPoint.y) / mCameraCurrent.mSceneWindowScale.y );
  707. }
  708. //-----------------------------------------------------------------------------
  709. void SceneWindow::dispatchInputEvent( StringTableEntry name, const GuiEvent& event )
  710. {
  711. // Debug Profiling.
  712. PROFILE_SCOPE(SceneWindow_DispatchInputEvent);
  713. // Dispatch input event to window if appropriate.
  714. if ( getUseWindowInputEvents() )
  715. sendWindowInputEvent( name, event );
  716. // Dispatch input event to scene objects if appropriate.
  717. if ( getUseObjectInputEvents() )
  718. sendObjectInputEvent( name, event );
  719. }
  720. //-----------------------------------------------------------------------------
  721. void SceneWindow::sendWindowInputEvent( StringTableEntry name, const GuiEvent& event )
  722. {
  723. // Debug Profiling.
  724. PROFILE_SCOPE(SceneWindow_SendWindowInputEvent);
  725. Vector2 worldMousePoint;
  726. // Calculate Current Camera View.
  727. calculateCameraView( &mCameraCurrent );
  728. // Convert to local gui coordinates.
  729. const Vector2 localGuiPoint = globalToLocalCoord(event.mousePoint);
  730. // Are we bound to a scene?
  731. if ( getScene() )
  732. {
  733. // Yes, so convert window into scene coordinates...
  734. windowToScenePoint(localGuiPoint, worldMousePoint);
  735. }
  736. else
  737. {
  738. // No, so use window screen coordinates.
  739. worldMousePoint = localGuiPoint;
  740. }
  741. // Argument Buffers.
  742. char argBuffer[3][64];
  743. // Format Event-Modifier Buffer.
  744. dSprintf(argBuffer[0], 64, "%d", event.eventID);
  745. // Format Mouse-Position Buffer.
  746. dSprintf(argBuffer[1], 64, "%g %g", worldMousePoint.x, worldMousePoint.y);
  747. // Format Mouse-Click Count Buffer.
  748. dSprintf(argBuffer[2], 64, "%d", event.mouseClickCount);
  749. // Call Scripts.
  750. Con::executef(this, 4, name, argBuffer[0], argBuffer[1], argBuffer[2]);
  751. }
  752. //-----------------------------------------------------------------------------
  753. void SceneWindow::sendObjectInputEvent( StringTableEntry name, const GuiEvent& event )
  754. {
  755. // Debug Profiling.
  756. PROFILE_SCOPE(SceneWindow_SendObjectInputEvent);
  757. // Finish if we're not bound to a scene?
  758. if ( !getScene() ) return;
  759. // Only process appropriate input events.
  760. if ( !( name == mInputEventDownName ||
  761. name == mInputEventUpName ||
  762. name == mInputEventMovedName ||
  763. name == mInputEventDraggedName ) )
  764. return;
  765. // Convert Event-Position into scene coordinates.
  766. Vector2 worldMousePoint;
  767. windowToScenePoint(Vector2(globalToLocalCoord(event.mousePoint)), worldMousePoint);
  768. // Fetch old pick count.
  769. const U32 oldPickCount = (U32)mInputEventWatching.size();
  770. // Fetch world query and clear results.
  771. WorldQuery* pWorldQuery = getScene()->getWorldQuery( true );
  772. // Set filter.
  773. WorldQueryFilter queryFilter( mInputEventLayerMaskFilter, mInputEventGroupMaskFilter, true, mInputEventInvisibleFilter, true, true );
  774. pWorldQuery->setQueryFilter( queryFilter );
  775. // Perform world query.
  776. const U32 newPickCount = pWorldQuery->anyQueryPoint( worldMousePoint );
  777. // Early-out if nothing to do.
  778. if ( newPickCount == 0 && oldPickCount == 0 )
  779. return;
  780. // Fetch results.
  781. mInputEventQuery = pWorldQuery->getQueryResults();
  782. pWorldQuery->clearQuery();
  783. // Determine "enter" events.
  784. for( U32 newIndex = 0; newIndex < newPickCount; ++newIndex )
  785. {
  786. // Fetch new scene object.
  787. SceneObject* pNewSceneObject = mInputEventQuery[newIndex].mpSceneObject;
  788. // Ignore object if it's not using input events.
  789. // NOTE:- We only check this for "enter" events in-case the option is
  790. // changed whilst it's currently picked. We want to guarantee
  791. // that any "enter" event is paired with a "leave" event.
  792. if ( !pNewSceneObject->getUseInputEvents() )
  793. continue;
  794. bool alreadyPresent = false;
  795. for ( U32 oldIndex = 0; oldIndex < oldPickCount; ++oldIndex )
  796. {
  797. // Skip if scene object is not present...
  798. if ( mInputEventWatching[oldIndex] != pNewSceneObject )
  799. continue;
  800. // Flag as already present.
  801. alreadyPresent = true;
  802. break;
  803. }
  804. // Add scene object as entering if not already present.
  805. if ( !alreadyPresent )
  806. mInputEventEntering.push_back( pNewSceneObject );
  807. }
  808. // Determine "leave" events.
  809. for ( U32 oldIndex = 0; oldIndex < oldPickCount; ++oldIndex )
  810. {
  811. // Fetch old scene object.
  812. SceneObject* pOldSceneObject = dynamic_cast<SceneObject*>( mInputEventWatching[oldIndex] );
  813. // Sanity!
  814. AssertFatal( pOldSceneObject != NULL, "Invalid object found in mouse-event pick vector." );
  815. bool stillPresent = false;
  816. for( U32 newIndex = 0; newIndex < newPickCount; ++newIndex )
  817. {
  818. // Skip if scene object is not present.
  819. if ( mInputEventQuery[newIndex].mpSceneObject != pOldSceneObject )
  820. continue;
  821. // Flag as still present.
  822. stillPresent = true;
  823. break;
  824. }
  825. // Add scene object as leaving if not still present.
  826. if ( !stillPresent )
  827. mInputEventLeaving.push_back( pOldSceneObject );
  828. }
  829. for ( U32 index = 0; index < (U32)mInputEventQuery.size(); ++index )
  830. {
  831. // Fetch scene object.
  832. SceneObject* pSceneObject = mInputEventQuery[index].mpSceneObject;
  833. // Ignore object if it's not using input events.
  834. if ( !pSceneObject->getUseInputEvents() )
  835. continue;
  836. // Emit event.
  837. pSceneObject->onInputEvent( name, event, worldMousePoint );
  838. }
  839. // Process "leave" events.
  840. for ( U32 index = 0; index < (U32)mInputEventLeaving.size(); ++index )
  841. {
  842. // Fetch scene object.
  843. SceneObject* pSceneObject = mInputEventLeaving[index];
  844. // Emit event.
  845. pSceneObject->onInputEvent( mInputEventLeaveName, event, worldMousePoint );
  846. // Remove scene object.
  847. mInputEventWatching.removeObject( pSceneObject );
  848. }
  849. // Process "enter" events.
  850. for ( U32 index = 0; index < (U32)mInputEventEntering.size(); ++index )
  851. {
  852. // Fetch scene object.
  853. SceneObject* pSceneObject = mInputEventEntering[index];
  854. // Emit event.
  855. pSceneObject->onInputEvent( mInputEventEnterName, event, worldMousePoint );
  856. // Process "moved" or "dragged" events.
  857. if ( name == mInputEventMovedName || name == mInputEventDraggedName )
  858. pSceneObject->onInputEvent( name, event, worldMousePoint );
  859. // Add scene object.
  860. mInputEventWatching.addObject( pSceneObject );
  861. }
  862. // Clear input event vectors.
  863. mInputEventQuery.clear();
  864. mInputEventEntering.clear();
  865. mInputEventLeaving.clear();
  866. }
  867. //-----------------------------------------------------------------------------
  868. void SceneWindow::onMouseDown( const GuiEvent& event )
  869. {
  870. // Lock Mouse (if necessary).
  871. if(mLockMouse)
  872. mouseLock();
  873. // Dispatch input event.
  874. dispatchInputEvent( mInputEventDownName, event);
  875. }
  876. //-----------------------------------------------------------------------------
  877. void SceneWindow::onMouseUp( const GuiEvent& event )
  878. {
  879. // Lock Mouse (if necessary).
  880. if(mLockMouse)
  881. mouseUnlock();
  882. // Dispatch input event.
  883. dispatchInputEvent(mInputEventUpName, event);
  884. }
  885. //-----------------------------------------------------------------------------
  886. void SceneWindow::onMouseMove( const GuiEvent& event )
  887. {
  888. // Dispatch input event.
  889. dispatchInputEvent(mInputEventMovedName, event);
  890. }
  891. //-----------------------------------------------------------------------------
  892. void SceneWindow::onMouseDragged( const GuiEvent& event )
  893. {
  894. // Dispatch input event.
  895. dispatchInputEvent(mInputEventDraggedName, event);
  896. }
  897. //-----------------------------------------------------------------------------
  898. void SceneWindow::onMouseEnter( const GuiEvent& event )
  899. {
  900. // Dispatch input event.
  901. dispatchInputEvent(mMouseEventEnterName, event);
  902. }
  903. //-----------------------------------------------------------------------------
  904. void SceneWindow::onMouseLeave( const GuiEvent& event )
  905. {
  906. // Dispatch input event.
  907. dispatchInputEvent(mMouseEventLeaveName, event);
  908. }
  909. //-----------------------------------------------------------------------------
  910. void SceneWindow::onRightMouseDown( const GuiEvent& event )
  911. {
  912. // Lock Mouse (if necessary).
  913. if(mLockMouse)
  914. mouseLock();
  915. // Dispatch input event.
  916. dispatchInputEvent(mMouseEventRightMouseDownName, event);
  917. }
  918. //-----------------------------------------------------------------------------
  919. void SceneWindow::onRightMouseUp( const GuiEvent& event )
  920. {
  921. // Lock Mouse (if necessary).
  922. if(mLockMouse)
  923. mouseUnlock();
  924. // Dispatch input event.
  925. dispatchInputEvent(mMouseEventRightMouseUpName, event);
  926. }
  927. //-----------------------------------------------------------------------------
  928. void SceneWindow::onRightMouseDragged( const GuiEvent& event )
  929. {
  930. // Dispatch input event.
  931. dispatchInputEvent(mMouseEventRightMouseDraggedName, event);
  932. }
  933. //-----------------------------------------------------------------------------
  934. bool SceneWindow::onMouseWheelUp( const GuiEvent& event )
  935. {
  936. // Call Parent.
  937. Parent::onMouseWheelUp( event );
  938. // Dispatch input event.
  939. dispatchInputEvent(mMouseEventWheelUpName, event);
  940. // Return Success.
  941. return true;
  942. }
  943. //-----------------------------------------------------------------------------
  944. bool SceneWindow::onMouseWheelDown( const GuiEvent& event )
  945. {
  946. // Call Parent.
  947. Parent::onMouseWheelDown( event );
  948. // Dispatch input event.
  949. dispatchInputEvent(mMouseEventWheelDownName, event);
  950. // Return Success.
  951. return true;
  952. }
  953. //-----------------------------------------------------------------------------
  954. void SceneWindow::calculateCameraMount( const F32 elapsedTime )
  955. {
  956. // Debug Profiling.
  957. PROFILE_SCOPE(SceneWindow_CalculateCameraMount);
  958. // Fetch Mount Position.
  959. const Vector2& mountPos = mpMountedTo->getBody()->GetWorldPoint( mMountOffset );
  960. // Set Pre-Tick Position.
  961. mPreTickPosition = mPostTickPosition;
  962. // Set Current Camera Position.
  963. mCameraCurrent.mSourceArea.point = mPreTickPosition;
  964. // Calculate Window Half-Dimensions.
  965. const F32 halfWidth = mCameraCurrent.mSourceArea.len_x() * 0.5f;
  966. const F32 halfHeight = mCameraCurrent.mSourceArea.len_y() * 0.5f;
  967. // Calculate Target Position.
  968. const Point2F targetPos = Point2F( mountPos.x - halfWidth, mountPos.y - halfHeight );
  969. // Mount the angle?
  970. if ( mMountAngle )
  971. mCameraCurrent.mCameraAngle = -mpMountedTo->getAngle();
  972. // Rigid Mount?
  973. if ( mIsZero( mMountForce ) )
  974. {
  975. // Yes, so easy post-tick position.
  976. mPostTickPosition = targetPos;
  977. return;
  978. }
  979. // Calculate Time/Force Product.
  980. const F32 timeForce = elapsedTime * mMountForce;
  981. // Will we exceed our step?
  982. if ( timeForce > 1.0f )
  983. {
  984. // Yes, so clamp at step.
  985. mPostTickPosition = targetPos;
  986. return;
  987. }
  988. else
  989. {
  990. // No, so calculate Direction to move.
  991. const Point2F direction = (targetPos - mPreTickPosition) * timeForce;
  992. // Calculate post-tick position.
  993. mPostTickPosition = mPreTickPosition + direction;
  994. return;
  995. }
  996. }
  997. //-----------------------------------------------------------------------------
  998. void SceneWindow::calculateCameraView( CameraView* pCameraView )
  999. {
  1000. // Debug Profiling.
  1001. PROFILE_SCOPE(SceneWindow_CalculateCameraView);
  1002. // Calculate Zoom Reciprocal.
  1003. const F32 zoomRecip = pCameraView->mCameraZoom > 0.0f ? 1.0f / pCameraView->mCameraZoom : pCameraView->mCameraZoom;
  1004. // Calculate Zoom X/Y Factors.
  1005. const F32 zoomFactorX = (pCameraView->mSourceArea.len_x() - (pCameraView->mSourceArea.len_x() * zoomRecip))/2;
  1006. const F32 zoomFactorY = (pCameraView->mSourceArea.len_y() - (pCameraView->mSourceArea.len_y() * zoomRecip))/2;
  1007. // Fetch Camera View.
  1008. pCameraView->mDestinationArea = pCameraView->mSourceArea;
  1009. // Inset Window by Zoom Factor (if it's big enough to do so).
  1010. if ( pCameraView->mDestinationArea.extent.x > (zoomFactorX*2.0f) &&
  1011. pCameraView->mDestinationArea.extent.y > (zoomFactorY*2.0f) )
  1012. {
  1013. pCameraView->mDestinationArea.inset( zoomFactorX, zoomFactorY );
  1014. }
  1015. // Ensure we've got a valid window.
  1016. if ( !pCameraView->mDestinationArea.isValidRect() )
  1017. // Make it real!
  1018. pCameraView->mDestinationArea.extent = Point2F(1,1);
  1019. // Calculate Scene Min/Max.
  1020. pCameraView->mSceneMin.x = pCameraView->mDestinationArea.point.x;
  1021. pCameraView->mSceneMin.y = pCameraView->mDestinationArea.point.y;
  1022. pCameraView->mSceneMax.x = pCameraView->mDestinationArea.point.x + pCameraView->mDestinationArea.len_x();
  1023. pCameraView->mSceneMax.y = pCameraView->mDestinationArea.point.y + pCameraView->mDestinationArea.len_y();
  1024. // Is View Limit Active?
  1025. if ( mViewLimitActive )
  1026. {
  1027. // Yes, so is the limit area X less than the current view X?
  1028. if ( mViewLimitArea.x < pCameraView->mDestinationArea.len_x() )
  1029. {
  1030. // Yes, so calculate center of view.
  1031. const F32 viewCenterX = mViewLimitMin.x + ( mViewLimitArea.x * 0.5f );
  1032. // Half Camera Width.
  1033. const F32 halfCameraX = pCameraView->mDestinationArea.len_x() * 0.5f;
  1034. // Calculate Min/Max X.
  1035. pCameraView->mSceneMin.x = viewCenterX - halfCameraX;
  1036. pCameraView->mSceneMax.x = viewCenterX + halfCameraX;
  1037. }
  1038. else
  1039. {
  1040. // No, so calculate window min overlap.
  1041. const F32 windowMinOverlapX = getMax(0.0f, mViewLimitMin.x - pCameraView->mSceneMin.x);
  1042. // Calculate window max overlap.
  1043. const F32 windowMaxOverlapX = getMin(0.0f, mViewLimitMax.x - pCameraView->mSceneMax.x);
  1044. // Adjust Window.
  1045. pCameraView->mSceneMin.x += windowMinOverlapX + windowMaxOverlapX;
  1046. pCameraView->mSceneMax.x += windowMinOverlapX + windowMaxOverlapX;
  1047. }
  1048. // Is the limit area Y less than the current view Y?
  1049. if ( mViewLimitArea.y < pCameraView->mDestinationArea.len_y() )
  1050. {
  1051. // Yes, so calculate center of view.
  1052. const F32 viewCenterY = mViewLimitMin.y + ( mViewLimitArea.y * 0.5f );
  1053. // Half Camera Height.
  1054. const F32 halfCameraY = pCameraView->mDestinationArea.len_y() * 0.5f;
  1055. // Calculate Min/Max Y.
  1056. pCameraView->mSceneMin.y = viewCenterY - halfCameraY;
  1057. pCameraView->mSceneMax.y = viewCenterY + halfCameraY;
  1058. }
  1059. else
  1060. {
  1061. // No, so calculate window min overlap.
  1062. const F32 windowMinOverlapY = getMax(0.0f, mViewLimitMin.y - pCameraView->mSceneMin.y);
  1063. // Calculate window max overlap.
  1064. const F32 windowMaxOverlapY = getMin(0.0f, mViewLimitMax.y - pCameraView->mSceneMax.y);
  1065. // Adjust Window.
  1066. pCameraView->mSceneMin.y += windowMinOverlapY + windowMaxOverlapY;
  1067. pCameraView->mSceneMax.y += windowMinOverlapY + windowMaxOverlapY;
  1068. }
  1069. // Recalculate destination area.
  1070. pCameraView->mDestinationArea.point = pCameraView->mSceneMin;
  1071. pCameraView->mDestinationArea.extent = pCameraView->mSceneMax - pCameraView->mSceneMin;
  1072. // Inset Window by Zoom Factor (if it's big enough to do so).
  1073. if ( pCameraView->mDestinationArea.extent.x > (zoomFactorX*2.0f) &&
  1074. pCameraView->mDestinationArea.extent.y > (zoomFactorY*2.0f) )
  1075. {
  1076. pCameraView->mDestinationArea.inset( zoomFactorX, zoomFactorY );
  1077. }
  1078. }
  1079. // Calculate Scene Window Scale.
  1080. pCameraView->mSceneWindowScale.x = (pCameraView->mSceneMax.x - pCameraView->mSceneMin.x) / mBounds.len_x();
  1081. pCameraView->mSceneWindowScale.y = (pCameraView->mSceneMax.y - pCameraView->mSceneMin.y) / mBounds.len_y();
  1082. }
  1083. //-----------------------------------------------------------------------------
  1084. void SceneWindow::resize(const Point2I &newPosition, const Point2I &newExtent)
  1085. {
  1086. // Resize Parent.
  1087. Parent::resize( newPosition, newExtent);
  1088. // Argument Buffer.
  1089. char argBuffer[64];
  1090. // Format Buffer.
  1091. dSprintf( argBuffer, 64, "%d %d %d %d", newPosition.x, newPosition.y, newExtent.x, newExtent.y );
  1092. // Resize Callback.
  1093. Con::executef( this, 2, "onExtentChange", argBuffer );
  1094. }
  1095. //-----------------------------------------------------------------------------
  1096. void SceneWindow::processTick( void )
  1097. {
  1098. // Debug Profiling.
  1099. PROFILE_SCOPE(SceneWindow_ProcessTick);
  1100. // Are we moving the camera.
  1101. if ( mMovingCamera )
  1102. {
  1103. // Yes, so add Elapsed Time (scaled appropriately).
  1104. mCurrentCameraTime += Tickable::smTickSec;
  1105. // Update Tick Camera Time.
  1106. updateTickCameraTime();
  1107. // Update Camera.
  1108. updateCamera();
  1109. }
  1110. // Is the Camera Shaking?
  1111. if ( mCameraShaking )
  1112. {
  1113. // Reduce Shake Life.
  1114. mShakeLife -= Tickable::smTickSec;
  1115. // Is the Shake still active?
  1116. if ( mShakeLife > 0.0f )
  1117. {
  1118. // Calculate Current Shake.
  1119. mCurrentShake -= mShakeRamp * Tickable::smTickSec;
  1120. // Clamp Shake.
  1121. mCurrentShake = getMax(mCurrentShake, 0.0f);
  1122. // Calculate the Screen Shake-Ratio.
  1123. const Point2F shakeRatio( mCameraCurrent.mDestinationArea.len_x() / F32(mBounds.len_x()), mCameraCurrent.mDestinationArea.len_y() / F32(mBounds.len_y()) );
  1124. // Calculate the Camera Shake Magnitude based upon the Screen Shake-Ratio.
  1125. const F32 shakeMagnitudeX = mCurrentShake * shakeRatio.x * 0.5f;
  1126. const F32 shakeMagnitudeY = mCurrentShake * shakeRatio.y * 0.5f;
  1127. // Choose a random Shake.
  1128. mCameraShakeOffset.Set( CoreMath::mGetRandomF( -shakeMagnitudeX, shakeMagnitudeX ), CoreMath::mGetRandomF( -shakeMagnitudeY, shakeMagnitudeY ) );
  1129. }
  1130. else
  1131. {
  1132. // No, so stop shake.
  1133. stopCameraShake();
  1134. }
  1135. }
  1136. }
  1137. //-----------------------------------------------------------------------------
  1138. void SceneWindow::interpolateTick( F32 timeDelta )
  1139. {
  1140. // Are we moving the camera.
  1141. if ( mMovingCamera )
  1142. {
  1143. // Calculate Render Tick Position.
  1144. mRenderCameraTime = (mPreCameraTime * timeDelta) + ((1.0f-timeDelta) * mPostCameraTime);
  1145. // Update Camera.
  1146. updateCamera();
  1147. }
  1148. }
  1149. //-----------------------------------------------------------------------------
  1150. void SceneWindow::interpolateCameraMount( const F32 timeDelta )
  1151. {
  1152. // Camera Mounted?
  1153. if ( !isCameraMounted() ) return;
  1154. // Calculate position.
  1155. mCameraCurrent.mSourceArea.point = (mPreTickPosition * timeDelta) + ((1.0f-timeDelta) * mPostTickPosition);
  1156. }
  1157. //-----------------------------------------------------------------------------
  1158. void SceneWindow::zeroCameraTime( void )
  1159. {
  1160. // Reset Camera Time.
  1161. mRenderCameraTime = mPreCameraTime = mPostCameraTime = mCurrentCameraTime = 0.0f;
  1162. }
  1163. //-----------------------------------------------------------------------------
  1164. void SceneWindow::resetTickCameraTime( void )
  1165. {
  1166. // Reset Camera Time.
  1167. mRenderCameraTime = mPreCameraTime = mPostCameraTime = mCurrentCameraTime;
  1168. }
  1169. //-----------------------------------------------------------------------------
  1170. void SceneWindow::updateTickCameraTime( void )
  1171. {
  1172. // Store Pre Camera Time.
  1173. mPreCameraTime = mPostCameraTime;
  1174. // Store Current Camera Time.
  1175. mPostCameraTime = mCurrentCameraTime;
  1176. // Render Camera Time is at Pre-Tick Time.
  1177. mRenderCameraTime = mPreCameraTime;
  1178. }
  1179. //-----------------------------------------------------------------------------
  1180. void SceneWindow::resetTickCameraPosition( void )
  1181. {
  1182. mPreTickPosition = mPostTickPosition = mCameraCurrent.mSourceArea.point;
  1183. }
  1184. //-----------------------------------------------------------------------------
  1185. void SceneWindow::onRender( Point2I offset, const RectI& updateRect )
  1186. {
  1187. // Debug Profiling.
  1188. PROFILE_SCOPE(SceneWindow_onRender);
  1189. // Fetch scene.
  1190. Scene* pScene = getScene();
  1191. // Cannot render without scene!
  1192. if ( !pScene )
  1193. return;
  1194. // Calculate current camera View ( if needed ).
  1195. calculateCameraView( &mCameraCurrent );
  1196. // Fetch current camera.
  1197. const Point2F& sceneWindowScale = mCameraCurrent.mSceneWindowScale;
  1198. Point2F sceneMin = mCameraCurrent.mSceneMin;
  1199. Point2F sceneMax = mCameraCurrent.mSceneMax;
  1200. // Fetch bounds.
  1201. const RectI& bounds = getBounds();
  1202. const Point2I globalTopLeft( updateRect.point.x, updateRect.point.y );
  1203. const Point2I globalBottomRight( updateRect.point.x + updateRect.extent.x, updateRect.point.y + updateRect.extent.y );
  1204. const Point2I localTopLeft = globalToLocalCoord( globalTopLeft );
  1205. const Point2I localBottomRight = globalToLocalCoord( globalBottomRight );
  1206. // Clip top?
  1207. if ( localTopLeft.y > 0 )
  1208. {
  1209. sceneMax.y -= localTopLeft.y * sceneWindowScale.y;
  1210. }
  1211. // Clip left?
  1212. if ( localTopLeft.x > 0 )
  1213. {
  1214. sceneMin.x += localTopLeft.x * sceneWindowScale.x;
  1215. }
  1216. // Clip bottom?
  1217. if ( localBottomRight.y < bounds.extent.y )
  1218. {
  1219. sceneMin.y += (bounds.extent.y - localBottomRight.y ) * sceneWindowScale.y;
  1220. }
  1221. // Clip right?
  1222. if ( localBottomRight.x < bounds.extent.x )
  1223. {
  1224. sceneMax.x -= (bounds.extent.x - localBottomRight.x ) * sceneWindowScale.x;
  1225. }
  1226. // Add camera shake offset if active.
  1227. if ( mCameraShaking )
  1228. {
  1229. sceneMin += mCameraShakeOffset;
  1230. sceneMax += mCameraShakeOffset;
  1231. }
  1232. // Setup new logical coordinate system.
  1233. glMatrixMode(GL_PROJECTION);
  1234. glPushMatrix();
  1235. glLoadIdentity();
  1236. // Set orthographic projection.
  1237. glOrtho( sceneMin.x, sceneMax.x, sceneMin.y, sceneMax.y, 0.0f, MAX_LAYERS_SUPPORTED );
  1238. // Set ModelView.
  1239. glMatrixMode(GL_MODELVIEW);
  1240. glPushMatrix();
  1241. glLoadIdentity();
  1242. // Disable Alpha Test by default
  1243. glDisable( GL_ALPHA_TEST );
  1244. glDisable( GL_DEPTH_TEST );
  1245. // Get Debug Stats.
  1246. DebugStats& debugStats = pScene->getDebugStats();
  1247. // Create a scene render state.
  1248. SceneRenderState sceneRenderState(
  1249. mCameraCurrent.mDestinationArea,
  1250. mCameraCurrent.mDestinationArea.centre(),
  1251. mCameraCurrent.mCameraAngle,
  1252. mRenderLayerMask,
  1253. mRenderGroupMask,
  1254. Vector2( mCameraCurrent.mSceneWindowScale ),
  1255. &debugStats,
  1256. this );
  1257. // Clear the background color if requested.
  1258. if ( mUseBackgroundColor )
  1259. {
  1260. // Enable the scissor.
  1261. const RectI& clipRect = dglGetClipRect();
  1262. glEnable(GL_SCISSOR_TEST );
  1263. glScissor( clipRect.point.x, Platform::getWindowSize().y - (clipRect.point.y + clipRect.extent.y), clipRect.len_x(), clipRect.len_y() );
  1264. // Clear the background.
  1265. glClearColor( mBackgroundColor.red, mBackgroundColor.green, mBackgroundColor.blue, mBackgroundColor.alpha );
  1266. glClear(GL_COLOR_BUFFER_BIT);
  1267. // Disable the scissor.
  1268. glDisable( GL_SCISSOR_TEST );
  1269. }
  1270. // Render View.
  1271. pScene->sceneRender( &sceneRenderState );
  1272. // Restore Matrices.
  1273. glMatrixMode(GL_MODELVIEW);
  1274. glPopMatrix();
  1275. glMatrixMode(GL_PROJECTION);
  1276. glPopMatrix();
  1277. glMatrixMode(GL_MODELVIEW);
  1278. // Render the metrics.
  1279. renderMetricsOverlay( offset, updateRect );
  1280. // Render Children.
  1281. renderChildControls( offset, updateRect );
  1282. // Update Window.
  1283. setUpdate();
  1284. }
  1285. //------------------------------------------------------------------------------
  1286. void SceneWindow::renderMetricsOverlay( Point2I offset, const RectI& updateRect )
  1287. {
  1288. // Debug Profiling.
  1289. PROFILE_SCOPE(SceneWindow_RenderMetricsOverlay);
  1290. // Fetch scene.
  1291. Scene* pScene = getScene();
  1292. #if 0
  1293. // Force on debug rendering.
  1294. pScene->setDebugOn( 0xFFFFFFFF );
  1295. #endif
  1296. // Fetch full metrics mode.
  1297. const bool fullMetrics = pScene->getDebugMask() & Scene::SCENE_DEBUG_METRICS;
  1298. const bool fpsMetrics = pScene->getDebugMask() & Scene::SCENE_DEBUG_FPS_METRICS;
  1299. // Finish if should not or cannot render.
  1300. if ( ( !fullMetrics && !fpsMetrics ) ||
  1301. mProfile == NULL ||
  1302. mProfile->mFont.isNull() )
  1303. return;
  1304. // Fetch the font.
  1305. Resource<GFont>& font = mProfile->mFont;
  1306. // Blending for banner background.
  1307. glEnable ( GL_BLEND );
  1308. glBlendFunc ( GL_SRC_ALPHA , GL_ONE_MINUS_SRC_ALPHA );
  1309. // Set banner background colour.
  1310. const ColorI& fillColor = mProfile->mFillColor;
  1311. const F32 colorScale = 1.0f / 255.0f;
  1312. glColor4f( fillColor.red * colorScale, fillColor.green * colorScale, fillColor.blue * colorScale, fillColor.alpha * colorScale );
  1313. // Fetch debug scene object.
  1314. SceneObject* pDebugSceneObject = pScene->getDebugSceneObject();
  1315. // Get Debug Stats.
  1316. DebugStats& debugStats = pScene->getDebugStats();
  1317. // Set metrics offset
  1318. const S32 metricsOffset = (S32)font->getStrWidth( "WWWWWWWWWWWW" );
  1319. // Set Banner Height.
  1320. F32 bannerLineHeight = fullMetrics ? 17.0f : 1.0f;
  1321. // Add an extra line if we're monitoring a scene object.
  1322. if ( pDebugSceneObject != NULL )
  1323. bannerLineHeight += 5.0f;
  1324. U32 bannerHeight = (U32)((bannerLineHeight * (F32)font->getHeight()));
  1325. // Calculate Debug Banner Offset.
  1326. Point2I bannerOffset = updateRect.point + Point2I(8,8);
  1327. // Draw Banner Background.
  1328. glBegin(GL_TRIANGLE_STRIP);
  1329. glVertex2i( updateRect.point.x, updateRect.point.y );
  1330. glVertex2i( updateRect.point.x + updateRect.extent.x, updateRect.point.y );
  1331. glVertex2i( updateRect.point.x, updateRect.point.y + bannerHeight + 16);
  1332. glVertex2i( updateRect.point.x + updateRect.extent.x, updateRect.point.y + bannerHeight + 16);
  1333. glEnd();
  1334. // Disable Banner Blending.
  1335. glDisable ( GL_BLEND );
  1336. // Set Debug Text Colour.
  1337. dglSetBitmapModulation( mProfile->mFontColor );
  1338. // ****************************************************************
  1339. // Draw Banner Text.
  1340. // ****************************************************************
  1341. F32 linePositionY = 0.25f;
  1342. F32 linePositionOffsetY = font->getHeight() * 1.25f;
  1343. if ( fullMetrics )
  1344. {
  1345. // Rendering.
  1346. dglDrawText( font, bannerOffset + Point2I(0,(S32)linePositionY), "Render", NULL );
  1347. dSprintf( mDebugText, sizeof( mDebugText ), "- FPS=%4.1f<%4.1f/%4.1f>, Frames=%u, Picked=%d<%d>, RenderRequests=%d<%d>, RenderFallbacks=%d<%d>",
  1348. debugStats.fps, debugStats.minFPS, debugStats.maxFPS,
  1349. debugStats.frameCount,
  1350. debugStats.renderPicked, debugStats.maxRenderPicked,
  1351. debugStats.renderRequests, debugStats.maxRenderRequests,
  1352. debugStats.renderFallbacks, debugStats.maxRenderFallbacks );
  1353. dglDrawText( font, bannerOffset + Point2I(metricsOffset,(S32)linePositionY), mDebugText, NULL );
  1354. linePositionY += linePositionOffsetY;
  1355. // Scene.
  1356. dglDrawText( font, bannerOffset + Point2I(0,(S32)linePositionY), "Scene", NULL );
  1357. dSprintf( mDebugText, sizeof( mDebugText ), "- Count=%d, Index=%d, Time=%0.1fs, Objects=%d<%d>(Global=%d), Enabled=%d<%d>, Visible=%d<%d>, Awake=%d<%d>, Controllers=%d",
  1358. Scene::getGlobalSceneCount(), pScene->getSceneIndex(),
  1359. pScene->getSceneTime(),
  1360. debugStats.objectsCount, debugStats.maxObjectsCount, SceneObject::getGlobalSceneObjectCount(),
  1361. debugStats.objectsEnabled, debugStats.maxObjectsEnabled,
  1362. debugStats.objectsVisible, debugStats.maxObjectsVisible,
  1363. debugStats.objectsAwake, debugStats.maxObjectsAwake,
  1364. pScene->getControllers() == NULL ? 0 : pScene->getControllers()->size() );
  1365. dglDrawText( font, bannerOffset + Point2I(metricsOffset,(S32)linePositionY), mDebugText, NULL );
  1366. linePositionY += linePositionOffsetY;
  1367. // Camera Window #1.
  1368. dglDrawText( font, bannerOffset + Point2I(0,(S32)linePositionY), "Camera", NULL );
  1369. Vector2 cameraPosition = getCameraPosition();
  1370. dSprintf( mDebugText, sizeof( mDebugText ), "- Pos=(%0.1f,%0.1f), Size=(%0.1f,%0.1f), Zoom=%0.1f, Angle=%0.1f, Lower=(%0.1f,%0.1f), Upper=(%0.1f,%0.1f)",
  1371. cameraPosition.x,
  1372. cameraPosition.y,
  1373. mCameraCurrent.mSourceArea.extent.x,
  1374. mCameraCurrent.mSourceArea.extent.y,
  1375. mCameraCurrent.mCameraZoom,
  1376. mRadToDeg(mCameraCurrent.mCameraAngle),
  1377. mCameraCurrent.mSourceArea.point.x,
  1378. mCameraCurrent.mSourceArea.point.y,
  1379. mCameraCurrent.mSourceArea.point.x + mCameraCurrent.mSourceArea.extent.x,
  1380. mCameraCurrent.mSourceArea.point.y + mCameraCurrent.mSourceArea.extent.y );
  1381. dglDrawText( font, bannerOffset + Point2I(metricsOffset,(S32)linePositionY), mDebugText, NULL );
  1382. linePositionY += linePositionOffsetY;
  1383. // Camera Window #2.
  1384. Point2I windowExtent = getExtent();
  1385. Vector2 windowScale = getCameraWindowScale();
  1386. dSprintf( mDebugText, sizeof( mDebugText ), "- Window=(%d,%d), WorldScale=(%0.3f,%0.3f), RenderScale=(%0.3f,%0.3f)",
  1387. windowExtent.x, windowExtent.y,
  1388. windowScale.x, windowScale.y,
  1389. 1.0f / windowScale.x, 1.0f / windowScale.y);
  1390. dglDrawText( font, bannerOffset + Point2I(metricsOffset,(S32)linePositionY), mDebugText, NULL );
  1391. linePositionY += linePositionOffsetY;
  1392. // Batching #1.
  1393. dglDrawText( font, bannerOffset + Point2I(0,(S32)linePositionY), "Batching", NULL );
  1394. dSprintf( mDebugText, sizeof( mDebugText ), "- %sTris=%d<%d>, MaxTriDraw=%d, MaxVerts=%d, Single=%d<%d>, Mult=%d<%d>, Sorted=%d<%d>",
  1395. pScene->getBatchingEnabled() ? "" : "(OFF) ",
  1396. debugStats.batchTrianglesSubmitted, debugStats.maxBatchTrianglesSubmitted,
  1397. debugStats.batchMaxTriangleDrawn,
  1398. debugStats.batchMaxVertexBuffer,
  1399. debugStats.batchDrawCallsStrictSingle, debugStats.maxBatchDrawCallsStrictSingle,
  1400. debugStats.batchDrawCallsStrictMultiple, debugStats.maxBatchDrawCallsStrictMultiple,
  1401. debugStats.batchDrawCallsSorted, debugStats.maxBatchDrawCallsSorted
  1402. );
  1403. dglDrawText( font, bannerOffset + Point2I(metricsOffset,(S32)linePositionY), mDebugText, NULL );
  1404. linePositionY += linePositionOffsetY;
  1405. // Batching #2.
  1406. dSprintf( mDebugText, sizeof( mDebugText ), "- Flush=%d<%d>, BlendFlush=%d<%d>, ColorFlush=%d<%d>, AlphaFlush=%d<%d>, TexFlush=%d<%d>",
  1407. debugStats.batchFlushes, debugStats.maxBatchFlushes,
  1408. debugStats.batchBlendStateFlush, debugStats.maxBatchBlendStateFlush,
  1409. debugStats.batchColorStateFlush, debugStats.maxBatchColorStateFlush,
  1410. debugStats.batchAlphaStateFlush, debugStats.maxBatchAlphaStateFlush,
  1411. debugStats.batchTextureChangeFlush, debugStats.maxBatchTextureChangeFlushes
  1412. );
  1413. dglDrawText( font, bannerOffset + Point2I(metricsOffset,(S32)linePositionY), mDebugText, NULL );
  1414. linePositionY += linePositionOffsetY;
  1415. // Batching #3.
  1416. dSprintf( mDebugText, sizeof( mDebugText ), "- IsolatedFlush=%d<%d>, FullFlush=%d<%d>, LayerFlush=%d<%d>, NoBatchFlush=%d<%d>, AnonFlush=%d<%d>",
  1417. debugStats.batchIsolatedFlush, debugStats.maxBatchIsolatedFlush,
  1418. debugStats.batchBufferFullFlush, debugStats.maxBatchBufferFullFlush,
  1419. debugStats.batchLayerFlush, debugStats.maxBatchLayerFlush,
  1420. debugStats.batchNoBatchFlush, debugStats.maxBatchNoBatchFlush,
  1421. debugStats.batchAnonymousFlush, debugStats.maxBatchAnonymousFlush
  1422. );
  1423. dglDrawText( font, bannerOffset + Point2I(metricsOffset,(S32)linePositionY), mDebugText, NULL );
  1424. linePositionY += linePositionOffsetY;
  1425. // Textures.
  1426. dglDrawText( font, bannerOffset + Point2I(0,(S32)linePositionY), "Textures", NULL );
  1427. dSprintf( mDebugText, sizeof( mDebugText ), "- TextureCount=%d, TextureSize=%d, TextureWaste=%d, BitmapSize=%d",
  1428. TextureManager::getTextureResidentCount(),
  1429. TextureManager::getTextureResidentSize(),
  1430. TextureManager::getTextureResidentWasteSize(),
  1431. TextureManager::getBitmapResidentSize()
  1432. );
  1433. dglDrawText( font, bannerOffset + Point2I(metricsOffset,(S32)linePositionY), mDebugText, NULL );
  1434. linePositionY += linePositionOffsetY;
  1435. // Physics.
  1436. dglDrawText( font, bannerOffset + Point2I(0,(S32)linePositionY), "Physics", NULL );
  1437. dSprintf( mDebugText, sizeof( mDebugText ), "- Bodies=%d<%d>, Joints=%d<%d>, Contacts=%d<%d>, Proxies=%d<%d>",
  1438. debugStats.bodyCount, debugStats.maxBodyCount,
  1439. debugStats.jointCount, debugStats.maxJointCount,
  1440. debugStats.contactCount, debugStats.maxContactCount,
  1441. debugStats.proxyCount, debugStats.maxProxyCount );
  1442. dglDrawText( font, bannerOffset + Point2I(metricsOffset,(S32)linePositionY), mDebugText, NULL );
  1443. linePositionY += linePositionOffsetY;
  1444. const b2Profile& worldProfile = debugStats.worldProfile;
  1445. const b2Profile& maxWorldProfile = debugStats.maxWorldProfile;
  1446. // Physics timings #1.
  1447. dglDrawText( font, bannerOffset + Point2I(0,(S32)linePositionY), "Timings", NULL );
  1448. dSprintf( mDebugText, sizeof( mDebugText ), "- Step=%0.0f<%0.0f>, Collide=%0.0f<%0.0f>, BroadPhase=%0.0f<%0.0f>",
  1449. worldProfile.step, maxWorldProfile.step,
  1450. worldProfile.collide, maxWorldProfile.collide,
  1451. worldProfile.broadphase, maxWorldProfile.broadphase );
  1452. dglDrawText( font, bannerOffset + Point2I(metricsOffset,(S32)linePositionY), mDebugText, NULL );
  1453. linePositionY += linePositionOffsetY;
  1454. // Physics timings #2.
  1455. dSprintf( mDebugText, sizeof( mDebugText ), "- Solve=%0.0f<%0.0f>, SolveInit=%0.0f<%0.0f>, SolveVel=%0.0f<%0.0f>, SolvePos=%0.0f<%0.0f>, SolveTOI=%0.0f<%0.0f>",
  1456. worldProfile.solve, maxWorldProfile.solve,
  1457. worldProfile.solveInit, maxWorldProfile.solveInit,
  1458. worldProfile.solveVelocity, maxWorldProfile.solveVelocity,
  1459. worldProfile.solvePosition, maxWorldProfile.solvePosition,
  1460. worldProfile.solveTOI, maxWorldProfile.solveTOI );
  1461. dglDrawText( font, bannerOffset + Point2I(metricsOffset,(S32)linePositionY), mDebugText, NULL );
  1462. linePositionY += linePositionOffsetY;
  1463. // Physics spatial tree.
  1464. dglDrawText( font, bannerOffset + Point2I(0,(S32)linePositionY), "Partition", NULL );
  1465. const b2World* pWorld = pScene->getWorld();
  1466. const S32 treeBalance = pWorld->GetTreeBalance();
  1467. const S32 treeHeight = pWorld->GetTreeHeight();
  1468. const F32 treeQuality = pWorld->GetTreeQuality();
  1469. dSprintf( mDebugText, sizeof( mDebugText ), "- Balance=%d, Height=%d, Quality=%0.2f",
  1470. treeBalance,
  1471. treeHeight,
  1472. treeQuality );
  1473. dglDrawText( font, bannerOffset + Point2I(metricsOffset,(S32)linePositionY), mDebugText, NULL );
  1474. linePositionY += linePositionOffsetY;
  1475. // Particles.
  1476. dglDrawText( font, bannerOffset + Point2I(0,(S32)linePositionY), "Particles", NULL );
  1477. dSprintf( mDebugText, sizeof( mDebugText ), "- Allocated=%d, Used=%d<%d>, Free=%d",
  1478. debugStats.particlesAlloc,
  1479. debugStats.particlesUsed, debugStats.maxParticlesUsed,
  1480. debugStats.particlesFree );
  1481. dglDrawText( font, bannerOffset + Point2I(metricsOffset,(S32)linePositionY), mDebugText, NULL );
  1482. linePositionY += linePositionOffsetY;
  1483. // Asset Manager.
  1484. dglDrawText( font, bannerOffset + Point2I(0,(S32)linePositionY), "Assets", NULL );
  1485. dSprintf( mDebugText, sizeof( mDebugText ), "- AcquiredRefs=%d, Declared=%d, Referenced=%d, LoadedInternal=%d<%d>, LoadedExternal=%d<%d>, LoadedPrivate=%d<%d>",
  1486. AssetDatabase.getAcquiredReferenceCount(),
  1487. AssetDatabase.getDeclaredAssetCount(),
  1488. AssetDatabase.getReferencedAssetCount(),
  1489. AssetDatabase.getLoadedInternalAssetCount(), AssetDatabase.getMaxLoadedInternalAssetCount(),
  1490. AssetDatabase.getLoadedExternalAssetCount(), AssetDatabase.getMaxLoadedExternalAssetCount(),
  1491. AssetDatabase.getLoadedPrivateAssetCount(), AssetDatabase.getMaxLoadedPrivateAssetCount() );
  1492. dglDrawText( font, bannerOffset + Point2I(metricsOffset,(S32)linePositionY), mDebugText, NULL );
  1493. linePositionY += linePositionOffsetY;
  1494. }
  1495. else if ( fpsMetrics )
  1496. {
  1497. // Rendering.
  1498. dSprintf( mDebugText, sizeof( mDebugText ), "FPS=%4.1f<%4.1f/%4.1f>",
  1499. debugStats.fps, debugStats.minFPS, debugStats.maxFPS );
  1500. dglDrawText( font, bannerOffset + Point2I(0,(S32)linePositionY), mDebugText, NULL );
  1501. linePositionY += linePositionOffsetY;
  1502. }
  1503. // Monitored scene object.
  1504. if ( pDebugSceneObject != NULL )
  1505. {
  1506. // SceneObject #1.
  1507. dglDrawText( font, bannerOffset + Point2I(0,(S32)linePositionY), "SceneObject", NULL );
  1508. const b2Vec2 position = pDebugSceneObject->getRenderPosition();
  1509. const F32 angle = mRadToDeg( pDebugSceneObject->getRenderAngle() );
  1510. const Vector2 size = pDebugSceneObject->getSize();
  1511. const U32 sceneLayer = pDebugSceneObject->getSceneLayer();
  1512. const U32 sceneGroup = pDebugSceneObject->getSceneGroup();
  1513. const U32 serialId = pDebugSceneObject->getSerialId();
  1514. StringTableEntry renderGroup = pDebugSceneObject->getRenderGroup();
  1515. dSprintf( mDebugText, sizeof( mDebugText ), "- Id=%d, Pos=(%0.3f,%0.3f), Angle=%0.3f, Size=(%0.3f,%0.3f), Layer=%d, Group=%d, SerialId=%d, RenderGroup='%s'",
  1516. pDebugSceneObject->getId(),
  1517. position.x, position.y,
  1518. angle,
  1519. size.x, size.y,
  1520. sceneLayer,
  1521. sceneGroup,
  1522. serialId,
  1523. renderGroup
  1524. );
  1525. dglDrawText( font, bannerOffset + Point2I(metricsOffset,(S32)linePositionY), mDebugText, NULL );
  1526. linePositionY += linePositionOffsetY;
  1527. // SceneObject #2.
  1528. const char* pBodyType = SceneObject::getBodyTypeDescription(pDebugSceneObject->getBodyType());
  1529. const bool enabled = pDebugSceneObject->isEnabled();
  1530. const bool active = pDebugSceneObject->getActive();
  1531. const bool visible = pDebugSceneObject->getVisible();
  1532. const bool isBullet = pDebugSceneObject->getBullet();
  1533. const bool awake = pDebugSceneObject->getAwake();
  1534. const bool sleepingAllowed = pDebugSceneObject->getSleepingAllowed();
  1535. dSprintf( mDebugText, sizeof( mDebugText ), "- Body=%s, Enabled=%d, Active=%d, Visible=%d, Bullet=%d, Awake=%d, CanSleep=%d",
  1536. pBodyType,
  1537. enabled,
  1538. active,
  1539. visible,
  1540. isBullet,
  1541. awake,
  1542. sleepingAllowed
  1543. );
  1544. dglDrawText( font, bannerOffset + Point2I(metricsOffset,(S32)linePositionY), mDebugText, NULL );
  1545. linePositionY += linePositionOffsetY;
  1546. // SceneObject #3.
  1547. const Vector2 linearVelocity = pDebugSceneObject->getLinearVelocity();
  1548. const F32 angularVelocity = pDebugSceneObject->getAngularVelocity();
  1549. const F32 linearDamping = pDebugSceneObject->getLinearDamping();
  1550. const F32 angularDamping = pDebugSceneObject->getAngularDamping();
  1551. const Vector2 localCenter = pDebugSceneObject->getLocalCenter();
  1552. dSprintf( mDebugText, sizeof( mDebugText ), "- LinVel=(%0.3f,%0.3f), AngVel=%0.3f, LinDamp=%0.3f, AngDamp=%0.3f, Com=(%0.3f,%0.3f)",
  1553. linearVelocity.x, linearVelocity.y,
  1554. angularVelocity,
  1555. linearDamping,
  1556. angularDamping,
  1557. localCenter.x, localCenter.y
  1558. );
  1559. dglDrawText( font, bannerOffset + Point2I(metricsOffset,(S32)linePositionY), mDebugText, NULL );
  1560. linePositionY += linePositionOffsetY;
  1561. // SceneObject #4.
  1562. const bool collisionSuppress = pDebugSceneObject->getCollisionSuppress();
  1563. const U32 collisionLayerMask = pDebugSceneObject->getCollisionLayerMask();
  1564. const U32 collisionGroupMask = pDebugSceneObject->getCollisionGroupMask();
  1565. const U32 collisionShapeCount = pDebugSceneObject->getCollisionShapeCount();
  1566. dSprintf( mDebugText, sizeof( mDebugText ), "- Shapes=%d, ColSuppress=%d, ColLayer=%0.8x, ColGroup=%0.8x",
  1567. collisionShapeCount,
  1568. collisionSuppress,
  1569. collisionLayerMask,
  1570. collisionGroupMask
  1571. );
  1572. dglDrawText( font, bannerOffset + Point2I(metricsOffset,(S32)linePositionY), mDebugText, NULL );
  1573. }
  1574. // Clear Bitmap Modulation.
  1575. dglClearBitmapModulation();
  1576. }