-
-
Save xLite/a05b932023862abe3091 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 com.sivicity.assets | |
{ | |
import com.greensock.loading.display.ContentDisplay; | |
import com.greensock.loading.LoaderMax; | |
import flash.display.Bitmap; | |
import flash.geom.Rectangle; | |
import flash.utils.Dictionary; | |
import org.villekoskela.utils.RectanglePacker; | |
import starling.core.Starling; | |
import starling.display.Image; | |
import starling.events.Event; | |
import starling.textures.RenderTexture; | |
import starling.textures.Texture; | |
import starling.textures.TextureAtlas; | |
public class Assets | |
{ | |
private static var _initialised:Boolean = false; | |
private static var _mouseMap:Bitmap; | |
private static var _renderTextureAtlases:Dictionary; | |
private static var _textureAtlases:Dictionary; | |
/* helper objects */ | |
private static var _blankTexture:Texture; | |
public static function addTextureAtlas(name:String, texture:Texture, atlasXML:XML):void | |
{ | |
if (!_initialised) throw new Error("Assets class not initialised."); | |
_textureAtlases[name] = new TextureAtlas(texture, atlasXML); | |
} | |
private static function _buildTextureAtlases():void | |
{ | |
Assets.addTextureAtlas("atlas1", Texture.fromBitmap(ContentDisplay(LoaderMax.getContent("atlas1PNG")).rawContent as Bitmap), LoaderMax.getContent("atlas1XML") as XML); | |
Assets.addTextureAtlas("human", Texture.fromBitmap(ContentDisplay(LoaderMax.getContent("humanPNG")).rawContent as Bitmap), LoaderMax.getContent("humanXML") as XML); | |
_generateTextureAtlases(); | |
} | |
public static function generateTextureAtlas(folder:String):void | |
{ | |
if (!_initialised) throw new Error("Assets class not initialised."); | |
if (_renderTextureAtlases[folder] != null) | |
{ | |
(_renderTextureAtlases[folder] as TextureAtlas).dispose(); | |
} | |
var i:int; | |
var image:Image = new Image(_blankTexture); | |
var itemName:String; | |
var itemNames:Vector.<String> = Assets.getNames(folder + "/"); | |
var itemsTexture:RenderTexture; | |
var itemTexture:Texture; | |
var itemTextures:Vector.<Texture> = Assets.getTextures(folder + "/"); | |
var noOfRectangles:int; | |
var rect:Rectangle; | |
var rectanglePacker:RectanglePacker = new RectanglePacker(); | |
var texture:Texture; | |
noOfRectangles = itemTextures.length; | |
if (noOfRectangles == 0) | |
{ | |
return; | |
} | |
for (i = 0; i < noOfRectangles; i++) | |
{ | |
rectanglePacker.insertRectangle(Math.round(itemTextures[i].width), Math.round(itemTextures[i].height), i); | |
} | |
rectanglePacker.packRectangles(); | |
if (rectanglePacker.rectangleCount != noOfRectangles) | |
{ | |
throw new Error("Only " + rectanglePacker.rectangleCount + " out of " + noOfRectangles + " rectangles packed for folder: " + folder); | |
} | |
itemsTexture = new RenderTexture(rectanglePacker.width, rectanglePacker.height); | |
itemsTexture.drawBundled(function():void | |
{ | |
for (i = 0; i < noOfRectangles; i++) | |
{ | |
itemTexture = itemTextures[rectanglePacker.getRectangleId(i)]; | |
rect = rectanglePacker.getRectangle(i, rect); | |
image.texture = itemTexture; | |
image.readjustSize(); | |
image.x = rect.x + itemTexture.frame.x; | |
image.y = rect.y + itemTexture.frame.y; | |
itemsTexture.draw(image); | |
} | |
}); | |
_renderTextureAtlases[folder] = new TextureAtlas(itemsTexture); | |
for (i = 0; i < noOfRectangles; i++) | |
{ | |
itemName = itemNames[rectanglePacker.getRectangleId(i)]; | |
itemTexture = itemTextures[rectanglePacker.getRectangleId(i)]; | |
rect = rectanglePacker.getRectangle(i); | |
(_renderTextureAtlases[folder] as TextureAtlas).addRegion(itemName, rect, itemTexture.frame); | |
} | |
} | |
private static function _generateTextureAtlases():void | |
{ | |
Assets.generateTextureAtlas("gridbg"); | |
Assets.generateTextureAtlas("items"); | |
Assets.generateTextureAtlas("roombg"); | |
} | |
public static function getNames(prefix:String, result:Vector.<String> = null):Vector.<String> | |
{ | |
if (!_initialised) throw new Error("Assets class not initialised."); | |
result = (result != null) ? result : new Vector.<String>; | |
for (var key:String in _textureAtlases) | |
{ | |
result = (_textureAtlases[key] as TextureAtlas).getNames(prefix, result); | |
} | |
return result; | |
} | |
public static function getTexture(name:String, folder:String = null):Texture | |
{ | |
if (!_initialised) throw new Error("Assets class not initialised."); | |
if (folder != null) | |
{ | |
return (_renderTextureAtlases[folder] != null) ? (_renderTextureAtlases[folder] as TextureAtlas).getTexture(folder + "/" + name) : null; | |
} | |
var texture:Texture; | |
for (var key:String in _textureAtlases) | |
{ | |
if ((texture = (_textureAtlases[key] as TextureAtlas).getTexture(name)) != null) | |
{ | |
return texture; | |
} | |
} | |
return null; | |
} | |
public static function getTextures(prefix:String, result:Vector.<Texture> = null):Vector.<Texture> | |
{ | |
if (!_initialised) throw new Error("Assets class not initialised."); | |
result = (result != null) ? result : new Vector.<Texture>; | |
for each (var name:String in Assets.getNames(prefix)) | |
{ | |
result.push(Assets.getTexture(name)); | |
} | |
return result; | |
} | |
public static function init():void | |
{ | |
_initialised = true; | |
_blankTexture = Texture.empty(1, 1); | |
_mouseMap = ContentDisplay(LoaderMax.getContent("mouseMap")).rawContent as Bitmap; | |
_renderTextureAtlases = new Dictionary(); | |
_textureAtlases = new Dictionary(); | |
_buildTextureAtlases(); | |
Starling.current.stage3D.addEventListener(Event.CONTEXT3D_CREATE, _onContext3DCreateHandler); | |
} | |
public static function get mouseMap():Bitmap | |
{ | |
if (!_initialised) throw new Error("Assets class not initialised."); | |
return _mouseMap; | |
} | |
public static function removeTextureAtlas(name:String):void | |
{ | |
if (!_initialised) throw new Error("Assets class not initialised."); | |
if (_textureAtlases[name] != null) | |
{ | |
(_textureAtlases[name] as TextureAtlas).dispose(); | |
delete _textureAtlases[name]; | |
} | |
} | |
private static function _onContext3DCreateHandler(e:Object):void | |
{ | |
_generateTextureAtlases(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment