jesterxl

DebugWindowv2

Jun 5th, 2011
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package
  2. {
  3.     import flash.display.Graphics;
  4.     import flash.display.Sprite;
  5.     import flash.events.Event;
  6.     import flash.events.MouseEvent;
  7.     import flash.net.SharedObject;
  8.     import flash.system.Capabilities;
  9.     import flash.system.System;
  10.     import flash.text.TextField;
  11.     import flash.text.TextFormat;
  12.    
  13.     import mx.core.UIComponent;
  14.    
  15.     import spark.components.Button;
  16.     import spark.components.CheckBox;
  17.     import spark.components.Label;
  18.     import spark.components.ToggleButton;
  19.    
  20.     public class Debug extends UIComponent
  21.     {
  22.         private var debugTextField:TextField;
  23.        
  24.         private static var _inst:Debug;
  25.         private static var instances:Array = [];
  26.         private static const SO_NAME:String = "DebugWindowPosition";
  27.         private static const HEADER:String = "--------------------";
  28.         private static const ERROR_COLOR:String = "#FF0000";
  29.         private static const DEBUG_COLOR:String = "#0033FF";
  30.         private static const INFO_COLOR:String = "#009966";
  31.         private static const WARN_COLOR:String = "#FF9933";
  32.        
  33.         private static var backLog:Array = [];
  34.        
  35.         public static var debugging:Boolean = true;
  36.        
  37.         private var background:Sprite;
  38.         private var autoScrollCheckBox:CheckBox;
  39.         private var clearButton:Button;
  40.         private var minMaxButton:ToggleButton;
  41.         private var copyButton:Button;
  42.         private var panelLabel:Label;
  43.        
  44.         private var dragging:Boolean = false;
  45.        
  46.         public function Debug()
  47.         {
  48.             super();
  49.            
  50.            
  51.            
  52.             width           = 540;
  53.             height          = 400;
  54.            
  55.             this.addEventListener(Event.ADDED_TO_STAGE, onAdded);
  56.             this.addEventListener(Event.REMOVED_FROM_STAGE, onRemoved);
  57.         }
  58.        
  59.         protected override function createChildren():void
  60.         {
  61.             super.createChildren();
  62.            
  63.             this.mouseChildren = true;
  64.            
  65.             background = new Sprite();
  66.             addChild(background);
  67.            
  68.             background.mouseChildren = false;
  69.             background.buttonMode = background.useHandCursor = true;
  70.            
  71.             panelLabel = new Label();
  72.             addChild(panelLabel);
  73.             panelLabel.text = "Debug Window";
  74.             panelLabel.setStyle("color", 0xFFFFFF);
  75.            
  76.             debugTextField              = new TextField();
  77.             addChild(debugTextField);
  78.             debugTextField.multiline    = true;
  79.             debugTextField.wordWrap     = true;
  80.             debugTextField.width        = 320;
  81.             debugTextField.height       = 200;
  82.             debugTextField.border       = true;
  83.             debugTextField.borderColor  = 0x666666;
  84.             debugTextField.selectable   = true;
  85.             //debugTextField.background     = true;
  86.             //debugTextField.backgroundColor    = 0xFFFFFF;
  87.             var fmt:TextFormat = debugTextField.defaultTextFormat;
  88.             fmt.font = "_sans";
  89.             fmt.size = 9;
  90.             debugTextField.defaultTextFormat = fmt;
  91.            
  92.             autoScrollCheckBox = new CheckBox();
  93.             addChild(autoScrollCheckBox);
  94.             autoScrollCheckBox.label = "Auto-Scroll";
  95.             autoScrollCheckBox.selected = true;
  96.            
  97.             clearButton = new Button();
  98.             addChild(clearButton);
  99.             clearButton.label = "Clear";
  100.             clearButton.addEventListener(MouseEvent.CLICK, onClear);
  101.            
  102.             copyButton = new Button();
  103.             addChild(copyButton);
  104.             copyButton.label = "Copy To Clipboard";
  105.             copyButton.addEventListener(MouseEvent.CLICK, onCopy);
  106.            
  107.             minMaxButton = new ToggleButton();
  108.             if(isPopUp)
  109.                 addChild(minMaxButton);
  110.             minMaxButton.label = "-";
  111.             minMaxButton.addEventListener(MouseEvent.CLICK, onMinMax);
  112.            
  113.             if(_inst == null)
  114.                 _inst = this;
  115.            
  116.             if(backLog)
  117.             {
  118.                 var len:int = backLog.length;
  119.                 for(var index:uint = 0; index < len; index++)
  120.                 {
  121.                     var obj:Object = backLog[index];
  122.                     if(obj.message)
  123.                     {
  124.                         obj.func.call(obj, obj.message);
  125.                     }
  126.                     else
  127.                     {
  128.                         obj.func.call();
  129.                     }
  130.                 }
  131.                
  132.                 backLog = null;
  133.             }
  134.            
  135.             if(Capabilities.version.toLowerCase().indexOf("and") != -1)
  136.             {
  137.                 if(isPopUp == false)
  138.                     return;
  139.                 this.mouseChildren = false;
  140.                 this.buttonMode = true;
  141.                 this.useHandCursor = true;
  142.                 this.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDownDevice);
  143.                 this.addEventListener(MouseEvent.MOUSE_UP, onMouseUpDevice);
  144.             }
  145.             else
  146.             {
  147.                 if(isPopUp == false)
  148.                     return;
  149.                 background.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
  150.                 background.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
  151.             }
  152.         }
  153.        
  154.         protected override function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
  155.         {
  156.             super.updateDisplayList(unscaledWidth, unscaledHeight);
  157.            
  158.             panelLabel.x = 4;
  159.            
  160.             minMaxButton.setActualSize(12, 12);
  161.             minMaxButton.move(width - (minMaxButton.width + 4), 2);
  162.            
  163.             var g:Graphics = background.graphics;
  164.             g.clear();
  165.            
  166.             if(minMaxButton.selected == false)
  167.             {
  168.                 debugTextField.visible = true;
  169.                 clearButton.visible = true;
  170.                 autoScrollCheckBox.visible = true;
  171.                 clearButton.visible = true;
  172.                
  173.                 g.beginFill(0x333333, .9);
  174.                 g.drawRoundRect(0, 0, width, height, 6, 6);
  175.                
  176.                
  177.                 debugTextField.width = width - 8;
  178.                 debugTextField.height = height - clearButton.height - 24;
  179.                 debugTextField.x = 4;
  180.                 debugTextField.y = 16;
  181.                
  182.                 g.beginFill(0xFFFFFF, .7);
  183.                 g.moveTo(debugTextField.x, debugTextField.y);
  184.                 g.drawRect(debugTextField.x, debugTextField.y, debugTextField.width, debugTextField.height);
  185.                 //g.drawRect(debugTextField.x, debugTextField.y, debugTextField.width, debugTextField.height);
  186.                 g.endFill();
  187.                
  188.                 clearButton.x = 4;
  189.                 clearButton.y = height - (clearButton.height + 4);
  190.                
  191.                 autoScrollCheckBox.x = width - (autoScrollCheckBox.width + 4);
  192.                 autoScrollCheckBox.y = clearButton.y;
  193.                 autoScrollCheckBox.setActualSize(22, 22);
  194.                
  195.                 copyButton.setActualSize(120, 22);
  196.                 clearButton.width = width - (width - autoScrollCheckBox.x) - 8 - copyButton.width;
  197.                 clearButton.setActualSize(clearButton.width, 22);
  198.                 copyButton.move(clearButton.x + clearButton.width + 4, clearButton.y);
  199.             }
  200.             else
  201.             {
  202.                 g.beginFill(0x333333, .5);
  203.                 g.drawRoundRect(0, 0, width, 16, 6, 6);
  204.                 g.endFill();
  205.                
  206.                 debugTextField.visible = false;
  207.                 clearButton.visible = false;
  208.                 autoScrollCheckBox.visible = false;
  209.                 clearButton.visible = false;
  210.                 copyButton.visible = false;
  211.             }
  212.         }
  213.        
  214.         private function scrollIt():void
  215.         {
  216.             if(this.autoScrollCheckBox.selected)
  217.             {
  218.                 debugTextField.scrollV = debugTextField.maxScrollV;
  219.             }
  220.         }
  221.        
  222.        
  223.         private function onAdded(event:Event):void
  224.         {
  225.             instances.push(this);
  226.             if(this.isPopUp)
  227.             {
  228.                 try
  229.                 {
  230.                     var so:SharedObject = SharedObject.getLocal(Debug.SO_NAME);
  231.                     if(so != null)
  232.                     {
  233.                         if(so.data.pos != null)
  234.                         {
  235.                             if(so.data.pos.x != undefined && so.data.pos.y != undefined)
  236.                             {
  237.                                 move(so.data.pos.x, so.data.pos.y);
  238.                             }
  239.                         }
  240.                     }
  241.                 }
  242.                 catch(err:Error)
  243.                 {
  244.                 }
  245.             }
  246.            
  247.         }
  248.        
  249.         private function onRemoved(event:Event):void
  250.         {
  251.             instances.splice(instances.indexOf(this), 1);
  252.             if(instances.length < 1)
  253.                 _inst = null;
  254.         }
  255.        
  256.         private function onClear(event:MouseEvent):void
  257.         {
  258.             backLog             = [];
  259.             debugTextField.htmlText = "";
  260.         }
  261.        
  262.         private function onCopy(event:MouseEvent):void
  263.         {
  264.             System.setClipboard(debugTextField.htmlText);
  265.         }
  266.        
  267.         private function onMouseDown(event:MouseEvent):void
  268.         {
  269.             if(dragging == false)
  270.             {
  271.                 dragging = true;
  272.                 stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp, false, 0, true);
  273.                 this.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
  274.                 this.startDrag(false);
  275.             }
  276.         }
  277.        
  278.         private function onMouseMove(event:MouseEvent):void
  279.         {
  280.             event.updateAfterEvent();
  281.         }
  282.        
  283.         private function onMouseUp(event:MouseEvent):void
  284.         {
  285.             if(dragging)
  286.             {
  287.                 dragging = false;
  288.                 this.removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
  289.                 stage.removeEventListener(MouseEvent.MOUSE_UP, onMouseUp);
  290.                 stopDrag();
  291.                 event.updateAfterEvent();
  292.                
  293.                 var so:SharedObject = SharedObject.getLocal(Debug.SO_NAME);
  294.                 so.data.pos = {x: x, y: y};
  295.             }
  296.         }
  297.        
  298.         private function onMouseDownDevice(event:MouseEvent):void
  299.         {
  300.             if(dragging == false)
  301.             {
  302.                 dragging = true;
  303.                 stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUpDevice, false, 0, true);
  304.                 this.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
  305.                 this.startDrag(false);
  306.             }
  307.         }
  308.        
  309.         private function onMouseUpDevice(event:MouseEvent):void
  310.         {
  311.             if(dragging)
  312.             {
  313.                 dragging = false;
  314.                 this.removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
  315.                 stage.removeEventListener(MouseEvent.MOUSE_UP, onMouseUpDevice);
  316.                 stopDrag();
  317.                 event.updateAfterEvent();
  318.                
  319.                 var so:SharedObject = SharedObject.getLocal(Debug.SO_NAME);
  320.                 so.data.pos = {x: x, y: y};
  321.             }
  322.         }
  323.        
  324.         private function onMinMax(event:MouseEvent):void
  325.         {
  326.             if(minMaxButton.selected)
  327.             {
  328.                 minMaxButton.label = "+";
  329.             }
  330.             else
  331.             {
  332.                 minMaxButton.label = "-";
  333.             }
  334.                
  335.             invalidateDisplayList();
  336.         }
  337.        
  338.        
  339.         // -- instance methods --
  340.         private function write(o:*):void
  341.         {
  342.             debugTextField.htmlText += o + "\n";
  343.             scrollIt();
  344.         }
  345.        
  346.        
  347.         public function log(o:*):void
  348.         {
  349.             if(debugging == false) return;
  350.             trace(o);
  351.             //debugTextField.appendText(o + "\n");
  352.             write(o);
  353.         }
  354.        
  355.         public function logHeader():void
  356.         {
  357.             if(debugging == false) return;
  358.             trace(HEADER);
  359.             write(HEADER + "\n");
  360.         }
  361.        
  362.         public function debug(o:*):void
  363.         {
  364.             if(debugging == false) return;
  365.             trace(o);
  366.             write(formatDebug(o));
  367.         }
  368.        
  369.         public function debugHeader():void
  370.         {
  371.             if(debugging == false) return;
  372.             trace(HEADER);
  373.             write(formatDebug(HEADER));
  374.         }
  375.        
  376.         public function info(o:*):void
  377.         {
  378.             if(debugging == false) return;
  379.             trace(o);
  380.             write(formatInfo(o));
  381.         }
  382.        
  383.         public function infoHeader():void
  384.         {
  385.             if(debugging == false) return;
  386.             trace(HEADER);
  387.             write(formatInfo(HEADER));
  388.         }
  389.        
  390.         public function warn(o:*):void
  391.         {
  392.             if(debugging == false) return;
  393.             trace(o);
  394.             write(formatInfo(o));
  395.         }
  396.        
  397.         public function warnHeader():void
  398.         {
  399.             if(debugging == false) return;
  400.             trace(HEADER);
  401.             write(formatWarn(HEADER));
  402.         }
  403.        
  404.         public function error(o:*):void
  405.         {
  406.             if(debugging == false) return;
  407.             trace(o);
  408.             write(formatError(o));
  409.         }
  410.        
  411.         public function errorHeader():void
  412.         {
  413.             if(debugging == false) return;
  414.             trace(HEADER);
  415.             write(formatError(HEADER));
  416.         }
  417.        
  418.         // -- static methods --
  419.         public static function log(o:*):void
  420.         {
  421.             if(debugging == false) return;
  422.             if(_inst)
  423.             {
  424.                 _inst.log(o);
  425.             }
  426.             else
  427.             {
  428.                 backLog.push({func: log, message: o});
  429.             }
  430.         }
  431.        
  432.         public static function logHeader():void
  433.         {
  434.             if(debugging == false) return;
  435.             if(_inst)
  436.             {
  437.                 _inst.logHeader();
  438.             }
  439.             else
  440.             {
  441.                 backLog.push({func: logHeader});
  442.             }
  443.         }
  444.        
  445.         public static function debug(o:*):void
  446.         {
  447.             if(debugging == false) return;
  448.             if(_inst)
  449.             {
  450.                 _inst.debug(o);
  451.             }
  452.             else
  453.             {
  454.                 backLog.push({func: debug, message: o});
  455.             }
  456.         }
  457.        
  458.         public static function debugHeader():void  
  459.         {
  460.             if(debugging == false) return;
  461.             if(_inst)
  462.             {
  463.                 _inst.debugHeader();
  464.             }
  465.             else
  466.             {
  467.                 backLog.push({func: debugHeader});
  468.             }
  469.         }
  470.        
  471.         public static function info(o:*):void
  472.         {
  473.             if(debugging == false) return;
  474.             if(_inst)
  475.             {
  476.                 _inst.info(o);
  477.             }
  478.             else
  479.             {
  480.                 backLog.push({func: info, message: o});
  481.             }
  482.         }
  483.        
  484.         public static function infoHeader():void   
  485.         {
  486.             if(debugging == false) return;
  487.             if(_inst)
  488.             {
  489.                 _inst.infoHeader();
  490.             }
  491.             else
  492.             {
  493.                 backLog.push({func: infoHeader});
  494.             }
  495.         }
  496.        
  497.         public static function warn(o:*):void
  498.         {
  499.             if(debugging == false) return;
  500.             if(_inst)
  501.             {
  502.                 _inst.warn(o);
  503.             }
  504.             else
  505.             {
  506.                 backLog.push({func: warn, message: o});
  507.             }
  508.         }
  509.        
  510.         public static function warnHeader():void   
  511.         {
  512.             if(debugging == false) return;
  513.             if(_inst)
  514.             {
  515.                 _inst.warnHeader();
  516.             }
  517.             else
  518.             {
  519.                 backLog.push({func: warnHeader});
  520.             }
  521.         }
  522.        
  523.         public static function error(o:*):void
  524.         {
  525.             if(debugging == false) return;
  526.             if(_inst)
  527.             {
  528.                 _inst.error(o);
  529.             }
  530.             else
  531.             {
  532.                 backLog.push({func: error, message: o});
  533.             }
  534.         }
  535.        
  536.         public static function errorHeader():void  
  537.         {
  538.             if(debugging == false) return;
  539.             if(_inst)
  540.             {
  541.                 _inst.errorHeader();
  542.             }
  543.             else
  544.             {
  545.                 backLog.push({func: errorHeader});
  546.             }
  547.         }
  548.        
  549.         // -- formatters --
  550.         private function formatError(o:*):String
  551.         {
  552.             return "<font family='_sans' size='9' color='" + ERROR_COLOR + "'>" + o + "</font>";
  553.         }
  554.        
  555.         private function formatDebug(o:*):String
  556.         {
  557.             return "<font family='_sans' size='9' color='" + DEBUG_COLOR + "'>" + o + "</font>";
  558.         }
  559.        
  560.         private function formatInfo(o:*):String
  561.         {
  562.             return "<font family='_sans' size='9' color='" + INFO_COLOR + "'>" + o + "</font>";
  563.         }
  564.        
  565.         private function formatWarn(o:*):String
  566.         {
  567.             return "<font family='_sans' size='9' color='" + WARN_COLOR + "'>" + o + "</font>";
  568.         }
  569.        
  570.     }
  571. }
Advertisement
Add Comment
Please, Sign In to add comment