📄 virtualmouse.as
字号:
/**
* @author Trevor McCauley
* @link www.senocular.com
*/
package org.papervision3d.core.utils.virtualmouse
{
import com.blitzagency.xray.logger.XrayLog;
import flash.display.DisplayObject;
import flash.display.DisplayObjectContainer;
import flash.display.InteractiveObject;
import flash.display.SimpleButton;
import flash.display.Sprite;
import flash.display.Stage;
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.events.KeyboardEvent;
import flash.events.MouseEvent;
import flash.geom.Point;
import flash.utils.Dictionary;
import org.papervision3d.core.components.as3.utils.CoordinateTools;
/**
* Dispatched when the virtual mouse state is updated.
* @eventType flash.events.Event
*/
[Event(name="update", type="flash.events.Event")]
/**
* Dispatched when the virtual mouse fires an
* Event.MOUSE_LEAVE event.
* @eventType flash.events.Event
*/
[Event(name="mouseLeave", type="flash.events.Event")]
/**
* Dispatched when the virtual mouse fires an
* MouseEvent.MOUSE_MOVE event.
* @eventType flash.events.MouseEvent
*/
[Event(name="mouseMove", type="flash.events.MouseEvent")]
/**
* Dispatched when the virtual mouse fires an
* MouseEvent.MOUSE_OUT event.
* @eventType flash.events.MouseEvent
*/
[Event(name="mouseOut", type="flash.events.MouseEvent")]
/**
* Dispatched when the virtual mouse fires an
* MouseEvent.ROLL_OUT event.
* @eventType flash.events.MouseEvent
*/
[Event(name="rollOut", type="flash.events.MouseEvent")]
/**
* Dispatched when the virtual mouse fires an
* MouseEvent.MOUSE_OVER event.
* @eventType flash.events.MouseEvent
*/
[Event(name="mouseOver", type="flash.events.MouseEvent")]
/**
* Dispatched when the virtual mouse fires an
* MouseEvent.ROLL_OVER event.
* @eventType flash.events.MouseEvent
*/
[Event(name="rollOver", type="flash.events.MouseEvent")]
/**
* Dispatched when the virtual mouse fires an
* MouseEvent.MOUSE_DOWN event.
* @eventType flash.events.MouseEvent
*/
[Event(name="mouseDown", type="flash.events.MouseEvent")]
/**
* Dispatched when the virtual mouse fires an
* MouseEvent.MOUSE_UP event.
* @eventType flash.events.MouseEvent
*/
[Event(name="mouseUp", type="flash.events.MouseEvent")]
/**
* Dispatched when the virtual mouse fires an
* MouseEvent.CLICK event.
* @eventType flash.events.MouseEvent
*/
[Event(name="click", type="flash.events.MouseEvent")]
/**
* Dispatched when the virtual mouse fires an
* MouseEvent.DOUBLE_CLICK event.
* @eventType flash.events.MouseEvent
*/
[Event(name="doubleClick", type="flash.events.MouseEvent")]
/**
* The VirtualMouse class is used to create a programmatic
* version of the users mouse that can be moved about the
* Flash player stage firing off mouse events of the display
* objects it interacts with. This can allow you to simulate
* interaction with buttons and movie clips through ActionScript.
* <br />
* Handled events include:
* Event.MOUSE_LEAVE,
* MouseEvent.MOUSE_MOVE,
* MouseEvent.MOUSE_OUT,
* MouseEvent.ROLL_OUT,
* MouseEvent.MOUSE_OVER,
* MouseEvent.ROLL_OVER,
* MouseEvent.MOUSE_DOWN,
* MouseEvent.MOUSE_UP.
* MouseEvent.CLICK, and,
* MouseEvent.DOUBLE_CLICK.
* Along with dispatching those events for their respective
* targets, the VirtualMouse instance will also dispatch the
* event on itself allowing to capture which events are being
* fired by the virtual mouse. The last event fired can also
* be referenced in the lastEvent property.
* <br />
* VirtualMouse mouse cannot:
* activate states of SimpleButton instances,
* change object focus,
* handle mouseWheel related events,
* change the system's cursor location, or
* spoof the location of the mouseX and mouseY properties
* (which some components rely on).
*/
public class VirtualMouse extends EventDispatcher {
public static const UPDATE:String = "update";
private var altKey:Boolean = false;
private var ctrlKey:Boolean = false;
private var shiftKey:Boolean = false;
private var delta:int = 0; // mouseWheel unsupported
private var _stage:Stage;
private var _container:Sprite;
private var target:InteractiveObject;
private var location:Point;
private var isLocked:Boolean = false;
private var isDoubleClickEvent:Boolean = false;
private static var _mouseIsDown:Boolean = false;
private var disabledEvents:Object = new Object();
private var ignoredInstances:Dictionary = new Dictionary(true);
private var _lastEvent:Event;
private var lastMouseDown:Boolean = false;
private var updateMouseDown:Boolean = false;
private var lastLocation:Point;
private var lastDownTarget:DisplayObject;
private var lastWithinStage:Boolean = true;
private var _useNativeEvents:Boolean = false;
private var eventEvent:Class = VirtualMouseEvent;
private var mouseEventEvent:Class = VirtualMouseMouseEvent;
private var log:XrayLog = new XrayLog();
/**
* A reference to the Stage instance. This
* reference needs to be passed to the
* VirtualMouse instance either in its constructor
* or through assigning it's stage property.
* Without a valid reference to the stage, the
* virtual mouse will not function.
* @see VirtualMouse()
*/
public function get stage():Stage {
return _stage;
}
public function set stage(s:Stage):void {
var hadStage:Boolean;
if (_stage){
hadStage = true;
_stage.removeEventListener(KeyboardEvent.KEY_DOWN, keyHandler);
_stage.removeEventListener(KeyboardEvent.KEY_UP, keyHandler);
}else{
hadStage = false;
}
_stage = s;
if (_stage) {
_stage.addEventListener(KeyboardEvent.KEY_DOWN, keyHandler);
_stage.addEventListener(KeyboardEvent.KEY_UP, keyHandler);
target = _stage;
if (!hadStage) update();
}
}
/**
*
* @param value Sprite container you want VirtualMouse to use with its testing of sub containers
* @return
*/
public function set container(value:Sprite):void
{
_container = value;
}
public function get container():Sprite { return _container; }
/**
* The last event dispatched by the VirtualMouse
* instance. This can be useful for preventing
* event recursion if performing VirtualMouse
* operations within MouseEvent handlers.
*/
public function get lastEvent():Event {
return _lastEvent;
}
/**
* True if the virtual mouse is being
* pressed, false if not. The mouse is
* down for the virtual mouse if press()
* was called.
* @see press()
* @see release()
*/
public function get mouseIsDown():Boolean {
return _mouseIsDown;
}
/**
* The x location of the virtual mouse. If you are
* setting both the x and y properties of the
* virtual mouse at the same time, you would probably
* want to lock the VirtualMouse instance to prevent
* additional events from firing.
* @see lock
* @see unlock
* @see y
* @see setLocation()
* @see getLocation()
*/
public function get x():Number {
return location.x;
}
public function set x(n:Number):void {
location.x = n;
if (!isLocked) update();
}
/**
* The y location of the virtual mouse. If you are
* setting both the x and y properties of the
* virtual mouse at the same time, you would probably
* want to lock the VirtualMouse instance to prevent
* additional events from firing.
* @see lock
* @see unlock
* @see x
* @see setLocation()
* @see getLocation()
*/
public function get y():Number {
return location.y;
}
public function set y(n:Number):void {
location.y = n;
if (!isLocked) update();
}
/**
* Determines if the events dispatched by the
* VirtualMouse instance are IVirualMouseEvent
* Events (wrapping Event and MouseEvent) or events
* of the native Event and MouseEvent type. When using
* non-native events, you can check to see if the
* events originated from VirtualMouse by seeing if
* the events are of the type IVirualMouseEvent.
* @see lastEvent
*/
public function get useNativeEvents():Boolean {
return _useNativeEvents;
}
public function set useNativeEvents(b:Boolean):void {
if (b == _useNativeEvents) return;
_useNativeEvents = b;
if (_useNativeEvents){
eventEvent = VirtualMouseEvent;
mouseEventEvent = VirtualMouseMouseEvent;
}else{
eventEvent = Event;
mouseEventEvent = MouseEvent;
}
}
/**
* Initializes a new VirtualMouse instance.
* @param stage A reference to the stage instance.
* @param startX The initial x location of
* the virtual mouse.
* @param startY The initial y location of
* the virtual mouse.
*/
public function VirtualMouse(stage:Stage = null, container:Sprite = null, startX:Number = 0, startY:Number = 0) {
this.stage = stage;
this.container = container;
location = new Point(startX, startY);
lastLocation = location.clone();
addEventListener(UPDATE, handleUpdate);
update();
}
/**
* Returns the location (x and y) of the current
* VirtualMouse instance. The location of the
* virtual mouse is based in the global
* coordinate space.
* @return A Point instance representing the
* location of the virtual mouse in
* global coordinate space.
* @see x
* @see y
* @see setLocation()
*/
public function getLocation():Point {
return location.clone();
}
/**
* Sets the location (x and y) of the current
* VirtualMouse instance. There are two ways to
* call setLocation, either passing in a single
* Point instance, or by passing in two Number
* instances representing x and y coordinates.
* The location of the virtual mouse is based in
* the global coordinate space.
* @param a A Point instance or x Number value.
* @param b A y Number value if a is a Number.
* @see x
* @see y
* @see getLocation()
*/
public function setLocation(a:*, b:* = null):void
{
//log.debug("VM setLocation", a, b);
if (a is Point) {
var loc:Point = a as Point;
trace(loc);
location.x = loc.x;
location.y = loc.y;
}else{
location.x = Number(a);
location.y = Number(b);
}
if (!isLocked) update();
}
/**
* Locks the current VirtualMouse instance
* preventing updates from being made as
* properties change within the instance.
* To release and allow an update, call unlock().
* @see lock()
* @see update()
*/
public function lock():void {
isLocked = true;
}
/**
* Unlocks the current VirtualMouse instance
* allowing updates to be made for the
* dispatching of virtual mouse events. After
* unlocking the instance, it will update and
* additional calls to press(), release(), or
* changing the location of the virtual mouse
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -