-
-
Save markknol/88893e41f3bb0200452f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package; | |
import flambe.texturepacker.TexturePackerLibrary; | |
import flambe.asset.Manifest; | |
import flambe.Entity; | |
import flambe.System; | |
/** | |
* @author Mark Knol [mediamonks] | |
*/ | |
class Main | |
{ | |
static public function main() | |
{ | |
System.init(); | |
new Main(); | |
} | |
public function new() | |
{ | |
System.loadAssetPack(Manifest.fromAssets("test")).get(function(pack) { | |
// create library bases on "generic XML" with multipack enabled | |
var library = new TexturePackerLibrary(pack, "sprites{n}.xml"); | |
// get sprite by its name. I enabled 'trim sprite names' to not have to add the extension | |
var sprite:TexturePackerSprite = library.createSprite("my-image"); | |
// put it on the stage. | |
System.root.addChild(new Entity().add(sprite)); | |
}); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package flambe.texturepacker; | |
import flambe.asset.AssetPack; | |
import flambe.display.SubTexture; | |
import flambe.display.Texture; | |
using flambe.util.Strings; | |
/** | |
* An exported TexturePacker library containing movies and bitmaps. | |
*/ | |
class TexturePackerLibrary | |
{ | |
var firstLibrary:SpritesLibrary; | |
/** | |
* Creates a library using files in an AssetPack. | |
* @param baseFile The filename in the pack of the TexturePackers's xml | |
* If you use "sprites{n}.xml", the multipack-feature will work, {n} should start at 0. | |
* If you use "sprites.xml" only a single file will be used | |
*/ | |
public function new (pack :AssetPack, baseFile :String) | |
{ | |
if (baseFile.indexOf("{n}") > -1) | |
{ | |
var n = 0; | |
while (true) { | |
var fileName = baseFile.split("{n}").join(Std.string(n)); | |
if (pack.getFile(fileName, false) == null) { | |
break; | |
} | |
var library = new SpritesLibrary(pack, fileName); | |
library.next = firstLibrary; | |
firstLibrary = library; | |
// trace("created library: " + fileName); | |
n++; | |
} | |
} | |
else | |
{ | |
firstLibrary = new SpritesLibrary(pack, baseFile); | |
} | |
} | |
/** | |
* Creates a sprite from a symbol name. | |
* @param required If true and the symbol is not in this library, an error is thrown. | |
*/ | |
public function createSprite (name :String, required :Bool = true) :TexturePackerSprite | |
{ | |
var sprite:TexturePackerSprite = null; | |
var child = firstLibrary; | |
while (child != null) { | |
sprite = child.createSprite(name, false); | |
if (sprite != null) { | |
break; | |
} | |
child = child.next; | |
} | |
if (required && sprite == null) { | |
throw "Missing sprite".withFields(["name", name]); | |
} | |
return sprite; | |
} | |
} | |
class SpritesLibrary | |
{ | |
public var next:SpritesLibrary = null; | |
var _xml:Xml; | |
var _pack:AssetPack; | |
var _items:Map<String, TexturePackerSymbol>; | |
/** | |
* Creates a library using single texturepacker xml from an AssetPack. | |
* @param baseFile The filename in the pack of the TexturePackers's xml. | |
*/ | |
public function new (pack :AssetPack, baseFile :String) | |
{ | |
_pack = pack; | |
var file = pack.getFile(baseFile); | |
_xml = Xml.parse(file.toString()); | |
_items = new Map(); | |
var imagePath = _xml.firstElement().get("imagePath"); | |
var texture = _pack.getTexture(imagePath.removeFileExtension()); | |
for (node in _xml.firstElement().elements()) { | |
var name:String = node.get("n"); | |
var item = new TexturePackerSymbol(name, texture, Std.parseInt(node.get("x")), Std.parseInt(node.get("y")), Std.parseInt(node.get("w")), Std.parseInt(node.get("h")), node.exists("r") ? node.get("r") == "y" : false); | |
if (node.exists("oX")) { | |
item.setOffset(Std.parseInt(node.get("oX")), Std.parseInt(node.get("oY")), Std.parseInt(node.get("oW")), Std.parseInt(node.get("oH"))); | |
} | |
// trace("found symbol: " + name); | |
_items.set(name, item); | |
} | |
} | |
public function createSprite (name :String, required :Bool = true) :TexturePackerSprite | |
{ | |
var item = _items.get(name); | |
if (item == null) | |
{ | |
if (required) { | |
throw "Missing sprite".withFields(["name", name]); | |
} | |
return null; | |
} | |
return new TexturePackerSprite(item); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package flambe.texturepacker; | |
import flambe.display.Graphics; | |
import flambe.display.ImageSprite; | |
/** | |
* An instanced fixed-size sprite that displays a subtexture | |
*/ | |
class TexturePackerSprite extends ImageSprite | |
{ | |
/** The data this sprite displays. */ | |
public var symbol (default, null) :TexturePackerSymbol; | |
public function new (symbol :TexturePackerSymbol) | |
{ | |
super(texture); | |
this.symbol = symbol; | |
texture = symbol.getSubTexture(); | |
} | |
override public function draw (g :Graphics) | |
{ | |
if (symbol.rotated) { | |
g.save(); | |
g.translate(0, symbol.width); | |
g.rotate(-90); | |
g.drawTexture(texture, symbol.offsetY, symbol.offsetX); | |
g.restore(); | |
} | |
else { | |
g.drawTexture(texture, symbol.offsetX, symbol.offsetY); | |
} | |
} | |
override public function getNaturalHeight () :Float | |
{ | |
return symbol.rotated ? super.getNaturalWidth() + symbol.offsetWidth : super.getNaturalHeight() + symbol.offsetHeight; | |
} | |
override public function getNaturalWidth () :Float | |
{ | |
return symbol.rotated ? super.getNaturalHeight() + symbol.offsetHeight : super.getNaturalWidth() + symbol.offsetWidth; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package flambe.texturepacker; | |
import flambe.display.SubTexture; | |
import flambe.display.Texture; | |
/** | |
* Defines a TexturePacker texture. | |
*/ | |
class TexturePackerSymbol | |
{ | |
public var rotated:Bool; | |
public var height:Int; | |
public var width:Int; | |
public var x:Int; | |
public var y:Int; | |
public var name:String; | |
public var texture:Texture; | |
public var offsetX:Int = 0; | |
public var offsetY:Int = 0; | |
public var offsetWidth:Int = 0; | |
public var offsetHeight:Int = 0; | |
public function new (name :String, texture :Texture, x :Int, y :Int, width :Int, height :Int, rotated :Bool) | |
{ | |
this.width = width; | |
this.height = height; | |
this.rotated = rotated; | |
this.x = x; | |
this.y = y; | |
this.name = name; | |
this.texture = texture; | |
} | |
@:allow(flambe) function setOffset (x :Int, y :Int, width :Int, height :Int) | |
{ | |
offsetX = x; | |
offsetY = y; | |
offsetWidth = width; | |
offsetHeight = height; | |
} | |
@:allow(flambe) function getSubTexture () :SubTexture | |
{ | |
return texture.subTexture(x, y, width, height); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment