Skip to content

Commit

Permalink
added new 'TextureOptions' class, used by 'Texture.fromData' and 'Ass…
Browse files Browse the repository at this point in the history
…etManager.enqueueWithName'

In the Texture class, this makes the huge number of parameters to the "from..."-methods easier to handle; in the AssetManager, this allows to supply custom options for each added texture.
  • Loading branch information
PrimaryFeather committed Jan 14, 2014
1 parent 518cb8a commit 9370a00
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 17 deletions.
39 changes: 39 additions & 0 deletions starling/src/starling/textures/Texture.as
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ package starling.textures
import flash.geom.Rectangle;
import flash.system.Capabilities;
import flash.utils.ByteArray;
import flash.utils.clearTimeout;
import flash.utils.getQualifiedClassName;
import flash.utils.setTimeout;

import starling.core.Starling;
import starling.errors.AbstractClassError;
Expand Down Expand Up @@ -121,6 +123,43 @@ package starling.textures
// override in subclasses
}

/** Creates a texture object from any of the supported data types, using the specified
* options.
*
* @param data: Either an embedded asset class, a Bitmap, BitmapData, or a ByteArray
* with ATF data.
* @param options: Specifies options about the texture settings, e.g. scale factor.
*/
public static function fromData(data:Object, options:TextureOptions=null):Texture
{
var texture:Texture = null;

if (data is Bitmap) data = (data as Bitmap).bitmapData;
if (options == null) options = new TextureOptions();

if (data is Class)
{
texture = fromEmbeddedAsset(data as Class,
options.mipMapping, options.optimizeForRenderToTexture, options.scale,
options.format, options.repeat);
}
else if (data is BitmapData)
{
texture = fromBitmapData(data as BitmapData,
options.mipMapping, options.optimizeForRenderToTexture, options.scale,
options.format, options.repeat);
}
else if (data is ByteArray)
{
texture = fromAtfData(data as ByteArray,
options.scale, options.mipMapping, options.onReady, options.repeat);
}
else
throw new ArgumentError("Unsupported 'data' type: " + getQualifiedClassName(data));

return texture;
}

/** Creates a texture object from an embedded asset class. Textures created with this
* method will be restored directly from the asset class in case of a context loss,
* which guarantees a very economic memory usage.
Expand Down
71 changes: 71 additions & 0 deletions starling/src/starling/textures/TextureOptions.as
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// =================================================================================================
//
// Starling Framework
// Copyright 2013 Gamua OG. All Rights Reserved.
//
// This program is free software. You can redistribute and/or modify it
// in accordance with the terms of the accompanying license agreement.
//
// =================================================================================================

package starling.textures
{
import starling.core.Starling;

/** The TextureOptions class specifies options for loading textures with the 'Texture.fromData'
* method. */
public class TextureOptions
{
private var mScale:Number;
private var mFormat:String;
private var mMipMapping:Boolean;
private var mOptimizeForRenderToTexture:Boolean = false;
private var mOnReady:Function = null;
private var mRepeat:Boolean = false;

public function TextureOptions(scale:Number=1.0, mipMapping:Boolean=false,
format:String="bgra")
{
mScale = scale;
mFormat = format;
mMipMapping = mipMapping;
}

/** The scale factor, which influences width and height properties. If you pass '-1',
* the current global content scale factor will be used. */
public function get scale():Number { return mScale; }
public function set scale(value:Number):void
{
mScale = value > 0 ? value : Starling.contentScaleFactor;
}

/** The <code>Context3DTextureFormat</code> of the underlying texture data. */
public function get format():String { return mFormat; }
public function set format(value:String):void { mFormat = value; }

/** Indicates if the texture contains mip maps. */
public function get mipMapping():Boolean { return mMipMapping; }
public function set mipMapping(value:Boolean):void { mMipMapping = value; }

/** Indicates if the texture will be used as render target. */
public function get optimizeForRenderToTexture():Boolean { return mOptimizeForRenderToTexture; }
public function set optimizeForRenderToTexture(value:Boolean):void { mOptimizeForRenderToTexture = value; }

/** Indicates if the texture should repeat like a wallpaper or stretch the outermost pixels.
* Note: this only works in textures with sidelengths that are powers of two and
* that are not loaded from a texture atlas (i.e. no subtextures). @default false */
public function get repeat():Boolean { return mRepeat; }
public function set repeat(value:Boolean):void { mRepeat = value; }

/** A callback that is used only for ATF textures; if it is set, the ATF data will be
* decoded asynchronously. The texture can only be used when the callback has been
* executed. This property is ignored for all other texture types (they are ready
* immediately when the 'Texture.from...' method returns, anyway).
*
* <p>This is the expected function definition:
* <code>function(texture:Texture):void;</code></p>
*/
public function get onReady():Function { return mOnReady; }
public function set onReady(value:Function):void { mOnReady = value; }
}
}
46 changes: 29 additions & 17 deletions starling/src/starling/utils/AssetManager.as
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ package starling.utils
import starling.textures.AtfData;
import starling.textures.Texture;
import starling.textures.TextureAtlas;
import starling.textures.TextureOptions;

/** Dispatched when all textures have been restored after a context loss. */
[Event(name="texturesRestored", type="starling.events.Event")]
Expand Down Expand Up @@ -64,8 +65,7 @@ package starling.utils
*/
public class AssetManager extends EventDispatcher
{
private var mScaleFactor:Number;
private var mUseMipMaps:Boolean;
private var mDefaultTextureOptions:TextureOptions;
private var mCheckPolicyFile:Boolean;
private var mVerbose:Boolean;
private var mNumLostTextures:int;
Expand All @@ -90,9 +90,8 @@ package starling.utils
* how enqueued bitmaps will be converted to textures. */
public function AssetManager(scaleFactor:Number=1, useMipmaps:Boolean=false)
{
mDefaultTextureOptions = new TextureOptions(scaleFactor, useMipmaps);
mVerbose = mCheckPolicyFile = mIsLoading = false;
mScaleFactor = scaleFactor > 0 ? scaleFactor : Starling.contentScaleFactor;
mUseMipMaps = useMipmaps;
mQueue = [];
mTextures = new Dictionary();
mAtlases = new Dictionary();
Expand Down Expand Up @@ -459,20 +458,30 @@ package starling.utils
}
}

/** Enqueues a single asset with a custom name that can be used to access it later.
* If you don't pass a name, it's attempted to generate it automatically.
* @returns the name under which the asset was registered. */
public function enqueueWithName(asset:Object, name:String=null):String
/** Enqueues a single asset with a custom name that can be used to access it later.
* If the asset is a texture, you can also add custom texture options.
*
* @param asset: The asset that will be enqueued; accepts the same objects as the
* 'enqueue' method.
* @param name: The name under which the asset will be found later. If you pass null or
* omit the parameter, it's attempted to generate a name automatically.
* @param options: Custom options that will be used if 'asset' points to texture data.
* @returns: the name under which the asset was registered. */
public function enqueueWithName(asset:Object, name:String=null,
options:TextureOptions=null):String
{
if (getQualifiedClassName(asset) == "flash.filesystem::File")
asset = asset["url"];

if (name == null) name = getName(asset);
if (options == null) options = mDefaultTextureOptions;

log("Enqueuing '" + name + "'");

mQueue.push({
name: name,
asset: asset
asset: asset,
options: options
});

return name;
Expand Down Expand Up @@ -525,7 +534,8 @@ package starling.utils
{
var assetInfo:Object = mQueue.pop();
clearTimeout(mTimeoutID);
processRawAsset(assetInfo.name, assetInfo.asset, xmls, progress, resume);
processRawAsset(assetInfo.name, assetInfo.asset, assetInfo.options,
xmls, progress, resume);
}

function processXmls():void
Expand Down Expand Up @@ -582,7 +592,8 @@ package starling.utils
}
}

private function processRawAsset(name:String, rawAsset:Object, xmls:Vector.<XML>,
private function processRawAsset(name:String, rawAsset:Object, options:TextureOptions,
xmls:Vector.<XML>,
onProgress:Function, onComplete:Function):void
{
var canceled:Boolean = false;
Expand Down Expand Up @@ -610,7 +621,7 @@ package starling.utils
}
else if (asset is Bitmap)
{
texture = Texture.fromBitmap(asset as Bitmap, mUseMipMaps, false, mScaleFactor);
texture = Texture.fromData(asset, options);
texture.root.onRestore = function():void
{
mNumLostTextures++;
Expand All @@ -637,7 +648,8 @@ package starling.utils

if (AtfData.isAtfData(bytes))
{
texture = Texture.fromAtfData(bytes, mScaleFactor, mUseMipMaps, onComplete);
options.onReady = onComplete;
texture = Texture.fromData(bytes, options);
texture.root.onRestore = function():void
{
mNumLostTextures++;
Expand Down Expand Up @@ -890,13 +902,13 @@ package starling.utils
/** For bitmap textures, this flag indicates if mip maps should be generated when they
* are loaded; for ATF textures, it indicates if mip maps are valid and should be
* used. */
public function get useMipMaps():Boolean { return mUseMipMaps; }
public function set useMipMaps(value:Boolean):void { mUseMipMaps = value; }
public function get useMipMaps():Boolean { return mDefaultTextureOptions.mipMapping; }
public function set useMipMaps(value:Boolean):void { mDefaultTextureOptions.mipMapping = value; }

/** Textures that are created from Bitmaps or ATF files will have the scale factor
* assigned here. */
public function get scaleFactor():Number { return mScaleFactor; }
public function set scaleFactor(value:Number):void { mScaleFactor = value; }
public function get scaleFactor():Number { return mDefaultTextureOptions.scale; }
public function set scaleFactor(value:Number):void { mDefaultTextureOptions.scale = value; }

/** Specifies whether a check should be made for the existence of a URL policy file before
* loading an object from a remote server. More information about this topic can be found
Expand Down

0 comments on commit 9370a00

Please sign in to comment.