SceneWindow.cc 73 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065
  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 linearInterpolate( from, to, delta );
  448. // Sigmoid.
  449. else if ( mCameraInterpolationMode == SIGMOID )
  450. return sigmoidInterpolate( from, to, delta );
  451. // Hmmm...
  452. else
  453. return from;
  454. }
  455. //-----------------------------------------------------------------------------
  456. F32 SceneWindow::linearInterpolate( F32 from, F32 to, F32 delta )
  457. {
  458. // Clamp if we're over/under time.
  459. if ( delta <= 0.0f )
  460. return from;
  461. else if ( delta >= 1.0f )
  462. return to;
  463. // Calculate resultant interpolation.
  464. return ( from * ( 1.0f - delta ) ) + ( to * delta );
  465. }
  466. //-----------------------------------------------------------------------------
  467. F32 SceneWindow::sigmoidInterpolate( F32 from, F32 to, F32 delta )
  468. {
  469. // Range Expand/Clamp Delta to (-1 -> +1).
  470. delta = mClampF( (delta - 0.5f) * 2.0f, -1.0f, 1.0f );
  471. // Calculate interpolator value using sigmoid function.
  472. F32 sigmoid = mClampF ( 1.0f / (1.0f + mPow(2.718282f, -15.0f * delta)), 0.0f, 1.0f );
  473. // Calculate resultant interpolation.
  474. return ( from * ( 1.0f - sigmoid ) ) + ( to * sigmoid );
  475. }
  476. //-----------------------------------------------------------------------------
  477. void SceneWindow::startCameraShake( const F32 magnitude, const F32 time )
  478. {
  479. // Is the time zero?
  480. if ( mIsZero( time ) && mIsZero( magnitude ) )
  481. {
  482. // Yes, so simply stop the camera shaking.
  483. stopCameraShake();
  484. // Finish here.
  485. return;
  486. }
  487. // Set Current Shake.
  488. mCurrentShake = mFabs(magnitude);
  489. // Set Shake Life.
  490. mShakeLife = time;
  491. // Calculate Shake Ramp.
  492. mShakeRamp = mCurrentShake / mShakeLife;
  493. // Flag camera shaking.
  494. mCameraShaking = true;
  495. }
  496. //-----------------------------------------------------------------------------
  497. void SceneWindow::stopCameraShake( void )
  498. {
  499. // Flag camera not shaking.
  500. mCameraShaking = false;
  501. // Reset Shake Offset.
  502. mCameraShakeOffset.setZero();
  503. }
  504. //-----------------------------------------------------------------------------
  505. void SceneWindow::mount( SceneObject* pSceneObject, const Vector2& mountOffset, const F32 mountForce, const bool sendToMount, const bool mountAngle )
  506. {
  507. // Sanity!
  508. AssertFatal( pSceneObject != NULL, "Scene object cannot be NULL." );
  509. // Cannot mount if not in a scene.
  510. if ( !mpScene )
  511. {
  512. // Warn!
  513. Con::warnf("Cannot mount scene window (%d) to a scene object (%d) if scene window is not attached to a scene.", getId(), pSceneObject->getId() );
  514. return;
  515. }
  516. // Fetch objects' scene.
  517. const Scene* pScene = pSceneObject->getScene();
  518. // Scene object must be in a scene.
  519. if ( !pScene )
  520. {
  521. // Warn!
  522. Con::warnf("Cannot mount scene window (%d) to a scene object (%d) that is not in a scene.", getId(), pSceneObject->getId() );
  523. return;
  524. }
  525. // Scene object must be in same scene as the one the scene window is attached to.
  526. if ( pScene != mpScene )
  527. {
  528. // Warn!
  529. Con::warnf("Cannot mount scene window (%d) to a scene object (%d) that are not using the same scene.", getId(), pSceneObject->getId() );
  530. return;
  531. }
  532. // Are we mounted to an object?
  533. if ( isCameraMounted() )
  534. {
  535. // Yes, so dismount object.
  536. dismount();
  537. }
  538. else
  539. {
  540. // No, so stop any Camera Move.
  541. if ( mMovingCamera ) stopCameraMove();
  542. }
  543. // Set Mount Object Reference.
  544. mpMountedTo = pSceneObject;
  545. // Store Mount Offset.
  546. mMountOffset = mountOffset;
  547. // Set Mount Force.
  548. mMountForce = mountForce;
  549. // Set Mount Angle.
  550. mMountAngle = mountAngle;
  551. // Add Camera Mount Reference.
  552. pSceneObject->addCameraMountReference( this );
  553. // Flag Camera mounted.
  554. mCameraMounted = true;
  555. // Send directly to mount (if selected).
  556. if ( sendToMount )
  557. {
  558. // Fetch Mount Position.
  559. const Vector2& mountPos = mpMountedTo->getBody()->GetWorldPoint( mountOffset );
  560. // Calculate Window Half-Dimensions.
  561. const F32 halfWidth = mCameraCurrent.mSourceArea.len_x() * 0.5f;
  562. const F32 halfHeight = mCameraCurrent.mSourceArea.len_y() * 0.5f;
  563. // Set Current View to Object Position.
  564. mCameraCurrent.mSourceArea.point.set( mountPos.x - halfWidth, mountPos.y - halfHeight );
  565. }
  566. // Reset Tick Camera Position.
  567. resetTickCameraPosition();
  568. }
  569. //-----------------------------------------------------------------------------
  570. void SceneWindow::dismount( void )
  571. {
  572. // Nothing to do if we're not mounted!
  573. if (!isCameraMounted() )
  574. return;
  575. // Remove Camera Mount Reference.
  576. mpMountedTo->removeCameraMountReference();
  577. // Reset Camera Object Mount.
  578. mpMountedTo = NULL;
  579. // Flag Camera not mounted.
  580. mCameraMounted = false;
  581. // Reset Tick Camera Position.
  582. resetTickCameraPosition();
  583. }
  584. //-----------------------------------------------------------------------------
  585. void SceneWindow::setViewLimitOn( const Vector2& limitMin, const Vector2& limitMax )
  586. {
  587. // Activate View Limit.
  588. mViewLimitActive = true;
  589. // Set View Limit Min/Max.
  590. mViewLimitMin = limitMin;
  591. mViewLimitMax = limitMax;
  592. // Calculate Camera Area.
  593. mViewLimitArea = mViewLimitMax - mViewLimitMin;
  594. }
  595. //-----------------------------------------------------------------------------
  596. void SceneWindow::clampCameraViewLimit( void )
  597. {
  598. // Finish if the view-limit is not on or the camera is moving.
  599. if ( !mViewLimitActive || mMovingCamera )
  600. return;
  601. // Calculate Current Camera View.
  602. calculateCameraView( &mCameraCurrent );
  603. // Calculate the source and destination centres.
  604. const Point2F sourceCentre = mCameraCurrent.mSourceArea.centre();
  605. const Point2F destinationCentre = mCameraCurrent.mDestinationArea.centre();
  606. // Are the source and destination areas the same?
  607. if ( sourceCentre != destinationCentre )
  608. {
  609. // No, so adjust the source position to be at the same position as the destination i.e. don't change the source area.
  610. mCameraCurrent.mSourceArea.point += destinationCentre - sourceCentre;
  611. }
  612. }
  613. //-----------------------------------------------------------------------------
  614. void SceneWindow::setObjectInputEventFilter( const U32 groupMask, const U32 layerMask, const bool useInvisible )
  615. {
  616. // Set Object Mouse Event Filter.
  617. mInputEventGroupMaskFilter = groupMask;
  618. mInputEventLayerMaskFilter = layerMask;
  619. mInputEventInvisibleFilter = useInvisible;
  620. }
  621. //-----------------------------------------------------------------------------
  622. void SceneWindow::setObjectInputEventGroupFilter( const U32 groupMask )
  623. {
  624. mInputEventGroupMaskFilter = groupMask;
  625. // Clear existing watched input events.
  626. clearWatchedInputEvents();
  627. }
  628. //-----------------------------------------------------------------------------
  629. void SceneWindow::setObjectInputEventLayerFilter( const U32 layerMask )
  630. {
  631. mInputEventLayerMaskFilter = layerMask;
  632. // Clear existing watched input events.
  633. clearWatchedInputEvents();
  634. }
  635. //-----------------------------------------------------------------------------
  636. void SceneWindow::setObjectInputEventInvisibleFilter( const bool useInvisible )
  637. {
  638. mInputEventInvisibleFilter = useInvisible;
  639. // Clear existing watched input events.
  640. clearWatchedInputEvents();
  641. }
  642. //-----------------------------------------------------------------------------
  643. void SceneWindow::addInputListener( SimObject* pSimObject )
  644. {
  645. // Sanity!
  646. AssertFatal( pSimObject != NULL, "SceneWindow::addInputEventListener() - Cannot add NULL object as input event listener." );
  647. // Ignore if the object is already a listener.
  648. if ( mInputListeners.find( pSimObject ) != mInputListeners.end() )
  649. return;
  650. // Add as listener.
  651. mInputListeners.addObject( pSimObject );
  652. }
  653. //-----------------------------------------------------------------------------
  654. void SceneWindow::removeInputListener( SimObject* pSimObject )
  655. {
  656. mInputListeners.removeObject( pSimObject );
  657. }
  658. //-----------------------------------------------------------------------------
  659. void SceneWindow::setMousePosition( const Vector2& mousePosition )
  660. {
  661. // Fetch Canvas.
  662. GuiCanvas* pCanvas = getRoot();
  663. // Canvas available?
  664. if ( pCanvas )
  665. {
  666. // Are we bound to a scene?
  667. if ( getScene() )
  668. {
  669. Vector2 windowMousePosition;
  670. // Yes, so convert window into scene coordinates...
  671. sceneToWindowPoint( mousePosition, windowMousePosition );
  672. // Copy into a compatible format for TGE.
  673. Point2I localWindowPosition( S32(windowMousePosition.x), S32(windowMousePosition.y) );
  674. // Set Cursor Position.
  675. pCanvas->setCursorPos( localToGlobalCoord(localWindowPosition) );
  676. }
  677. else
  678. {
  679. // No, so error.
  680. Con::warnf("SceneObject::setMousePosition() - No scene attached to window!");
  681. return;
  682. }
  683. }
  684. }
  685. //-----------------------------------------------------------------------------
  686. Vector2 SceneWindow::getMousePosition( void )
  687. {
  688. // Calculate Current Camera View.
  689. calculateCameraView( &mCameraCurrent );
  690. // Fetch Canvas.
  691. GuiCanvas* pCanvas = getRoot();
  692. // World Mouse Position.
  693. Vector2 worldMousePoint(0, 0);
  694. // Canvas available?
  695. if ( pCanvas )
  696. {
  697. // Yes, so fetch local GUI coordinates.
  698. const Vector2 localGuiPoint = globalToLocalCoord( pCanvas->getCursorPos() );
  699. // Are we bound to a scene?
  700. if ( getScene() )
  701. {
  702. // Yes, so convert window into scene coordinates...
  703. windowToScenePoint(localGuiPoint, worldMousePoint);
  704. }
  705. else
  706. {
  707. // No, so use window screen coordinates.
  708. worldMousePoint = localGuiPoint;
  709. }
  710. }
  711. #if 0
  712. else
  713. {
  714. // No, so warn
  715. Con::warnf("SceneWindow::getMousePosition() - Window not attached to canvas!" );
  716. }
  717. #endif
  718. // Return World Mouse Position.
  719. return worldMousePoint;
  720. }
  721. //-----------------------------------------------------------------------------
  722. void SceneWindow::windowToScenePoint( const Vector2& srcPoint, Vector2& dstPoint ) const
  723. {
  724. // Return Conversion.
  725. dstPoint.Set( (srcPoint.x * mCameraCurrent.mSceneWindowScale.x) + mCameraCurrent.mSceneMin.x, mCameraCurrent.mSceneMax.y - (srcPoint.y * mCameraCurrent.mSceneWindowScale.y) );
  726. }
  727. //-----------------------------------------------------------------------------
  728. void SceneWindow::sceneToWindowPoint( const Vector2& srcPoint, Vector2& dstPoint ) const
  729. {
  730. // Return Conversion.
  731. dstPoint.Set( (srcPoint.x - mCameraCurrent.mSceneMin.x) / mCameraCurrent.mSceneWindowScale.x, (mCameraCurrent.mSceneMax.y - srcPoint.y) / mCameraCurrent.mSceneWindowScale.y );
  732. }
  733. //-----------------------------------------------------------------------------
  734. void SceneWindow::dispatchInputEvent( StringTableEntry name, const GuiEvent& event )
  735. {
  736. // Debug Profiling.
  737. PROFILE_SCOPE(SceneWindow_DispatchInputEvent);
  738. // Dispatch input event to window if appropriate.
  739. if ( getUseWindowInputEvents() )
  740. sendWindowInputEvent( name, event );
  741. // Dispatch input event to scene objects if appropriate.
  742. if ( getUseObjectInputEvents() )
  743. sendObjectInputEvent( name, event );
  744. }
  745. //-----------------------------------------------------------------------------
  746. void SceneWindow::sendWindowInputEvent( StringTableEntry name, const GuiEvent& event )
  747. {
  748. // Debug Profiling.
  749. PROFILE_SCOPE(SceneWindow_SendWindowInputEvent);
  750. Vector2 worldMousePoint;
  751. // Calculate Current Camera View.
  752. calculateCameraView( &mCameraCurrent );
  753. // Convert to local gui coordinates.
  754. const Vector2 localGuiPoint = globalToLocalCoord(event.mousePoint);
  755. // Are we bound to a scene?
  756. if ( getScene() )
  757. {
  758. // Yes, so convert window into scene coordinates...
  759. windowToScenePoint(localGuiPoint, worldMousePoint);
  760. }
  761. else
  762. {
  763. // No, so use window screen coordinates.
  764. worldMousePoint = localGuiPoint;
  765. }
  766. // Argument Buffers.
  767. char argBuffer[3][64];
  768. // Format Event-Modifier Buffer.
  769. dSprintf(argBuffer[0], 64, "%d", event.eventID);
  770. // Format Mouse-Position Buffer.
  771. dSprintf(argBuffer[1], 64, "%g %g", worldMousePoint.x, worldMousePoint.y);
  772. // Format Mouse-Click Count Buffer.
  773. dSprintf(argBuffer[2], 64, "%d", event.mouseClickCount);
  774. // Call Scripts.
  775. Con::executef(this, 4, name, argBuffer[0], argBuffer[1], argBuffer[2]);
  776. // Iterate listeners.
  777. for( SimSet::iterator listenerItr = mInputListeners.begin(); listenerItr != mInputListeners.end(); ++listenerItr )
  778. {
  779. // Call scripts on listener.
  780. Con::executef( *listenerItr, 4, name, argBuffer[0], argBuffer[1], argBuffer[2] );
  781. }
  782. }
  783. //-----------------------------------------------------------------------------
  784. void SceneWindow::sendObjectInputEvent( StringTableEntry name, const GuiEvent& event )
  785. {
  786. // Debug Profiling.
  787. PROFILE_SCOPE(SceneWindow_SendObjectInputEvent);
  788. // Finish if we're not bound to a scene?
  789. if ( !getScene() ) return;
  790. // Only process appropriate input events.
  791. if ( !( name == inputEventDownName ||
  792. name == inputEventUpName ||
  793. name == inputEventMovedName ||
  794. name == inputEventDraggedName ) )
  795. return;
  796. // Convert Event-Position into scene coordinates.
  797. Vector2 worldMousePoint;
  798. windowToScenePoint(Vector2(globalToLocalCoord(event.mousePoint)), worldMousePoint);
  799. // Fetch old pick count.
  800. const U32 oldPickCount = (U32)mInputEventWatching.size();
  801. // Fetch world query and clear results.
  802. WorldQuery* pWorldQuery = getScene()->getWorldQuery( true );
  803. // Set filter.
  804. WorldQueryFilter queryFilter( mInputEventLayerMaskFilter, mInputEventGroupMaskFilter, true, mInputEventInvisibleFilter, true, true );
  805. pWorldQuery->setQueryFilter( queryFilter );
  806. // Perform world query.
  807. const U32 newPickCount = pWorldQuery->anyQueryPoint( worldMousePoint );
  808. // Early-out if nothing to do.
  809. if ( newPickCount == 0 && oldPickCount == 0 )
  810. return;
  811. // Fetch results.
  812. mInputEventQuery = pWorldQuery->getQueryResults();
  813. pWorldQuery->clearQuery();
  814. // Determine "enter" events.
  815. for( U32 newIndex = 0; newIndex < newPickCount; ++newIndex )
  816. {
  817. // Fetch new scene object.
  818. SceneObject* pNewSceneObject = mInputEventQuery[newIndex].mpSceneObject;
  819. // Ignore object if it's not using input events.
  820. // NOTE:- We only check this for "enter" events in-case the option is
  821. // changed whilst it's currently picked. We want to guarantee
  822. // that any "enter" event is paired with a "leave" event.
  823. if ( !pNewSceneObject->getUseInputEvents() )
  824. continue;
  825. bool alreadyPresent = false;
  826. for ( U32 oldIndex = 0; oldIndex < oldPickCount; ++oldIndex )
  827. {
  828. // Skip if scene object is not present...
  829. if ( mInputEventWatching[oldIndex] != pNewSceneObject )
  830. continue;
  831. // Flag as already present.
  832. alreadyPresent = true;
  833. break;
  834. }
  835. // Add scene object as entering if not already present.
  836. if ( !alreadyPresent )
  837. mInputEventEntering.push_back( pNewSceneObject );
  838. }
  839. // Determine "leave" events.
  840. for ( U32 oldIndex = 0; oldIndex < oldPickCount; ++oldIndex )
  841. {
  842. // Fetch old scene object.
  843. SceneObject* pOldSceneObject = dynamic_cast<SceneObject*>( mInputEventWatching[oldIndex] );
  844. // Sanity!
  845. AssertFatal( pOldSceneObject != NULL, "Invalid object found in mouse-event pick vector." );
  846. bool stillPresent = false;
  847. for( U32 newIndex = 0; newIndex < newPickCount; ++newIndex )
  848. {
  849. // Skip if scene object is not present.
  850. if ( mInputEventQuery[newIndex].mpSceneObject != pOldSceneObject )
  851. continue;
  852. // Flag as still present.
  853. stillPresent = true;
  854. break;
  855. }
  856. // Add scene object as leaving if not still present.
  857. if ( !stillPresent )
  858. mInputEventLeaving.push_back( pOldSceneObject );
  859. }
  860. for ( U32 index = 0; index < (U32)mInputEventQuery.size(); ++index )
  861. {
  862. // Fetch scene object.
  863. SceneObject* pSceneObject = mInputEventQuery[index].mpSceneObject;
  864. // Ignore object if it's not using input events.
  865. if ( !pSceneObject->getUseInputEvents() )
  866. continue;
  867. // Emit event.
  868. pSceneObject->onInputEvent( name, event, worldMousePoint );
  869. }
  870. // Process "leave" events.
  871. for ( U32 index = 0; index < (U32)mInputEventLeaving.size(); ++index )
  872. {
  873. // Fetch scene object.
  874. SceneObject* pSceneObject = mInputEventLeaving[index];
  875. // Emit event.
  876. pSceneObject->onInputEvent( inputEventLeaveName, event, worldMousePoint );
  877. // Remove scene object.
  878. mInputEventWatching.removeObject( pSceneObject );
  879. }
  880. // Process "enter" events.
  881. for ( U32 index = 0; index < (U32)mInputEventEntering.size(); ++index )
  882. {
  883. // Fetch scene object.
  884. SceneObject* pSceneObject = mInputEventEntering[index];
  885. // Emit event.
  886. pSceneObject->onInputEvent( inputEventEnterName, event, worldMousePoint );
  887. // Process "moved" or "dragged" events.
  888. if ( name == inputEventMovedName || name == inputEventDraggedName )
  889. pSceneObject->onInputEvent( name, event, worldMousePoint );
  890. // Add scene object.
  891. mInputEventWatching.addObject( pSceneObject );
  892. }
  893. // Clear input event vectors.
  894. mInputEventQuery.clear();
  895. mInputEventEntering.clear();
  896. mInputEventLeaving.clear();
  897. }
  898. //-----------------------------------------------------------------------------
  899. void SceneWindow::onMouseEnter( const GuiEvent& event )
  900. {
  901. // Dispatch input event.
  902. dispatchInputEvent(mouseEventEnterName, event);
  903. }
  904. //-----------------------------------------------------------------------------
  905. void SceneWindow::onMouseLeave( const GuiEvent& event )
  906. {
  907. // Dispatch input event.
  908. dispatchInputEvent(mouseEventLeaveName, event);
  909. }
  910. //-----------------------------------------------------------------------------
  911. void SceneWindow::onMouseDown( const GuiEvent& event )
  912. {
  913. // Lock Mouse (if necessary).
  914. if(mLockMouse)
  915. mouseLock();
  916. // Dispatch input event.
  917. dispatchInputEvent( inputEventDownName, event);
  918. }
  919. //-----------------------------------------------------------------------------
  920. void SceneWindow::onMouseUp( const GuiEvent& event )
  921. {
  922. // Lock Mouse (if necessary).
  923. if(mLockMouse)
  924. mouseUnlock();
  925. // Dispatch input event.
  926. dispatchInputEvent(inputEventUpName, event);
  927. }
  928. //-----------------------------------------------------------------------------
  929. void SceneWindow::onMouseMove( const GuiEvent& event )
  930. {
  931. // Dispatch input event.
  932. dispatchInputEvent(inputEventMovedName, event);
  933. }
  934. //-----------------------------------------------------------------------------
  935. void SceneWindow::onMouseDragged( const GuiEvent& event )
  936. {
  937. // Dispatch input event.
  938. dispatchInputEvent(inputEventDraggedName, event);
  939. }
  940. //-----------------------------------------------------------------------------
  941. void SceneWindow::onMiddleMouseDown( const GuiEvent& event )
  942. {
  943. // Lock Mouse (if necessary).
  944. if(mLockMouse)
  945. mouseLock();
  946. // Dispatch input event.
  947. dispatchInputEvent(mouseEventMiddleMouseDownName, event);
  948. }
  949. //-----------------------------------------------------------------------------
  950. void SceneWindow::onMiddleMouseUp( const GuiEvent& event )
  951. {
  952. // Lock Mouse (if necessary).
  953. if(mLockMouse)
  954. mouseUnlock();
  955. // Dispatch input event.
  956. dispatchInputEvent(mouseEventMiddleMouseUpName, event);
  957. }
  958. //-----------------------------------------------------------------------------
  959. void SceneWindow::onMiddleMouseDragged( const GuiEvent& event )
  960. {
  961. // Dispatch input event.
  962. dispatchInputEvent(mouseEventMiddleMouseDraggedName, event);
  963. }
  964. //-----------------------------------------------------------------------------
  965. void SceneWindow::onRightMouseDown( const GuiEvent& event )
  966. {
  967. // Lock Mouse (if necessary).
  968. if(mLockMouse)
  969. mouseLock();
  970. // Dispatch input event.
  971. dispatchInputEvent(mouseEventRightMouseDownName, event);
  972. }
  973. //-----------------------------------------------------------------------------
  974. void SceneWindow::onRightMouseUp( const GuiEvent& event )
  975. {
  976. // Lock Mouse (if necessary).
  977. if(mLockMouse)
  978. mouseUnlock();
  979. // Dispatch input event.
  980. dispatchInputEvent(mouseEventRightMouseUpName, event);
  981. }
  982. //-----------------------------------------------------------------------------
  983. void SceneWindow::onRightMouseDragged( const GuiEvent& event )
  984. {
  985. // Dispatch input event.
  986. dispatchInputEvent(mouseEventRightMouseDraggedName, event);
  987. }
  988. //-----------------------------------------------------------------------------
  989. bool SceneWindow::onMouseWheelUp( const GuiEvent& event )
  990. {
  991. // Call Parent.
  992. Parent::onMouseWheelUp( event );
  993. // Dispatch input event.
  994. dispatchInputEvent(mouseEventWheelUpName, event);
  995. // Return Success.
  996. return true;
  997. }
  998. //-----------------------------------------------------------------------------
  999. bool SceneWindow::onMouseWheelDown( const GuiEvent& event )
  1000. {
  1001. // Call Parent.
  1002. Parent::onMouseWheelDown( event );
  1003. // Dispatch input event.
  1004. dispatchInputEvent(mouseEventWheelDownName, event);
  1005. // Return Success.
  1006. return true;
  1007. }
  1008. //-----------------------------------------------------------------------------
  1009. void SceneWindow::calculateCameraMount( const F32 elapsedTime )
  1010. {
  1011. // Debug Profiling.
  1012. PROFILE_SCOPE(SceneWindow_CalculateCameraMount);
  1013. // Fetch Mount Position.
  1014. const Vector2& mountPos = mpMountedTo->getBody()->GetWorldPoint( mMountOffset );
  1015. // Set Pre-Tick Position.
  1016. mPreTickPosition = mPostTickPosition;
  1017. // Set Current Camera Position.
  1018. mCameraCurrent.mSourceArea.point = mPreTickPosition;
  1019. // Calculate Window Half-Dimensions.
  1020. const F32 halfWidth = mCameraCurrent.mSourceArea.len_x() * 0.5f;
  1021. const F32 halfHeight = mCameraCurrent.mSourceArea.len_y() * 0.5f;
  1022. // Calculate Target Position.
  1023. const Point2F targetPos = Point2F( mountPos.x - halfWidth, mountPos.y - halfHeight );
  1024. // Mount the angle?
  1025. if ( mMountAngle )
  1026. mCameraCurrent.mCameraAngle = -mpMountedTo->getAngle();
  1027. // Rigid Mount?
  1028. if ( mIsZero( mMountForce ) )
  1029. {
  1030. // Yes, so easy post-tick position.
  1031. mPostTickPosition = targetPos;
  1032. return;
  1033. }
  1034. // Calculate Time/Force Product.
  1035. const F32 timeForce = elapsedTime * mMountForce;
  1036. // Will we exceed our step?
  1037. if ( timeForce > 1.0f )
  1038. {
  1039. // Yes, so clamp at step.
  1040. mPostTickPosition = targetPos;
  1041. return;
  1042. }
  1043. else
  1044. {
  1045. // No, so calculate Direction to move.
  1046. const Point2F direction = (targetPos - mPreTickPosition) * timeForce;
  1047. // Calculate post-tick position.
  1048. mPostTickPosition = mPreTickPosition + direction;
  1049. return;
  1050. }
  1051. }
  1052. //-----------------------------------------------------------------------------
  1053. void SceneWindow::calculateCameraView( CameraView* pCameraView )
  1054. {
  1055. // Debug Profiling.
  1056. PROFILE_SCOPE(SceneWindow_CalculateCameraView);
  1057. // Calculate Zoom Reciprocal.
  1058. const F32 zoomRecip = pCameraView->mCameraZoom > 0.0f ? 1.0f / pCameraView->mCameraZoom : pCameraView->mCameraZoom;
  1059. // Calculate Zoom X/Y Factors.
  1060. const F32 zoomFactorX = (pCameraView->mSourceArea.len_x() - (pCameraView->mSourceArea.len_x() * zoomRecip))/2;
  1061. const F32 zoomFactorY = (pCameraView->mSourceArea.len_y() - (pCameraView->mSourceArea.len_y() * zoomRecip))/2;
  1062. // Fetch Camera View.
  1063. pCameraView->mDestinationArea = pCameraView->mSourceArea;
  1064. // Inset Window by Zoom Factor (if it's big enough to do so).
  1065. if ( pCameraView->mDestinationArea.extent.x > (zoomFactorX*2.0f) &&
  1066. pCameraView->mDestinationArea.extent.y > (zoomFactorY*2.0f) )
  1067. {
  1068. pCameraView->mDestinationArea.inset( zoomFactorX, zoomFactorY );
  1069. }
  1070. // Ensure we've got a valid window.
  1071. if ( !pCameraView->mDestinationArea.isValidRect() )
  1072. // Make it real!
  1073. pCameraView->mDestinationArea.extent = Point2F(1,1);
  1074. // Calculate Scene Min/Max.
  1075. pCameraView->mSceneMin.x = pCameraView->mDestinationArea.point.x;
  1076. pCameraView->mSceneMin.y = pCameraView->mDestinationArea.point.y;
  1077. pCameraView->mSceneMax.x = pCameraView->mDestinationArea.point.x + pCameraView->mDestinationArea.len_x();
  1078. pCameraView->mSceneMax.y = pCameraView->mDestinationArea.point.y + pCameraView->mDestinationArea.len_y();
  1079. // Is View Limit Active?
  1080. if ( mViewLimitActive )
  1081. {
  1082. // Yes, so is the limit area X less than the current view X?
  1083. if ( mViewLimitArea.x < pCameraView->mDestinationArea.len_x() )
  1084. {
  1085. // Yes, so calculate center of view.
  1086. const F32 viewCenterX = mViewLimitMin.x + ( mViewLimitArea.x * 0.5f );
  1087. // Half Camera Width.
  1088. const F32 halfCameraX = pCameraView->mDestinationArea.len_x() * 0.5f;
  1089. // Calculate Min/Max X.
  1090. pCameraView->mSceneMin.x = viewCenterX - halfCameraX;
  1091. pCameraView->mSceneMax.x = viewCenterX + halfCameraX;
  1092. }
  1093. else
  1094. {
  1095. // No, so calculate window min overlap.
  1096. const F32 windowMinOverlapX = getMax(0.0f, mViewLimitMin.x - pCameraView->mSceneMin.x);
  1097. // Calculate window max overlap.
  1098. const F32 windowMaxOverlapX = getMin(0.0f, mViewLimitMax.x - pCameraView->mSceneMax.x);
  1099. // Adjust Window.
  1100. pCameraView->mSceneMin.x += windowMinOverlapX + windowMaxOverlapX;
  1101. pCameraView->mSceneMax.x += windowMinOverlapX + windowMaxOverlapX;
  1102. }
  1103. // Is the limit area Y less than the current view Y?
  1104. if ( mViewLimitArea.y < pCameraView->mDestinationArea.len_y() )
  1105. {
  1106. // Yes, so calculate center of view.
  1107. const F32 viewCenterY = mViewLimitMin.y + ( mViewLimitArea.y * 0.5f );
  1108. // Half Camera Height.
  1109. const F32 halfCameraY = pCameraView->mDestinationArea.len_y() * 0.5f;
  1110. // Calculate Min/Max Y.
  1111. pCameraView->mSceneMin.y = viewCenterY - halfCameraY;
  1112. pCameraView->mSceneMax.y = viewCenterY + halfCameraY;
  1113. }
  1114. else
  1115. {
  1116. // No, so calculate window min overlap.
  1117. const F32 windowMinOverlapY = getMax(0.0f, mViewLimitMin.y - pCameraView->mSceneMin.y);
  1118. // Calculate window max overlap.
  1119. const F32 windowMaxOverlapY = getMin(0.0f, mViewLimitMax.y - pCameraView->mSceneMax.y);
  1120. // Adjust Window.
  1121. pCameraView->mSceneMin.y += windowMinOverlapY + windowMaxOverlapY;
  1122. pCameraView->mSceneMax.y += windowMinOverlapY + windowMaxOverlapY;
  1123. }
  1124. // Recalculate destination area.
  1125. pCameraView->mDestinationArea.point = pCameraView->mSceneMin;
  1126. pCameraView->mDestinationArea.extent = pCameraView->mSceneMax - pCameraView->mSceneMin;
  1127. // Inset Window by Zoom Factor (if it's big enough to do so).
  1128. if ( pCameraView->mDestinationArea.extent.x > (zoomFactorX*2.0f) &&
  1129. pCameraView->mDestinationArea.extent.y > (zoomFactorY*2.0f) )
  1130. {
  1131. pCameraView->mDestinationArea.inset( zoomFactorX, zoomFactorY );
  1132. }
  1133. }
  1134. // Calculate Scene Window Scale.
  1135. pCameraView->mSceneWindowScale.x = (pCameraView->mSceneMax.x - pCameraView->mSceneMin.x) / mBounds.len_x();
  1136. pCameraView->mSceneWindowScale.y = (pCameraView->mSceneMax.y - pCameraView->mSceneMin.y) / mBounds.len_y();
  1137. }
  1138. //-----------------------------------------------------------------------------
  1139. void SceneWindow::resize(const Point2I &newPosition, const Point2I &newExtent)
  1140. {
  1141. // Resize Parent.
  1142. Parent::resize( newPosition, newExtent);
  1143. // Argument Buffer.
  1144. char argBuffer[64];
  1145. // Format Buffer.
  1146. dSprintf( argBuffer, 64, "%d %d %d %d", newPosition.x, newPosition.y, newExtent.x, newExtent.y );
  1147. // Resize Callback.
  1148. Con::executef( this, 2, "onExtentChange", argBuffer );
  1149. }
  1150. //-----------------------------------------------------------------------------
  1151. void SceneWindow::processTick( void )
  1152. {
  1153. // Debug Profiling.
  1154. PROFILE_SCOPE(SceneWindow_ProcessTick);
  1155. // Are we moving the camera.
  1156. if ( mMovingCamera )
  1157. {
  1158. // Yes, so add Elapsed Time (scaled appropriately).
  1159. mCurrentCameraTime += Tickable::smTickSec;
  1160. // Update Tick Camera Time.
  1161. updateTickCameraTime();
  1162. // Update Camera.
  1163. updateCamera();
  1164. }
  1165. // Is the Camera Shaking?
  1166. if ( mCameraShaking )
  1167. {
  1168. // Reduce Shake Life.
  1169. mShakeLife -= Tickable::smTickSec;
  1170. // Is the Shake still active?
  1171. if ( mShakeLife > 0.0f )
  1172. {
  1173. // Calculate Current Shake.
  1174. mCurrentShake -= mShakeRamp * Tickable::smTickSec;
  1175. // Clamp Shake.
  1176. mCurrentShake = getMax(mCurrentShake, 0.0f);
  1177. // Calculate the Screen Shake-Ratio.
  1178. const Point2F shakeRatio( mCameraCurrent.mDestinationArea.len_x() / F32(mBounds.len_x()), mCameraCurrent.mDestinationArea.len_y() / F32(mBounds.len_y()) );
  1179. // Calculate the Camera Shake Magnitude based upon the Screen Shake-Ratio.
  1180. const F32 shakeMagnitudeX = mCurrentShake * shakeRatio.x * 0.5f;
  1181. const F32 shakeMagnitudeY = mCurrentShake * shakeRatio.y * 0.5f;
  1182. // Choose a random Shake.
  1183. mCameraShakeOffset.Set( CoreMath::mGetRandomF( -shakeMagnitudeX, shakeMagnitudeX ), CoreMath::mGetRandomF( -shakeMagnitudeY, shakeMagnitudeY ) );
  1184. }
  1185. else
  1186. {
  1187. // No, so stop shake.
  1188. stopCameraShake();
  1189. }
  1190. }
  1191. }
  1192. //-----------------------------------------------------------------------------
  1193. void SceneWindow::interpolateTick( F32 timeDelta )
  1194. {
  1195. // Are we moving the camera.
  1196. if ( mMovingCamera )
  1197. {
  1198. // Calculate Render Tick Position.
  1199. mRenderCameraTime = (mPreCameraTime * timeDelta) + ((1.0f-timeDelta) * mPostCameraTime);
  1200. // Update Camera.
  1201. updateCamera();
  1202. }
  1203. }
  1204. //-----------------------------------------------------------------------------
  1205. void SceneWindow::interpolateCameraMount( const F32 timeDelta )
  1206. {
  1207. // Camera Mounted?
  1208. if ( !isCameraMounted() ) return;
  1209. // Calculate position.
  1210. mCameraCurrent.mSourceArea.point = (mPreTickPosition * timeDelta) + ((1.0f-timeDelta) * mPostTickPosition);
  1211. }
  1212. //-----------------------------------------------------------------------------
  1213. void SceneWindow::zeroCameraTime( void )
  1214. {
  1215. // Reset Camera Time.
  1216. mRenderCameraTime = mPreCameraTime = mPostCameraTime = mCurrentCameraTime = 0.0f;
  1217. }
  1218. //-----------------------------------------------------------------------------
  1219. void SceneWindow::resetTickCameraTime( void )
  1220. {
  1221. // Reset Camera Time.
  1222. mRenderCameraTime = mPreCameraTime = mPostCameraTime = mCurrentCameraTime;
  1223. }
  1224. //-----------------------------------------------------------------------------
  1225. void SceneWindow::updateTickCameraTime( void )
  1226. {
  1227. // Store Pre Camera Time.
  1228. mPreCameraTime = mPostCameraTime;
  1229. // Store Current Camera Time.
  1230. mPostCameraTime = mCurrentCameraTime;
  1231. // Render Camera Time is at Pre-Tick Time.
  1232. mRenderCameraTime = mPreCameraTime;
  1233. }
  1234. //-----------------------------------------------------------------------------
  1235. void SceneWindow::resetTickCameraPosition( void )
  1236. {
  1237. mPreTickPosition = mPostTickPosition = mCameraCurrent.mSourceArea.point;
  1238. }
  1239. //-----------------------------------------------------------------------------
  1240. void SceneWindow::onRender( Point2I offset, const RectI& updateRect )
  1241. {
  1242. // Debug Profiling.
  1243. PROFILE_SCOPE(SceneWindow_onRender);
  1244. // Fetch scene.
  1245. Scene* pScene = getScene();
  1246. // Cannot render without scene!
  1247. if ( !pScene )
  1248. return;
  1249. // Calculate current camera View ( if needed ).
  1250. calculateCameraView( &mCameraCurrent );
  1251. // Fetch current camera.
  1252. const Point2F& sceneWindowScale = mCameraCurrent.mSceneWindowScale;
  1253. Point2F sceneMin = mCameraCurrent.mSceneMin;
  1254. Point2F sceneMax = mCameraCurrent.mSceneMax;
  1255. // Fetch bounds.
  1256. const RectI& bounds = getBounds();
  1257. const Point2I globalTopLeft( updateRect.point.x, updateRect.point.y );
  1258. const Point2I globalBottomRight( updateRect.point.x + updateRect.extent.x, updateRect.point.y + updateRect.extent.y );
  1259. const Point2I localTopLeft = globalToLocalCoord( globalTopLeft );
  1260. const Point2I localBottomRight = globalToLocalCoord( globalBottomRight );
  1261. // Clip top?
  1262. if ( localTopLeft.y > 0 )
  1263. {
  1264. sceneMax.y -= localTopLeft.y * sceneWindowScale.y;
  1265. }
  1266. // Clip left?
  1267. if ( localTopLeft.x > 0 )
  1268. {
  1269. sceneMin.x += localTopLeft.x * sceneWindowScale.x;
  1270. }
  1271. // Clip bottom?
  1272. if ( localBottomRight.y < bounds.extent.y )
  1273. {
  1274. sceneMin.y += (bounds.extent.y - localBottomRight.y ) * sceneWindowScale.y;
  1275. }
  1276. // Clip right?
  1277. if ( localBottomRight.x < bounds.extent.x )
  1278. {
  1279. sceneMax.x -= (bounds.extent.x - localBottomRight.x ) * sceneWindowScale.x;
  1280. }
  1281. // Add camera shake offset if active.
  1282. if ( mCameraShaking )
  1283. {
  1284. sceneMin += mCameraShakeOffset;
  1285. sceneMax += mCameraShakeOffset;
  1286. }
  1287. // Setup new logical coordinate system.
  1288. glMatrixMode(GL_PROJECTION);
  1289. glPushMatrix();
  1290. glLoadIdentity();
  1291. // Set orthographic projection.
  1292. glOrtho( sceneMin.x, sceneMax.x, sceneMin.y, sceneMax.y, 0.0f, MAX_LAYERS_SUPPORTED );
  1293. // Set ModelView.
  1294. glMatrixMode(GL_MODELVIEW);
  1295. glPushMatrix();
  1296. glLoadIdentity();
  1297. // Disable Alpha Test by default
  1298. glDisable( GL_ALPHA_TEST );
  1299. glDisable( GL_DEPTH_TEST );
  1300. // Get Debug Stats.
  1301. DebugStats& debugStats = pScene->getDebugStats();
  1302. // Create a scene render state.
  1303. SceneRenderState sceneRenderState(
  1304. mCameraCurrent.mDestinationArea,
  1305. mCameraCurrent.mDestinationArea.centre(),
  1306. mCameraCurrent.mCameraAngle,
  1307. mRenderLayerMask,
  1308. mRenderGroupMask,
  1309. Vector2( mCameraCurrent.mSceneWindowScale ),
  1310. &debugStats,
  1311. this );
  1312. // Clear the background color if requested.
  1313. if ( mUseBackgroundColor )
  1314. {
  1315. // Enable the scissor.
  1316. const RectI& clipRect = dglGetClipRect();
  1317. glEnable(GL_SCISSOR_TEST );
  1318. glScissor( clipRect.point.x, Platform::getWindowSize().y - (clipRect.point.y + clipRect.extent.y), clipRect.len_x(), clipRect.len_y() );
  1319. // Clear the background.
  1320. glClearColor( mBackgroundColor.red, mBackgroundColor.green, mBackgroundColor.blue, mBackgroundColor.alpha );
  1321. glClear(GL_COLOR_BUFFER_BIT);
  1322. // Disable the scissor.
  1323. glDisable( GL_SCISSOR_TEST );
  1324. }
  1325. // Render View.
  1326. pScene->sceneRender( &sceneRenderState );
  1327. // Restore Matrices.
  1328. glMatrixMode(GL_MODELVIEW);
  1329. glPopMatrix();
  1330. glMatrixMode(GL_PROJECTION);
  1331. glPopMatrix();
  1332. glMatrixMode(GL_MODELVIEW);
  1333. // Render the metrics.
  1334. renderMetricsOverlay( offset, updateRect );
  1335. // Render Children.
  1336. renderChildControls( offset, updateRect );
  1337. // Update Window.
  1338. setUpdate();
  1339. }
  1340. //------------------------------------------------------------------------------
  1341. void SceneWindow::renderMetricsOverlay( Point2I offset, const RectI& updateRect )
  1342. {
  1343. // Debug Profiling.
  1344. PROFILE_SCOPE(SceneWindow_RenderMetricsOverlay);
  1345. // Fetch scene.
  1346. Scene* pScene = getScene();
  1347. #if 0
  1348. // Force on debug rendering.
  1349. pScene->setDebugOn( 0xFFFFFFFF );
  1350. #endif
  1351. // Fetch full metrics mode.
  1352. const bool fullMetrics = pScene->getDebugMask() & Scene::SCENE_DEBUG_METRICS;
  1353. const bool fpsMetrics = pScene->getDebugMask() & Scene::SCENE_DEBUG_FPS_METRICS;
  1354. // Finish if should not or cannot render.
  1355. if ( ( !fullMetrics && !fpsMetrics ) ||
  1356. mProfile == NULL ||
  1357. mProfile->mFont.isNull() )
  1358. return;
  1359. // Fetch the font.
  1360. Resource<GFont>& font = mProfile->mFont;
  1361. // Blending for banner background.
  1362. glEnable ( GL_BLEND );
  1363. glBlendFunc ( GL_SRC_ALPHA , GL_ONE_MINUS_SRC_ALPHA );
  1364. // Set banner background color.
  1365. const ColorI& fillColor = mProfile->mFillColor;
  1366. const F32 colorScale = 1.0f / 255.0f;
  1367. glColor4f( fillColor.red * colorScale, fillColor.green * colorScale, fillColor.blue * colorScale, fillColor.alpha * colorScale );
  1368. // Fetch debug scene object.
  1369. SceneObject* pDebugSceneObject = pScene->getDebugSceneObject();
  1370. // Get Debug Stats.
  1371. DebugStats& debugStats = pScene->getDebugStats();
  1372. // Set metrics offset
  1373. const S32 metricsOffset = (S32)font->getStrWidth( "WWWWWWWWWWWW" );
  1374. // Set Banner Height.
  1375. F32 bannerLineHeight = fullMetrics ? 17.0f : 1.0f;
  1376. // Add an extra line if we're monitoring a scene object.
  1377. if ( pDebugSceneObject != NULL )
  1378. bannerLineHeight += 5.0f;
  1379. U32 bannerHeight = (U32)((bannerLineHeight * (F32)font->getHeight()));
  1380. // Calculate Debug Banner Offset.
  1381. Point2I bannerOffset = updateRect.point + Point2I(8,8);
  1382. // Draw Banner Background.
  1383. glBegin(GL_TRIANGLE_STRIP);
  1384. glVertex2i( updateRect.point.x, updateRect.point.y );
  1385. glVertex2i( updateRect.point.x + updateRect.extent.x, updateRect.point.y );
  1386. glVertex2i( updateRect.point.x, updateRect.point.y + bannerHeight + 16);
  1387. glVertex2i( updateRect.point.x + updateRect.extent.x, updateRect.point.y + bannerHeight + 16);
  1388. glEnd();
  1389. // Disable Banner Blending.
  1390. glDisable ( GL_BLEND );
  1391. // Set Debug Text color.
  1392. dglSetBitmapModulation( mProfile->mFontColor );
  1393. // ****************************************************************
  1394. // Draw Banner Text.
  1395. // ****************************************************************
  1396. F32 linePositionY = 0.25f;
  1397. F32 linePositionOffsetY = font->getHeight() * 1.25f;
  1398. if ( fullMetrics )
  1399. {
  1400. // Rendering.
  1401. dglDrawText( font, bannerOffset + Point2I(0,(S32)linePositionY), "Render", NULL );
  1402. dSprintf( mDebugText, sizeof( mDebugText ), "- FPS=%4.1f<%4.1f/%4.1f>, Frames=%u, Picked=%d<%d>, RenderRequests=%d<%d>, RenderFallbacks=%d<%d>",
  1403. debugStats.fps, debugStats.minFPS, debugStats.maxFPS,
  1404. debugStats.frameCount,
  1405. debugStats.renderPicked, debugStats.maxRenderPicked,
  1406. debugStats.renderRequests, debugStats.maxRenderRequests,
  1407. debugStats.renderFallbacks, debugStats.maxRenderFallbacks );
  1408. dglDrawText( font, bannerOffset + Point2I(metricsOffset,(S32)linePositionY), mDebugText, NULL );
  1409. linePositionY += linePositionOffsetY;
  1410. // Scene.
  1411. dglDrawText( font, bannerOffset + Point2I(0,(S32)linePositionY), "Scene", NULL );
  1412. 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",
  1413. Scene::getGlobalSceneCount(), pScene->getSceneIndex(),
  1414. pScene->getSceneTime(),
  1415. debugStats.objectsCount, debugStats.maxObjectsCount, SceneObject::getGlobalSceneObjectCount(),
  1416. debugStats.objectsEnabled, debugStats.maxObjectsEnabled,
  1417. debugStats.objectsVisible, debugStats.maxObjectsVisible,
  1418. debugStats.objectsAwake, debugStats.maxObjectsAwake,
  1419. pScene->getControllers() == NULL ? 0 : pScene->getControllers()->size() );
  1420. dglDrawText( font, bannerOffset + Point2I(metricsOffset,(S32)linePositionY), mDebugText, NULL );
  1421. linePositionY += linePositionOffsetY;
  1422. // Camera Window #1.
  1423. dglDrawText( font, bannerOffset + Point2I(0,(S32)linePositionY), "Camera", NULL );
  1424. Vector2 cameraPosition = getCameraPosition();
  1425. 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)",
  1426. cameraPosition.x,
  1427. cameraPosition.y,
  1428. mCameraCurrent.mSourceArea.extent.x,
  1429. mCameraCurrent.mSourceArea.extent.y,
  1430. mCameraCurrent.mCameraZoom,
  1431. mRadToDeg(mCameraCurrent.mCameraAngle),
  1432. mCameraCurrent.mSourceArea.point.x,
  1433. mCameraCurrent.mSourceArea.point.y,
  1434. mCameraCurrent.mSourceArea.point.x + mCameraCurrent.mSourceArea.extent.x,
  1435. mCameraCurrent.mSourceArea.point.y + mCameraCurrent.mSourceArea.extent.y );
  1436. dglDrawText( font, bannerOffset + Point2I(metricsOffset,(S32)linePositionY), mDebugText, NULL );
  1437. linePositionY += linePositionOffsetY;
  1438. // Camera Window #2.
  1439. Point2I windowExtent = getExtent();
  1440. Vector2 windowScale = getCameraWindowScale();
  1441. dSprintf( mDebugText, sizeof( mDebugText ), "- Window=(%d,%d), WorldScale=(%0.3f,%0.3f), RenderScale=(%0.3f,%0.3f)",
  1442. windowExtent.x, windowExtent.y,
  1443. windowScale.x, windowScale.y,
  1444. 1.0f / windowScale.x, 1.0f / windowScale.y);
  1445. dglDrawText( font, bannerOffset + Point2I(metricsOffset,(S32)linePositionY), mDebugText, NULL );
  1446. linePositionY += linePositionOffsetY;
  1447. // Batching #1.
  1448. dglDrawText( font, bannerOffset + Point2I(0,(S32)linePositionY), "Batching", NULL );
  1449. dSprintf( mDebugText, sizeof( mDebugText ), "- %sTris=%d<%d>, MaxTriDraw=%d, MaxVerts=%d, Strict=%d<%d>, Sorted=%d<%d>",
  1450. pScene->getBatchingEnabled() ? "" : "(OFF) ",
  1451. debugStats.batchTrianglesSubmitted, debugStats.maxBatchTrianglesSubmitted,
  1452. debugStats.batchMaxTriangleDrawn,
  1453. debugStats.batchMaxVertexBuffer,
  1454. debugStats.batchDrawCallsStrict, debugStats.maxBatchDrawCallsStrict,
  1455. debugStats.batchDrawCallsSorted, debugStats.maxBatchDrawCallsSorted
  1456. );
  1457. dglDrawText( font, bannerOffset + Point2I(metricsOffset,(S32)linePositionY), mDebugText, NULL );
  1458. linePositionY += linePositionOffsetY;
  1459. // Batching #2.
  1460. dSprintf( mDebugText, sizeof( mDebugText ), "- Flush=%d<%d>, BlendFlush=%d<%d>, ColorFlush=%d<%d>, AlphaFlush=%d<%d>, TexFlush=%d<%d>",
  1461. debugStats.batchFlushes, debugStats.maxBatchFlushes,
  1462. debugStats.batchBlendStateFlush, debugStats.maxBatchBlendStateFlush,
  1463. debugStats.batchColorStateFlush, debugStats.maxBatchColorStateFlush,
  1464. debugStats.batchAlphaStateFlush, debugStats.maxBatchAlphaStateFlush,
  1465. debugStats.batchTextureChangeFlush, debugStats.maxBatchTextureChangeFlushes
  1466. );
  1467. dglDrawText( font, bannerOffset + Point2I(metricsOffset,(S32)linePositionY), mDebugText, NULL );
  1468. linePositionY += linePositionOffsetY;
  1469. // Batching #3.
  1470. dSprintf( mDebugText, sizeof( mDebugText ), "- IsolatedFlush=%d<%d>, FullFlush=%d<%d>, LayerFlush=%d<%d>, NoBatchFlush=%d<%d>, AnonFlush=%d<%d>",
  1471. debugStats.batchIsolatedFlush, debugStats.maxBatchIsolatedFlush,
  1472. debugStats.batchBufferFullFlush, debugStats.maxBatchBufferFullFlush,
  1473. debugStats.batchLayerFlush, debugStats.maxBatchLayerFlush,
  1474. debugStats.batchNoBatchFlush, debugStats.maxBatchNoBatchFlush,
  1475. debugStats.batchAnonymousFlush, debugStats.maxBatchAnonymousFlush
  1476. );
  1477. dglDrawText( font, bannerOffset + Point2I(metricsOffset,(S32)linePositionY), mDebugText, NULL );
  1478. linePositionY += linePositionOffsetY;
  1479. // Textures.
  1480. dglDrawText( font, bannerOffset + Point2I(0,(S32)linePositionY), "Textures", NULL );
  1481. dSprintf( mDebugText, sizeof( mDebugText ), "- TextureCount=%d, TextureSize=%d, TextureWaste=%d, BitmapSize=%d",
  1482. TextureManager::getTextureResidentCount(),
  1483. TextureManager::getTextureResidentSize(),
  1484. TextureManager::getTextureResidentWasteSize(),
  1485. TextureManager::getBitmapResidentSize()
  1486. );
  1487. dglDrawText( font, bannerOffset + Point2I(metricsOffset,(S32)linePositionY), mDebugText, NULL );
  1488. linePositionY += linePositionOffsetY;
  1489. // Physics.
  1490. dglDrawText( font, bannerOffset + Point2I(0,(S32)linePositionY), "Physics", NULL );
  1491. dSprintf( mDebugText, sizeof( mDebugText ), "- Bodies=%d<%d>, Joints=%d<%d>, Contacts=%d<%d>, Proxies=%d<%d>",
  1492. debugStats.bodyCount, debugStats.maxBodyCount,
  1493. debugStats.jointCount, debugStats.maxJointCount,
  1494. debugStats.contactCount, debugStats.maxContactCount,
  1495. debugStats.proxyCount, debugStats.maxProxyCount );
  1496. dglDrawText( font, bannerOffset + Point2I(metricsOffset,(S32)linePositionY), mDebugText, NULL );
  1497. linePositionY += linePositionOffsetY;
  1498. const b2Profile& worldProfile = debugStats.worldProfile;
  1499. const b2Profile& maxWorldProfile = debugStats.maxWorldProfile;
  1500. // Physics timings #1.
  1501. dglDrawText( font, bannerOffset + Point2I(0,(S32)linePositionY), "Timings", NULL );
  1502. dSprintf( mDebugText, sizeof( mDebugText ), "- Step=%0.0f<%0.0f>, Collide=%0.0f<%0.0f>, BroadPhase=%0.0f<%0.0f>",
  1503. worldProfile.step, maxWorldProfile.step,
  1504. worldProfile.collide, maxWorldProfile.collide,
  1505. worldProfile.broadphase, maxWorldProfile.broadphase );
  1506. dglDrawText( font, bannerOffset + Point2I(metricsOffset,(S32)linePositionY), mDebugText, NULL );
  1507. linePositionY += linePositionOffsetY;
  1508. // Physics timings #2.
  1509. 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>",
  1510. worldProfile.solve, maxWorldProfile.solve,
  1511. worldProfile.solveInit, maxWorldProfile.solveInit,
  1512. worldProfile.solveVelocity, maxWorldProfile.solveVelocity,
  1513. worldProfile.solvePosition, maxWorldProfile.solvePosition,
  1514. worldProfile.solveTOI, maxWorldProfile.solveTOI );
  1515. dglDrawText( font, bannerOffset + Point2I(metricsOffset,(S32)linePositionY), mDebugText, NULL );
  1516. linePositionY += linePositionOffsetY;
  1517. // Physics spatial tree.
  1518. dglDrawText( font, bannerOffset + Point2I(0,(S32)linePositionY), "Partition", NULL );
  1519. const b2World* pWorld = pScene->getWorld();
  1520. const S32 treeBalance = pWorld->GetTreeBalance();
  1521. const S32 treeHeight = pWorld->GetTreeHeight();
  1522. const F32 treeQuality = pWorld->GetTreeQuality();
  1523. dSprintf( mDebugText, sizeof( mDebugText ), "- Balance=%d, Height=%d, Quality=%0.2f",
  1524. treeBalance,
  1525. treeHeight,
  1526. treeQuality );
  1527. dglDrawText( font, bannerOffset + Point2I(metricsOffset,(S32)linePositionY), mDebugText, NULL );
  1528. linePositionY += linePositionOffsetY;
  1529. // Particles.
  1530. dglDrawText( font, bannerOffset + Point2I(0,(S32)linePositionY), "Particles", NULL );
  1531. dSprintf( mDebugText, sizeof( mDebugText ), "- Allocated=%d, Used=%d<%d>, Free=%d",
  1532. debugStats.particlesAlloc,
  1533. debugStats.particlesUsed, debugStats.maxParticlesUsed,
  1534. debugStats.particlesFree );
  1535. dglDrawText( font, bannerOffset + Point2I(metricsOffset,(S32)linePositionY), mDebugText, NULL );
  1536. linePositionY += linePositionOffsetY;
  1537. // Asset Manager.
  1538. dglDrawText( font, bannerOffset + Point2I(0,(S32)linePositionY), "Assets", NULL );
  1539. dSprintf( mDebugText, sizeof( mDebugText ), "- AcquiredRefs=%d, Declared=%d, Referenced=%d, LoadedInternal=%d<%d>, LoadedExternal=%d<%d>, LoadedPrivate=%d<%d>",
  1540. AssetDatabase.getAcquiredReferenceCount(),
  1541. AssetDatabase.getDeclaredAssetCount(),
  1542. AssetDatabase.getReferencedAssetCount(),
  1543. AssetDatabase.getLoadedInternalAssetCount(), AssetDatabase.getMaxLoadedInternalAssetCount(),
  1544. AssetDatabase.getLoadedExternalAssetCount(), AssetDatabase.getMaxLoadedExternalAssetCount(),
  1545. AssetDatabase.getLoadedPrivateAssetCount(), AssetDatabase.getMaxLoadedPrivateAssetCount() );
  1546. dglDrawText( font, bannerOffset + Point2I(metricsOffset,(S32)linePositionY), mDebugText, NULL );
  1547. linePositionY += linePositionOffsetY;
  1548. }
  1549. else if ( fpsMetrics )
  1550. {
  1551. // Rendering.
  1552. dSprintf( mDebugText, sizeof( mDebugText ), "FPS=%4.1f<%4.1f/%4.1f>",
  1553. debugStats.fps, debugStats.minFPS, debugStats.maxFPS );
  1554. dglDrawText( font, bannerOffset + Point2I(0,(S32)linePositionY), mDebugText, NULL );
  1555. linePositionY += linePositionOffsetY;
  1556. }
  1557. // Monitored scene object.
  1558. if ( pDebugSceneObject != NULL )
  1559. {
  1560. // SceneObject #1.
  1561. dglDrawText( font, bannerOffset + Point2I(0,(S32)linePositionY), "SceneObject", NULL );
  1562. const b2Vec2 position = pDebugSceneObject->getRenderPosition();
  1563. const F32 angle = mRadToDeg( pDebugSceneObject->getRenderAngle() );
  1564. const Vector2 size = pDebugSceneObject->getSize();
  1565. const U32 sceneLayer = pDebugSceneObject->getSceneLayer();
  1566. const U32 sceneGroup = pDebugSceneObject->getSceneGroup();
  1567. const U32 serialId = pDebugSceneObject->getSerialId();
  1568. StringTableEntry renderGroup = pDebugSceneObject->getRenderGroup();
  1569. 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'",
  1570. pDebugSceneObject->getId(),
  1571. position.x, position.y,
  1572. angle,
  1573. size.x, size.y,
  1574. sceneLayer,
  1575. sceneGroup,
  1576. serialId,
  1577. renderGroup
  1578. );
  1579. dglDrawText( font, bannerOffset + Point2I(metricsOffset,(S32)linePositionY), mDebugText, NULL );
  1580. linePositionY += linePositionOffsetY;
  1581. // SceneObject #2.
  1582. const char* pBodyType = SceneObject::getBodyTypeDescription(pDebugSceneObject->getBodyType());
  1583. const bool enabled = pDebugSceneObject->isEnabled();
  1584. const bool active = pDebugSceneObject->getActive();
  1585. const bool visible = pDebugSceneObject->getVisible();
  1586. const bool isBullet = pDebugSceneObject->getBullet();
  1587. const bool awake = pDebugSceneObject->getAwake();
  1588. const bool sleepingAllowed = pDebugSceneObject->getSleepingAllowed();
  1589. dSprintf( mDebugText, sizeof( mDebugText ), "- Body=%s, Enabled=%d, Active=%d, Visible=%d, Bullet=%d, Awake=%d, CanSleep=%d",
  1590. pBodyType,
  1591. enabled,
  1592. active,
  1593. visible,
  1594. isBullet,
  1595. awake,
  1596. sleepingAllowed
  1597. );
  1598. dglDrawText( font, bannerOffset + Point2I(metricsOffset,(S32)linePositionY), mDebugText, NULL );
  1599. linePositionY += linePositionOffsetY;
  1600. // SceneObject #3.
  1601. const Vector2 linearVelocity = pDebugSceneObject->getLinearVelocity();
  1602. const F32 angularVelocity = pDebugSceneObject->getAngularVelocity();
  1603. const F32 linearDamping = pDebugSceneObject->getLinearDamping();
  1604. const F32 angularDamping = pDebugSceneObject->getAngularDamping();
  1605. const Vector2 localCenter = pDebugSceneObject->getLocalCenter();
  1606. dSprintf( mDebugText, sizeof( mDebugText ), "- LinVel=(%0.3f,%0.3f), AngVel=%0.3f, LinDamp=%0.3f, AngDamp=%0.3f, Com=(%0.3f,%0.3f)",
  1607. linearVelocity.x, linearVelocity.y,
  1608. angularVelocity,
  1609. linearDamping,
  1610. angularDamping,
  1611. localCenter.x, localCenter.y
  1612. );
  1613. dglDrawText( font, bannerOffset + Point2I(metricsOffset,(S32)linePositionY), mDebugText, NULL );
  1614. linePositionY += linePositionOffsetY;
  1615. // SceneObject #4.
  1616. const bool collisionSuppress = pDebugSceneObject->getCollisionSuppress();
  1617. const U32 collisionLayerMask = pDebugSceneObject->getCollisionLayerMask();
  1618. const U32 collisionGroupMask = pDebugSceneObject->getCollisionGroupMask();
  1619. const U32 collisionShapeCount = pDebugSceneObject->getCollisionShapeCount();
  1620. dSprintf( mDebugText, sizeof( mDebugText ), "- Shapes=%d, ColSuppress=%d, ColLayer=%0.8x, ColGroup=%0.8x",
  1621. collisionShapeCount,
  1622. collisionSuppress,
  1623. collisionLayerMask,
  1624. collisionGroupMask
  1625. );
  1626. dglDrawText( font, bannerOffset + Point2I(metricsOffset,(S32)linePositionY), mDebugText, NULL );
  1627. }
  1628. // Clear Bitmap Modulation.
  1629. dglClearBitmapModulation();
  1630. }