Skip to content

Instantly share code, notes, and snippets.

@bendmorris
Created September 4, 2016 16:29
Show Gist options
  • Save bendmorris/7a6e66f8e1a17659fd50338fbc445366 to your computer and use it in GitHub Desktop.
Save bendmorris/7a6e66f8e1a17659fd50338fbc445366 to your computer and use it in GitHub Desktop.
Flx9SliceSprite, which simply moves and scales 9 other FlxSprites
package flixel;
import flixel.FlxSprite;
import flixel.graphics.FlxGraphic;
import flixel.graphics.frames.FlxImageFrame;
import flixel.math.FlxPoint;
import flixel.math.FlxRect;
import flixel.system.FlxAssets;
import flixel.util.FlxColor;
class Flx9SliceSprite extends FlxSprite
{
public var sliceRect:FlxRect;
var tl:FlxSprite;
var tc:FlxSprite;
var tr:FlxSprite;
var cl:FlxSprite;
var cc:FlxSprite;
var cr:FlxSprite;
var bl:FlxSprite;
var bc:FlxSprite;
var br:FlxSprite;
public function new(graphic:FlxGraphicAsset, sliceRect:FlxRect, ?width:Float=0, ?height:Float=0)
{
super();
this.sliceRect = sliceRect;
var graphic = FlxG.bitmap.add(graphic);
frameWidth = graphic.width;
frameHeight = graphic.height;
tl = loadFrame(graphic, 0, 0, sliceRect.left, sliceRect.top);
tc = loadFrame(graphic, sliceRect.left, 0, sliceRect.right, sliceRect.top);
tr = loadFrame(graphic, sliceRect.right, 0, frameWidth, sliceRect.top);
cl = loadFrame(graphic, 0, sliceRect.top, sliceRect.left, sliceRect.bottom);
cc = loadFrame(graphic, sliceRect.left, sliceRect.top, sliceRect.right, sliceRect.bottom);
cr = loadFrame(graphic, sliceRect.right, sliceRect.top, frameWidth, sliceRect.bottom);
bl = loadFrame(graphic, 0, sliceRect.bottom, sliceRect.left, frameHeight);
bc = loadFrame(graphic, sliceRect.left, sliceRect.bottom, sliceRect.right, frameHeight);
br = loadFrame(graphic, sliceRect.right, sliceRect.bottom, frameWidth, frameHeight);
this.width = width == 0 ? frameWidth : width;
this.height = height == 0 ? frameHeight : height;
}
inline function loadFrame(
graphic:FlxGraphic,
x0:Float,
y0:Float,
x1:Float,
y1:Float
):FlxSprite
{
var sprite = new FlxSprite(0, 0, FlxGraphic.fromFrame(
FlxImageFrame.fromRectangle(
graphic,
FlxRect.get(x0, y0, x1-x0, y1-y0)
).frame
));
return sprite;
}
override public function destroy():Void
{
sliceRect = null;
tl.destroy();
tl = null;
tc.destroy();
tc = null;
tr.destroy();
tr = null;
cl.destroy();
cl = null;
cc.destroy();
cc = null;
cr.destroy();
cr = null;
bl.destroy();
bl = null;
bc.destroy();
bc = null;
br.destroy();
br = null;
super.destroy();
}
override public function draw():Void
{
var leftWidth:Float = Std.int(sliceRect.left * scale.x),
rightWidth:Float = Std.int((frameWidth - sliceRect.right) * scale.x),
centerWidth:Float = Std.int(width - leftWidth - rightWidth);
var topHeight:Float = Std.int(sliceRect.top * scale.y),
bottomHeight:Float = Std.int((frameHeight - sliceRect.bottom) * scale.y),
centerHeight:Float = Std.int(height - topHeight - bottomHeight);
var leftX = 0, centerX = leftWidth, rightX = leftWidth + centerWidth,
topY = 0, centerY = topHeight, bottomY = topHeight + centerHeight;
drawSlice(tl, leftX, topY, leftWidth, topHeight);
drawSlice(tc, centerX, topY, centerWidth, topHeight);
drawSlice(tr, rightX, topY, rightWidth, topHeight);
drawSlice(cl, leftX, centerY, leftWidth, centerHeight);
drawSlice(cc, centerX, centerY, centerWidth, centerHeight);
drawSlice(cr, rightX, centerY, rightWidth, centerHeight);
drawSlice(bl, leftX, bottomY, leftWidth, bottomHeight);
drawSlice(bc, centerX, bottomY, centerWidth, bottomHeight);
drawSlice(br, rightX, bottomY, rightWidth, bottomHeight);
}
inline function drawSlice(slice:FlxSprite, x:Float, y:Float, width:Float, height:Float)
{
if (clipRect != null)
{
width = Math.min(clipRect.right - x, width);
x = Math.max(clipRect.x, x);
height = Math.min(clipRect.bottom - y, height);
y = Math.max(clipRect.y, y);
slice.visible = !(width <= 0 || height <= 0);
}
slice.x = this.x + x;
slice.y = this.y + y;
slice.width = width;
slice.height = height;
slice.origin.set(0, 0);
slice.scrollFactor.set(scrollFactor.x, scrollFactor.y);
slice.scale.set(width / slice.frameWidth, height / slice.frameHeight);
if (slice.visible) slice.draw();
}
override public function pixelsOverlapPoint(point:FlxPoint, mask:Int = 0xFF, ?camera:FlxCamera):Bool
{
return tl.overlapsPoint(point, true, camera)
|| tc.overlapsPoint(point, true, camera)
|| tr.overlapsPoint(point, true, camera)
|| cl.overlapsPoint(point, true, camera)
|| cc.overlapsPoint(point, true, camera)
|| cr.overlapsPoint(point, true, camera)
|| bl.overlapsPoint(point, true, camera)
|| bc.overlapsPoint(point, true, camera)
|| br.overlapsPoint(point, true, camera);
}
override function set_antialiasing(value:Bool):Bool
{
return antialiasing =
tl.antialiasing =
tc.antialiasing =
tr.antialiasing =
cl.antialiasing =
cc.antialiasing =
cr.antialiasing =
bl.antialiasing =
bc.antialiasing =
br.antialiasing =
value;
}
override function set_color(value:FlxColor):FlxColor
{
return color =
tl.color =
tc.color =
tr.color =
cl.color =
cc.color =
cr.color =
bl.color =
bc.color =
br.color =
value;
}
override function set_alpha(value:Float):Float
{
return alpha =
tl.alpha =
tc.alpha =
tr.alpha =
cl.alpha =
cc.alpha =
cr.alpha =
bl.alpha =
bc.alpha =
br.alpha =
value;
}
override function get_width():Float return this.width;
override function set_width(value:Float):Float return this.width = value;
override function get_height():Float return this.height;
override function set_height(value:Float):Float return this.height = value;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment