PageRenderTime 205ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/Flash/project-needs/src/away3d/debug/AwayStats.as

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