一个简单的鼠标手势模拟
Demo
/**
* @author chenqian
* @contact yesterday[at]gmail.com
* @URI www.stopdesign.cn
*/
package
{
import flash.display.*;
import flash.events.*;
import flash.geom.Point;
import flash.text.TextField;
public class MouseGesture extends Sprite
{
public static const GESTURE_UP:String="GESTURE_UP";
public static const GESTURE_RIGHT:String="GESTURE_RIGHT";
public static const GESTURE_DOWN:String="GESTURE_DOWN";
public static const GESTURE_LEFT:String="GESTURE_LEFT";
// the color of the drawing line
public var lineColor:Number;
// the size of the drawing line
public var lineSize:Number;
// where the mouse start drawing
private var _startPoint : Point;
// where the mouse stop drawing
private var _newPoint : Point;
// the drawing direction
private var _direction : String;
// the drawing line
private var _drawLine : Sprite;
public function MouseGesture(...rest){
trace("mouse gesture init")
// default mouse gestrue line color and size
lineColor=0xFF0000;
lineSize= 2;
// parse parameters
if(rest[0] != null){
lineColor = rest[0];
}
if(rest[1] != null){
lineSize=rest[1];
}
//add the shape, used for drawing the gesture
var child:Shape = new Shape();
child.graphics.beginFill(0x000000);
child.graphics.lineStyle(0, 0xFFFFFF);
child.graphics.drawRect(0, 0, 1600, 1600);
child.graphics.endFill();
addChild(child);
child.alpha=0;
_drawLine = new Sprite();
addChild( _drawLine );
addEventListener( MouseEvent.MOUSE_DOWN , mouseDownHandler );
}
// set the gesture color
public function setLineColor(c:Number):void{
lineColor=c;
}
// set the gesture size
public function setLineSize(s:Number):void{
lineSize=s;
}
// fire when user press mouse down
private function mouseDownHandler( e:MouseEvent ):void
{
var _color : Number = lineColor;
var _size : Number = lineSize;
_startPoint = new Point( mouseX , mouseY );
_drawLine.graphics.lineStyle( _size , _color , 1.0 );
_drawLine.graphics.moveTo( _startPoint.x , _startPoint.y );
addEventListener( MouseEvent.MOUSE_MOVE , mouseMoveHandler );
addEventListener( MouseEvent.MOUSE_UP , mouseUpHander );
}
// fire when user move the mouse
private function mouseMoveHandler( e:MouseEvent ):void
{
_newPoint = new Point( mouseX , mouseY );
var _distance : Number = Point.distance( _startPoint , _newPoint );
_drawLine.graphics.lineTo( _newPoint.x , _newPoint.y );
}
// fire when user's mouse is up
private function mouseUpHander( e:MouseEvent ):void
{
detectDir();
_drawLine.graphics.clear();
removeEventListener( MouseEvent.MOUSE_MOVE , mouseMoveHandler );
removeEventListener( MouseEvent.MOUSE_UP , mouseUpHander );
}
/* detecting the mouse move direction */
private function detectDir():void
{
var _vector:Point = _newPoint.subtract( _startPoint );
var _angle : Number = Math.atan2( _vector.y , _vector.x ) * 180 / Math.PI;
// move down
if ( _angle >= 45 && _angle < 125 ){
dispatchEvent(new Event(MouseGesture.GESTURE_DOWN,true));
removeEventListener( MouseEvent.MOUSE_MOVE , mouseMoveHandler );
removeEventListener( MouseEvent.MOUSE_UP , mouseUpHander );
}
// move left
else if ( _angle >= 125 || _angle < -125 ){
dispatchEvent(new Event(MouseGesture.GESTURE_LEFT,true))
removeEventListener( MouseEvent.MOUSE_MOVE , mouseMoveHandler );
removeEventListener( MouseEvent.MOUSE_UP , mouseUpHander );
}
// move right
else if ( _angle >= -45 && _angle < 45 ){
dispatchEvent(new Event(MouseGesture.GESTURE_RIGHT,true))
removeEventListener( MouseEvent.MOUSE_MOVE , mouseMoveHandler );
removeEventListener( MouseEvent.MOUSE_UP , mouseUpHander );
}
// move up
else if ( _angle >= -125 && _angle < -45 ){
dispatchEvent(new Event(MouseGesture.GESTURE_UP,true))
removeEventListener( MouseEvent.MOUSE_MOVE , mouseMoveHandler );
removeEventListener( MouseEvent.MOUSE_UP , mouseUpHander );
}
}
}
}
Labels: actionscript, flash, mouse gesture
posted by sike at 11:46 PM | 0 Comments Links to this post