Flex 2: CursorPool for cursor management
06 Sep 2006Someone on Flexcoders asked what is the best practice to manage cursors. I, myself, don't know but I thought something and implemented. As I always say, quick and dirty but can be better. I know, there are lots of things I can do but this is something very basic..
CursorPool class basically let's you register components and kind of cursor you want to show when mouse is over the component. You have to register once and now CursorPool would take care of switching mouse cursor whenever mouse is over different components.
Check out code and demo :)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.abdulqabiz.utils | |
{ | |
public class CursorAttributes extends Object | |
{ | |
public var cursorType:Class = null; | |
public var priority:int = 2; | |
public var xOffset:Number = 0; | |
public var yOffset:Number = 0; | |
public function CursorAttributes (cursorType:Class, | |
priority:int=2, xOffset:Number = 0, yOffset:Number = 0) | |
{ | |
this.cursorType = cursorType; | |
this.priority = priority; | |
this.xOffset = xOffset; | |
this.yOffset = yOffset; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.abdulqabiz.utils | |
{ | |
import mx.managers.CursorManager; | |
import mx.core.UIComponent; | |
import flash.events.*; | |
public class CursorPool extends Object | |
{ | |
static private var instance:CursorPool = null; | |
static public function getInstance ():CursorPool | |
{ | |
if (!instance) | |
{ | |
instance = new CursorPool (); | |
} | |
return instance; | |
} | |
private var registry:Object; | |
private var cursorID:int; | |
public function CursorPool () | |
{ | |
registry = new Object (); | |
} | |
public function register (component:UIComponent, cursorAttributes:CursorAttributes):void | |
{ | |
if (component) | |
{ | |
registry[String (component)] = cursorAttributes; | |
component.addEventListener ("mouseOver", handleMouseOver); | |
component.addEventListener ("mouseOut", handleMouseOut); | |
} | |
} | |
public function unregister (component:UIComponent):void | |
{ | |
component.removeEventListener ("mouseOver", handleMouseOver); | |
component.removeEventListener ("mouseOut", handleMouseOut); | |
delete registry[String (component)]; | |
} | |
private function handleMouseOver (event:MouseEvent):void | |
{ | |
var component:UIComponent = UIComponent (event.target); | |
var ca:CursorAttributes = registry[String (component)]; | |
if (cursorID) | |
{ | |
CursorManager.removeCursor (cursorID); | |
} | |
cursorID = CursorManager.setCursor (ca.cursorType, ca.priority, ca.xOffset, ca.yOffset); | |
} | |
private function handleMouseOut (event:MouseEvent):void | |
{ | |
CursorManager.removeCursor (cursorID); | |
} | |
} | |
} |
Update: I have also posted code on JAM, it would appear once Ted approves. You can see it there also.