Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Code by rodrigolopezpeker (aka 7interactive™) on 2/16/14 9:08 PM.
- *
- * GStats7 is a custom stats for Genome2D, hides the default GStats, although uses the context
- * rendering and it can be hidden any time by other contenxt draw call. (No depth sorting).
- *
- * Simple usage form:
- * // after G2D initialization call
- * GStats7.init();
- *
- * // transform properties.
- * GStats.x = 20;
- * GStats.y = 20;
- * GStats.scale = 2 ; // useful for retina high DPI.
- *
- * // toggle basic info, only show FPS and drawcalls.
- * GStats7.showStats(true,true,false,false);
- *
- * // styling.
- * // 1st param = format is the base textformat for the TextField
- * // 2nd param = background color for the TextField/Stats
- * // 3rd param = Labels properties (labels are the basic info titles).
- * Available props: italic, bold, underline, face, size, color .
- * var format:TextFormat = new TextFormat('Tahoma', 22, 0xFFFFFF);
- * GStats7.setStyle( format, 0x232323, { underline: true, color: 0x35cee0, size: 7 });
- *
- * // Uses GStats custom stats:
- * GStats.customStats[0] = 'Custom stats!';
- * GStats.customStats[1] = 'counter';
- * GStats.customStats[2] = 'nodes';
- * GStats.customStats[3] = 'cameras';
- *
- * // disable mouse interactions.
- * GStats7.enableMouse(false);
- *
- * // double click to minimize, you can change the timming threshold:
- * GStats7.DOUBLECLICK_THRESHOLD = 1000 // 2 clicks between a 1000ms range.
- *
- * // minimize (doesnt show customStats)
- * GStats7.minimize = true ;
- *
- * // Set graph:
- * // on init=GStats7.init(null, 0x0, null, 30, 0xFF000000 ) ;
- *
- * // set graph colours (and value texts)
- * GStats7.graphColors.fps = 0xFF0000 ;
- * GStats7.graphColors.mem = 0xFF00FF ;
- * GStats7.graphColors.time = 0x0000FF ;
- *
- * GStats7.setGraphStyle( 30, 0xFF000000 ) ;
- *
- * // hide Graph.
- * GStats7.viewGraph = false
- *
- * CHANGES:
- *
- * v0.1 - Initial build.
- * v0.2 - Added mouse events to drag&drop and minimize
- * v0.3 - MrDoob's Graph added, initialization validation (avoiding reconfiguration).
- *
- */
- package rodrigolopezpeker.g2d.utils.stats {
- import com.genome2d.Genome2D;
- import com.genome2d.context.GStats;
- import com.genome2d.textures.GTexture;
- import com.genome2d.textures.GTextureFilteringType;
- import com.genome2d.textures.factories.GTextureFactory;
- import flash.display.BitmapData;
- import flash.display.Stage;
- import flash.events.MouseEvent;
- import flash.geom.Matrix;
- import flash.geom.Point;
- import flash.geom.Rectangle;
- import flash.system.System;
- import flash.text.TextField;
- import flash.text.TextFormat;
- import flash.ui.Mouse;
- import flash.ui.MouseCursor;
- import flash.utils.getTimer;
- /**
- * Usage
- */
- public class GStats7 {
- public static var VERSION:String = '0.3';
- public static var DOUBLECLICK_THRESHOLD:uint = 200;
- private static var _bd:BitmapData;
- private static var _tx:GTexture;
- private static var _tf:TextField;
- private static var _g2d:Genome2D;
- private static var _initialized:Boolean = false ;
- public static var x:Number = 10;
- public static var y:Number = 10;
- public static var scale:Number = 1;
- public static var visible:Boolean = true;
- public static var graphColors:Object = {fps: 0x6FCCD2, mem: 0xF9E60E, time: 0xF92C74 };
- private static var _oldTime:uint;
- private static var _prevTime:uint = 0;
- private static var _lastFPS:int = -1;
- private static var _currFPS:int;
- private static var _graphH:int = 16;
- private static var _fps_str:String = '';
- private static var _mem_str:String = '';
- private static var _ms_str:String = '';
- private static var _draw_str:String = '';
- private static var _mem:int;
- private static var _memMax:int;
- private static var _mtx:Matrix;
- private static var _labelStyle:Object;
- private static var _prevFieldH:Number;
- private static var _text:String = '';
- private static var _labelTag1:String = '';
- private static var _labelTag2:String = '';
- private static var _showFPS:Boolean = true;
- private static var _showDrawCalls:Boolean = true;
- private static var _showFrameTime:Boolean = true;
- private static var _showSystemMemory:Boolean = true;
- private static var _stage:Stage;
- private static var _emptyPoint:Point = new Point();
- private static var _grabPoint:Point = new Point();
- private static var _lastClickTime:uint;
- private static var _mouseEnabled:Boolean;
- public static var minimized:Boolean = false;
- private static var _viewGraph:Boolean = true;
- private static var _graph_bd:BitmapData;
- //============================
- // CONSTRUCTOR --
- //============================
- public function GStats7() {
- }
- private static function render():void {
- var t:uint = getTimer();
- var time_diff:int = t - _oldTime;
- _oldTime = t;
- var currText:String = '';
- var redrawText:Boolean = false;
- // update 1 once per sec.
- if (t - 1000 > _prevTime) {
- _prevTime = t;
- _lastFPS = _currFPS;
- var lblTag2:String = _labelTag2 + ' ';
- var maxFPS:int = _stage.frameRate;
- var fpsColor:String = "<font color='#" + graphColors.fps.toString(16) + "'>" + _lastFPS + "</font>";
- var memColor:String = "<font color='#" + graphColors.mem.toString(16) + "'>" + _mem + "</font>";
- var msColor:String = "<font color='#" + graphColors.time.toString(16) + "'>" + time_diff + "</font>";
- _currFPS = 0;
- _mem = System.totalMemory * 0.000000954;
- _memMax = _mem > _memMax ? _mem : _memMax;
- _fps_str = _labelTag1 + '_FPS:' + lblTag2 + fpsColor + "-" + maxFPS;
- _mem_str = _labelTag1 + "_MEM:" + lblTag2 + memColor + "-" + _memMax + "MB";
- _ms_str = _labelTag1 + "_MS:" + lblTag2 + msColor;
- _draw_str = _labelTag1 + "_DRAWS:" + lblTag2 + GStats.g2d_currentDrawCalls;
- var fpsValue:int = _graphH - Math.min(_graphH, ( _lastFPS / maxFPS ) * _graphH);
- var memValue:int = _graphH - Math.min(_graphH, Math.sqrt(Math.sqrt(_mem * 5000))) - 2;
- // var memMaxValue:int = _graphH - Math.min(_graphH, Math.sqrt(Math.sqrt(_memMax * 5000))) - 2;
- var timeValue:int = _graphH - (time_diff >> 1);
- var baseY:Number = 2;
- _graph_bd.setPixel32(253, baseY + fpsValue, 0xFF << 24 | graphColors.fps);
- _graph_bd.setPixel32(253, baseY + memValue, 0xFF << 24 | graphColors.mem);
- _graph_bd.setPixel32(253, baseY + timeValue, 0xFF << 24 | graphColors.time);
- _graph_bd.scroll(-1, 0);
- }
- if (_showFPS) currText += _fps_str + ' ';
- if (_showSystemMemory) currText += _mem_str + ' ';
- if (_showFrameTime) currText += _ms_str + ' ';
- if (_showDrawCalls) currText += _draw_str + ' ';
- if (!minimized) {
- for each(var cstat:String in GStats.customStats) {
- if (cstat != null) currText += '\n' + cstat;
- }
- }
- if (_text != currText) {
- _text = currText;
- _tf.htmlText = _text;
- redrawText = true;
- }
- _tf.height = _tf.textHeight + 2;
- // invalidate texture's bitmap
- if (_prevFieldH != _tf.height) {
- var bdH:int = _tf.height + _graphH;
- _bd = new BitmapData(256, bdH, true, 0x0);
- calculateDrawMatrix();
- reinitTexture();
- }
- _prevFieldH = _tf.height;
- if (redrawText) {
- _bd.fillRect(_bd.rect, 0x0);
- _bd.draw(_tf, _mtx);
- var r:Rectangle = new Rectangle(_graph_bd.width - _tf.width, 0, _tf.width, _graph_bd.height);
- if (viewGraph) {
- _bd.copyPixels(_graph_bd, r, _emptyPoint, null, null, true);
- }
- _tx.invalidateNativeTexture(false);
- }
- _currFPS++;
- if (visible) {
- _g2d.getContext().draw(_tx, x, y, scale, scale);
- }
- }
- private static function reinitTexture():void {
- if (_tx) _tx.dispose();
- _tx = GTextureFactory.createFromBitmapData('stats_extended', _bd);
- _tx.setFilteringType(GTextureFilteringType.NEAREST);
- _tx.pivotX = -_tx.width / 2;
- _tx.pivotY = -_tx.height / 2;
- }
- private static function calculateDrawMatrix():void {
- _mtx.identity();
- var offsetY:int = _tf.height - (_tf.textHeight + 4) >> 1;
- if (viewGraph) offsetY += _graphH;
- _mtx.translate(0, offsetY);
- }
- public static function setStyle(pTextFormat:TextFormat = null, pBackgroundColor:uint = 0xFFFFFF, pLabelStyle:Object = null):void {
- if (!pTextFormat) {
- pTextFormat = new TextFormat('Verdana', 8, 0x323232);
- }
- if (!pLabelStyle) pLabelStyle = {};
- _tf.backgroundColor = pBackgroundColor;
- _labelStyle = pLabelStyle;
- if (!('color' in pLabelStyle )) _labelStyle.color = 0x898989;
- if (!('size' in pLabelStyle )) _labelStyle.size = 7;
- if (!('face' in pLabelStyle )) _labelStyle.face = pTextFormat.font;
- _labelTag1 = "<font face='" + _labelStyle.face + "' size='" + _labelStyle.size + "' color='#" + _labelStyle.color.toString(16) + "'>";
- _labelTag2 = "</font>";
- if (_labelStyle.bold) {
- _labelTag1 += '<b>';
- _labelTag2 = '</b>' + _labelTag2;
- }
- if (_labelStyle.underline) {
- _labelTag1 += '<u>';
- _labelTag2 = '</u>' + _labelTag2;
- }
- if (_labelStyle.italic) {
- _labelTag1 += '<i>';
- _labelTag2 = '</i>' + _labelTag2;
- }
- _tf.defaultTextFormat = pTextFormat;
- _tf.setTextFormat(pTextFormat);
- }
- public static function setGraphStyle(pBarHeight:int = 10, pBackgroundColor:uint = 0xDD000000):void {
- if (_graph_bd) _graph_bd.dispose();
- _graphH = pBarHeight;
- _graph_bd = new BitmapData(256, _graphH, true, pBackgroundColor);
- }
- public static function init(pTextFormat:TextFormat = null, pBackgroundColor:uint = 0xFFFFFF, pLabelStyle:Object = null, pGraphH:int = 10, pGraphBgColor:uint = 0xDD000000):void {
- if (_initialized) return ;
- _initialized = true ;
- _prevFieldH = 16;
- setGraphStyle(pGraphH, pGraphBgColor);
- _tf = new TextField();
- _tf.autoSize = 'left';
- _tf.multiline = true;
- _tf.background = true;
- _g2d = Genome2D.g2d_instance;
- _g2d.onPostRender.add(render);
- _stage = _g2d.g2d_context.g2d_nativeStage;
- _prevFieldH = -1;
- // disable native rgl.g2d.utils.stats.
- _g2d.g2d_context.enableStats(false);
- _mtx = new Matrix();
- setStyle(pTextFormat, pBackgroundColor, pLabelStyle);
- mouseEnabled = true;
- }
- /**
- * Enables or disables the mouse interaction
- * @param pFlag - Boolean to toggle the mouse listener.
- */
- public static function get mouseEnabled():Boolean {
- return _mouseEnabled;
- }
- public static function set mouseEnabled(pFlag:Boolean):void {
- if (_mouseEnabled == pFlag) return;
- _mouseEnabled = pFlag;
- if (pFlag) {
- _stage.addEventListener(MouseEvent.MOUSE_DOWN, handleStageMouse);
- } else {
- _stage.removeEventListener(MouseEvent.MOUSE_DOWN, handleStageMouse);
- }
- }
- private static function handleStageMouse(event:MouseEvent):void {
- switch (event.type) {
- case MouseEvent.MOUSE_DOWN:
- var mouseHit:Boolean = _bd.getPixel32((event.stageX - x) / scale, (event.stageY - y) / scale) != 0x0;
- if (!mouseHit) return;
- _stage.addEventListener(MouseEvent.MOUSE_MOVE, handleStageMouse);
- _stage.addEventListener(MouseEvent.MOUSE_UP, handleStageMouse);
- _grabPoint.setTo(event.stageX - x, event.stageY - y);
- break;
- case MouseEvent.MOUSE_MOVE:
- Mouse.cursor = MouseCursor.HAND;
- x = event.stageX - _grabPoint.x;
- y = event.stageY - _grabPoint.y;
- break;
- case MouseEvent.MOUSE_UP:
- Mouse.cursor = MouseCursor.AUTO;
- var t:uint = getTimer();
- if (t - _lastClickTime <= DOUBLECLICK_THRESHOLD) {
- // double click!
- minimized = !minimized;
- }
- _lastClickTime = t;
- _stage.removeEventListener(MouseEvent.MOUSE_MOVE, handleStageMouse);
- _stage.removeEventListener(MouseEvent.MOUSE_UP, handleStageMouse);
- break;
- }
- }
- public static function showStats(pFPS:Boolean = true, pDrawCalls:Boolean = true, pFrameTime:Boolean = true, pSystemMemory:Boolean = true):void {
- _showFPS = pFPS;
- _showDrawCalls = pDrawCalls;
- _showFrameTime = pFrameTime;
- _showSystemMemory = pSystemMemory;
- }
- public static function get viewGraph():Boolean {
- return _viewGraph;
- }
- public static function set viewGraph(value:Boolean):void {
- if (_viewGraph == value) return;
- _viewGraph = value;
- _prevFieldH = -1;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment