Skip to content

Instantly share code, notes, and snippets.

@markknol
Last active November 9, 2015 21:40
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 markknol/fa2049f9fee27580dccc to your computer and use it in GitHub Desktop.
Save markknol/fa2049f9fee27580dccc to your computer and use it in GitHub Desktop.
Flambe - 9-slice example drawing http://projects.stroep.nl/flambe-9slice/
package urgame;
import flambe.animation.Sine;
import flambe.asset.Manifest;
import flambe.Entity;
import flambe.System;
class MainTest {
private static function main () {
System.init();
System.loadAssetPack(Manifest.fromAssets("bootstrap")).get(function(bootstrapPack) {
var image = new NineSlice(bootstrapPack.getTexture("image"), 4);
image.scale.behavior = new Sine(1, 3, 5); // fancy sine here
System.root.addChild(new Entity()
.add(image));
});
}
}
package urgame;
import flambe.animation.AnimatedFloat;
import flambe.display.Graphics;
import flambe.display.ImageSprite;
import flambe.display.SubTexture;
import flambe.display.Texture;
/**
* @author Mark Knol
*/
class NineSlice extends ImageSprite {
public var scale:AnimatedFloat;
var _subTextures:Array<SubTexture>;
public function new(texture:Texture, scaling:Float = 1) {
super(texture);
scale = new AnimatedFloat(scaling);
_subTextures = [for (y in 0...3) {
for (x in 0...3) {
trace(texture.width / 3 * x, texture.width / 3 * y);
texture.subTexture(
Std.int(texture.width / 3 * x),
Std.int(texture.height / 3 * y),
Std.int(texture.width / 3),
Std.int(texture.height / 3));
}
}];
}
override public function draw(g:Graphics) {
//super.draw(g); // don't
var scale:Float = scale._;
var scaleBack:Float = 1 / scale;
var dontScale:Float = 1;
var x0 = 0;
var x1 = _subTextures[0].width / scale;
var x2 = x1 * scale + _subTextures[1].width * scale;
var y0 = 0;
var y1 = _subTextures[0].height / scale;
var y2 = y1 * scale + _subTextures[1].height * scale;
// save state
g.save();
// draw textures
g.drawTexture(_subTextures[0], x0, y0);
g.scale(scale, dontScale);
g.drawTexture(_subTextures[1], x1, y0);
g.scale(scaleBack, dontScale);
g.drawTexture(_subTextures[2], x2, y0);
g.scale(dontScale, scale);
g.drawTexture(_subTextures[3], x0, y1);
g.scale(scale, dontScale);
g.drawTexture(_subTextures[4], x1, y1);
g.scale(scaleBack, dontScale);
g.drawTexture(_subTextures[5], x2, y1);
g.scale(dontScale, scaleBack);
g.drawTexture(_subTextures[6], x0, y2);
g.scale(scale, dontScale);
g.drawTexture(_subTextures[7], x1, y2);
g.scale(scaleBack, dontScale);
g.drawTexture(_subTextures[8], x2, y2);
// restore state for further drawing
g.restore();
}
override public function onUpdate(dt:Float) {
super.onUpdate(dt);
scale.update(dt);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment