Advertisement
Guest User

Untitled

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