中国开发网: 论坛: 程序员情感CBD: 贴子 240811
JoJo
还是Flash写得舒服
package koala.control {

import mx.core.*;
import mx.managers.*;
import flash.display.*;
import flash.events.*;

public class CompControl extends UIComponent {

/************************************************************************
Override Functions
************************************************************************/

override public function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number) : Void{
super.updateDisplayList(unscaledWidth, unscaledHeight);
clear();
drawBorder();
drawBoxs();
}

override public function initialize():Void{
super.initialize();
this.addEventListener(MouseEventType.MOUSE_DOWN, onMouseDownHandle);
this.addEventListener(MouseEventType.MOUSE_UP, onMouseUpHandle);
this.addEventListener(MouseEventType.MOUSE_MOVE, onMouseMoveHandle);
}

/************************************************************************
Graphics Functions
************************************************************************/

//Border Color
[Bindable]
public var bdColor:uint = 0;

//Force Color
[Bindable]
public var fcColor:uint = 0xFFFFFF;

//Box Color
public var ctColor:uint = 0xFFFFFF;

//Force Alpha
[Bindable]
public var fcAlpha:Number = 0.1;

//Control Box Size
[Bindable]
public var ctSize:uint = 6;

//Control Box Type Constans
private static var NW:uint = 0;
private static var N:uint = 1;
private static var NE:uint = 2
private static var W:uint = 3;
private static var E:uint = 4;
private static var SW:uint = 5;
private static var S:uint = 6;
private static var SE:uint = 7;

//Clear Graphics
private function clear():Void{
var g:Graphics = this.graphics;
g.clear();
}

//Draw Border
private function drawBorder():Void{
var g:Graphics = this.graphics;
g.lineStyle(0,bdColor);
g.beginFill(fcColor,fcAlpha);
g.drawRect(0,0,width,height);
g.endFill();
}

/************************************************************************
Control Box Functions
************************************************************************/

//Draw Control Boxs
private function drawBoxs():Void{
for (var i:uint=NW; i < SE+1; i++){
if (needBox(i)){
drawBox(i);
}
}
}

//Test Control Box Needs
private function needBox(cellType:uint):Boolean{
if (cellType == SE) return true;
if (width > ctSize*2 && height > ctSize*2){
return true;
}
if (width > ctSize*2 && height > ctSize){
switch (cellType){
case NW : return true;
case N : return true;
case NE : return true;
case SW : return true;
case S : return true;
case SE : return true;
default : return false;
}
}
if (height > ctSize*2 && width > ctSize){
switch (cellType){
case NW : return true;
case NE : return true;
case W : return true;
case E : return true;
case SW : return true;
case SE : return true;
default : return false;
}
}
if (width > ctSize && height > ctSize){
switch (cellType){
case NW : return true;
case SE : return true;
default : return false;
}
}
return false;
}

//Draw Box
private function drawBox(cellType:uint):Void{
var px:Number;
var py:Number;
switch (cellType){
case NW :
px = -ctSize / 2;
py = -ctSize / 2;
break;
case N :
px = (width - ctSize) / 2;
py = -ctSize / 2;
break;
case NE :
px = width - ctSize / 2;
py = -ctSize / 2;
break;
case W :
px = -ctSize / 2;
py = (height - ctSize) / 2;
break;
case E :
px = width - ctSize / 2;
py = (height - ctSize) / 2;
break;
case SW :
px = -ctSize / 2;
py = height - ctSize / 2;
break;
case S :
px = (width - ctSize) / 2;
py = height - ctSize / 2;
break;
case SE :
px = width - ctSize / 2;
py = height - ctSize / 2;
break;
default : return;
}
var g:Graphics = this.graphics;
g.lineStyle(0,ctColor);
g.beginFill(bdColor);
g.drawRect(px,py,ctSize,ctSize);
g.endFill();
}

private function pointInBox(tx:Number,ty:Number,cellType:uint):Boolean{
var px:Number;
var py:Number;
switch (cellType){
case NW :
px = -ctSize / 2;
py = -ctSize / 2;
break;
case N :
px = (width - ctSize) / 2;
py = -ctSize / 2;
break;
case NE :
px = width - ctSize / 2;
py = -ctSize / 2;
break;
case W :
px = -ctSize / 2;
py = (height - ctSize) / 2;
break;
case E :
px = width - ctSize / 2;
py = (height - ctSize) / 2;
break;
case SW :
px = -ctSize / 2;
py = height - ctSize / 2;
break;
case S :
px = (width - ctSize) / 2;
py = height - ctSize / 2;
break;
case SE :
px = width - ctSize / 2;
py = height - ctSize / 2;
break;
default : return false;
}
return tx >= px && tx <= px+ctSize && ty >= py && ty <= py+ctSize;
}

//Get Mouse Point Control Box
private function mouseBox():uint{
var mx:Number = this.mouseX;
var my:Number = this.mouseY;
for (var i:uint=NW; i < SE+1; i++){
if (needBox(i)){
if (pointInBox(mx,my,i)) return i;
}
}
return SE+1;
}

//Get Mouse Point Control Box Name
private function mouseBoxName():String{
return cellName(mouseBox());
}

//Translate Control Boxs Name
private function cellName(cellType:uint):String{
switch (cellType){
case NW :
return "NW";
case N :
return "N";
case NE :
return "NE";
case W :
return "W";
case E :
return "E";
case SW :
return "SW";
case S :
return "S";
case SE :
return "SE"
default : return "NONE";
}
}

/************************************************************************
Action Functions
************************************************************************/
private var __sx:Number;
private var __sy:Number;
private function startMove():Void{
__sx = parent.mouseX;
__sy = parent.mouseY;
this.setCapture();
this.startDrag(false,parent.getBounds(parent));
}

private function stopMove():Void{
this.stopDrag();
this.releaseCapture();
var nx:Number = x + parent.mouseX - __sx;
if (nx < 0) nx = 0;
var ny:Number = y + parent.mouseY - __sy;
if (ny < 0) ny = 0;
this.move(nx,ny);
}

private var __ow:Number;
private var __oh:Number;
private function startResize():Void{
this.setCapture();
__ow = width;
__oh = height;
__sx = parent.mouseX;
__sy = parent.mouseY;
}
private function doResize():Void{
var dx:Number = parent.mouseX - __sx;
var dy:Number = parent.mouseY - __sy;
switch (activeBox){
case NW :
x = parent.mouseX;
width = __ow - dx > 0 ? __ow - dx : 0;
y = parent.mouseY;
height = __oh - dy > 0 ? __oh - dy : 0;
return;
case N :
y = parent.mouseY;
height = __oh - dy > 0 ? __oh - dy : 0;
return;
case NE :
y = parent.mouseY;
height = __oh - dy > 0 ? __oh - dy : 0;
width = parent.mouseX - x > 0 ? parent.mouseX - x : 0;
return;
case W :
x = parent.mouseX;
width = __ow - dx > 0 ? __ow - dx : 0;
return;
case E :
width = parent.mouseX - x > 0 ? parent.mouseX - x : 0;
return;
case SW :
x = parent.mouseX;
width = __ow - dx > 0 ? __ow - dx : 0;
height = parent.mouseY - y > 0 ? parent.mouseY - y : 0;
return;
case S :
height = parent.mouseY - y > 0 ? parent.mouseY - y : 0;
return;
case SE :
width = parent.mouseX - x > 0 ? parent.mouseX - x : 0;
height = parent.mouseY - y > 0 ? parent.mouseY - y : 0;
return;
}
}
private function stopResize():Void{
this.releaseCapture();
}

/************************************************************************
Event Handle
************************************************************************/
private var actionState:Boolean = false;
private var activeBox:uint;
private function onMouseDownHandle(e:MouseEvent):Void{
var code:uint = this.mouseBox();
activeBox = code;
actionState = true;
switch (code){
case NW :
case N :
case NE :
case W :
case E :
case SW :
case S :
case SE :
this.startResize();
return;
default :
this.startMove();
return;
}
}

private function onMouseUpHandle(e:MouseEvent):Void{
var code:uint = activeBox;
actionState = false;
switch (code){
case NW :
case N :
case NE :
case W :
case E :
case SW :
case S :
case SE :
this.stopResize();
return;
default :
this.stopMove();
return;
}
}

private function onMouseMoveHandle(e:MouseEvent):Void{
debug = "x:"+ e.localX.toString() + ",y:" + e.localY.toString() + ",box:" + mouseBoxName();
if (!actionState) return;
var code:uint = activeBox;
switch (code){
case NW :
case N :
case NE :
case W :
case E :
case SW :
case S :
case SE :
this.doResize();
return;
default :
return;
}
}

[Bindable]
public var debug:String = "none";

}
}

相关信息:


欢迎光临本社区,您还没有登录,不能发贴子。请在 这里登录