home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Freeware / Adobe Air 1.5 / AdobeAIRInstaller.exe / setup.swf / scripts / mx / managers / CursorManagerImpl.as < prev    next >
Encoding:
Text File  |  2008-10-29  |  16.3 KB  |  513 lines

  1. package mx.managers
  2. {
  3.    import flash.display.DisplayObject;
  4.    import flash.display.InteractiveObject;
  5.    import flash.display.Sprite;
  6.    import flash.display.Stage;
  7.    import flash.events.ContextMenuEvent;
  8.    import flash.events.Event;
  9.    import flash.events.EventDispatcher;
  10.    import flash.events.IOErrorEvent;
  11.    import flash.events.MouseEvent;
  12.    import flash.events.ProgressEvent;
  13.    import flash.text.TextField;
  14.    import flash.text.TextFieldType;
  15.    import flash.ui.Mouse;
  16.    import mx.core.ApplicationGlobals;
  17.    import mx.core.EventPriority;
  18.    import mx.core.FlexSprite;
  19.    import mx.core.mx_internal;
  20.    import mx.styles.CSSStyleDeclaration;
  21.    import mx.styles.StyleManager;
  22.    
  23.    use namespace mx_internal;
  24.    
  25.    public class CursorManagerImpl implements ICursorManager
  26.    {
  27.       private static var instance:ICursorManager;
  28.       
  29.       mx_internal static const VERSION:String = "3.0.0.0";
  30.       
  31.       private var showSystemCursor:Boolean = false;
  32.       
  33.       private var nextCursorID:int = 1;
  34.       
  35.       private var systemManager:ISystemManager = null;
  36.       
  37.       private var cursorList:Array;
  38.       
  39.       private var _currentCursorYOffset:Number = 0;
  40.       
  41.       private var cursorHolder:Sprite;
  42.       
  43.       private var currentCursor:DisplayObject;
  44.       
  45.       private var showCustomCursor:Boolean = false;
  46.       
  47.       private var _currentCursorID:int = 0;
  48.       
  49.       private var listenForContextMenu:Boolean = false;
  50.       
  51.       private var customCursorLeftStage:Boolean = false;
  52.       
  53.       private var initialized:Boolean = false;
  54.       
  55.       private var overTextField:Boolean = false;
  56.       
  57.       private var _currentCursorXOffset:Number = 0;
  58.       
  59.       private var busyCursorList:Array;
  60.       
  61.       private var sourceArray:Array;
  62.       
  63.       public function CursorManagerImpl(param1:ISystemManager = null)
  64.       {
  65.          cursorList = [];
  66.          busyCursorList = [];
  67.          sourceArray = [];
  68.          super();
  69.          if(Boolean(instance) && !param1)
  70.          {
  71.             throw new Error("Instance already exists.");
  72.          }
  73.          if(param1)
  74.          {
  75.             this.systemManager = param1;
  76.          }
  77.          else
  78.          {
  79.             this.systemManager = ApplicationGlobals.application.systemManager;
  80.          }
  81.       }
  82.       
  83.       public static function getInstance() : ICursorManager
  84.       {
  85.          if(!instance)
  86.          {
  87.             instance = new CursorManagerImpl();
  88.          }
  89.          return instance;
  90.       }
  91.       
  92.       public function set currentCursorYOffset(param1:Number) : void
  93.       {
  94.          _currentCursorYOffset = param1;
  95.       }
  96.       
  97.       public function get currentCursorXOffset() : Number
  98.       {
  99.          return _currentCursorXOffset;
  100.       }
  101.       
  102.       public function removeCursor(param1:int) : void
  103.       {
  104.          var _loc2_:Object = null;
  105.          var _loc3_:CursorQueueItem = null;
  106.          for(_loc2_ in cursorList)
  107.          {
  108.             _loc3_ = cursorList[_loc2_];
  109.             if(_loc3_.cursorID == param1)
  110.             {
  111.                cursorList.splice(_loc2_,1);
  112.                showCurrentCursor();
  113.                break;
  114.             }
  115.          }
  116.       }
  117.       
  118.       public function get currentCursorID() : int
  119.       {
  120.          return _currentCursorID;
  121.       }
  122.       
  123.       private function updateCursorHelper() : void
  124.       {
  125.          var _loc1_:Boolean = false;
  126.          _loc1_ = Boolean(systemManager.stage.hasOwnProperty("nativeWindow"));
  127.          var _loc2_:Boolean = !_loc1_ || Boolean(systemManager.stage["nativeWindow"]["active"]);
  128.          if(_loc2_)
  129.          {
  130.             if(showSystemCursor)
  131.             {
  132.                showSystemCursor = false;
  133.                hideCursor();
  134.                Mouse.show();
  135.             }
  136.             if(showCustomCursor)
  137.             {
  138.                showCustomCursor = false;
  139.                showCursor();
  140.                Mouse.hide();
  141.             }
  142.          }
  143.          else if(cursorList.length > 0)
  144.          {
  145.             Mouse.hide();
  146.          }
  147.       }
  148.       
  149.       private function removeContextMenuHandlers() : void
  150.       {
  151.          var _loc1_:InteractiveObject = null;
  152.          var _loc2_:InteractiveObject = null;
  153.          if(listenForContextMenu)
  154.          {
  155.             _loc1_ = systemManager.document as InteractiveObject;
  156.             _loc2_ = systemManager as InteractiveObject;
  157.             if(Boolean(_loc1_) && Boolean(_loc1_.contextMenu))
  158.             {
  159.                _loc1_.contextMenu.removeEventListener(ContextMenuEvent.MENU_SELECT,contextMenu_menuSelectHandler,true);
  160.             }
  161.             if(Boolean(_loc2_) && Boolean(_loc2_.contextMenu))
  162.             {
  163.                _loc2_.contextMenu.removeEventListener(ContextMenuEvent.MENU_SELECT,contextMenu_menuSelectHandler,true);
  164.             }
  165.             listenForContextMenu = false;
  166.          }
  167.       }
  168.       
  169.       public function set currentCursorID(param1:int) : void
  170.       {
  171.          _currentCursorID = param1;
  172.       }
  173.       
  174.       private function priorityCompare(param1:CursorQueueItem, param2:CursorQueueItem) : int
  175.       {
  176.          if(param1.priority < param2.priority)
  177.          {
  178.             return -1;
  179.          }
  180.          if(param1.priority == param2.priority)
  181.          {
  182.             return 0;
  183.          }
  184.          return 1;
  185.       }
  186.       
  187.       public function removeAllCursors() : void
  188.       {
  189.          cursorList.splice(0);
  190.          showCurrentCursor();
  191.       }
  192.       
  193.       private function mouseOutHandler(param1:MouseEvent) : void
  194.       {
  195.          if(param1.relatedObject == null && cursorList.length > 0)
  196.          {
  197.             customCursorLeftStage = true;
  198.             hideCursor();
  199.             Mouse.show();
  200.          }
  201.       }
  202.       
  203.       public function setBusyCursor() : void
  204.       {
  205.          var _loc1_:CSSStyleDeclaration = StyleManager.getStyleDeclaration("CursorManager");
  206.          var _loc2_:Class = _loc1_.getStyle("busyCursor");
  207.          busyCursorList.push(setCursor(_loc2_,CursorManagerPriority.LOW));
  208.       }
  209.       
  210.       private function findSource(param1:Object) : int
  211.       {
  212.          var _loc2_:int = int(sourceArray.length);
  213.          var _loc3_:int = 0;
  214.          while(_loc3_ < _loc2_)
  215.          {
  216.             if(sourceArray[_loc3_] === param1)
  217.             {
  218.                return _loc3_;
  219.             }
  220.             _loc3_++;
  221.          }
  222.          return -1;
  223.       }
  224.       
  225.       private function showCurrentCursor() : void
  226.       {
  227.          var _loc1_:CursorQueueItem = null;
  228.          var _loc2_:ISystemManager = null;
  229.          if(cursorList.length > 0)
  230.          {
  231.             if(!initialized)
  232.             {
  233.                cursorHolder = new FlexSprite();
  234.                cursorHolder.name = "cursorHolder";
  235.                cursorHolder.mouseEnabled = false;
  236.                initialized = true;
  237.             }
  238.             _loc1_ = cursorList[0];
  239.             if(currentCursorID == CursorManager.NO_CURSOR)
  240.             {
  241.                Mouse.hide();
  242.             }
  243.             if(_loc1_.cursorID != currentCursorID)
  244.             {
  245.                if(cursorHolder.numChildren > 0)
  246.                {
  247.                   cursorHolder.removeChildAt(0);
  248.                }
  249.                currentCursor = new _loc1_.cursorClass();
  250.                if(currentCursor)
  251.                {
  252.                   if(currentCursor is InteractiveObject)
  253.                   {
  254.                      InteractiveObject(currentCursor).mouseEnabled = false;
  255.                   }
  256.                   _loc2_ = !!_loc1_.cursorSystemManager ? _loc1_.cursorSystemManager : ApplicationGlobals.application.systemManager;
  257.                   if(Boolean(systemManager) && systemManager != _loc2_)
  258.                   {
  259.                      systemManager.cursorChildren.removeChild(cursorHolder);
  260.                      removeSystemManagerHandlers();
  261.                      removeContextMenuHandlers();
  262.                      systemManager = _loc2_;
  263.                   }
  264.                   if(!systemManager.cursorChildren.contains(cursorHolder))
  265.                   {
  266.                      systemManager.cursorChildren.addChild(cursorHolder);
  267.                   }
  268.                   cursorHolder.addChild(currentCursor);
  269.                   addContextMenuHandlers();
  270.                   if(systemManager is SystemManager)
  271.                   {
  272.                      cursorHolder.x = SystemManager(systemManager).mouseX + _loc1_.x;
  273.                      cursorHolder.y = SystemManager(systemManager).mouseY + _loc1_.y;
  274.                   }
  275.                   else if(systemManager is DisplayObject)
  276.                   {
  277.                      cursorHolder.x = DisplayObject(systemManager).mouseX + _loc1_.x;
  278.                      cursorHolder.y = DisplayObject(systemManager).mouseY + _loc1_.y;
  279.                   }
  280.                   else
  281.                   {
  282.                      cursorHolder.x = _loc1_.x;
  283.                      cursorHolder.y = _loc1_.y;
  284.                   }
  285.                   systemManager.stage.addEventListener(MouseEvent.MOUSE_MOVE,mouseMoveHandler,true,EventPriority.CURSOR_MANAGEMENT);
  286.                   systemManager.stage.addEventListener(MouseEvent.MOUSE_OUT,mouseOutHandler,true,EventPriority.CURSOR_MANAGEMENT);
  287.                }
  288.                currentCursorID = _loc1_.cursorID;
  289.                currentCursorXOffset = _loc1_.x;
  290.                currentCursorYOffset = _loc1_.y;
  291.             }
  292.          }
  293.          else
  294.          {
  295.             showCustomCursor = false;
  296.             if(currentCursorID != CursorManager.NO_CURSOR)
  297.             {
  298.                currentCursorID = CursorManager.NO_CURSOR;
  299.                currentCursorXOffset = 0;
  300.                currentCursorYOffset = 0;
  301.                cursorHolder.removeChild(currentCursor);
  302.                removeSystemManagerHandlers();
  303.                removeContextMenuHandlers();
  304.             }
  305.             Mouse.show();
  306.          }
  307.       }
  308.       
  309.       private function contextMenu_menuSelectHandler(param1:ContextMenuEvent) : void
  310.       {
  311.          showCustomCursor = true;
  312.          systemManager.stage.addEventListener(MouseEvent.MOUSE_OVER,contextMenuMouseOverHandler,true,EventPriority.CURSOR_MANAGEMENT);
  313.       }
  314.       
  315.       public function hideCursor() : void
  316.       {
  317.          if(cursorHolder)
  318.          {
  319.             cursorHolder.visible = false;
  320.          }
  321.       }
  322.       
  323.       public function showCursor() : void
  324.       {
  325.          if(cursorHolder)
  326.          {
  327.             cursorHolder.visible = true;
  328.          }
  329.       }
  330.       
  331.       private function contextMenuMouseOverHandler(param1:MouseEvent) : void
  332.       {
  333.          systemManager.stage.removeEventListener(MouseEvent.MOUSE_OVER,contextMenuMouseOverHandler,true);
  334.          mouseMoveHandler(param1);
  335.       }
  336.       
  337.       private function completeHandler(param1:Event) : void
  338.       {
  339.          var _loc2_:int = findSource(param1.target);
  340.          if(_loc2_ != -1)
  341.          {
  342.             sourceArray.splice(_loc2_,1);
  343.             removeBusyCursor();
  344.          }
  345.       }
  346.       
  347.       public function get currentCursorYOffset() : Number
  348.       {
  349.          return _currentCursorYOffset;
  350.       }
  351.       
  352.       private function addContextMenuHandlers() : void
  353.       {
  354.          var _loc1_:InteractiveObject = null;
  355.          var _loc2_:InteractiveObject = null;
  356.          if(!listenForContextMenu)
  357.          {
  358.             _loc1_ = systemManager.document as InteractiveObject;
  359.             _loc2_ = systemManager as InteractiveObject;
  360.             if(Boolean(_loc1_) && Boolean(_loc1_.contextMenu))
  361.             {
  362.                _loc1_.contextMenu.addEventListener(ContextMenuEvent.MENU_SELECT,contextMenu_menuSelectHandler,true,EventPriority.CURSOR_MANAGEMENT);
  363.                listenForContextMenu = true;
  364.             }
  365.             if(Boolean(_loc2_) && Boolean(_loc2_.contextMenu))
  366.             {
  367.                _loc2_.contextMenu.addEventListener(ContextMenuEvent.MENU_SELECT,contextMenu_menuSelectHandler,true,EventPriority.CURSOR_MANAGEMENT);
  368.                listenForContextMenu = true;
  369.             }
  370.          }
  371.       }
  372.       
  373.       public function setCursor(param1:Class, param2:int = 2, param3:Number = 0, param4:Number = 0) : int
  374.       {
  375.          var _loc5_:int = int(nextCursorID++);
  376.          var _loc6_:CursorQueueItem = new CursorQueueItem();
  377.          _loc6_.cursorID = _loc5_;
  378.          _loc6_.cursorClass = param1;
  379.          _loc6_.priority = param2;
  380.          _loc6_.x = param3;
  381.          _loc6_.y = param4;
  382.          if(systemManager)
  383.          {
  384.             _loc6_.cursorSystemManager = systemManager;
  385.          }
  386.          else
  387.          {
  388.             _loc6_.cursorSystemManager = ApplicationGlobals.application.systemManager;
  389.          }
  390.          cursorList.push(_loc6_);
  391.          cursorList.sort(priorityCompare);
  392.          showCurrentCursor();
  393.          return _loc5_;
  394.       }
  395.       
  396.       private function progressHandler(param1:ProgressEvent) : void
  397.       {
  398.          var _loc2_:int = findSource(param1.target);
  399.          if(_loc2_ == -1)
  400.          {
  401.             sourceArray.push(param1.target);
  402.             setBusyCursor();
  403.          }
  404.       }
  405.       
  406.       public function removeBusyCursor() : void
  407.       {
  408.          if(busyCursorList.length > 0)
  409.          {
  410.             removeCursor(int(busyCursorList.pop()));
  411.          }
  412.       }
  413.       
  414.       private function removeSystemManagerHandlers() : void
  415.       {
  416.          var _loc1_:Stage = systemManager.stage;
  417.          _loc1_.removeEventListener(MouseEvent.MOUSE_MOVE,mouseMoveHandler,true);
  418.          _loc1_.removeEventListener(MouseEvent.MOUSE_OUT,mouseOutHandler,true);
  419.       }
  420.       
  421.       public function registerToUseBusyCursor(param1:Object) : void
  422.       {
  423.          if(Boolean(param1) && param1 is EventDispatcher)
  424.          {
  425.             param1.addEventListener(ProgressEvent.PROGRESS,progressHandler);
  426.             param1.addEventListener(Event.COMPLETE,completeHandler);
  427.             param1.addEventListener(IOErrorEvent.IO_ERROR,completeHandler);
  428.          }
  429.       }
  430.       
  431.       public function unRegisterToUseBusyCursor(param1:Object) : void
  432.       {
  433.          if(Boolean(param1) && param1 is EventDispatcher)
  434.          {
  435.             param1.removeEventListener(ProgressEvent.PROGRESS,progressHandler);
  436.             param1.removeEventListener(Event.COMPLETE,completeHandler);
  437.             param1.removeEventListener(IOErrorEvent.IO_ERROR,completeHandler);
  438.          }
  439.       }
  440.       
  441.       private function mouseMoveHandler(param1:MouseEvent) : void
  442.       {
  443.          if(customCursorLeftStage)
  444.          {
  445.             customCursorLeftStage = false;
  446.             showCursor();
  447.             if(cursorList.length > 0)
  448.             {
  449.                Mouse.hide();
  450.             }
  451.          }
  452.          if(systemManager is SystemManager)
  453.          {
  454.             cursorHolder.x = SystemManager(systemManager).mouseX + currentCursorXOffset;
  455.             cursorHolder.y = SystemManager(systemManager).mouseY + currentCursorYOffset;
  456.          }
  457.          else if(systemManager is DisplayObject)
  458.          {
  459.             cursorHolder.x = DisplayObject(systemManager).mouseX + currentCursorXOffset;
  460.             cursorHolder.y = DisplayObject(systemManager).mouseY + currentCursorYOffset;
  461.          }
  462.          else
  463.          {
  464.             cursorHolder.x = currentCursorXOffset;
  465.             cursorHolder.y = currentCursorYOffset;
  466.          }
  467.          var _loc2_:Object = param1.target;
  468.          if(!overTextField && _loc2_ is TextField && _loc2_.type == TextFieldType.INPUT)
  469.          {
  470.             overTextField = true;
  471.             showSystemCursor = true;
  472.          }
  473.          else if(overTextField && !(_loc2_ is TextField && _loc2_.type == TextFieldType.INPUT))
  474.          {
  475.             overTextField = false;
  476.             showCustomCursor = true;
  477.          }
  478.          updateCursorHelper();
  479.       }
  480.       
  481.       public function set currentCursorXOffset(param1:Number) : void
  482.       {
  483.          _currentCursorXOffset = param1;
  484.       }
  485.    }
  486. }
  487.  
  488. import mx.core.mx_internal;
  489.  
  490. use namespace mx_internal;
  491.  
  492. class CursorQueueItem
  493. {
  494.    mx_internal static const VERSION:String = "3.0.0.0";
  495.    
  496.    public var priority:int = 2;
  497.    
  498.    public var cursorClass:Class = null;
  499.    
  500.    public var cursorSystemManager:ISystemManager;
  501.    
  502.    public var cursorID:int = 0;
  503.    
  504.    public var x:Number;
  505.    
  506.    public var y:Number;
  507.    
  508.    public function CursorQueueItem()
  509.    {
  510.       super();
  511.    }
  512. }
  513.