rodrigolopezpeker

Genome2D > GStats7 v0.3 [custom stats]

Feb 17th, 2014
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Code by rodrigolopezpeker (aka 7interactive™) on 2/16/14 9:08 PM.
  3.  *
  4.  * GStats7 is a custom stats for Genome2D, hides the default GStats, although uses the context
  5.  * rendering and it can be hidden any time by other contenxt draw call. (No depth sorting).
  6.  *
  7.  * Simple usage form:
  8.  * // after G2D initialization call
  9.  * GStats7.init();
  10.  *
  11.  * // transform properties.
  12.  * GStats.x = 20;
  13.  * GStats.y = 20;
  14.  * GStats.scale = 2 ; // useful for retina high DPI.
  15.  *
  16.  * // toggle basic info, only show FPS and drawcalls.
  17.  * GStats7.showStats(true,true,false,false);
  18.  *
  19.  * // styling.
  20.  * // 1st param = format is the base textformat for the TextField
  21.  * // 2nd param = background color for the TextField/Stats
  22.  * // 3rd param = Labels properties (labels are the basic info titles).
  23.  *   Available props: italic, bold, underline, face, size, color .
  24.  * var format:TextFormat = new TextFormat('Tahoma', 22, 0xFFFFFF);
  25.  * GStats7.setStyle( format, 0x232323, { underline: true, color: 0x35cee0, size: 7 });
  26.  *
  27.  * // Uses GStats custom stats:
  28.  * GStats.customStats[0] = 'Custom stats!';
  29.  * GStats.customStats[1] = 'counter';
  30.  * GStats.customStats[2] = 'nodes';
  31.  * GStats.customStats[3] = 'cameras';
  32.  *
  33.  * // disable mouse interactions.
  34.  * GStats7.enableMouse(false);
  35.  *
  36.  * // double click to minimize, you can change the timming threshold:
  37.  * GStats7.DOUBLECLICK_THRESHOLD = 1000 // 2 clicks between a 1000ms range.
  38.  *
  39.  * // minimize (doesnt show customStats)
  40.  * GStats7.minimize = true ;
  41.  *
  42.  * // Set graph:
  43.  * // on init=GStats7.init(null, 0x0, null, 30, 0xFF000000 ) ;
  44.  *
  45.  * // set graph colours (and value texts)
  46.  * GStats7.graphColors.fps = 0xFF0000 ;
  47.  * GStats7.graphColors.mem = 0xFF00FF ;
  48.  * GStats7.graphColors.time = 0x0000FF ;
  49.  *
  50.  * GStats7.setGraphStyle( 30, 0xFF000000 ) ;
  51.  *
  52.  * // hide Graph.
  53.  * GStats7.viewGraph = false
  54.  *
  55.  * CHANGES:
  56.  *
  57.  * v0.1 - Initial build.
  58.  * v0.2 - Added mouse events to drag&drop and minimize
  59.  * v0.3 - MrDoob's Graph added, initialization validation (avoiding reconfiguration).
  60.  *
  61.  */
  62. package rodrigolopezpeker.g2d.utils.stats {
  63. import com.genome2d.Genome2D;
  64. import com.genome2d.context.GStats;
  65. import com.genome2d.textures.GTexture;
  66. import com.genome2d.textures.GTextureFilteringType;
  67. import com.genome2d.textures.factories.GTextureFactory;
  68.  
  69. import flash.display.BitmapData;
  70. import flash.display.Stage;
  71. import flash.events.MouseEvent;
  72. import flash.geom.Matrix;
  73. import flash.geom.Point;
  74. import flash.geom.Rectangle;
  75. import flash.system.System;
  76. import flash.text.TextField;
  77. import flash.text.TextFormat;
  78. import flash.ui.Mouse;
  79. import flash.ui.MouseCursor;
  80. import flash.utils.getTimer;
  81.  
  82. /**
  83.  * Usage
  84.  */
  85. public class GStats7 {
  86.  
  87.     public static var VERSION:String = '0.3';
  88.  
  89.     public static var DOUBLECLICK_THRESHOLD:uint = 200;
  90.  
  91.     private static var _bd:BitmapData;
  92.     private static var _tx:GTexture;
  93.     private static var _tf:TextField;
  94.     private static var _g2d:Genome2D;
  95.     private static var _initialized:Boolean = false ;
  96.  
  97.     public static var x:Number = 10;
  98.     public static var y:Number = 10;
  99.     public static var scale:Number = 1;
  100.     public static var visible:Boolean = true;
  101.     public static var graphColors:Object = {fps: 0x6FCCD2, mem: 0xF9E60E, time: 0xF92C74 };
  102.  
  103.     private static var _oldTime:uint;
  104.     private static var _prevTime:uint = 0;
  105.     private static var _lastFPS:int = -1;
  106.     private static var _currFPS:int;
  107.     private static var _graphH:int = 16;
  108.  
  109.     private static var _fps_str:String = '';
  110.     private static var _mem_str:String = '';
  111.     private static var _ms_str:String = '';
  112.     private static var _draw_str:String = '';
  113.     private static var _mem:int;
  114.     private static var _memMax:int;
  115.     private static var _mtx:Matrix;
  116.     private static var _labelStyle:Object;
  117.     private static var _prevFieldH:Number;
  118.     private static var _text:String = '';
  119.  
  120.     private static var _labelTag1:String = '';
  121.     private static var _labelTag2:String = '';
  122.  
  123.     private static var _showFPS:Boolean = true;
  124.     private static var _showDrawCalls:Boolean = true;
  125.     private static var _showFrameTime:Boolean = true;
  126.     private static var _showSystemMemory:Boolean = true;
  127.  
  128.     private static var _stage:Stage;
  129.  
  130.     private static var _emptyPoint:Point = new Point();
  131.     private static var _grabPoint:Point = new Point();
  132.     private static var _lastClickTime:uint;
  133.     private static var _mouseEnabled:Boolean;
  134.     public static var minimized:Boolean = false;
  135.     private static var _viewGraph:Boolean = true;
  136.     private static var _graph_bd:BitmapData;
  137.  
  138.     //============================
  139.     // CONSTRUCTOR --
  140.     //============================
  141.     public function GStats7() {
  142.     }
  143.  
  144.  
  145.     private static function render():void {
  146.         var t:uint = getTimer();
  147.         var time_diff:int = t - _oldTime;
  148.         _oldTime = t;
  149.  
  150.         var currText:String = '';
  151.         var redrawText:Boolean = false;
  152.  
  153.         // update 1 once per sec.
  154.         if (t - 1000 > _prevTime) {
  155.             _prevTime = t;
  156.             _lastFPS = _currFPS;
  157.             var lblTag2:String = _labelTag2 + ' ';
  158.             var maxFPS:int = _stage.frameRate;
  159.             var fpsColor:String = "<font color='#" + graphColors.fps.toString(16) + "'>" + _lastFPS + "</font>";
  160.             var memColor:String = "<font color='#" + graphColors.mem.toString(16) + "'>" + _mem + "</font>";
  161.             var msColor:String = "<font color='#" + graphColors.time.toString(16) + "'>" + time_diff + "</font>";
  162.             _currFPS = 0;
  163.             _mem = System.totalMemory * 0.000000954;
  164.             _memMax = _mem > _memMax ? _mem : _memMax;
  165.             _fps_str = _labelTag1 + '_FPS:' + lblTag2 + fpsColor + "-" + maxFPS;
  166.             _mem_str = _labelTag1 + "_MEM:" + lblTag2 + memColor + "-" + _memMax + "MB";
  167.             _ms_str = _labelTag1 + "_MS:" + lblTag2 + msColor;
  168.             _draw_str = _labelTag1 + "_DRAWS:" + lblTag2 + GStats.g2d_currentDrawCalls;
  169.             var fpsValue:int = _graphH - Math.min(_graphH, ( _lastFPS / maxFPS ) * _graphH);
  170.             var memValue:int = _graphH - Math.min(_graphH, Math.sqrt(Math.sqrt(_mem * 5000))) - 2;
  171. //          var memMaxValue:int = _graphH - Math.min(_graphH, Math.sqrt(Math.sqrt(_memMax * 5000))) - 2;
  172.             var timeValue:int = _graphH - (time_diff >> 1);
  173.             var baseY:Number = 2;
  174.             _graph_bd.setPixel32(253, baseY + fpsValue, 0xFF << 24 | graphColors.fps);
  175.             _graph_bd.setPixel32(253, baseY + memValue, 0xFF << 24 | graphColors.mem);
  176.             _graph_bd.setPixel32(253, baseY + timeValue, 0xFF << 24 | graphColors.time);
  177.             _graph_bd.scroll(-1, 0);
  178.         }
  179.  
  180.         if (_showFPS) currText += _fps_str + ' ';
  181.         if (_showSystemMemory) currText += _mem_str + ' ';
  182.         if (_showFrameTime) currText += _ms_str + ' ';
  183.         if (_showDrawCalls) currText += _draw_str + ' ';
  184.  
  185.         if (!minimized) {
  186.             for each(var cstat:String in GStats.customStats) {
  187.                 if (cstat != null) currText += '\n' + cstat;
  188.             }
  189.         }
  190.  
  191.         if (_text != currText) {
  192.             _text = currText;
  193.             _tf.htmlText = _text;
  194.             redrawText = true;
  195.         }
  196.         _tf.height = _tf.textHeight + 2;
  197.         // invalidate texture's bitmap
  198.         if (_prevFieldH != _tf.height) {
  199.             var bdH:int = _tf.height + _graphH;
  200.             _bd = new BitmapData(256, bdH, true, 0x0);
  201.             calculateDrawMatrix();
  202.             reinitTexture();
  203.         }
  204.         _prevFieldH = _tf.height;
  205.         if (redrawText) {
  206.             _bd.fillRect(_bd.rect, 0x0);
  207.             _bd.draw(_tf, _mtx);
  208.             var r:Rectangle = new Rectangle(_graph_bd.width - _tf.width, 0, _tf.width, _graph_bd.height);
  209.             if (viewGraph) {
  210.                 _bd.copyPixels(_graph_bd, r, _emptyPoint, null, null, true);
  211.             }
  212.             _tx.invalidateNativeTexture(false);
  213.         }
  214.         _currFPS++;
  215.         if (visible) {
  216.             _g2d.getContext().draw(_tx, x, y, scale, scale);
  217.         }
  218.     }
  219.  
  220.     private static function reinitTexture():void {
  221.         if (_tx) _tx.dispose();
  222.         _tx = GTextureFactory.createFromBitmapData('stats_extended', _bd);
  223.         _tx.setFilteringType(GTextureFilteringType.NEAREST);
  224.         _tx.pivotX = -_tx.width / 2;
  225.         _tx.pivotY = -_tx.height / 2;
  226.     }
  227.  
  228.     private static function calculateDrawMatrix():void {
  229.         _mtx.identity();
  230.         var offsetY:int = _tf.height - (_tf.textHeight + 4) >> 1;
  231.         if (viewGraph) offsetY += _graphH;
  232.         _mtx.translate(0, offsetY);
  233.     }
  234.  
  235.     public static function setStyle(pTextFormat:TextFormat = null, pBackgroundColor:uint = 0xFFFFFF, pLabelStyle:Object = null):void {
  236.         if (!pTextFormat) {
  237.             pTextFormat = new TextFormat('Verdana', 8, 0x323232);
  238.         }
  239.         if (!pLabelStyle) pLabelStyle = {};
  240.         _tf.backgroundColor = pBackgroundColor;
  241.         _labelStyle = pLabelStyle;
  242.         if (!('color' in pLabelStyle )) _labelStyle.color = 0x898989;
  243.         if (!('size' in pLabelStyle )) _labelStyle.size = 7;
  244.         if (!('face' in pLabelStyle )) _labelStyle.face = pTextFormat.font;
  245.  
  246.         _labelTag1 = "<font face='" + _labelStyle.face + "' size='" + _labelStyle.size + "' color='#" + _labelStyle.color.toString(16) + "'>";
  247.         _labelTag2 = "</font>";
  248.  
  249.         if (_labelStyle.bold) {
  250.             _labelTag1 += '<b>';
  251.             _labelTag2 = '</b>' + _labelTag2;
  252.         }
  253.         if (_labelStyle.underline) {
  254.             _labelTag1 += '<u>';
  255.             _labelTag2 = '</u>' + _labelTag2;
  256.         }
  257.         if (_labelStyle.italic) {
  258.             _labelTag1 += '<i>';
  259.             _labelTag2 = '</i>' + _labelTag2;
  260.         }
  261.         _tf.defaultTextFormat = pTextFormat;
  262.         _tf.setTextFormat(pTextFormat);
  263.     }
  264.  
  265.     public static function setGraphStyle(pBarHeight:int = 10, pBackgroundColor:uint = 0xDD000000):void {
  266.         if (_graph_bd) _graph_bd.dispose();
  267.         _graphH = pBarHeight;
  268.         _graph_bd = new BitmapData(256, _graphH, true, pBackgroundColor);
  269.     }
  270.  
  271.     public static function init(pTextFormat:TextFormat = null, pBackgroundColor:uint = 0xFFFFFF, pLabelStyle:Object = null, pGraphH:int = 10, pGraphBgColor:uint = 0xDD000000):void {
  272.         if (_initialized) return ;
  273.         _initialized = true ;
  274.         _prevFieldH = 16;
  275.         setGraphStyle(pGraphH, pGraphBgColor);
  276.         _tf = new TextField();
  277.         _tf.autoSize = 'left';
  278.         _tf.multiline = true;
  279.         _tf.background = true;
  280.         _g2d = Genome2D.g2d_instance;
  281.         _g2d.onPostRender.add(render);
  282.         _stage = _g2d.g2d_context.g2d_nativeStage;
  283.         _prevFieldH = -1;
  284.         // disable native rgl.g2d.utils.stats.
  285.         _g2d.g2d_context.enableStats(false);
  286.         _mtx = new Matrix();
  287.         setStyle(pTextFormat, pBackgroundColor, pLabelStyle);
  288.         mouseEnabled = true;
  289.     }
  290.  
  291.     /**
  292.      * Enables or disables the mouse interaction
  293.      * @param pFlag - Boolean to toggle the mouse listener.
  294.      */
  295.     public static function get mouseEnabled():Boolean {
  296.         return _mouseEnabled;
  297.     }
  298.  
  299.     public static function set mouseEnabled(pFlag:Boolean):void {
  300.         if (_mouseEnabled == pFlag) return;
  301.         _mouseEnabled = pFlag;
  302.         if (pFlag) {
  303.             _stage.addEventListener(MouseEvent.MOUSE_DOWN, handleStageMouse);
  304.         } else {
  305.             _stage.removeEventListener(MouseEvent.MOUSE_DOWN, handleStageMouse);
  306.         }
  307.     }
  308.  
  309.  
  310.     private static function handleStageMouse(event:MouseEvent):void {
  311.         switch (event.type) {
  312.             case MouseEvent.MOUSE_DOWN:
  313.                 var mouseHit:Boolean = _bd.getPixel32((event.stageX - x) / scale, (event.stageY - y) / scale) != 0x0;
  314.                 if (!mouseHit) return;
  315.                 _stage.addEventListener(MouseEvent.MOUSE_MOVE, handleStageMouse);
  316.                 _stage.addEventListener(MouseEvent.MOUSE_UP, handleStageMouse);
  317.                 _grabPoint.setTo(event.stageX - x, event.stageY - y);
  318.                 break;
  319.             case MouseEvent.MOUSE_MOVE:
  320.                 Mouse.cursor = MouseCursor.HAND;
  321.                 x = event.stageX - _grabPoint.x;
  322.                 y = event.stageY - _grabPoint.y;
  323.                 break;
  324.             case MouseEvent.MOUSE_UP:
  325.                 Mouse.cursor = MouseCursor.AUTO;
  326.                 var t:uint = getTimer();
  327.                 if (t - _lastClickTime <= DOUBLECLICK_THRESHOLD) {
  328.                     // double click!
  329.                     minimized = !minimized;
  330.                 }
  331.                 _lastClickTime = t;
  332.                 _stage.removeEventListener(MouseEvent.MOUSE_MOVE, handleStageMouse);
  333.                 _stage.removeEventListener(MouseEvent.MOUSE_UP, handleStageMouse);
  334.                 break;
  335.         }
  336.     }
  337.  
  338.     public static function showStats(pFPS:Boolean = true, pDrawCalls:Boolean = true, pFrameTime:Boolean = true, pSystemMemory:Boolean = true):void {
  339.         _showFPS = pFPS;
  340.         _showDrawCalls = pDrawCalls;
  341.         _showFrameTime = pFrameTime;
  342.         _showSystemMemory = pSystemMemory;
  343.     }
  344.  
  345.     public static function get viewGraph():Boolean {
  346.         return _viewGraph;
  347.     }
  348.  
  349.     public static function set viewGraph(value:Boolean):void {
  350.         if (_viewGraph == value) return;
  351.         _viewGraph = value;
  352.         _prevFieldH = -1;
  353.     }
  354. }
  355. }
Advertisement
Add Comment
Please, Sign In to add comment