Created
November 22, 2013 14:01
-
-
Save mikedotalmond/7600302 to your computer and use it in GitHub Desktop.
StarlingSpriteSheet / SubImageSprite for Flambe
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
class StarlingSpriteSheet { | |
public static function parse(atlasXml:Xml, texture:Texture):Map<String,SubImageSprite> { | |
var spritesMap = new Map<String, SubImageSprite>(); | |
for (el in atlasXml.elements()) { | |
if (el.nodeName == 'TextureAtlas') { | |
var textures = el.elementsNamed('SubTexture'); | |
for (sub in textures) { | |
var name = sub.get('name'); | |
var rect = new Rectangle( | |
Std.parseInt(sub.get('x')), | |
Std.parseInt(sub.get('y')), | |
Std.parseInt(sub.get('width')), | |
Std.parseInt(sub.get('height')) | |
); | |
// added pivot point, not a part of the TextureAtlas spec/reference... | |
var pivotX = Std.parseInt(sub.get('pivotX')); | |
var pivotY = Std.parseInt(sub.get('pivotY')); | |
spritesMap.set(name, new SubImageSprite(texture, rect, pivotX, pivotY)); | |
} | |
} | |
} | |
return spritesMap; | |
} | |
} | |
class SubImageSprite extends Sprite { | |
var sourceBounds:Rectangle; | |
public var texture :Texture; | |
/** | |
* | |
* @param texture | |
* @param bounds - position of a sub-texture within the texture | |
* @param pivotX | |
* @param pivotY | |
*/ | |
public function new (texture:Texture, bounds:Rectangle, pivotX:Float = 0, pivotY:Float = 0) { | |
super(); | |
this.texture = texture; | |
this.sourceBounds = bounds; | |
setAnchor(pivotX, pivotY); | |
} | |
override public function draw (g :Graphics) g.drawSubImage(texture, 0, 0, sourceBounds.x, sourceBounds.y, sourceBounds.width, sourceBounds.height); | |
override public function getNaturalWidth () return sourceBounds.width; | |
override public function getNaturalHeight () return sourceBounds.height; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment