SceneWindow.cc 72 KB

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