Skip to content

Instantly share code, notes, and snippets.

@PrimaryFeather
Created April 19, 2012 17:02
Show Gist options
  • Select an option

  • Save PrimaryFeather/2422317 to your computer and use it in GitHub Desktop.

Select an option

Save PrimaryFeather/2422317 to your computer and use it in GitHub Desktop.

Revisions

  1. PrimaryFeather revised this gist Nov 20, 2012. 1 changed file with 6 additions and 37 deletions.
    43 changes: 6 additions & 37 deletions ClippedSprite.as
    Original file line number Diff line number Diff line change
    @@ -23,12 +23,12 @@ package starling.extensions
    if (context == null) throw new MissingContextError();

    support.finishQuadBatch();
    context.setScissorRectangle(mClipRect);
    support.scissorRectangle = mClipRect;

    super.render(support, alpha);

    support.finishQuadBatch();
    context.setScissorRectangle(null);
    support.scissorRectangle = null;
    }
    }

    @@ -40,52 +40,21 @@ package starling.extensions
    // on a touch test, invisible or untouchable objects cause the test to fail
    if (forTouch && (!visible || !touchable)) return null;

    var scale:Number = Starling.current.contentScaleFactor;
    var globalPoint:Point = localToGlobal(localPoint);

    if (mClipRect.contains(globalPoint.x * contentScaleX, globalPoint.y * contentScaleY))
    if (mClipRect.containsPoint(localToGlobal(localPoint)))
    return super.hitTest(localPoint, forTouch);
    else
    return null;
    }

    public function get clipRect():Rectangle
    {
    if (mClipRect)
    {
    var scaleX:Number = contentScaleX;
    var scaleY:Number = contentScaleY;

    return new Rectangle(mClipRect.x / scaleX, mClipRect.y / scaleY,
    mClipRect.width / scaleX, mClipRect.height / scaleY);
    }
    else return null;
    }

    public function get clipRect():Rectangle { return mClipRect; }
    public function set clipRect(value:Rectangle):void
    {
    if (value)
    {
    var scaleX:Number = contentScaleX;
    var scaleY:Number = contentScaleY;

    if (mClipRect == null) mClipRect = new Rectangle();
    mClipRect.setTo(scaleX * value.x, scaleY * value.y,
    scaleX * value.width, scaleY * value.height);
    if (mClipRect == null) mClipRect = value.clone();
    else mClipRect.setTo(value.x, value.y, value.width, value.height);
    }
    else mClipRect = null;
    }

    private function get contentScaleX():Number
    {
    var currentStarling:Starling = Starling.current;
    return currentStarling.viewPort.width / currentStarling.stage.stageWidth;
    }

    private function get contentScaleY():Number
    {
    var currentStarling:Starling = Starling.current;
    return currentStarling.viewPort.height / currentStarling.stage.stageHeight;
    }
    }
    }
  2. PrimaryFeather revised this gist Aug 7, 2012. 1 changed file with 12 additions and 13 deletions.
    25 changes: 12 additions & 13 deletions ClippedSprite.as
    Original file line number Diff line number Diff line change
    @@ -51,30 +51,29 @@ package starling.extensions

    public function get clipRect():Rectangle
    {
    if (mClipRect == null) return null;

    var scaleX:Number = contentScaleX;
    var scaleY:Number = contentScaleY;

    return new Rectangle(mClipRect.x / scaleX, mClipRect.y / scaleY,
    mClipRect.width / scaleX, mClipRect.height / scaleY);
    if (mClipRect)
    {
    var scaleX:Number = contentScaleX;
    var scaleY:Number = contentScaleY;

    return new Rectangle(mClipRect.x / scaleX, mClipRect.y / scaleY,
    mClipRect.width / scaleX, mClipRect.height / scaleY);
    }
    else return null;
    }

    public function set clipRect(value:Rectangle):void
    {
    if (value == null)
    if (value)
    {
    mClipRect = null;
    }
    else
    {
    var scaleX:Number = contentScaleX;
    var scaleY:Number = contentScaleY;

    if (mClipRect == null) mClipRect = new Rectangle();
    mClipRect.setTo(scaleX * value.x, scaleY * value.y,
    scaleX * value.width, scaleY * value.height);
    scaleX * value.width, scaleY * value.height);
    }
    else mClipRect = null;
    }

    private function get contentScaleX():Number
  3. PrimaryFeather revised this gist Aug 7, 2012. 1 changed file with 21 additions and 12 deletions.
    33 changes: 21 additions & 12 deletions ClippedSprite.as
    Original file line number Diff line number Diff line change
    @@ -49,23 +49,32 @@ package starling.extensions
    return null;
    }

    public function get clipRect():Rectangle
    {
    public function get clipRect():Rectangle
    {
    if (mClipRect == null) return null;

    var scaleX:Number = contentScaleX;
    var scaleY:Number = contentScaleY;

    return new Rectangle(mClipRect.x / scaleX, mClipRect.y / scaleY,
    mClipRect.width / scaleX, mClipRect.height / scaleY);
    return new Rectangle(mClipRect.x / scaleX, mClipRect.y / scaleY,
    mClipRect.width / scaleX, mClipRect.height / scaleY);
    }

    public function set clipRect(value:Rectangle):void
    {
    var scaleX:Number = contentScaleX;
    var scaleY:Number = contentScaleY;

    if (mClipRect == null) mClipRect = new Rectangle();
    mClipRect.setTo(scaleX * value.x, scaleY * value.y,
    scaleX * value.width, scaleY * value.height);
    public function set clipRect(value:Rectangle):void
    {
    if (value == null)
    {
    mClipRect = null;
    }
    else
    {
    var scaleX:Number = contentScaleX;
    var scaleY:Number = contentScaleY;

    if (mClipRect == null) mClipRect = new Rectangle();
    mClipRect.setTo(scaleX * value.x, scaleY * value.y,
    scaleX * value.width, scaleY * value.height);
    }
    }

    private function get contentScaleX():Number
  4. Daniel Sperl revised this gist Jul 17, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion ClippedSprite.as
    Original file line number Diff line number Diff line change
    @@ -8,7 +8,6 @@ package starling.extensions
    import starling.core.Starling;
    import starling.display.DisplayObject;
    import starling.display.Sprite;
    import starling.display.Stage;
    import starling.errors.MissingContextError;

    public class ClippedSprite extends Sprite
    @@ -41,6 +40,7 @@ package starling.extensions
    // on a touch test, invisible or untouchable objects cause the test to fail
    if (forTouch && (!visible || !touchable)) return null;

    var scale:Number = Starling.current.contentScaleFactor;
    var globalPoint:Point = localToGlobal(localPoint);

    if (mClipRect.contains(globalPoint.x * contentScaleX, globalPoint.y * contentScaleY))
  5. Daniel Sperl revised this gist Jul 17, 2012. 1 changed file with 17 additions and 10 deletions.
    27 changes: 17 additions & 10 deletions ClippedSprite.as
    Original file line number Diff line number Diff line change
    @@ -41,36 +41,43 @@ package starling.extensions
    // on a touch test, invisible or untouchable objects cause the test to fail
    if (forTouch && (!visible || !touchable)) return null;

    var scale:Number = Starling.current.contentScaleFactor;
    var globalPoint:Point = localToGlobal(localPoint);

    if (mClipRect.contains(globalPoint.x * scale, globalPoint.y * scale))
    if (mClipRect.contains(globalPoint.x * contentScaleX, globalPoint.y * contentScaleY))
    return super.hitTest(localPoint, forTouch);
    else
    return null;
    }

    public function get clipRect():Rectangle
    {
    var viewPort:Rectangle = Starling.current.viewPort;
    var stage:Stage = Starling.current.stage;
    var scaleX:Number = viewPort.width / stage.stageWidth;
    var scaleY:Number = viewPort.height / stage.stageHeight;
    var scaleX:Number = contentScaleX;
    var scaleY:Number = contentScaleY;

    return new Rectangle(mClipRect.x / scaleX, mClipRect.y / scaleY,
    mClipRect.width / scaleX, mClipRect.height / scaleY);
    }

    public function set clipRect(value:Rectangle):void
    {
    var viewPort:Rectangle = Starling.current.viewPort;
    var stage:Stage = Starling.current.stage;
    var scaleX:Number = viewPort.width / stage.stageWidth;
    var scaleY:Number = viewPort.height / stage.stageHeight;
    var scaleX:Number = contentScaleX;
    var scaleY:Number = contentScaleY;

    if (mClipRect == null) mClipRect = new Rectangle();
    mClipRect.setTo(scaleX * value.x, scaleY * value.y,
    scaleX * value.width, scaleY * value.height);
    }

    private function get contentScaleX():Number
    {
    var currentStarling:Starling = Starling.current;
    return currentStarling.viewPort.width / currentStarling.stage.stageWidth;
    }

    private function get contentScaleY():Number
    {
    var currentStarling:Starling = Starling.current;
    return currentStarling.viewPort.height / currentStarling.stage.stageHeight;
    }
    }
    }
  6. Daniel Sperl revised this gist Jul 16, 2012. 1 changed file with 18 additions and 9 deletions.
    27 changes: 18 additions & 9 deletions ClippedSprite.as
    Original file line number Diff line number Diff line change
    @@ -8,12 +8,13 @@ package starling.extensions
    import starling.core.Starling;
    import starling.display.DisplayObject;
    import starling.display.Sprite;
    import starling.display.Stage;
    import starling.errors.MissingContextError;

    public class ClippedSprite extends Sprite
    {
    private var mClipRect:Rectangle;

    public override function render(support:RenderSupport, alpha:Number):void
    {
    if (mClipRect == null) super.render(support, alpha);
    @@ -24,14 +25,14 @@ package starling.extensions

    support.finishQuadBatch();
    context.setScissorRectangle(mClipRect);

    super.render(support, alpha);

    support.finishQuadBatch();
    context.setScissorRectangle(null);
    }
    }

    public override function hitTest(localPoint:Point, forTouch:Boolean=false):DisplayObject
    {
    // without a clip rect, the sprite should behave just like before
    @@ -51,17 +52,25 @@ package starling.extensions

    public function get clipRect():Rectangle
    {
    var scale:Number = Starling.current.contentScaleFactor;
    return new Rectangle(mClipRect.x / scale, mClipRect.y / scale,
    mClipRect.width / scale, mClipRect.height / scale);
    var viewPort:Rectangle = Starling.current.viewPort;
    var stage:Stage = Starling.current.stage;
    var scaleX:Number = viewPort.width / stage.stageWidth;
    var scaleY:Number = viewPort.height / stage.stageHeight;

    return new Rectangle(mClipRect.x / scaleX, mClipRect.y / scaleY,
    mClipRect.width / scaleX, mClipRect.height / scaleY);
    }

    public function set clipRect(value:Rectangle):void
    {
    var scale:Number = Starling.current.contentScaleFactor;
    var viewPort:Rectangle = Starling.current.viewPort;
    var stage:Stage = Starling.current.stage;
    var scaleX:Number = viewPort.width / stage.stageWidth;
    var scaleY:Number = viewPort.height / stage.stageHeight;

    if (mClipRect == null) mClipRect = new Rectangle();
    mClipRect.x = scale * value.x; mClipRect.y = scale * value.y;
    mClipRect.width = scale * value.width; mClipRect.height = scale * value.height;
    mClipRect.setTo(scaleX * value.x, scaleY * value.y,
    scaleX * value.width, scaleY * value.height);
    }
    }
    }
  7. Daniel Sperl revised this gist Jul 9, 2012. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions ClippedSprite.as
    Original file line number Diff line number Diff line change
    @@ -1,10 +1,12 @@
    package starling.extensions
    {
    import flash.display3D.Context3D;
    import flash.geom.Point;
    import flash.geom.Rectangle;

    import starling.core.RenderSupport;
    import starling.core.Starling;
    import starling.display.DisplayObject;
    import starling.display.Sprite;
    import starling.errors.MissingContextError;

  8. Daniel Sperl revised this gist Jun 10, 2012. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions ClippedSprite.as
    Original file line number Diff line number Diff line change
    @@ -32,12 +32,12 @@ package starling.extensions

    public override function hitTest(localPoint:Point, forTouch:Boolean=false):DisplayObject
    {
    // without a clip rect, the sprite should behave just like before
    if (mClipRect == null) return super.hitTest(localPoint, forTouch);

    // on a touch test, invisible or untouchable objects cause the test to fail
    if (forTouch && (!visible || !touchable)) return null;

    // without a clip rect, the sprite should behave just like before
    if (mClipRect == null) return super.hitTest(localPoint, forTouch);

    var scale:Number = Starling.current.contentScaleFactor;
    var globalPoint:Point = localToGlobal(localPoint);

  9. Daniel Sperl revised this gist Jun 10, 2012. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions ClippedSprite.as
    Original file line number Diff line number Diff line change
    @@ -35,6 +35,9 @@ package starling.extensions
    // on a touch test, invisible or untouchable objects cause the test to fail
    if (forTouch && (!visible || !touchable)) return null;

    // without a clip rect, the sprite should behave just like before
    if (mClipRect == null) return super.hitTest(localPoint, forTouch);

    var scale:Number = Starling.current.contentScaleFactor;
    var globalPoint:Point = localToGlobal(localPoint);

  10. Daniel Sperl revised this gist Apr 21, 2012. 1 changed file with 14 additions and 0 deletions.
    14 changes: 14 additions & 0 deletions ClippedSprite.as
    Original file line number Diff line number Diff line change
    @@ -29,6 +29,20 @@ package starling.extensions
    context.setScissorRectangle(null);
    }
    }

    public override function hitTest(localPoint:Point, forTouch:Boolean=false):DisplayObject
    {
    // on a touch test, invisible or untouchable objects cause the test to fail
    if (forTouch && (!visible || !touchable)) return null;

    var scale:Number = Starling.current.contentScaleFactor;
    var globalPoint:Point = localToGlobal(localPoint);

    if (mClipRect.contains(globalPoint.x * scale, globalPoint.y * scale))
    return super.hitTest(localPoint, forTouch);
    else
    return null;
    }

    public function get clipRect():Rectangle
    {
  11. Daniel Sperl revised this gist Apr 20, 2012. 1 changed file with 4 additions and 3 deletions.
    7 changes: 4 additions & 3 deletions ClippedSprite.as
    Original file line number Diff line number Diff line change
    @@ -40,8 +40,9 @@ package starling.extensions
    public function set clipRect(value:Rectangle):void
    {
    var scale:Number = Starling.current.contentScaleFactor;
    mClipRect = new Rectangle(value.x * scale, value.y * scale,
    value.width * scale, value.height * scale);
    }
    if (mClipRect == null) mClipRect = new Rectangle();
    mClipRect.x = scale * value.x; mClipRect.y = scale * value.y;
    mClipRect.width = scale * value.width; mClipRect.height = scale * value.height;
    }
    }
    }
  12. Daniel Sperl revised this gist Apr 19, 2012. 1 changed file with 9 additions and 7 deletions.
    16 changes: 9 additions & 7 deletions ClippedSprite.as
    Original file line number Diff line number Diff line change
    @@ -10,18 +10,18 @@ package starling.extensions

    public class ClippedSprite extends Sprite
    {
    private var mMask:Rectangle;
    private var mClipRect:Rectangle;

    public override function render(support:RenderSupport, alpha:Number):void
    {
    if (mMask == null) super.render(support, alpha);
    if (mClipRect == null) super.render(support, alpha);
    else
    {
    var context:Context3D = Starling.context;
    if (context == null) throw new MissingContextError();

    support.finishQuadBatch();
    context.setScissorRectangle(mMask);
    context.setScissorRectangle(mClipRect);

    super.render(support, alpha);

    @@ -30,16 +30,18 @@ package starling.extensions
    }
    }

    public function get mask():Rectangle
    public function get clipRect():Rectangle
    {
    var scale:Number = Starling.current.contentScaleFactor;
    return new Rectangle(mMask.x/scale, mMask.y/scale, mMask.width/scale, mMask.height/scale);
    return new Rectangle(mClipRect.x / scale, mClipRect.y / scale,
    mClipRect.width / scale, mClipRect.height / scale);
    }

    public function set mask(value:Rectangle):void
    public function set clipRect(value:Rectangle):void
    {
    var scale:Number = Starling.current.contentScaleFactor;
    mMask = new Rectangle(value.x*scale, value.y*scale, value.width*scale, value.height*scale);
    mClipRect = new Rectangle(value.x * scale, value.y * scale,
    value.width * scale, value.height * scale);
    }
    }
    }
  13. Daniel Sperl renamed this gist Apr 19, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion MaskedSprite.as → ClippedSprite.as
    Original file line number Diff line number Diff line change
    @@ -8,7 +8,7 @@ package starling.extensions
    import starling.display.Sprite;
    import starling.errors.MissingContextError;

    public class MaskedSprite extends Sprite
    public class ClippedSprite extends Sprite
    {
    private var mMask:Rectangle;

  14. Daniel Sperl created this gist Apr 19, 2012.
    45 changes: 45 additions & 0 deletions MaskedSprite.as
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,45 @@
    package starling.extensions
    {
    import flash.display3D.Context3D;
    import flash.geom.Rectangle;

    import starling.core.RenderSupport;
    import starling.core.Starling;
    import starling.display.Sprite;
    import starling.errors.MissingContextError;

    public class MaskedSprite extends Sprite
    {
    private var mMask:Rectangle;

    public override function render(support:RenderSupport, alpha:Number):void
    {
    if (mMask == null) super.render(support, alpha);
    else
    {
    var context:Context3D = Starling.context;
    if (context == null) throw new MissingContextError();

    support.finishQuadBatch();
    context.setScissorRectangle(mMask);

    super.render(support, alpha);

    support.finishQuadBatch();
    context.setScissorRectangle(null);
    }
    }

    public function get mask():Rectangle
    {
    var scale:Number = Starling.current.contentScaleFactor;
    return new Rectangle(mMask.x/scale, mMask.y/scale, mMask.width/scale, mMask.height/scale);
    }

    public function set mask(value:Rectangle):void
    {
    var scale:Number = Starling.current.contentScaleFactor;
    mMask = new Rectangle(value.x*scale, value.y*scale, value.width*scale, value.height*scale);
    }
    }
    }