操作:
鼠标拖动
上下左右键移动1像素
按住shift再按上下左右键移动5像素
视图如下:
package com.lsp.core.utils
{
import com.lsp.core.component.Message;
import flash.display.DisplayObject;
import flash.display.DisplayObjectContainer;
import flash.display.Sprite;
import flash.events.FocusEvent;
import flash.events.KeyboardEvent;
import flash.events.MouseEvent;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.text.TextFormatAlign;
import flash.utils.Dictionary;
import flash.utils.getQualifiedClassName;
import flash.utils.setTimeout;
import game.GameProxy;
/**
* 使用方式
* UIConfig.getInstance().bindDisplayObject(your displayObject);
* @yaolihua qq:854963846
* 上下左右控制对象移动 每次移动一个像素
* 同时按下shift 每次移动5像素
*/
public class UIConfig
{
private static var instance:UIConfig;
private var interactiveObjectList:Dictionary;
private var startPosition:Dictionary;
public var endPosition:Dictionary;
private var positionTxt:Dictionary;
private var borderDict:Dictionary;
private var keyDict:Dictionary;
private var sub:int = 15;
private var currentObject:Sprite;
private var currentMouseObject:Sprite;
private var parentName:String = "";
public function UIConfig()
{
interactiveObjectList = new Dictionary();
startPosition = new Dictionary();
endPosition = new Dictionary();
positionTxt = new Dictionary();
borderDict = new Dictionary();
keyDict = new Dictionary();
}
public static function getInstance():UIConfig
{
return instance = instance || new UIConfig();
}
public function bindDisplayObject(displayObj:DisplayObject):void
{
setTimeout(function():void
{
var sprite:Sprite = new Sprite();
sprite.name = displayObj.name;
var x:Number = displayObj.x;
var y:Number = displayObj.y;
var width:Number = displayObj.width;
var height:Number = displayObj.height;
if(null == displayObj.parent)
{
throw new Error("目标对象不在舞台上....");
}
parentName = getQualifiedClassName(displayObj.parent);
displayObj.parent.addChildAt(sprite, displayObj.parent.getChildIndex(displayObj));
sprite.x = x;
sprite.y = y;
sprite.addChild(displayObj);
sprite.mouseChildren = false;
displayObj.x = 0;
displayObj.y = 0;
sprite.width = width;
sprite.height = height;
sprite.addEventListener(MouseEvent.CLICK, onMouseClickHandler);
sprite.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDownHandler);
sprite.addEventListener(MouseEvent.MOUSE_UP, onMouseUpHandler);
sprite.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMoveHandler);
sprite.addEventListener(MouseEvent.DOUBLE_CLICK, onDoubleClickHandler);
interactiveObjectList[sprite.name] = sprite;
startPosition[sprite.name] = [sprite.x, sprite.y];
createFourTxtField(sprite);
GameProxy.stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDownHandler);
GameProxy.stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUpHandler);
},1000);
}
private function createFourTxtField(sprite:Sprite):void
{
var tfStartPoint:TextField = createTxtField();
tfStartPoint.htmlText = "<font face='SimSun'>("+sprite.x + "," + sprite.y+")</font>";
tfStartPoint.width = tfStartPoint.textWidth + 5;
tfStartPoint.height = tfStartPoint.textHeight + 3;
tfStartPoint.x = sprite.x - 30;
tfStartPoint.y = sprite.y - 22;
sprite.parent.addChild(tfStartPoint);
tfStartPoint.filters = [ColorFilterUtils.GLOW_FILTER_BLACK];
tfStartPoint.mouseEnabled = false;
var tfPoint:TextField = createTxtField();
tfPoint.text = "("+sprite.x + "," + sprite.y+")";
tfPoint.width = tfPoint.textWidth + 5;
tfPoint.height = tfPoint.textHeight + 3;
tfPoint.x = sprite.x + 5;
tfPoint.y = sprite.y + 5;
sprite.parent.addChild(tfPoint);
tfPoint.filters = [ColorFilterUtils.GLOW_FILTER_BLACK];
tfPoint.mouseEnabled = false;
var tfX:TextField = createTxtField();
tfX.text = "x\r||\r"+(sprite.x - startPosition[sprite.name][0]);
tfX.width = tfX.textWidth + 5;
tfX.height = tfX.textHeight + 20;
tfX.x = sprite.x - sub;
tfX.y = sprite.y;
sprite.parent.addChild(tfX);
tfX.filters = [ColorFilterUtils.GLOW_FILTER_BLACK];
tfX.mouseEnabled = false;
var tfY:TextField = createTxtField();
tfY.text = "y=" +(sprite.y - startPosition[sprite.name][1]);
tfY.width = tfY.textWidth + 5;
tfY.height = tfY.textHeight + 3;
tfY.x = sprite.x;
tfY.y = sprite.y - sub;
sprite.parent.addChild(tfY);
tfY.filters = [ColorFilterUtils.GLOW_FILTER_BLACK];
tfY.mouseEnabled = false;
positionTxt[sprite.name] = [tfPoint, tfX, tfY, tfStartPoint];
}
private function changeTxtValueAndPosition(colorIndex:int):void
{
if(null == currentObject)
{
return;
}
var tfStartPoint:TextField = positionTxt[currentObject.name][3] as TextField;
tfStartPoint.x = currentObject.x - 30;
tfStartPoint.y = currentObject.y - 22;
tfStartPoint.htmlText = "<font color='"+COLOR[colorIndex]+"' face='SimSun'>"+tfStartPoint.text+"</font>";
tfStartPoint.visible = true;
var tfPoint:TextField = positionTxt[currentObject.name][0] as TextField;
tfPoint.text = "("+currentObject.x + "," + currentObject.y+")";
tfPoint.width = tfPoint.textWidth + 5;
tfPoint.height = tfPoint.textHeight + 3;
tfPoint.x = currentObject.x + 5;
tfPoint.y = currentObject.y + 5;
tfPoint.htmlText = "<font color='"+COLOR[colorIndex]+"'>"+tfPoint.text+"</font>";
tfPoint.visible = true;
var tfX:TextField = positionTxt[currentObject.name][1] as TextField;
tfX.text = "x\r||\r"+(currentObject.x - startPosition[currentObject.name][0]);
tfX.width = tfX.textWidth + 5;
tfX.height = tfX.textHeight + 20;
tfX.x = currentObject.x - sub;
tfX.y = currentObject.y;
tfX.htmlText = "<font color='"+COLOR[colorIndex]+"' >"+tfX.text+"</font>";
tfX.visible = true;
var tfY:TextField = positionTxt[currentObject.name][2] as TextField;
tfY.text = "y=" +(currentObject.y - startPosition[currentObject.name][1])
tfY.width = tfY.textWidth + 5;
tfY.height = tfY.textHeight + 3;
tfY.x = currentObject.x;
tfY.y = currentObject.y - sub;
tfY.htmlText = "<font color='"+COLOR[colorIndex]+"'>"+tfY.text+"</font>";
tfY.visible = true;
endPosition[currentObject.name] = [currentObject.x, currentObject.y];
}
private function onMouseMoveHandler(e:MouseEvent):void
{
if(null != currentMouseObject)
{
changeTxtValueAndPosition(colorIndex);
}
}
private function onDoubleClickHandler(e:MouseEvent):void
{
if(null != currentMouseObject)
{
currentMouseObject.stopDrag();
currentMouseObject = null;
}
}
private function onMouseUpHandler(e:MouseEvent):void
{
if(null != currentMouseObject)
{
currentMouseObject.stopDrag();
currentMouseObject = null;
}
}
private var mouseStartX:Number;
private var mouseStartY:Number;
private function onMouseDownHandler(e:MouseEvent):void
{
if(e.target != currentMouseObject)
{
currentMouseObject = e.target as Sprite;
currentMouseObject.startDrag();
}
}
private function onMouseClickHandler(e:MouseEvent):void
{
if(null == currentObject)
{
currentObject = interactiveObjectList[e.target.name];
currentObject.stage.focus = currentObject;
drawContainerBorder(currentObject);
changeTxtValueAndPosition(0);
}
else if(currentObject.name != e.target.name)
{
hideFrame();
currentObject = interactiveObjectList[e.target.name];
currentObject.stage.focus = currentObject;
drawContainerBorder(currentObject);
changeTxtValueAndPosition(0);
}
}
private const COLOR:Array=["#FF0000", "#FFFFFF", "#66CC00", "#0099FF", "#D11AFF", "#FFD528", "#FF6633", "#D70000"];
private const ENTER:int = 13;
private const LEFT:int = 37;
private const UP:int = 38;
private const RIGHT:int = 39;
private const DOWN:int = 40;
private const SHIFT:int = 16;
private const PAUSE:int = 187;
private var colorIndex:int = 0;
private var multiple:int = 1;
private function onKeyDownHandler(e:KeyboardEvent):void
{
keyDict[e.keyCode] = 1;
if(null == currentObject)
{
Message.getInstance().showMessage(["请先选中【"+parentName.split("::")[1]+"】移动的对象,选中后会出现黄边"]);
return;
}
if(null != keyDict[SHIFT] && 1 == keyDict[SHIFT])
{
multiple = 5;
}
else
{
multiple = 1;
}
switch(e.keyCode)
{
case LEFT:
currentObject.x -= 1*multiple;
break;
case UP:
currentObject.y -= 1*multiple;
break;
case RIGHT:
currentObject.x += 1*multiple;
break;
case DOWN:
currentObject.y += 1*multiple;
break;
default:break;
}
if(PAUSE == e.keyCode)
{
colorIndex = (int(Math.random()*7));
}
//显示所有的位置文本
if(ENTER == e.keyCode)
{
showAllTxt();
}
changeTxtValueAndPosition(colorIndex);
}
private function showAllTxt():void
{
for each(var arr:Array in positionTxt)
{
for(var i:int = 0; i < arr.length; i++)
{
arr[i].visible = true;
}
}
}
protected function onKeyUpHandler(e:KeyboardEvent):void
{
keyDict[e.keyCode] = 0;
}
private function hideFrame():void
{
for each(var spt:Sprite in borderDict[currentObject.name])
{
spt.visible = false;
}
for(var i:int = 0; i < positionTxt[currentObject.name].length; i++)
{
positionTxt[currentObject.name][i].visible = false;
}
}
protected function onFocusInHandler(e:FocusEvent):void
{
if(null != borderDict[e.target.name])
{
for each(var spt:Sprite in borderDict[e.target.name])
{
spt.visible = false;
}
}
}
private function createTxtField():TextField
{
var txtField:TextField = new TextField();
txtField.selectable = false;
var txtFormat:TextFormat = new TextFormat();
txtFormat.color = 0xFF0000;
txtFormat.align = TextFormatAlign.LEFT;
txtFormat.size = 10;
txtField.defaultTextFormat = txtFormat;
return txtField;
}
private function drawContainerBorder(container:DisplayObjectContainer, lineHeight:int = 1, color:int = 0xFFFF00):void
{
if(null == borderDict[container.name])
{
var width:Number = container.width;
var height:Number = container.height;
var arr:Array = [];
var alphaNum:Number = 0.2;
if(0 == width || 0 == height)
{
throw new Error("target displayObject width:0, height:0, can not draw border..");
}
var topBorderSprite:Sprite = new Sprite();
topBorderSprite.graphics.beginFill(color);
topBorderSprite.graphics.drawRect(0, 0, width, lineHeight);
topBorderSprite.graphics.endFill();
container.addChild(topBorderSprite);
topBorderSprite.alpha = alphaNum;
arr[arr.length] = topBorderSprite;
var bottomBorderSprite:Sprite = new Sprite();
bottomBorderSprite.graphics.beginFill(color);
bottomBorderSprite.graphics.drawRect(0, height-lineHeight, width, lineHeight);
bottomBorderSprite.graphics.endFill();
container.addChild(bottomBorderSprite);
bottomBorderSprite.alpha = alphaNum;
arr[arr.length] = bottomBorderSprite;
var leftBorderSprite:Sprite = new Sprite();
leftBorderSprite.graphics.beginFill(color);
leftBorderSprite.graphics.drawRect(0, 0, lineHeight, height);
leftBorderSprite.graphics.endFill();
container.addChild(leftBorderSprite);
leftBorderSprite.alpha = alphaNum;
arr[arr.length] = leftBorderSprite;
var rightBorderSprite:Sprite = new Sprite();
rightBorderSprite.graphics.beginFill(color);
rightBorderSprite.graphics.drawRect(width-lineHeight, 0,lineHeight, height);
rightBorderSprite.graphics.endFill();
container.addChild(rightBorderSprite);
arr[arr.length] = rightBorderSprite;
rightBorderSprite.alpha = alphaNum;
borderDict[container.name] = arr;
}
else
{
for each(var spt:Sprite in borderDict[container.name])
{
spt.visible = true;
}
}
}
}
}