Line ending happy fun times

git-svn-id: svn+ssh://src.earth.threerings.net/nenya/trunk@935 ed5b42cb-e716-0410-a449-f6a68f950b19
This commit is contained in:
Dave Hoover
2010-07-06 17:28:12 +00:00
parent 75f00f82ec
commit 91b0a812ba
+302 -302
View File
@@ -19,305 +19,305 @@
// License along with this library; if not, write to the Free Software // License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
package com.threerings.flex { package com.threerings.flex {
import flash.display.DisplayObject; import flash.display.DisplayObject;
import flash.display.InteractiveObject; import flash.display.InteractiveObject;
import flash.display.Sprite; import flash.display.Sprite;
import flash.events.MouseEvent; import flash.events.MouseEvent;
import flash.text.TextField; import flash.text.TextField;
import flash.text.TextFieldType; import flash.text.TextFieldType;
import flash.ui.Mouse; import flash.ui.Mouse;
import mx.core.Application; import mx.core.Application;
import mx.managers.SystemManager; import mx.managers.SystemManager;
import mx.styles.CSSStyleDeclaration; import mx.styles.CSSStyleDeclaration;
import mx.styles.StyleManager; import mx.styles.StyleManager;
import com.threerings.util.Map; import com.threerings.util.Map;
import com.threerings.util.Maps; import com.threerings.util.Maps;
/** /**
* A better CursorManager. * A better CursorManager.
*/ */
public class CursorManager public class CursorManager
{ {
public static const SYSTEM_CURSOR :int = 0; public static const SYSTEM_CURSOR :int = 0;
public static const BUSY_CURSOR :int = 1; public static const BUSY_CURSOR :int = 1;
//-------------------------------------------------------------------------- //--------------------------------------------------------------------------
// //
// Class methods // Class methods
// //
//-------------------------------------------------------------------------- //--------------------------------------------------------------------------
/** /**
* Makes the cursor visible. * Makes the cursor visible.
* Cursor visibility is not reference-counted. * Cursor visibility is not reference-counted.
* A single call to the <code>showCursor()</code> method * A single call to the <code>showCursor()</code> method
* always shows the cursor regardless of how many calls * always shows the cursor regardless of how many calls
* to the <code>hideCursor()</code> method were made. * to the <code>hideCursor()</code> method were made.
*/ */
public static function showCursor () :void public static function showCursor () :void
{ {
if (_currentCursorId == SYSTEM_CURSOR) { if (_currentCursorId == SYSTEM_CURSOR) {
Mouse.show(); Mouse.show();
} else { } else {
_cursorHolder.visible = true; _cursorHolder.visible = true;
} }
} }
/** /**
* Makes the cursor invisible. * Makes the cursor invisible.
* Cursor visibility is not reference-counted. * Cursor visibility is not reference-counted.
* A single call to the <code>hideCursor()</code> method * A single call to the <code>hideCursor()</code> method
* always hides the cursor regardless of how many calls * always hides the cursor regardless of how many calls
* to the <code>showCursor()</code> method were made. * to the <code>showCursor()</code> method were made.
*/ */
public static function hideCursor () :void public static function hideCursor () :void
{ {
_cursorHolder.visible = false; _cursorHolder.visible = false;
Mouse.hide(); Mouse.hide();
} }
public static function getCurrentCursorId () :int public static function getCurrentCursorId () :int
{ {
return _currentCursorId; return _currentCursorId;
} }
/** /**
* Creates a new cursor. * Creates a new cursor.
* *
* @param cursorClass Class of the cursor to display. * @param cursorClass Class of the cursor to display.
* *
* @param xOffset Number that specifies the x offset * @param xOffset Number that specifies the x offset
* of the cursor, in pixels, relative to the mouse pointer. * of the cursor, in pixels, relative to the mouse pointer.
* The default value is 0. * The default value is 0.
* *
* @param yOffset Number that specifies the y offset * @param yOffset Number that specifies the y offset
* of the cursor, in pixels, relative to the mouse pointer. * of the cursor, in pixels, relative to the mouse pointer.
* The default value is 0. * The default value is 0.
* *
* @return The ID of the cursor. * @return The ID of the cursor.
*/ */
public static function addCursor ( public static function addCursor (
cursorClass :Class, xOffset :int = 0, yOffset :int = 0) :int cursorClass :Class, xOffset :int = 0, yOffset :int = 0) :int
{ {
var cursorId :int = _nextCursorId++; var cursorId :int = _nextCursorId++;
var rec :CursorRecord = new CursorRecord(cursorClass); var rec :CursorRecord = new CursorRecord(cursorClass);
rec.x = xOffset; rec.x = xOffset;
rec.y = yOffset; rec.y = yOffset;
_cursors.put(cursorId, rec); _cursors.put(cursorId, rec);
return cursorId; return cursorId;
} }
/** /**
* Set the current cursor. * Set the current cursor.
*/ */
public static function setCursor (id :int) :void public static function setCursor (id :int) :void
{ {
if (id == _currentCursorId) { if (id == _currentCursorId) {
return; return;
} }
if (!_initialized) { if (!_initialized) {
// oh, let's set it up // oh, let's set it up
_systemManager = Application.application.systemManager; _systemManager = Application.application.systemManager;
// set up the busy cursor // set up the busy cursor
var cursorManagerStyleDeclaration :CSSStyleDeclaration = var cursorManagerStyleDeclaration :CSSStyleDeclaration =
StyleManager.getStyleDeclaration("CursorManager"); StyleManager.getStyleDeclaration("CursorManager");
var busyCursorClass :Class = var busyCursorClass :Class =
cursorManagerStyleDeclaration.getStyle("busyCursor"); cursorManagerStyleDeclaration.getStyle("busyCursor");
_busyCursor = new CursorRecord(busyCursorClass); _busyCursor = new CursorRecord(busyCursorClass);
// The first time a cursor is requested of the CursorManager, // The first time a cursor is requested of the CursorManager,
// create a Sprite to hold the cursor symbol // create a Sprite to hold the cursor symbol
_cursorHolder = new Sprite(); _cursorHolder = new Sprite();
_cursorHolder.mouseEnabled = false; _cursorHolder.mouseEnabled = false;
_systemManager.cursorChildren.addChild(_cursorHolder); _systemManager.cursorChildren.addChild(_cursorHolder);
_initialized = true; _initialized = true;
} }
// figure out what the new cursor will be like // figure out what the new cursor will be like
var rec :CursorRecord; var rec :CursorRecord;
if (id == BUSY_CURSOR) { if (id == BUSY_CURSOR) {
rec = _busyCursor; rec = _busyCursor;
} else if (id != SYSTEM_CURSOR) { } else if (id != SYSTEM_CURSOR) {
rec = (_cursors.get(id) as CursorRecord); rec = (_cursors.get(id) as CursorRecord);
if (rec == null) { if (rec == null) {
// a bogus id was specified // a bogus id was specified
// TODO: throw an error? // TODO: throw an error?
return; return;
} }
} }
if (rec != null && (rec.cursor is Class)) { if (rec != null && (rec.cursor is Class)) {
// go ahead and instantiate the class // go ahead and instantiate the class
try { try {
var disp :DisplayObject = new (rec.cursor as Class); var disp :DisplayObject = new (rec.cursor as Class);
rec.cursor = disp; rec.cursor = disp;
if (disp is InteractiveObject) { if (disp is InteractiveObject) {
(disp as InteractiveObject).mouseEnabled = false; (disp as InteractiveObject).mouseEnabled = false;
} }
} catch (err :Error) { } catch (err :Error) {
// this cursor is not usable, bail // this cursor is not usable, bail
return; return;
} }
} }
// always remove any custom cursors from the hierarchy // always remove any custom cursors from the hierarchy
if (_cursorHolder.numChildren > 0) { if (_cursorHolder.numChildren > 0) {
_cursorHolder.removeChildAt(0); _cursorHolder.removeChildAt(0);
} }
if (id != SYSTEM_CURSOR) { if (id != SYSTEM_CURSOR) {
Mouse.hide(); Mouse.hide();
var currentCursor :DisplayObject = (rec.cursor as DisplayObject); var currentCursor :DisplayObject = (rec.cursor as DisplayObject);
_cursorHolder.addChild(currentCursor); _cursorHolder.addChild(currentCursor);
_cursorHolder.x = _systemManager.mouseX + rec.x; _cursorHolder.x = _systemManager.mouseX + rec.x;
_cursorHolder.y = _systemManager.mouseY + rec.y; _cursorHolder.y = _systemManager.mouseY + rec.y;
_currentXOffset = rec.x; _currentXOffset = rec.x;
_currentYOffset = rec.y; _currentYOffset = rec.y;
_systemManager.stage.addEventListener( _systemManager.stage.addEventListener(
MouseEvent.MOUSE_MOVE, mouseMoveHandler); MouseEvent.MOUSE_MOVE, mouseMoveHandler);
} else { } else {
_currentXOffset = 0; _currentXOffset = 0;
_currentYOffset = 0; _currentYOffset = 0;
_systemManager.stage.removeEventListener( _systemManager.stage.removeEventListener(
MouseEvent.MOUSE_MOVE, mouseMoveHandler); MouseEvent.MOUSE_MOVE, mouseMoveHandler);
Mouse.show(); Mouse.show();
} }
_currentCursorId = id; _currentCursorId = id;
} }
/** /**
* Removes a cursor from the cursor list. * Removes a cursor from the cursor list.
* If the cursor being removed is the currently displayed cursor, * If the cursor being removed is the currently displayed cursor,
* the CursorManager displays the next cursor in the list, if one exists. * the CursorManager displays the next cursor in the list, if one exists.
* If the list becomes empty, the CursorManager displays * If the list becomes empty, the CursorManager displays
* the default system cursor. * the default system cursor.
* *
* @param cursorID ID of cursor to remove. * @param cursorID ID of cursor to remove.
*/ */
public static function removeCursor (id :int) :void public static function removeCursor (id :int) :void
{ {
var rec :CursorRecord = (_cursors.remove(id) as CursorRecord); var rec :CursorRecord = (_cursors.remove(id) as CursorRecord);
if (rec != null && id == _currentCursorId) { if (rec != null && id == _currentCursorId) {
setCursor(SYSTEM_CURSOR); setCursor(SYSTEM_CURSOR);
} }
} }
/** /**
* Removes all of the cursors from the cursor list * Removes all of the cursors from the cursor list
* and restores the system cursor. * and restores the system cursor.
*/ */
public static function removeAllCursors () :void public static function removeAllCursors () :void
{ {
_cursors.clear(); _cursors.clear();
if (_currentCursorId != BUSY_CURSOR) { if (_currentCursorId != BUSY_CURSOR) {
setCursor(SYSTEM_CURSOR); setCursor(SYSTEM_CURSOR);
} }
} }
//-------------------------------------------------------------------------- //--------------------------------------------------------------------------
// //
// Class event handlers // Class event handlers
// //
//-------------------------------------------------------------------------- //--------------------------------------------------------------------------
/** /**
* @private * @private
*/ */
private static function mouseMoveHandler (event :MouseEvent) :void private static function mouseMoveHandler (event :MouseEvent) :void
{ {
_cursorHolder.x = _systemManager.mouseX + _currentXOffset; _cursorHolder.x = _systemManager.mouseX + _currentXOffset;
_cursorHolder.y = _systemManager.mouseY + _currentYOffset; _cursorHolder.y = _systemManager.mouseY + _currentYOffset;
var target :Object = event.target; var target :Object = event.target;
// Do target test. // Do target test.
if (!_overTextField && if (!_overTextField &&
target is TextField && target.type == TextFieldType.INPUT) { target is TextField && target.type == TextFieldType.INPUT) {
_overTextField = true; _overTextField = true;
_showSystemCursor = true; _showSystemCursor = true;
} else if (_overTextField && } else if (_overTextField &&
!(target is TextField && target.type == TextFieldType.INPUT)) { !(target is TextField && target.type == TextFieldType.INPUT)) {
_overTextField = false; _overTextField = false;
_showCustomCursor = true; _showCustomCursor = true;
} }
// Handle switching between system and custom cursor. // Handle switching between system and custom cursor.
if (_showSystemCursor) { if (_showSystemCursor) {
_showSystemCursor = false; _showSystemCursor = false;
_cursorHolder.visible = false; _cursorHolder.visible = false;
Mouse.show(); Mouse.show();
} }
if (_showCustomCursor) { if (_showCustomCursor) {
_showCustomCursor = false; _showCustomCursor = false;
_cursorHolder.visible = true; _cursorHolder.visible = true;
Mouse.hide(); Mouse.hide();
} }
} }
/** A mapping of all assigned cursor ids. */ /** A mapping of all assigned cursor ids. */
private static const _cursors :Map = Maps.newMapOf(int); private static const _cursors :Map = Maps.newMapOf(int);
private static var _currentCursorId :int = 0; // SYSTEM_CURSOR private static var _currentCursorId :int = 0; // SYSTEM_CURSOR
private static var _nextCursorId :int = 2; // skip BUSY private static var _nextCursorId :int = 2; // skip BUSY
private static var _initialized :Boolean = false; private static var _initialized :Boolean = false;
private static var _cursorHolder :Sprite; private static var _cursorHolder :Sprite;
private static var _currentXOffset :int = 0; private static var _currentXOffset :int = 0;
private static var _currentYOffset :int = 0; private static var _currentYOffset :int = 0;
/** A record for the busy cursor (where it can't be removed). */ /** A record for the busy cursor (where it can't be removed). */
private static var _busyCursor :CursorRecord; private static var _busyCursor :CursorRecord;
private static var _overTextField :Boolean = false; private static var _overTextField :Boolean = false;
private static var _overLink :Boolean = false; private static var _overLink :Boolean = false;
private static var _showSystemCursor :Boolean = false; private static var _showSystemCursor :Boolean = false;
private static var _showCustomCursor :Boolean = false; private static var _showCustomCursor :Boolean = false;
private static var _systemManager :SystemManager = null; private static var _systemManager :SystemManager = null;
} }
} }
/** /**
*/ */
class CursorRecord extends Object class CursorRecord extends Object
{ {
public function CursorRecord (clazz :Class) public function CursorRecord (clazz :Class)
{ {
cursor = clazz; cursor = clazz;
} }
/** The class, or instantiated cursor. */ /** The class, or instantiated cursor. */
public var cursor :Object public var cursor :Object
/** The x/y offset for the hotspot. */ /** The x/y offset for the hotspot. */
public var x :Number; public var x :Number;
public var y :Number; public var y :Number;
} }