[starling] Closes #651, StarlingAtlasAttachmentLoader can now work with texture(s) as well!

This commit is contained in:
badlogic 2016-11-07 14:47:32 +01:00
parent 5d22bef460
commit 53072a5a3e
2 changed files with 36 additions and 22 deletions

View File

@ -40,41 +40,55 @@ import flash.display.Bitmap;
import flash.display.BitmapData; import flash.display.BitmapData;
public class StarlingTextureLoader implements TextureLoader { public class StarlingTextureLoader implements TextureLoader {
public var bitmapDatas:Object = {}; public var bitmapDatasOrTextures:Object = {};
public var singleBitmapData:BitmapData; public var singleBitmapDataOrTexture:Object;
/** @param bitmaps A Bitmap or BitmapData for an atlas that has only one page, or for a multi page atlas an object where the /** @param bitmaps A Bitmap or BitmapData or Texture for an atlas that has only one page, or for a multi page atlas an object where the
* key is the image path and the value is the Bitmap or BitmapData. */ * key is the image path and the value is the Bitmap or BitmapData or Texture. */
public function StarlingTextureLoader (bitmaps:Object) { public function StarlingTextureLoader (bitmapsOrTextures:Object) {
if (bitmaps is BitmapData) { if (bitmapsOrTextures is BitmapData) {
singleBitmapData = BitmapData(bitmaps); singleBitmapDataOrTexture = BitmapData(bitmapsOrTextures);
return; return;
} }
if (bitmaps is Bitmap) { if (bitmapsOrTextures is Bitmap) {
singleBitmapData = Bitmap(bitmaps).bitmapData; singleBitmapDataOrTexture = Bitmap(bitmapsOrTextures).bitmapData;
return; return;
} }
if (bitmapsOrTextures is Texture) {
singleBitmapDataOrTexture = Texture(bitmapsOrTextures);
return;
}
for (var path:* in bitmaps) { for (var path:* in bitmapsOrTextures) {
var object:* = bitmaps[path]; var object:* = bitmapsOrTextures[path];
var bitmapData:BitmapData; var bitmapDataOrTexture:Object;
if (object is BitmapData) if (object is BitmapData)
bitmapData = BitmapData(object); bitmapDataOrTexture = BitmapData(object);
else if (object is Bitmap) else if (object is Bitmap)
bitmapData = Bitmap(object).bitmapData; bitmapDataOrTexture = Bitmap(object).bitmapData;
else if (object is Texture)
bitmapDataOrTexture = Texture(object);
else else
throw new ArgumentError("Object for path \"" + path + "\" must be a Bitmap or BitmapData: " + object); throw new ArgumentError("Object for path \"" + path + "\" must be a Bitmap, BitmapData or Texture: " + object);
bitmapDatas[path] = bitmapData; bitmapDatasOrTextures[path] = bitmapDataOrTexture;
} }
} }
public function loadPage (page:AtlasPage, path:String) : void { public function loadPage (page:AtlasPage, path:String) : void {
var bitmapData:BitmapData = singleBitmapData || bitmapDatas[path]; var bitmapDataOrTexture:Object = singleBitmapDataOrTexture || bitmapDatasOrTextures[path];
if (!bitmapData) if (!bitmapDataOrTexture)
throw new ArgumentError("BitmapData not found with name: " + path); throw new ArgumentError("BitmapData/Texture not found with name: " + path);
page.rendererObject = Texture.fromBitmapData(bitmapData); if (bitmapDataOrTexture is BitmapData) {
page.width = bitmapData.width; var bitmapData:BitmapData = BitmapData(bitmapDataOrTexture);
page.height = bitmapData.height; page.rendererObject = Texture.fromBitmapData(bitmapData);
page.width = bitmapData.width;
page.height = bitmapData.height;
} else {
var texture:Texture = Texture(bitmapDataOrTexture);
page.rendererObject = texture;
page.width = texture.width;
page.height = texture.height;
}
} }
public function loadRegion (region:AtlasRegion) : void { public function loadRegion (region:AtlasRegion) : void {