/** * DebugDisplayItem * Abstract debug display: shows a value and a graph * @author Zeh * @version 1.0 */ package com.zehfernando.display.debug { import com.zehfernando.display.shapes.Box; import flash.display.*; import flash.events.*; import flash.text.*; import flash.utils.getTimer; public class DebugDisplayItem extends Sprite { // Constants public static const GRAPH_WIDTH:int = 60; public static const GRAPH_HEIGHT:int = 30; protected static const DEFAULT_UPDATE_RATE:Number = 0.1; // Interval (in seconds) in which to report; the bigger, the more accurate protected static const DEFAULT_INTERVAL_RATE:Number = 2; // Interval (in seconds) in which to create an interval line // Static properties protected static var displayList:Array = new Array(); // List of existing properties in display protected static var isInited:Boolean = false; // Whether the class is inited or not protected static var staticStage:Stage; // Private properties protected var timeGraph:TimeGraph; // Graph protected var lastUpdate:uint; // Time the last update took place protected var lastInterval:uint; // Time the last INTERVAL update took place protected var updateRate:Number; // Ammount of time (in seconds) to wait for an update; 0 = realtime fps protected var intervalRate:Number; // Ammount of time (in seconds) to wait for an interval protected var inited:Boolean; // Stage instances protected var caption:TextField; protected var background:Box; // ================================================================================================================ // CONSTRUCTOR ---------------------------------------------------------------------------------------------------- public function DebugDisplayItem() { updateRate = DEFAULT_UPDATE_RATE; intervalRate = DEFAULT_INTERVAL_RATE; addEventListener(Event.ADDED_TO_STAGE, onAddToStage); } // ================================================================================================================ // INSTANCE functions --------------------------------------------------------------------------------------------- protected function onAddToStage(e:Event = null): void { // This object has been added to the stage if (!inited) init(); } protected function init(): void { // Initializes everything createAssets(); resetTimers(true); update(0); inited = true; } protected function onTick(e:Event = null): void { var timePassed:uint = getTimer() - lastUpdate; if (timePassed > updateRate * 1000 || updateRate == 0) { var needInterval:Boolean = (getTimer() - lastInterval) > (intervalRate * 1000); update(timePassed, needInterval); resetTimers(needInterval); } } protected function createAssets(): void { // Create background box background = new Box(GRAPH_WIDTH, GRAPH_HEIGHT, 0xbb0000); addChild(background); // Create graph timeGraph = new TimeGraph(GRAPH_WIDTH-2, GRAPH_HEIGHT-2); timeGraph.x = 1; timeGraph.y = 1; addChild(timeGraph); // Create textfield var fmt:TextFormat = new TextFormat(); fmt.font = "_sans"; fmt.color = 0xffffff;//0x000000; fmt.size = 9; fmt.align = TextFormatAlign.RIGHT; caption = new TextField(); caption.embedFonts = false; caption.width = GRAPH_WIDTH; caption.height = 14; caption.defaultTextFormat = fmt; caption.selectable = false; caption.x = 0; caption.y = GRAPH_HEIGHT - caption.height + 1; addChild(caption); // Start counting addEventListener(Event.ENTER_FRAME, onTick); } protected function update(timePassed:uint, includeInterval:Boolean = false): void { // Sets the new text and adds a new column to the graph // timePassed = time passed since the last frame (in ms); 0 if N/A var p_value:Number = timePassed/1000; setCaption(String(Math.round(p_value*10))); timeGraph.push(p_value, includeInterval); } protected function resetTimers(includeInterval:Boolean = false): void { // Reset all used timers lastUpdate = getTimer(); if (includeInterval) lastInterval = getTimer(); } protected function setCaption(text:String): void { // Sets the caption with some text caption.text = text; } // ================================================================================================================ // PUBLIC functions ----------------------------------------------------------------------------------------------- public function setUpdateRate(p_time:Number): void { // Set the update rate (seconds that wait for an update) and changes the interval accordingly var ir:Number = intervalRate / updateRate; updateRate = p_time; intervalRate = ir * p_time; } } }