SceneWindow.cc 72 KB

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