package com.dVyper.utils { // import flash.display.Shape; import flash.display.Sprite; import flash.display.Stage; import flash.events.Event; import flash.events.KeyboardEvent; import flash.events.TimerEvent; import flash.text.TextField; import flash.text.TextFormat; import flash.utils.Timer; // public class dPanel { // private static var firstTrace:Boolean = true; private static var myMaxLines:int; private static var myPanel:Sprite; private static var myTextField:TextField; private static var myTextColour:int; private static var myTimer:Timer; private static var myTransitionKey:int; private static var onStage:Boolean = false; private static var scrollBar:BespokeScrollBar; private static var stage:Stage; // public static function init(stageReference:Stage, noOfLines:int = 6, transitionKey:int = 96, colour:int = 0x000000, textColour:int = 0xFFFFFF, Alpha:Number = 0.8):void { if (stage != null) return; stage = stageReference; Alert.init(stage); myMaxLines = noOfLines; if (myMaxLines < 3) myMaxLines = 3; myTransitionKey = transitionKey; myTextColour = textColour; stage.addEventListener(KeyboardEvent.KEY_UP, doTransition); // begin construction of the panel myPanel = new Sprite(); var myBackground:Shape = createBackground(colour, noOfLines); myBackground.alpha = Alpha; myPanel.addChild(myBackground); myPanel.addChild(createTextField(noOfLines)); scrollBar = new BespokeScrollBar(myMaxLines, colour, myTextField, myPanel.width); myPanel.addChild(scrollBar); myPanel.y = 0-myBackground.height; stage.addEventListener(Event.RESIZE, resizeDebugWindow) myTimer = new Timer(50); } public static function clear():void { if (stage == null) return; myTextField.text = ""; firstTrace = true; scrollBar.update(); } private static function createBackground(colour:int, noOfLines:int):Shape { var Background:Shape = new Shape(); Background.graphics.beginFill(colour); var myHeight:Number; if (noOfLines == 1) { myHeight = 24; } else { myHeight = (noOfLines*14)+14; } Background.graphics.drawRect(0, 0, stage.stageWidth, myHeight); Background.graphics.endFill(); return Background; } private static function createTextField(noOfLines:int):TextField { myTextField = new TextField(); myTextField.x = 4; myTextField.y = 4; myTextField.width = stage.stageWidth-8; var myHeight:Number; if (noOfLines == 1) { myHeight = 16; } else { myHeight = (noOfLines*14)+4; } myTextField.height = myHeight; myTextField.border = true; myTextField.wordWrap = true; return myTextField; } private static function doTransition(event:KeyboardEvent):void { if (event.charCode == myTransitionKey) { if (onStage) { myTimer.removeEventListener(TimerEvent.TIMER, moveIn); myTimer.addEventListener(TimerEvent.TIMER, moveOut); onStage = false } else { myTimer.removeEventListener(TimerEvent.TIMER, moveOut); myTimer.addEventListener(TimerEvent.TIMER, moveIn); onStage = true } myTimer.start(); } } private static function moveIn(event:TimerEvent):void { stage.addChild(myPanel); if (myPanel.y < 0) { myPanel.y = Math.ceil(myPanel.y*0.7); } else { myTimer.stop(); myTimer.removeEventListener(TimerEvent.TIMER, moveIn); } } private static function moveOut(event:TimerEvent):void { if (myPanel.y > 0-myPanel.height) { myPanel.y = -1*Math.ceil(myPanel.height-((myPanel.height-(myPanel.y*-1))*0.7)); } else { stage.removeChild(myPanel); myTimer.stop(); myTimer.removeEventListener(TimerEvent.TIMER, moveOut); } } private static function resizeDebugWindow(event:Event):void { myPanel.width = stage.stageWidth; } public static function trace(Text:* = "Made by Dark Vyper", showDPanel:Boolean = false, newLine:Boolean = true):void { if (stage == null) return; if (Text == undefined) { trace("undefined"); return; } if (firstTrace) { newLine = false; firstTrace = false; } if (newLine) { myTextField.appendText("\n"+Text.toString().split("\r")); } else { myTextField.appendText(Text.toString().split("\r")); } var myFormat:TextFormat = new TextFormat("Verdana", 12, myTextColour); myTextField.setTextFormat(myFormat); myTextField.scrollV = myTextField.maxScrollV; if (showDPanel) { myTimer.removeEventListener(TimerEvent.TIMER, moveOut); myTimer.addEventListener(TimerEvent.TIMER, moveIn); onStage = true; myTimer.start(); } if (myTextField.numLines > myMaxLines) { scrollBar.update(); } } public static function get text():String { if (stage == null) return "dPanel not initialised!"; return myTextField.text; } } } import flash.display.Sprite; import flash.events.Event; import flash.events.MouseEvent; import flash.text.TextField; class BespokeScrollBar extends Sprite { // private var Colour:int; private var size:int; private var btnUp:Sprite; private var btnDown:Sprite; private var bar:Sprite; private var theTextField:TextField; private var barHeight:int; private var yOffset:Number; private var yMax:Number; private var yMin:Number; // public function BespokeScrollBar(noOfLines:int, colour:int, textField:TextField, panelWidth:Number):void { Colour = colour; size = noOfLines; theTextField = textField; yMin = 17; barHeight = (size*14)-29; btnUp = createButton("up"); btnDown = createButton("down"); bar = createBar(); addChild(btnUp); addChild(btnDown); addChild(bar); x = panelWidth-20; y = 4; yMax = (size*14)-29-bar.height+yMin; addEventListener(Event.ADDED_TO_STAGE, doAddListeners); addEventListener(Event.REMOVED_FROM_STAGE, doRemoveListeners); } private function createBar():Sprite { var myBar:Sprite = new Sprite(); myBar.graphics.beginFill(0x666666); myBar.graphics.drawRect(0, 0, 16, barHeight); myBar.graphics.endFill(); myBar.addEventListener(MouseEvent.MOUSE_DOWN, doMouseDown); myBar.y = yMin; return myBar; } private function createButton(type:String):Sprite { var btn:Sprite = new Sprite(); // draw btn base btn.graphics.beginFill(Colour); btn.graphics.drawRect(0, 0, 16, 16); btn.graphics.endFill(); // draw button arrow btn.graphics.lineStyle(1); btn.graphics.beginFill(0x999999); if (type == "up") { btn.graphics.moveTo(8, 2); btn.graphics.lineTo(14, 13); btn.graphics.lineTo(2, 13); btn.graphics.lineTo(8, 2); btn.addEventListener(MouseEvent.CLICK, moveUp); btn.y = 1; } else if (type == "down") { btn.graphics.moveTo(2, 2); btn.graphics.lineTo(14, 2); btn.graphics.lineTo(8, 13); btn.graphics.lineTo(2, 2); btn.y = (size*14)-12; btn.addEventListener(MouseEvent.CLICK, moveDown); } btn.graphics.endFill(); return btn; } private function doAddListeners(myEvent:Event):void { stage.addEventListener(MouseEvent.MOUSE_UP, doStopDrag); } private function doMouseDown(myEvent:MouseEvent):void { stage.addEventListener(MouseEvent.MOUSE_MOVE, doDrag); yOffset = mouseY - bar.y; } private function doDrag(myEvent:MouseEvent):void { bar.y = mouseY - yOffset; if(bar.y < yMin) bar.y = yMin; if(bar.y > yMax) bar.y = yMax; doScroll(); myEvent.updateAfterEvent(); } private function doRemoveListeners(myEvent:Event):void { stage.removeEventListener(MouseEvent.MOUSE_UP, doStopDrag); } private function doScroll():void { var percentage:Number = (bar.y - yMin)/(yMax - yMin); theTextField.scrollV = Math.ceil(percentage*theTextField.maxScrollV); } private function doStopDrag(myEvent:MouseEvent):void { stage.removeEventListener(MouseEvent.MOUSE_MOVE, doDrag); } private function moveDown(myEvent:MouseEvent):void { theTextField.scrollV = theTextField.scrollV+1; update(); } private function moveUp(myEvent:MouseEvent):void { theTextField.scrollV = theTextField.scrollV-1; update(); } public function update():void { bar.y = yMin+(barHeight/theTextField.numLines*(theTextField.scrollV-1)); if (theTextField.numLines>size) { bar.height = (size/theTextField.numLines)*barHeight; } else { bar.y = yMin; bar.height = barHeight; } yMax = (size*14)-29-bar.height+yMin; } }