Created
October 27, 2013 15:29
-
-
Save ifree/7183737 to your computer and use it in GitHub Desktop.
simple console for as3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.ifree.common | |
{ | |
import flash.display.Sprite; | |
import flash.events.Event; | |
import flash.events.KeyboardEvent; | |
import flash.events.MouseEvent; | |
import flash.text.TextField; | |
import flash.text.TextFieldType; | |
import flash.text.TextFormat; | |
import flash.ui.Keyboard; | |
import flash.utils.getTimer; | |
/** | |
* ... | |
* @author ifree | |
* Debug utility,just like firebug | |
*/ | |
public class Console extends Sprite | |
{ | |
private var mContentPanel:Sprite; | |
private var mInputBox:TextField; | |
private var mTxtContent:TextField; | |
private var mBtnSubmit:_SimpleButton; | |
private var motion:_MotionAdapter; | |
public function Console() | |
{ | |
mContentPanel = new Sprite; | |
mInputBox = new TextField; | |
mInputBox.type = TextFieldType.INPUT; | |
mTxtContent = new TextField; | |
mTxtContent.multiline = true; | |
mBtnSubmit = new _SimpleButton(); | |
motion = new _MotionAdapter(mTxtContent); | |
this.addEventListener(Event.ADDED_TO_STAGE, onAdded); | |
} | |
//------------------console API-------------------------- | |
private var mInfo:_CommandLineIO; | |
private var mWarn:_CommandLineIO; | |
private var mErr:_CommandLineIO; | |
private var mTime:int = 0; | |
private var mGroupStack:Array = []; | |
private function isSimple(value:Object):Boolean | |
{ | |
var type:String = typeof(value); | |
switch (type) | |
{ | |
case "number": | |
case "string": | |
case "boolean": | |
{ | |
return true; | |
} | |
} | |
if (value is Date) | |
return true; | |
if (value is Array) | |
return true; | |
return false; | |
} | |
private function initCommandLine():void | |
{ | |
var infoFormat:TextFormat = new TextFormat(); | |
var warnFormat:TextFormat = new TextFormat(); | |
var errFormat:TextFormat = new TextFormat(); | |
infoFormat.color = 0x0000cc; | |
warnFormat.color = 0xfff000; | |
errFormat.color = 0xff0000; | |
mInfo = new _TextIO(mTxtContent, infoFormat); | |
mWarn = new _TextIO(mTxtContent, warnFormat); | |
mErr = new _TextIO(mTxtContent, errFormat); | |
} | |
private function dirObject(str:String,obj:Object):String | |
{ | |
for (var i:* in obj) { | |
trace(i+":"+obj[i]); | |
var att:*= obj[i]; | |
if (isSimple(att)) | |
str += i + " : " + att+", \n"; | |
else { | |
str += "{\n"; | |
str+=dirObject(str, att); | |
str += "}\n"; | |
} | |
} | |
return str; | |
} | |
private function _writeLine(objs:Array, io:_CommandLineIO, cmd:Function = null):void | |
{ | |
for (var i:Object in objs) { | |
if(cmd!=null) | |
io.writeLine(cmd(objs[i])); | |
else | |
io.writeLine(objs[i].toString()); | |
} | |
} | |
public function log(...rest):void | |
{ | |
_writeLine(rest, mInfo); | |
} | |
public function warn(...rest):void | |
{ | |
_writeLine(rest, mWarn); | |
} | |
public function info(...rest):void | |
{ | |
_writeLine(rest, mInfo); | |
} | |
public function trac3(...rest):void | |
{ | |
_writeLine(rest, mInfo); | |
} | |
public function err(...rest):void | |
{ | |
_writeLine(rest, mErr); | |
} | |
public function dir(...rest):void | |
{ | |
_writeLine(rest, mInfo, function(o:Object):String { | |
var s:String = "{\n"; | |
return dirObject(s, o) + "}\n";; | |
}); | |
} | |
public function time():void | |
{ | |
mTime = getTimer(); | |
trac3("Time start at: "+new Date()); | |
} | |
public function timeEnd():void | |
{ | |
trac3("Time end at: " + new Date()+"Time elapsed "+(getTimer() - mTime)); | |
mTime = 0; | |
} | |
public function group(name:String):void | |
{ | |
trac3("<---------------------[" + name + "] start" + "------------------------>"); | |
mGroupStack.push(name); | |
} | |
public function groupEnd():void | |
{ | |
if (mGroupStack.length <= 0) | |
return; | |
trac3("<---------------------[" + mGroupStack.pop() + "] end" + "------------------------>"); | |
} | |
public function clear():void | |
{ | |
mTxtContent.text = ""; | |
} | |
public function assert(expr:Function,target:*):void | |
{ | |
var test:Boolean = false; | |
try | |
{ | |
if (expr !=null) | |
test = expr(target); | |
if (!test) | |
err("Assertion failed: "+target+" is test failed with "+expr ); | |
}catch (e:Error) | |
{ | |
err("Assertion failed: " + target + " is test failed with " + expr ); | |
err(e.name+":"+e.message); | |
err(e.getStackTrace()); | |
} | |
} | |
//------------------console API-------------------------- | |
private function onResize(e:Event=null):void | |
{ | |
layout(); | |
} | |
private function onAdded(e:Event):void | |
{ | |
removeEventListener(Event.ADDED_TO_STAGE, onAdded); | |
stage.addEventListener(Event.RESIZE, onResize); | |
initialContent(); | |
setupEvents(); | |
initCommandLine(); | |
} | |
protected function initialContent():void | |
{ | |
addChild(mContentPanel); | |
addChild(mInputBox); | |
addChild(mBtnSubmit); | |
mContentPanel.addChild(mTxtContent); | |
mTxtContent.backgroundColor = 0xf02f23; | |
mTxtContent.borderColor = 0xfccccc; | |
//mTxtContent.background = true; | |
mTxtContent.selectable = false; | |
mInputBox.background = true; | |
mInputBox.backgroundColor = 0x239fda; | |
mInputBox.height = 20; | |
//mInputBox.opaqueBackground = true; | |
onResize(); | |
} | |
protected function layout():void | |
{ | |
//txtContent | |
mTxtContent.x = 0; | |
mTxtContent.y = 0; | |
mTxtContent.width = stage.stageWidth-50; | |
mTxtContent.height = stage.stageHeight - mInputBox.height - 50; | |
//inputBox | |
mInputBox.x = 0; | |
mInputBox.y = stage.stageHeight - 50; | |
mInputBox.width = stage.stageWidth - 100; | |
//btn | |
mBtnSubmit.x = stage.stageWidth - 50-mBtnSubmit.width; | |
mBtnSubmit.y = stage.stageHeight - 50; | |
} | |
private function setupEvents():void | |
{ | |
mBtnSubmit.addEventListener(MouseEvent.CLICK, onSubmit); | |
mInputBox.addEventListener(KeyboardEvent.KEY_UP, function(e:KeyboardEvent):void { | |
if (Keyboard.ENTER == e.keyCode) | |
onSubmit(null); | |
}); | |
motion.MotionCallback = handleMotion; | |
} | |
private function handleMotion(motion:uint,len:Number):void | |
{ | |
if ((motion & _MotionAdapter.MOTION_UP) != 0) | |
{ | |
mTxtContent.scrollV += len * 2; | |
}else if ((motion & _MotionAdapter.MOTION_DOWN) != 0) | |
{ | |
mTxtContent.scrollV -= len * 2; | |
} | |
} | |
private function onSubmit(e:MouseEvent):void | |
{ | |
mTxtContent.appendText(mInputBox.text); | |
mTxtContent.appendText("\n"); | |
mTxtContent.scrollV = mTxtContent.maxScrollV; | |
mInputBox.text = ""; | |
} | |
} | |
} | |
import flash.display.DisplayObject; | |
import flash.display.Sprite; | |
import flash.events.MouseEvent; | |
import flash.geom.Point; | |
import flash.text.TextField; | |
import flash.text.TextFormat; | |
import flash.utils.Dictionary; | |
class _CommandLineIO{ | |
protected var mHandle:*; | |
function _CommandLineIO(handle:*) { | |
mHandle = handle; | |
} | |
public function writeLine(str:String):void | |
{ | |
//virtual | |
} | |
public function readLine(index:int=NaN/*EOF*/):String | |
{ | |
return ""; | |
} | |
} | |
class _TextIO extends _CommandLineIO { | |
protected var mTextFormat:TextFormat; | |
function _TextIO(handle:TextField,format:TextFormat) { | |
super(handle); | |
mTextFormat = format; | |
} | |
override public function writeLine(str:String):void | |
{ | |
var index:int = mHandle.length; | |
mHandle.appendText(str+"\n"); | |
mHandle.setTextFormat(mTextFormat, index, mHandle.length); | |
} | |
override public function readLine(index:int = NaN):String | |
{ | |
if (isNaN(index)) | |
index = mHandle.numLines; | |
return mHandle.getLineText(index); | |
} | |
} | |
class _MotionAdapter { | |
function _MotionAdapter(target:DisplayObject) | |
{ | |
mTarget = target; | |
mTarget.addEventListener(MouseEvent.MOUSE_DOWN, mouseDown); | |
mTarget.addEventListener(MouseEvent.MOUSE_UP, mouseUp ); | |
mTarget.addEventListener(MouseEvent.MOUSE_MOVE, mouseMove); | |
} | |
public static const MOTION_LEFT:uint = 1; | |
public static const MOTION_RIGHT:uint = 2; | |
public static const MOTION_UP:uint = 4; | |
public static const MOTION_DOWN:uint = 8; | |
private var mTarget:DisplayObject; | |
private var mLastPos:Point = new Point; | |
private var mDragStart:Boolean = false; | |
//function(motion,len) | |
public var MotionCallback:Function; | |
private function mouseDown(e:MouseEvent):void | |
{ | |
mDragStart = true; | |
mLastPos.x = e.localX; | |
mLastPos.y = e.localY; | |
} | |
private function mouseUp(e:MouseEvent):void | |
{ | |
mDragStart = false; | |
mLastPos.x = mLastPos.y = 0; | |
} | |
private function mouseMove(e:MouseEvent):void | |
{ | |
if (!mDragStart) | |
return; | |
var curPos:Point = new Point(e.localX, e.localY); | |
var pt:Point = curPos.add(mLastPos); | |
pt.normalize(1); | |
var len:Number = pt.length; | |
var motion:uint; | |
if (curPos.y - mLastPos.y < 0){//is up | |
motion = MOTION_UP; | |
}else{ | |
motion = MOTION_DOWN; | |
} | |
if (curPos.x - mLastPos.x < 0){//is left | |
motion |= MOTION_LEFT; | |
}else{ | |
motion |= MOTION_DOWN; | |
} | |
MotionCallback && MotionCallback(motion, len); | |
mLastPos = curPos; | |
} | |
} | |
class _SimpleButton extends Sprite{ | |
function _SimpleButton(w:Number=40,h:Number=20) { | |
this.graphics.beginFill(0xcccccc); | |
this.graphics.drawRect(0, 0, w, h); | |
this.graphics.endFill(); | |
this.graphics.beginFill(0x0f001f); | |
this.graphics.drawRect(2, 2, w-4, h-4); | |
this.graphics.endFill(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment