PageRenderTime 1020ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/src/away3d/debug/AwayStats.as

https://github.com/davidcoleman007/AS3MazeSolver
ActionScript | 867 lines | 544 code | 169 blank | 154 comment | 47 complexity | b0f1f86e61c47a89913e79630e1031c1 MD5 | raw file
  1. package away3d.debug
  2. {
  3. import away3d.containers.View3D;
  4. import flash.display.BitmapData;
  5. import flash.display.CapsStyle;
  6. import flash.display.Graphics;
  7. import flash.display.LineScaleMode;
  8. import flash.display.Shape;
  9. import flash.display.Sprite;
  10. import flash.events.Event;
  11. import flash.events.MouseEvent;
  12. import flash.system.System;
  13. import flash.text.TextField;
  14. import flash.text.TextFieldAutoSize;
  15. import flash.text.TextFormat;
  16. import flash.utils.Timer;
  17. import flash.utils.getTimer;
  18. /**
  19. * <p>Stats monitor for Away3D or general use in any project. The widget was designed to
  20. * display all the necessary data in ways that are easily readable, while maintaining a
  21. * tiny size.</p>
  22. *
  23. * <p>The following data is displayed by the widget, either graphically, through
  24. * text, or both.</p>
  25. * <ul>
  26. * <li>Current frame rate in FPS (white in graph/bar)</li>
  27. * <li>SWF frame rate (Stage.frameRate)</li>
  28. * <li>Average FPS (blue in graph/bar)</li>
  29. * <li>Min/Max FPS (only on frame rate bar in minimized mode)</li>
  30. * <li>Current RAM usage (pink in graph)</li>
  31. * <li>Maximum RAM usage</li>
  32. * <li>Number of polygons in scene</li>
  33. * <li>Number of polygons last rendered (yellow in graph)</li>
  34. * </ul>
  35. *
  36. * <p>There are two display modes; standard and minimized, which are alternated by clicking
  37. * the button in the upper right corner, at runtime. The widget can also be configured to
  38. * start in minimized mode by setting the relevant constructor parameter.</p>
  39. *
  40. * <p>All data can be reset at any time, by clicking the lower part of the widget (where
  41. * the RAM and POLY counters are located. The average FPS can be reset separately by
  42. * clicking it's displayed value. Furthermore, the stage frame rate can be increased or
  43. * decreased by clicking the upper and lower parts of the graph, respectively. Clicking close
  44. * to the center will increment in small values, and further away will increase the steps.
  45. * The graph itself is only visible in standard (as opposed to minimized) display mode.</p>
  46. *
  47. * <p>The average FPS is calculated using one of two methods, configurable via constructor
  48. * parameters. By setting the meanDataLength to a non-zero value, the number of recorded
  49. * frame rate values on which the average is based can be configured. This has a tiny
  50. * impact on CPU usage, which is the reason why the default number is zero, denoting that
  51. * the average is calculated from a running sum since the widget was last reset.</p>
  52. */
  53. public class AwayStats extends Sprite
  54. {
  55. private var _views : Vector.<View3D>;
  56. private var _timer : Timer;
  57. private var _last_frame_timestamp : Number;
  58. private var _fps : uint;
  59. private var _ram : Number;
  60. private var _max_ram : Number;
  61. private var _min_fps : uint;
  62. private var _avg_fps : Number;
  63. private var _max_fps : uint;
  64. private var _tfaces : uint;
  65. private var _rfaces : uint;
  66. private var _num_frames : uint;
  67. private var _fps_sum : uint;
  68. private var _top_bar : Sprite;
  69. private var _btm_bar : Sprite;
  70. private var _btm_bar_hit : Sprite;
  71. private var _data_format : TextFormat;
  72. private var _label_format : TextFormat;
  73. private var _fps_bar : Shape;
  74. private var _afps_bar : Shape;
  75. private var _lfps_bar : Shape;
  76. private var _hfps_bar : Shape;
  77. private var _diagram : Sprite;
  78. private var _dia_bmp : BitmapData;
  79. private var _mem_points : Array;
  80. private var _mem_graph : Shape;
  81. private var _updates : int;
  82. private var _min_max_btn : Sprite;
  83. private var _fps_tf : TextField;
  84. private var _afps_tf : TextField;
  85. private var _ram_tf : TextField;
  86. private var _poly_tf : TextField;
  87. private var _drag_dx : Number;
  88. private var _drag_dy : Number;
  89. private var _dragging : Boolean;
  90. private var _mean_data : Array;
  91. private var _mean_data_length : int;
  92. private var _enable_reset : Boolean;
  93. private var _enable_mod_fr : Boolean;
  94. private var _transparent : Boolean;
  95. private var _minimized : Boolean;
  96. private static const _WIDTH : Number = 125;
  97. private static const _MAX_HEIGHT : Number = 75;
  98. private static const _MIN_HEIGHT : Number = 41;
  99. private static const _UPPER_Y : Number = -1;
  100. private static const _LOWER_Y : Number = 9;
  101. private static const _POLY_COL : uint = 0xffcc00;
  102. private static const _MEM_COL : uint = 0xff00cc;
  103. // Singleton instance reference
  104. private static var _INSTANCE : AwayStats;
  105. /**
  106. * <p>Create an Away3D stats widget. The widget can be added to the stage
  107. * and positioned like any other display object. Once on the stage, you
  108. * can drag the widget to re-position it at runtime.</p>
  109. *
  110. * <p>If you pass a View3D instance, the widget will be able to display
  111. * the total number of faces in your scene, and the amount of faces that
  112. * were rendered during the last render() call. Views can also be registered
  113. * after construction using the registerView() method. Omit the view
  114. * constructor parameter to disable this feature altogether.</p>
  115. *
  116. * @param view A reference to your Away3D view. This is required if you
  117. * want the stats widget to display polycounts.
  118. *
  119. * @param minimized Defines whether the widget should start up in minimized
  120. * mode. By default, it is shown in full-size mode on launch.
  121. *
  122. * @param transparent Defines whether to omit the background plate and print
  123. * statistics directly on top of the underlying stage.
  124. *
  125. * @param meanDataLength The number of frames on which to base the average
  126. * frame rate calculation. The default value of zero indicates that all
  127. * frames since the last reset will be used.
  128. *
  129. * @param enableClickToReset Enables interaction allowing you to reset all
  130. * counters by clicking the bottom bar of the widget. When activated, you
  131. * can also click the average frame rate trace-out to reset just that one
  132. * value.
  133. *
  134. * @param enableModifyFramerate When enabled, allows you to click the upper
  135. * and lower parts of the graph area to increase and decrease SWF frame rate
  136. * respectively.
  137. */
  138. public function AwayStats(view3d : View3D = null, minimized : Boolean = false, transparent : Boolean = false, meanDataLength : uint = 0, enableClickToReset : Boolean = true, enableModifyFrameRate : Boolean = true)
  139. {
  140. super();
  141. _minimized = minimized;
  142. _transparent = transparent;
  143. _enable_reset = enableClickToReset;
  144. _enable_mod_fr = enableModifyFrameRate;
  145. _mean_data_length = meanDataLength;
  146. _views = new Vector.<View3D>();
  147. if (view3d)
  148. _views.push(view3d);
  149. // Store instance for singleton access. Singleton status
  150. // is not enforced, since the widget will work anyway.
  151. if (_INSTANCE) {
  152. trace('Creating several statistics windows in one project. Is this intentional?');
  153. }
  154. _INSTANCE = this;
  155. _fps = 0;
  156. _num_frames = 0;
  157. _avg_fps = 0;
  158. _ram = 0;
  159. _max_ram = 0;
  160. _tfaces = 0;
  161. _rfaces = 0;
  162. _init();
  163. }
  164. public function get fps():int
  165. {
  166. return _fps
  167. }
  168. private function _init() : void
  169. {
  170. _initMisc();
  171. _initTopBar();
  172. _initBottomBar();
  173. _initDiagrams();
  174. _initInteraction();
  175. _reset();
  176. _redrawWindow();
  177. addEventListener(Event.ADDED_TO_STAGE, _onAddedToStage);
  178. addEventListener(Event.REMOVED_FROM_STAGE, _onRemovedFromStage);
  179. }
  180. /**
  181. * Holds a reference to the stats widget (or if several have been created
  182. * during session, the one that was last instantiated.) Allows you to set
  183. * properties and register views from anywhere in your code.
  184. */
  185. public static function get instance() : AwayStats
  186. {
  187. return _INSTANCE;
  188. }
  189. /**
  190. * Add a view to the list of those that are taken into account when
  191. * calculating on-screen and total poly counts. Use this method when the
  192. * stats widget is not instantiated in the same place as where you create
  193. * your view, or when using several views, or when views are created and
  194. * destroyed dynamically at runtime.
  195. */
  196. public function registerView(view3d : View3D) : void
  197. {
  198. if (view3d && _views.indexOf(view3d)<0)
  199. _views.push(view3d);
  200. }
  201. /**
  202. * Remove a view from the list of those that are taken into account when
  203. * calculating on-screen and total poly counts. If the supplied view is
  204. * the only one known to the stats widget, calling this will leave the
  205. * list empty, disabling poly count statistics altogether.
  206. */
  207. public function unregisterView(view3d : View3D) : void
  208. {
  209. if (view3d) {
  210. var idx : int = _views.indexOf(view3d);
  211. if (idx >= 0)
  212. _views.splice(idx, 1);
  213. }
  214. }
  215. private function _initMisc() : void
  216. {
  217. _timer = new Timer(200, 0);
  218. _timer.addEventListener('timer', _onTimer);
  219. _label_format = new TextFormat('_sans', 9, 0xffffff, true);
  220. _data_format = new TextFormat('_sans', 9, 0xffffff, false);
  221. if (_mean_data_length>0) {
  222. var i : int;
  223. _mean_data = [];
  224. for (i=0; i<_mean_data_length;i++) {
  225. _mean_data[i] = 0.0;
  226. }
  227. }
  228. }
  229. /**
  230. * @private
  231. * Draw logo and create title textfield.
  232. */
  233. private function _initTopBar() : void
  234. {
  235. var logo : Shape;
  236. var markers : Shape;
  237. //var logo_tf : TextField;
  238. var fps_label_tf : TextField;
  239. var afps_label_tf : TextField;
  240. _top_bar = new Sprite;
  241. _top_bar.graphics.beginFill(0, 0);
  242. _top_bar.graphics.drawRect(0, 0, _WIDTH, 20);
  243. addChild(_top_bar);
  244. logo = new Shape;
  245. logo.x = 9;
  246. logo.y = 7.5;
  247. logo.scaleX = 0.6;
  248. logo.scaleY = 0.6;
  249. logo.graphics.beginFill(0xffffff, 1);
  250. // Left
  251. logo.graphics.moveTo(-0.5, -7);
  252. logo.graphics.curveTo(-0.5, -7.7, -1, -7);
  253. logo.graphics.lineTo(-9, 5);
  254. logo.graphics.curveTo(-9.3, 5.5, -8, 5);
  255. logo.graphics.curveTo(-1, 1, -0.5, -7);
  256. // Right
  257. logo.graphics.moveTo(0.5, -7);
  258. logo.graphics.curveTo(0.5, -7.7, 1, -7);
  259. logo.graphics.lineTo(9, 5);
  260. logo.graphics.curveTo(9.3, 5.5, 8, 5);
  261. logo.graphics.curveTo(1, 1, 0.5, -7);
  262. // Bottom
  263. logo.graphics.moveTo(-8, 7);
  264. logo.graphics.curveTo(-8.3, 6.7, -7.5, 6.3);
  265. logo.graphics.curveTo(0, 2, 7.5, 6.3);
  266. logo.graphics.curveTo(8.3, 6.7, 8, 7);
  267. logo.graphics.lineTo(-8, 7);
  268. _top_bar.addChild(logo);
  269. // Color markers
  270. markers = new Shape;
  271. markers.graphics.beginFill(0xffffff);
  272. markers.graphics.drawRect(20, 7, 4, 4);
  273. markers.graphics.beginFill(0x3388dd);
  274. markers.graphics.drawRect(77, 7, 4, 4);
  275. _top_bar.addChild(markers);
  276. // CURRENT FPS
  277. fps_label_tf = new TextField();
  278. fps_label_tf.defaultTextFormat = _label_format;
  279. fps_label_tf.autoSize = TextFieldAutoSize.LEFT;
  280. fps_label_tf.text = 'FR:';
  281. fps_label_tf.x = 24;
  282. fps_label_tf.y = 2;
  283. fps_label_tf.selectable = false;
  284. _top_bar.addChild(fps_label_tf);
  285. _fps_tf = new TextField;
  286. _fps_tf.defaultTextFormat = _data_format;
  287. _fps_tf.autoSize = TextFieldAutoSize.LEFT;
  288. _fps_tf.x = fps_label_tf.x + 16;
  289. _fps_tf.y = fps_label_tf.y;
  290. _fps_tf.selectable = false;
  291. _top_bar.addChild(_fps_tf);
  292. // AVG FPS
  293. afps_label_tf = new TextField;
  294. afps_label_tf.defaultTextFormat = _label_format;
  295. afps_label_tf.autoSize = TextFieldAutoSize.LEFT;
  296. afps_label_tf.text = 'A:';
  297. afps_label_tf.x = 81;
  298. afps_label_tf.y = 2;
  299. afps_label_tf.selectable = false;
  300. _top_bar.addChild(afps_label_tf);
  301. _afps_tf = new TextField;
  302. _afps_tf.defaultTextFormat = _data_format;
  303. _afps_tf.autoSize = TextFieldAutoSize.LEFT;
  304. _afps_tf.x = afps_label_tf.x + 12;
  305. _afps_tf.y = afps_label_tf.y;
  306. _afps_tf.selectable = false;
  307. _top_bar.addChild(_afps_tf);
  308. // Minimize / maximize button
  309. _min_max_btn = new Sprite;
  310. _min_max_btn.x = _WIDTH-8;
  311. _min_max_btn.y = 7;
  312. _min_max_btn.graphics.beginFill(0, 0);
  313. _min_max_btn.graphics.lineStyle(1, 0xefefef, 1, true);
  314. _min_max_btn.graphics.drawRect(-4, -4, 8, 8);
  315. _min_max_btn.graphics.moveTo(-3, 2);
  316. _min_max_btn.graphics.lineTo(3, 2);
  317. _min_max_btn.buttonMode = true;
  318. _min_max_btn.addEventListener(MouseEvent.CLICK, _onMinMaxBtnClick);
  319. _top_bar.addChild(_min_max_btn);
  320. }
  321. private function _initBottomBar() : void
  322. {
  323. var markers : Shape;
  324. var ram_label_tf : TextField;
  325. var poly_label_tf : TextField;
  326. _btm_bar = new Sprite();
  327. _btm_bar.graphics.beginFill(0, 0.2);
  328. _btm_bar.graphics.drawRect(0, 0, _WIDTH, 21);
  329. addChild(_btm_bar);
  330. // Hit area for bottom bar (to avoid having textfields
  331. // affect interaction badly.)
  332. _btm_bar_hit = new Sprite;
  333. _btm_bar_hit.graphics.beginFill(0xffcc00, 0);
  334. _btm_bar_hit.graphics.drawRect(0, 1, _WIDTH, 20);
  335. addChild(_btm_bar_hit);
  336. // Color markers
  337. markers = new Shape;
  338. markers.graphics.beginFill(_MEM_COL);
  339. markers.graphics.drawRect(5, 4, 4, 4);
  340. markers.graphics.beginFill(_POLY_COL);
  341. markers.graphics.drawRect(5, 14, 4, 4);
  342. _btm_bar.addChild(markers);
  343. // CURRENT RAM
  344. ram_label_tf = new TextField;
  345. ram_label_tf.defaultTextFormat = _label_format;
  346. ram_label_tf.autoSize = TextFieldAutoSize.LEFT;
  347. ram_label_tf.text = 'RAM:';
  348. ram_label_tf.x = 10;
  349. ram_label_tf.y = _UPPER_Y;
  350. ram_label_tf.selectable = false;
  351. ram_label_tf.mouseEnabled = false;
  352. _btm_bar.addChild(ram_label_tf);
  353. _ram_tf = new TextField;
  354. _ram_tf.defaultTextFormat = _data_format;
  355. _ram_tf.autoSize = TextFieldAutoSize.LEFT;
  356. _ram_tf.x = ram_label_tf.x + 31;
  357. _ram_tf.y = ram_label_tf.y;
  358. _ram_tf.selectable = false;
  359. _ram_tf.mouseEnabled = false;
  360. _btm_bar.addChild(_ram_tf);
  361. // POLY COUNT
  362. poly_label_tf = new TextField;
  363. poly_label_tf.defaultTextFormat = _label_format;
  364. poly_label_tf.autoSize = TextFieldAutoSize.LEFT;
  365. poly_label_tf.text = 'POLY:';
  366. poly_label_tf.x = 10;
  367. poly_label_tf.y = _LOWER_Y;
  368. poly_label_tf.selectable = false;
  369. poly_label_tf.mouseEnabled = false;
  370. _btm_bar.addChild(poly_label_tf);
  371. _poly_tf = new TextField;
  372. _poly_tf.defaultTextFormat = _data_format;
  373. _poly_tf.autoSize = TextFieldAutoSize.LEFT;
  374. _poly_tf.x = poly_label_tf.x + 31;
  375. _poly_tf.y = poly_label_tf.y;
  376. _poly_tf.selectable = false;
  377. _poly_tf.mouseEnabled = false;
  378. _btm_bar.addChild(_poly_tf);
  379. }
  380. private function _initDiagrams() : void
  381. {
  382. _dia_bmp = new BitmapData(_WIDTH, _MAX_HEIGHT-40, true, 0);
  383. _diagram = new Sprite;
  384. _diagram.graphics.beginBitmapFill(_dia_bmp);
  385. _diagram.graphics.drawRect(0, 0, _dia_bmp.width, _dia_bmp.height);
  386. _diagram.graphics.endFill();
  387. _diagram.y = 17;
  388. addChild(_diagram);
  389. _diagram.graphics.lineStyle(1, 0xffffff, 0.03);
  390. _diagram.graphics.moveTo(0, 0);
  391. _diagram.graphics.lineTo(_WIDTH, 0);
  392. _diagram.graphics.moveTo(0, Math.floor(_dia_bmp.height/2));
  393. _diagram.graphics.lineTo(_WIDTH, Math.floor(_dia_bmp.height/2));
  394. // FRAME RATE BAR
  395. _fps_bar = new Shape;
  396. _fps_bar.graphics.beginFill(0xffffff);
  397. _fps_bar.graphics.drawRect(0, 0, _WIDTH, 4);
  398. _fps_bar.x = 0;
  399. _fps_bar.y = 16;
  400. addChild(_fps_bar);
  401. // AVERAGE FPS
  402. _afps_bar = new Shape;
  403. _afps_bar.graphics.lineStyle(1, 0x3388dd, 1, false, LineScaleMode.NORMAL, CapsStyle.SQUARE);
  404. _afps_bar.graphics.lineTo(0, 4);
  405. _afps_bar.y = _fps_bar.y;
  406. addChild(_afps_bar);
  407. // MINIMUM FPS
  408. _lfps_bar = new Shape;
  409. _lfps_bar.graphics.lineStyle(1, 0xff0000, 1, false, LineScaleMode.NORMAL, CapsStyle.SQUARE);
  410. _lfps_bar.graphics.lineTo(0, 4);
  411. _lfps_bar.y = _fps_bar.y;
  412. addChild(_lfps_bar);
  413. // MAXIMUM FPS
  414. _hfps_bar = new Shape;
  415. _hfps_bar.graphics.lineStyle(1, 0x00ff00, 1, false, LineScaleMode.NORMAL, CapsStyle.SQUARE);
  416. _hfps_bar.graphics.lineTo(0, 4);
  417. _hfps_bar.y = _fps_bar.y;
  418. addChild(_hfps_bar);
  419. _mem_points = [];
  420. _mem_graph = new Shape;
  421. _mem_graph.y = _diagram.y + _diagram.height;
  422. addChildAt(_mem_graph, 0);
  423. }
  424. private function _initInteraction() : void
  425. {
  426. // Mouse down to drag on the title
  427. _top_bar.addEventListener(MouseEvent.MOUSE_DOWN, _onTopBarMouseDown);
  428. // Reset functionality
  429. if (_enable_reset) {
  430. _btm_bar.mouseEnabled = false;
  431. _btm_bar_hit.addEventListener(MouseEvent.CLICK, _onCountersClick_reset);
  432. _afps_tf.addEventListener(MouseEvent.MOUSE_UP, _onAverageFpsClick_reset, false, 1);
  433. }
  434. // Framerate increase/decrease by clicking on the diagram
  435. if (_enable_mod_fr) {
  436. _diagram.addEventListener(MouseEvent.CLICK, _onDiagramClick);
  437. }
  438. }
  439. private function _redrawWindow() : void
  440. {
  441. var plate_height : Number;
  442. plate_height = _minimized? _MIN_HEIGHT : _MAX_HEIGHT;
  443. // Main plate
  444. if (!_transparent) {
  445. this.graphics.clear();
  446. this.graphics.beginFill(0, 0.6);
  447. this.graphics.drawRect(0, 0, _WIDTH, plate_height);
  448. }
  449. // Minimize/maximize button
  450. _min_max_btn.rotation = _minimized? 180 : 0;
  451. // Position counters
  452. _btm_bar.y = plate_height-21;
  453. _btm_bar_hit.y = _btm_bar.y;
  454. // Hide/show diagram for minimized/maximized view respectively
  455. _diagram.visible = !_minimized;
  456. _mem_graph.visible = !_minimized;
  457. _fps_bar.visible = _minimized;
  458. _afps_bar.visible = _minimized;
  459. _lfps_bar.visible = _minimized;
  460. _hfps_bar.visible = _minimized;
  461. // Redraw memory graph
  462. if (!_minimized)
  463. _redrawMemGraph();
  464. }
  465. private function _redrawStats() : void
  466. {
  467. var dia_y : int;
  468. // Redraw counters
  469. _fps_tf.text = _fps.toString().concat('/', stage.frameRate);
  470. _afps_tf.text = Math.round(_avg_fps).toString();
  471. _ram_tf.text = _getRamString(_ram).concat(' / ', _getRamString(_max_ram));
  472. // Move entire diagram
  473. _dia_bmp.scroll(1, 0);
  474. // Only redraw polycount if there is a view available
  475. // or they won't have been calculated properly
  476. if (_views.length > 0) {
  477. _poly_tf.text = _rfaces.toString().concat(' / ', _tfaces);
  478. // Plot rendered faces
  479. dia_y = _dia_bmp.height - Math.floor(_rfaces/_tfaces * _dia_bmp.height);
  480. _dia_bmp.setPixel32(1, dia_y, _POLY_COL+0xff000000);
  481. }
  482. else {
  483. _poly_tf.text = 'n/a (no view)';
  484. }
  485. // Plot current framerate
  486. dia_y = _dia_bmp.height - Math.floor(_fps/stage.frameRate * _dia_bmp.height);
  487. _dia_bmp.setPixel32(1, dia_y, 0xffffffff);
  488. // Plot average framerate
  489. dia_y = _dia_bmp.height - Math.floor(_avg_fps/stage.frameRate * _dia_bmp.height);
  490. _dia_bmp.setPixel32(1, dia_y, 0xff33bbff);
  491. // Redraw diagrams
  492. if (_minimized) {
  493. _fps_bar.scaleX = Math.min(1, _fps/stage.frameRate);
  494. _afps_bar.x = Math.min(1, _avg_fps/stage.frameRate) * _WIDTH;
  495. _lfps_bar.x = Math.min(1, _min_fps/stage.frameRate) * _WIDTH;
  496. _hfps_bar.x = Math.min(1, _max_fps/stage.frameRate) * _WIDTH;
  497. }
  498. else if (_updates%5 == 0) {
  499. _redrawMemGraph();
  500. }
  501. // Move along regardless of whether the graph
  502. // was updated this time around
  503. _mem_graph.x = _updates%5;
  504. _updates++;
  505. }
  506. private function _redrawMemGraph() : void
  507. {
  508. var i : int;
  509. var g : Graphics;
  510. var max_val : Number = 0;
  511. // Redraw memory graph (only every 5th update)
  512. _mem_graph.scaleY = 1;
  513. g = _mem_graph.graphics;
  514. g.clear();
  515. g.lineStyle(.5, _MEM_COL, 1, true, LineScaleMode.NONE);
  516. g.moveTo(5*(_mem_points.length-1), -_mem_points[_mem_points.length-1]);
  517. for (i=_mem_points.length-1; i>=0; --i) {
  518. if (_mem_points[i+1]==0 || _mem_points[i]==0) {
  519. g.moveTo(i*5, -_mem_points[i]);
  520. continue;
  521. }
  522. g.lineTo(i*5, -_mem_points[i]);
  523. if (_mem_points[i] > max_val)
  524. max_val = _mem_points[i];
  525. }
  526. _mem_graph.scaleY = _dia_bmp.height / max_val;
  527. }
  528. private function _getRamString(ram : Number) : String
  529. {
  530. var ram_unit : String = 'B';
  531. if (ram > 1048576) {
  532. ram /= 1048576;
  533. ram_unit = 'M';
  534. }
  535. else if (ram > 1024) {
  536. ram /= 1024;
  537. ram_unit = 'K';
  538. }
  539. return ram.toFixed(1) + ram_unit;
  540. }
  541. private function _reset() : void
  542. {
  543. var i : int;
  544. // Reset all values
  545. _updates = 0;
  546. _num_frames = 0;
  547. _min_fps = int.MAX_VALUE;
  548. _max_fps = 0;
  549. _avg_fps = 0;
  550. _fps_sum = 0;
  551. _max_ram = 0;
  552. // Reset RAM usage log
  553. for (i=0; i<_WIDTH/5; i++) {
  554. _mem_points[i]=0;
  555. }
  556. // Reset FPS log if any
  557. if (_mean_data) {
  558. for (i=0; i<_mean_data.length; i++) {
  559. _mean_data[i] = 0.0;
  560. }
  561. }
  562. // Clear diagram graphics
  563. _mem_graph.graphics.clear();
  564. _dia_bmp.fillRect(_dia_bmp.rect, 0);
  565. }
  566. private function _endDrag() : void
  567. {
  568. if (this.x < -_WIDTH)
  569. this.x = -(_WIDTH-20);
  570. else if (this.x > stage.stageWidth)
  571. this.x = stage.stageWidth - 20;
  572. if (this.y < 0)
  573. this.y = 0;
  574. else if (this.y > stage.stageHeight)
  575. this.y = stage.stageHeight - 15;
  576. // Round x/y position to make sure it's on
  577. // whole pixels to avoid weird anti-aliasing
  578. this.x = Math.round(this.x);
  579. this.y = Math.round(this.y);
  580. _dragging = false;
  581. stage.removeEventListener(Event.MOUSE_LEAVE, _onMouseUpOrLeave);
  582. stage.removeEventListener(MouseEvent.MOUSE_UP, _onMouseUpOrLeave);
  583. stage.removeEventListener(MouseEvent.MOUSE_MOVE, _onMouseMove);
  584. }
  585. private function _onAddedToStage(ev : Event) : void
  586. {
  587. _timer.start();
  588. addEventListener(Event.ENTER_FRAME, _onEnterFrame);
  589. }
  590. private function _onRemovedFromStage(ev : Event) : void
  591. {
  592. _timer.stop();
  593. removeEventListener(Event.ENTER_FRAME, _onTimer);
  594. }
  595. private function _onTimer(ev : Event) : void
  596. {
  597. // Store current and max RAM
  598. _ram = System.totalMemory;
  599. if (_ram > _max_ram)
  600. _max_ram = _ram;
  601. // Remove first, add last
  602. if (_updates%5 == 0) {
  603. _mem_points.unshift(_ram/1024);
  604. _mem_points.pop();
  605. }
  606. // Update polycount if views are available
  607. if (_views.length > 0) {
  608. var i : int;
  609. _tfaces = _rfaces = 0;
  610. // Sum up poly counts across all registered views
  611. for (i=0; i<_views.length; i++) {
  612. _tfaces += _views[i].scene.polyCount;
  613. _rfaces += _views[i].session.getTotalFaces(_views[i]);
  614. }
  615. }
  616. _redrawStats();
  617. }
  618. private function _onEnterFrame(ev : Event) : void
  619. {
  620. var time : Number = getTimer() - _last_frame_timestamp;
  621. // Calculate current FPS
  622. _fps = Math.floor(1000/time);
  623. _fps_sum += _fps;
  624. // Update min/max fps
  625. if (_fps > _max_fps)
  626. _max_fps = _fps;
  627. else if (_fps!=0 && _fps < _min_fps)
  628. _min_fps = _fps;
  629. // If using a limited length log of frames
  630. // for the average, push the latest recorded
  631. // framerate onto fifo, shift one off and
  632. // subtract it from the running sum, to keep
  633. // the sum reflecting the log entries.
  634. if (_mean_data) {
  635. _mean_data.push(_fps);
  636. _fps_sum -= Number(_mean_data.shift());
  637. // Average = sum of all log entries over
  638. // number of log entries.
  639. _avg_fps = _fps_sum/_mean_data_length;
  640. }
  641. else {
  642. // Regular average calculation, i.e. using
  643. // a running sum since last reset
  644. _num_frames++;
  645. _avg_fps = _fps_sum/_num_frames;
  646. }
  647. _last_frame_timestamp = getTimer();
  648. }
  649. private function _onDiagramClick(ev : MouseEvent) : void
  650. {
  651. stage.frameRate -= Math.floor((_diagram.mouseY - _dia_bmp.height/2) / 5);
  652. }
  653. /**
  654. * @private
  655. * Reset just the average FPS counter.
  656. */
  657. private function _onAverageFpsClick_reset(ev : MouseEvent) : void
  658. {
  659. if (!_dragging) {
  660. var i : int;
  661. _num_frames = 0;
  662. _fps_sum = 0;
  663. if (_mean_data) {
  664. for (i=0; i<_mean_data.length; i++) {
  665. _mean_data[i] = 0.0;
  666. }
  667. }
  668. }
  669. }
  670. private function _onCountersClick_reset(ev : MouseEvent) : void
  671. {
  672. _reset();
  673. }
  674. private function _onMinMaxBtnClick(ev : MouseEvent) : void
  675. {
  676. _minimized = !_minimized;
  677. _redrawWindow();
  678. }
  679. private function _onTopBarMouseDown(ev : MouseEvent) : void
  680. {
  681. _drag_dx = this.mouseX;
  682. _drag_dy = this.mouseY;
  683. stage.addEventListener(MouseEvent.MOUSE_MOVE, _onMouseMove);
  684. stage.addEventListener(MouseEvent.MOUSE_UP, _onMouseUpOrLeave);
  685. stage.addEventListener(Event.MOUSE_LEAVE, _onMouseUpOrLeave);
  686. }
  687. private function _onMouseMove(ev : MouseEvent) : void
  688. {
  689. _dragging = true;
  690. this.x = stage.mouseX - _drag_dx;
  691. this.y = stage.mouseY - _drag_dy;
  692. }
  693. private function _onMouseUpOrLeave(ev : Event) : void
  694. {
  695. _endDrag();
  696. }
  697. }
  698. }