Tutorials, extensions, and source files for ActionScript, Flash, and other Adobe products.

 

senocular.com ActionScript Library

Output.as

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package com.senocular.utils {
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.display.GradientType;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Matrix;
	import flash.text.TextField;
	import flash.text.TextFieldType;
	import flash.text.TextFormat;
	import flash.text.TextFormatAlign;
	import flash.text.TextFieldAutoSize;
	
	/**
	 * Creates a pseudo Output panel in a publish
	 * swf for displaying trace statements.
	 * For the output panel to capture trace 
	 * statements, you must use Output.trace()
	 * and add an instance to the stage:
	 * stage.addChild(new Output());
	 *
	 * Note: You may want to place Output in an
	 * unnamed package to make it easier to
	 * trace within your classes without having
	 * to import com.senocular.utils.Output.
	 */
	public class Output extends Sprite {
		private var output_txt:TextField;
		private var titleBar:Sprite;
		private static var instance:Output;
		private static var autoExpand:Boolean = true;
		private static var maxLength:int = 1000;
		
		public function Output(outputHeight:uint = 150){
			if (instance && instance.parent){
				instance.parent.removeChild(instance);
			}
			
			instance = this;
			addChild(newOutputField(outputHeight));
			addChild(newTitleBar());
			
			addEventListener(Event.ADDED, added);
			addEventListener(Event.REMOVED, removed);
		}
		
		// public methods
		public static function trace(str:*):void {
			if (!instance) return;
			instance.output_txt.appendText(str+"\n");
			if (instance.output_txt.length > maxLength) {
				instance.output_txt.text = instance.output_txt.text.slice(-maxLength);
			}
			instance.output_txt.scrollV = instance.output_txt.maxScrollV;
			if (autoExpand && !instance.output_txt.visible) instance.toggleCollapse();
		}
		public static function clear():void {
			if (!instance) return;
			instance.output_txt.text = "";
		}
		
		private function newOutputField(outputHeight:uint):TextField {
			output_txt = new TextField();
			output_txt.type = TextFieldType.INPUT;
			output_txt.border = true;
			output_txt.borderColor = 0;
			output_txt.background = true;
			output_txt.backgroundColor = 0xFFFFFF;
			output_txt.height = outputHeight;
			var format:TextFormat = output_txt.getTextFormat();
			format.font = "_typewriter";
			output_txt.setTextFormat(format);
			output_txt.defaultTextFormat = format;
			return output_txt;
		}
		private function newTitleBar():Sprite {
			var barGraphics:Shape = new Shape();
			barGraphics.name = "bar";
			var colors:Array = new Array(0xE0E0F0, 0xB0C0D0, 0xE0E0F0);
			var alphas:Array = new Array(1, 1, 1);
			var ratios:Array = new Array(0, 50, 255);
			var gradientMatrix:Matrix = new Matrix();
			gradientMatrix.createGradientBox(18, 18, Math.PI/2, 0, 0);
			barGraphics.graphics.lineStyle(0);
			barGraphics.graphics.beginGradientFill(GradientType.LINEAR, colors, alphas, ratios, gradientMatrix);
			barGraphics.graphics.drawRect(0, 0, 18, 18);
			
			var barLabel:TextField = new TextField();
			barLabel.autoSize = TextFieldAutoSize.LEFT;
			barLabel.selectable = false;
			barLabel.text = "Output";
			var format:TextFormat = barLabel.getTextFormat();
			format.font = "_sans";
			barLabel.setTextFormat(format);
			
			titleBar = new Sprite();
			titleBar.addChild(barGraphics);
			titleBar.addChild(barLabel);
			return titleBar;
		}
		
		// Event handlers
		private function added(evt:Event):void {
			stage.addEventListener(Event.RESIZE, fitToStage);
			titleBar.addEventListener(MouseEvent.CLICK, toggleCollapse);
			fitToStage();
			toggleCollapse();
		}
		private function removed(evt:Event):void {
			stage.removeEventListener(Event.RESIZE, fitToStage);
			titleBar.removeEventListener(MouseEvent.CLICK, toggleCollapse);
		}
		private function toggleCollapse(evt:Event = null):void {
			if (!instance) return;
			output_txt.visible = !output_txt.visible;
			fitToStage(evt);
		}
		private function fitToStage(evt:Event = null):void {
			if (!stage) return;
			output_txt.width = stage.stageWidth;
			output_txt.y = stage.stageHeight - output_txt.height;
			titleBar.y = (output_txt.visible) ? output_txt.y - titleBar.height : stage.stageHeight - titleBar.height;
			titleBar.getChildByName("bar").width = stage.stageWidth;
		}
	}
}