SceneWindow.cc 73 KB

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