Skip to content

Instantly share code, notes, and snippets.

@resistcorp
Forked from PrimaryFeather/ClippedSprite.as
Created July 12, 2012 18:20
Show Gist options
  • Save resistcorp/3099899 to your computer and use it in GitHub Desktop.
Save resistcorp/3099899 to your computer and use it in GitHub Desktop.
Adds nesting to the Clipped Sprites extension in Starling see www.starling-framework.org // now compatible with the latest starling (as of 29 nov. 2012)
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;
protected static var sCurrentClipRect:Rectangle = null;
public static function get currentClipRect():Rectangle{
return sCurrentClipRect;
}
public override function render(support:RenderSupport, alpha:Number):void
{
if (mClipRect == null) super.render(support, alpha);
else
{
var context:Context3D = Starling.context,
previousRect:Rectangle = sCurrentClipRect;
if (context == null) throw new MissingContextError();
sCurrentClipRect = sCurrentClipRect?sCurrentClipRect.intersection(clipRect) : clipRect;
support.finishQuadBatch();
support.scissorRectangle = sCurrentClipRect;
if(sCurrentClipRect.width >= 1 && sCurrentClipRect.height >= 1){//prevent "noclipping" for values between 0 & 1
//trace(sCurrentClipRect.width && sCurrentClipRect.height);
super.render(support, alpha);
}
support.finishQuadBatch();
support.scissorRectangle = previousRect;
sCurrentClipRect = previousRect;
}
}
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 * scale, globalPoint.y * scale))
return super.hitTest(localPoint, forTouch);
else
return null;
}
public function get clipRect():Rectangle
{
if(mClipRect == null) return null;
var scale:Number = Starling.current.contentScaleFactor;
return new Rectangle(mClipRect.x / scale, mClipRect.y / scale,
mClipRect.width / scale, mClipRect.height / scale);
}
public function set clipRect(value:Rectangle):void
{
var scale:Number = Starling.current.contentScaleFactor;
if (value == null) {
mClipRect = null;
return;
}
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;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment