SceneWindow.cc 73 KB

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