Skip to content

Instantly share code, notes, and snippets.

@cnsoft
Forked from PrimaryFeather/ClippedSprite.as
Created November 20, 2012 08:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cnsoft/4116710 to your computer and use it in GitHub Desktop.
Save cnsoft/4116710 to your computer and use it in GitHub Desktop.
Simple Sprite subclass with a rectangular mask in stage coordinates
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;
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);
else
{
var context:Context3D = Starling.context;
if (context == null) throw new MissingContextError();
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
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;
var scale:Number = Starling.current.contentScaleFactor;
var globalPoint:Point = localToGlobal(localPoint);
if (mClipRect.contains(globalPoint.x * contentScaleX, globalPoint.y * contentScaleY))
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 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);
}
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;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment