Advertisement
Guest User

Untitled

a guest
Jul 17th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Haxe 9.67 KB | None | 0 0
  1. package openfl.display; #if (display || !flash)
  2.  
  3.  
  4. import openfl._internal.renderer.canvas.CanvasGraphics;
  5. import openfl._internal.renderer.canvas.CanvasShape;
  6. import openfl._internal.renderer.dom.DOMShape;
  7. import openfl.geom.Matrix;
  8. import openfl.geom.Point;
  9. import openfl.geom.Rectangle;
  10.  
  11. #if (lime >= "7.0.0")
  12. import lime.ui.Cursor;
  13. #else
  14. import lime.ui.MouseCursor in Cursor;
  15. #end
  16.  
  17.  
  18. /**
  19.  * The Sprite class is a basic display list building block: a display list
  20.  * node that can display graphics and can also contain children.
  21.  *
  22.  * A Sprite object is similar to a movie clip, but does not have a
  23.  * timeline. Sprite is an appropriate base class for objects that do not
  24.  * require timelines. For example, Sprite would be a logical base class for
  25.  * user interface(UI) components that typically do not use the timeline.
  26.  *
  27.  * The Sprite class is new in ActionScript 3.0. It provides an alternative
  28.  * to the functionality of the MovieClip class, which retains all the
  29.  * functionality of previous ActionScript releases to provide backward
  30.  * compatibility.
  31.  */
  32.  
  33. #if !openfl_debug
  34. @:fileXml('tags="haxe,release"')
  35. @:noDebug
  36. #end
  37.  
  38. @:access(openfl.display.Graphics)
  39. @:access(openfl.display.Stage)
  40. @:access(openfl.geom.Point)
  41.  
  42.  
  43. class Sprite extends DisplayObjectContainer {
  44.    
  45.    
  46.     /**
  47.      * Specifies the button mode of this sprite. If `true`, this
  48.      * sprite behaves as a button, which means that it triggers the display of
  49.      * the hand cursor when the pointer passes over the sprite and can receive a
  50.      * `click` event if the enter or space keys are pressed when the
  51.      * sprite has focus. You can suppress the display of the hand cursor by
  52.      * setting the `useHandCursor` property to `false`, in
  53.      * which case the pointer is displayed.
  54.      *
  55.      * Although it is better to use the SimpleButton class to create buttons,
  56.      * you can use the `buttonMode` property to give a sprite some
  57.      * button-like functionality. To include a sprite in the tab order, set the
  58.      * `tabEnabled` property(inherited from the InteractiveObject
  59.      * class and `false` by default) to `true`.
  60.      * Additionally, consider whether you want the children of your sprite to be
  61.      * user input enabled. Most buttons do not enable user input interactivity
  62.      * for their child objects because it confuses the event flow. To disable
  63.      * user input interactivity for all child objects, you must set the
  64.      * `mouseChildren` property(inherited from the
  65.      * DisplayObjectContainer class) to `false`.
  66.      *
  67.      * If you use the `buttonMode` property with the MovieClip
  68.      * class(which is a subclass of the Sprite class), your button might have
  69.      * some added functionality. If you include frames labeled _up, _over, and
  70.      * _down, Flash Player provides automatic state changes(functionality
  71.      * similar to that provided in previous versions of ActionScript for movie
  72.      * clips used as buttons). These automatic state changes are not available
  73.      * for sprites, which have no timeline, and thus no frames to label.
  74.      */
  75.     public var buttonMode (get, set):Bool;
  76.     public var dropTarget (default, null):DisplayObject;
  77.    
  78.     /**
  79.      * Specifies the Graphics object that belongs to this sprite where vector
  80.      * drawing commands can occur.
  81.      */
  82.     public var graphics (get, never):Graphics;
  83.     public var hitArea:Sprite;
  84.    
  85.     /**
  86.      * A Boolean value that indicates whether the pointing hand(hand cursor)
  87.      * appears when the pointer rolls over a sprite in which the
  88.      * `buttonMode` property is set to `true`. The default
  89.      * value of the `useHandCursor` property is `true`. If
  90.      * `useHandCursor` is set to `true`, the pointing hand
  91.      * used for buttons appears when the pointer rolls over a button sprite. If
  92.      * `useHandCursor` is `false`, the arrow pointer is
  93.      * used instead.
  94.      *
  95.      * You can change the `useHandCursor` property at any time; the
  96.      * modified sprite immediately takes on the new cursor appearance.
  97.      *
  98.      * **Note:** In Flex or Flash Builder, if your sprite has child
  99.      * sprites, you might want to set the `mouseChildren` property to
  100.      * `false`. For example, if you want a hand cursor to appear over
  101.      * a Flex <mx:Label> control, set the `useHandCursor` and
  102.      * `buttonMode` properties to `true`, and the
  103.      * `mouseChildren` property to `false`.
  104.      */
  105.     public var useHandCursor:Bool;
  106.    
  107.     @:noCompletion private var __buttonMode:Bool;
  108.    
  109.    
  110.     #if openfljs
  111.     private static function __init__ () {
  112.        
  113.         untyped Object.defineProperties (Sprite.prototype, {
  114.             "buttonMode": { get: untyped __js__ ("function () { return this.get_buttonMode (); }"), set: untyped __js__ ("function (v) { return this.set_buttonMode (v); }") },
  115.             "graphics": { get: untyped __js__ ("function () { return this.get_graphics (); }") },
  116.         });
  117.        
  118.     }
  119.     #end
  120.    
  121.    
  122.     /**
  123.      * Creates a new Sprite instance. After you create the Sprite instance, call
  124.      * the `DisplayObjectContainer.addChild()` or
  125.      * `DisplayObjectContainer.addChildAt()` method to add the Sprite
  126.      * to a parent DisplayObjectContainer.
  127.      */
  128.     public function new () {
  129.        
  130.         super ();
  131.        
  132.         __buttonMode = false;
  133.         useHandCursor = true;
  134.        
  135.     }
  136.    
  137.    
  138.     /**
  139.      * Lets the user drag the specified sprite. The sprite remains draggable
  140.      * until explicitly stopped through a call to the
  141.      * `Sprite.stopDrag()` method, or until another sprite is made
  142.      * draggable. Only one sprite is draggable at a time.
  143.      *
  144.      * Three-dimensional display objects follow the pointer and
  145.      * `Sprite.startDrag()` moves the object within the
  146.      * three-dimensional plane defined by the display object. Or, if the display
  147.      * object is a two-dimensional object and the child of a three-dimensional
  148.      * object, the two-dimensional object moves within the three dimensional
  149.      * plane defined by the three-dimensional parent object.
  150.      *
  151.      * @param lockCenter Specifies whether the draggable sprite is locked to the
  152.      *                   center of the pointer position(`true`), or
  153.      *                   locked to the point where the user first clicked the
  154.      *                   sprite(`false`).
  155.      * @param bounds     Value relative to the coordinates of the Sprite's parent
  156.      *                   that specify a constraint rectangle for the Sprite.
  157.      */
  158.     public function startDrag (lockCenter:Bool = false, bounds:Rectangle = null):Void {
  159.        
  160.         if (stage != null) {
  161.            
  162.             stage.__startDrag (this, lockCenter, bounds);
  163.            
  164.         }
  165.        
  166.     }
  167.    
  168.    
  169.     /**
  170.      * Ends the `startDrag()` method. A sprite that was made draggable
  171.      * with the `startDrag()` method remains draggable until a
  172.      * `stopDrag()` method is added, or until another sprite becomes
  173.      * draggable. Only one sprite is draggable at a time.
  174.      *
  175.      */
  176.     public function stopDrag ():Void {
  177.        
  178.         if (stage != null) {
  179.            
  180.             stage.__stopDrag (this);
  181.            
  182.         }
  183.        
  184.     }
  185.    
  186.    
  187.     @:noCompletion private override function __getCursor ():Cursor {
  188.        
  189.         return (__buttonMode && useHandCursor) ? POINTER : null;
  190.        
  191.     }
  192.    
  193.    
  194.     @:noCompletion private override function __hitTest (x:Float, y:Float, shapeFlag:Bool, stack:Array<DisplayObject>, interactiveOnly:Bool, hitObject:DisplayObject):Bool {
  195.        
  196.         if (interactiveOnly && !mouseEnabled && !mouseChildren) return false;
  197.         if (!hitObject.visible || __isMask) return __hitTestHitArea (x, y, shapeFlag, stack, interactiveOnly, hitObject);
  198.         if (mask != null && !mask.__hitTestMask (x, y)) return __hitTestHitArea (x, y, shapeFlag, stack, interactiveOnly, hitObject);
  199.        
  200.         if (__scrollRect != null) {
  201.            
  202.             var point = Point.__pool.get ();
  203.             point.setTo (x, y);
  204.             __getRenderTransform ().__transformInversePoint (point);
  205.            
  206.             if (!__scrollRect.containsPoint (point)) {
  207.                
  208.                 Point.__pool.release (point);
  209.                 return __hitTestHitArea (x, y, shapeFlag, stack, true, hitObject);
  210.                
  211.             }
  212.            
  213.             Point.__pool.release (point);
  214.            
  215.         }
  216.        
  217.         if (super.__hitTest (x, y, shapeFlag, stack, interactiveOnly, hitObject)) {
  218.            
  219.             return interactiveOnly;
  220.            
  221.         } else if (hitArea == null && __graphics != null && __graphics.__hitTest (x, y, shapeFlag, __getRenderTransform ())) {
  222.            
  223.             if (stack != null && (!interactiveOnly || mouseEnabled)) {
  224.                
  225.                 stack.push (hitObject);
  226.                
  227.             }
  228.            
  229.             return true;
  230.            
  231.         }
  232.        
  233.         return __hitTestHitArea (x, y, shapeFlag, stack, interactiveOnly, hitObject);
  234.        
  235.     }
  236.    
  237.    
  238.     @:noCompletion private function __hitTestHitArea (x:Float, y:Float, shapeFlag:Bool, stack:Array<DisplayObject>, interactiveOnly:Bool, hitObject:DisplayObject):Bool {
  239.        
  240.         if (hitArea != null) {
  241.            
  242.             if (!hitArea.mouseEnabled) {
  243.                
  244.                 hitArea.mouseEnabled = true;
  245.                 var hitTest = hitArea.__hitTest (x, y, shapeFlag, null, true, hitObject);
  246.                 hitArea.mouseEnabled = false;
  247.                
  248.                 if (stack != null && hitTest) {
  249.                    
  250.                     stack[stack.length] = hitObject;
  251.                    
  252.                 }
  253.                
  254.                 return hitTest;
  255.                
  256.             }
  257.            
  258.         }
  259.        
  260.         return false;
  261.        
  262.     }
  263.    
  264.    
  265.     @:noCompletion private override function __hitTestMask (x:Float, y:Float):Bool {
  266.        
  267.         if (super.__hitTestMask (x, y)) {
  268.            
  269.             return true;
  270.            
  271.         } else if (__graphics != null && __graphics.__hitTest (x, y, true, __getRenderTransform ())) {
  272.            
  273.             return true;
  274.            
  275.         }
  276.        
  277.         return false;
  278.        
  279.     }
  280.    
  281.    
  282.    
  283.    
  284.     // Get & Set Methods
  285.    
  286.    
  287.    
  288.    
  289.     @:noCompletion private function get_graphics ():Graphics {
  290.        
  291.         if (__graphics == null) {
  292.            
  293.             __graphics = new Graphics (this);
  294.            
  295.         }
  296.        
  297.         return __graphics;
  298.        
  299.     }
  300.    
  301.    
  302.     @:noCompletion private override function get_tabEnabled ():Bool {
  303.        
  304.         return (__tabEnabled == null ? __buttonMode : __tabEnabled);
  305.        
  306.     }
  307.    
  308.    
  309.     @:noCompletion private function get_buttonMode ():Bool {
  310.        
  311.         return __buttonMode;
  312.        
  313.     }
  314.    
  315.    
  316.     @:noCompletion private function set_buttonMode (value:Bool):Bool {
  317.        
  318.         return __buttonMode = value;
  319.        
  320.     }
  321.    
  322.    
  323. }
  324.  
  325.  
  326. #else
  327. typedef Sprite = flash.display.Sprite;
  328. #end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement