Skip to content

Commit

Permalink
added compatibility with new Animate CC 'optimized' export (refs #4)
Browse files Browse the repository at this point in the history
  • Loading branch information
PrimaryFeather committed May 13, 2019
1 parent 5a2e7ab commit f66354d
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 31 deletions.
2 changes: 1 addition & 1 deletion src/starling/extensions/animate/AnimAssetManager.as
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ class AnimationAtlasFactory extends JsonFactory
assets.addAsset(baseName, new JsonTextureAtlas(texture, json));
}, 100);
}
else if (json.ANIMATION && json.SYMBOL_DICTIONARY)
else if ((json.ANIMATION && json.SYMBOL_DICTIONARY) || (json.AN && json.SD))
{
helper.addPostProcessor(function(assets:AssetManager):void
{
Expand Down
107 changes: 91 additions & 16 deletions src/starling/extensions/animate/AnimationAtlas.as
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ package starling.extensions.animate

public function AnimationAtlas(data:Object, atlas:TextureAtlas)
{
data = normalizeJsonKeys(data);
parseData(data);

_atlas = atlas;
Expand Down Expand Up @@ -130,25 +131,25 @@ package starling.extensions.animate
_symbolData = new Dictionary();

// the actual symbol dictionary
for each (var symbolData:Object in data.SYMBOL_DICTIONARY.Symbols)
_symbolData[symbolData.SYMBOL_name] = preprocessSymbolData(symbolData);
for each (var symbolData:Object in data.symbolDictionary.symbols)
_symbolData[symbolData.symbolName] = preprocessSymbolData(symbolData);

// the main animation
var defaultSymbolData:Object = preprocessSymbolData(data.ANIMATION);
_defaultSymbolName = defaultSymbolData.SYMBOL_name;
var defaultSymbolData:Object = preprocessSymbolData(data.animation);
_defaultSymbolName = defaultSymbolData.symbolName;
_symbolData[_defaultSymbolName] = defaultSymbolData;

// a purely internal symbol for bitmaps - simplifies their handling
_symbolData[Symbol.BITMAP_SYMBOL_NAME] = {
SYMBOL_name: Symbol.BITMAP_SYMBOL_NAME,
TIMELINE: { LAYERS: [] }
symbolName: Symbol.BITMAP_SYMBOL_NAME,
timeline: { layers: [] }
};
}

private static function preprocessSymbolData(symbolData:Object):Object
{
var timeLineData:Object = symbolData.TIMELINE;
var layerDates:Array = timeLineData.LAYERS;
var timeLineData:Object = symbolData.timeline;
var layerDates:Array = timeLineData.layers;

// In Animate CC, layers are sorted front to back.
// In Starling, it's the other way round - so we simply reverse the layer data.
Expand All @@ -167,7 +168,7 @@ package starling.extensions.animate
for (var l:int=0; l<numLayers; ++l)
{
var layerData:Object = layerDates[l];
var frames:Array = layerData.Frames as Array;
var frames:Array = layerData.frames as Array;
var numFrames:int = frames.length;

for (var f:int=0; f<numFrames; ++f)
Expand All @@ -179,24 +180,25 @@ package starling.extensions.animate
{
var element:Object = elements[e];

if ("ATLAS_SPRITE_instance" in element)
if ("atlasSpriteInstance" in element)
{
element = elements[e] = {
SYMBOL_Instance: {
SYMBOL_name: Symbol.BITMAP_SYMBOL_NAME,
Instance_Name: "InstName",
bitmap: element.ATLAS_SPRITE_instance,
symbolInstance: {
symbolName: Symbol.BITMAP_SYMBOL_NAME,
instanceName: "InstName",
bitmap: element.atlasSpriteInstance,
symbolType: SymbolType.GRAPHIC,
firstFrame: 0,
loop: LoopMode.LOOP,
transformationPoint: { x: 0, y: 0 },
Matrix3D: STD_MATRIX3D_DATA
matrix3D: STD_MATRIX3D_DATA
}
}
}

// not needed - remove decomposed matrix to save some memory
delete element.SYMBOL_Instance.DecomposedMatrix;
if ("decomposedMatrix" in element.symbolInstance)
delete element.symbolInstance.decomposedMatrix;
}
}
}
Expand All @@ -215,5 +217,78 @@ package starling.extensions.animate

public function get frameRate():Number { return _frameRate; }
public function set frameRate(value:Number):void { _frameRate = value; }

// data normalization

private static function normalizeJsonKeys(data:Object):Object
{
if (data is String || data is Number || data is int) return data;
else if (data is Array)
{
var array:Array = [];
var arrayLength:uint = data.length;
for (var i:int=0; i<arrayLength; ++i)
array[i] = normalizeJsonKeys(data[i]);
return array;
}
else
{
var out:Object = {};
for (var key:String in data)
{
var value:Object = normalizeJsonKeys(data[key]);
if (key in JsonKeys) key = JsonKeys[key];
out[key] = value;
}
return out;
}
}

private static const JsonKeys:Object =
{
// fix inconsistent names
ANIMATION: "animation",
ATLAS_SPRITE_instance: "atlasSpriteInstance",
DecomposedMatrix: "decomposedMatrix",
Frames: "frames",
Instance_Name: "instanceName",
Layer_name: "layerName",
LAYERS: "layers",
Matrix3D: "matrix3D",
Position: "position",
Rotation: "rotation",
Scaling: "scaling",
SYMBOL_DICTIONARY: "symbolDictionary",
SYMBOL_Instance: "symbolInstance",
SYMBOL_name: "symbolName",
Symbols: "symbols",
TIMELINE: "timeline",

// fix shortened names
AN: "animation",
ASI: "atlasSpriteInstance",
BM: "bitmap",
DU: "duration",
E: "elements",
FF: "firstFrame",
FR: "frames",
FRT: "framerate",
I: "index",
IN: "instanceName",
L: "layers",
LN: "layerName",
LP: "loop",
M3D: "matrix3D",
MD: "metadata",
N: "name",
POS: "position",
S: "symbols",
SD: "symbolDictionary",
SI: "symbolInstance",
SN: "symbolName",
ST: "symbolType",
TL: "timeline",
TRP: "transformationPoint"
};
}
}
14 changes: 14 additions & 0 deletions src/starling/extensions/animate/LoopMode.as
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,19 @@ package starling.extensions.animate
{
return value == LOOP || value == PLAY_ONCE || value == SINGLE_FRAME;
}

public static function parse(value:String):String
{
switch (value)
{
case "LP":
case LOOP: return LOOP;
case "PO":
case PLAY_ONCE: return PLAY_ONCE;
case "SF":
case SINGLE_FRAME: return SINGLE_FRAME;
default: throw new ArgumentError("Invalid loop mode: " + value);
}
}
}
}
28 changes: 14 additions & 14 deletions src/starling/extensions/animate/Symbol.as
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ package starling.extensions.animate
_data = data;
_atlas = atlas;
_composedFrame = -1;
_numLayers = data.TIMELINE.LAYERS.length;
_numLayers = data.timeline.layers.length;
_numFrames = getNumFrames();
_frameLabels = getFrameLabels();
_symbolName = data.SYMBOL_name;
_symbolName = data.symbolName;
_type = SymbolType.GRAPHIC;
_loopMode = LoopMode.LOOP;

Expand Down Expand Up @@ -96,10 +96,10 @@ package starling.extensions.animate

for (var i:int=0; i<numElements; ++i)
{
var elementData:Object = elements[i].SYMBOL_Instance;
var elementData:Object = elements[i].symbolInstance;
var oldSymbol:Symbol = layer.numChildren > i ? layer.getChildAt(i) as Symbol : null;
var newSymbol:Symbol = null;
var symbolName:String = elementData.SYMBOL_name;
var symbolName:String = elementData.symbolName;

if (!_atlas.hasSymbol(symbolName))
symbolName = BITMAP_SYMBOL_NAME;
Expand All @@ -118,7 +118,7 @@ package starling.extensions.animate
layer.addChildAt(newSymbol, i);
}

newSymbol.setTransformationMatrix(elementData.Matrix3D);
newSymbol.setTransformationMatrix(elementData.matrix3D);
newSymbol.setBitmap(elementData.bitmap);
newSymbol.setColor(elementData.color);
newSymbol.setLoop(elementData.loop);
Expand Down Expand Up @@ -157,7 +157,7 @@ package starling.extensions.animate
for (var i:int = 0; i<_numLayers; ++i)
{
var layer:Sprite = new Sprite();
layer.name = getLayerData(i).Layer_name;
layer.name = getLayerData(i).layerName;
_layers.addChild(layer);
}
}
Expand All @@ -179,8 +179,8 @@ package starling.extensions.animate
addChild(_bitmap);
}

_bitmap.x = data.Position.x;
_bitmap.y = data.Position.y;
_bitmap.x = data.position.x;
_bitmap.y = data.position.y;
}
else if (_bitmap)
{
Expand Down Expand Up @@ -211,13 +211,13 @@ package starling.extensions.animate

private function setLoop(data:String):void
{
if (data) _loopMode = data;
if (data) _loopMode = LoopMode.parse(data);
else _loopMode = LoopMode.LOOP;
}

private function setType(data:String):void
{
if (data) _type = data;
if (data) _type = SymbolType.parse(data);
}

private function getNumFrames():int
Expand All @@ -226,7 +226,7 @@ package starling.extensions.animate

for (var i:int=0; i<_numLayers; ++i)
{
var frameDates:Array = getLayerData(i).Frames as Array;
var frameDates:Array = getLayerData(i).frames as Array;
var numFrameDates:int = frameDates ? frameDates.length : 0;
var layerNumFrames:int = numFrameDates ? frameDates[0].index : 0;

Expand All @@ -246,7 +246,7 @@ package starling.extensions.animate

for (var i:int=0; i<_numLayers; ++i)
{
var frameDates:Array = getLayerData(i).Frames as Array;
var frameDates:Array = getLayerData(i).frames as Array;
var numFrameDates:int = frameDates ? frameDates.length : 0;

for (var j:int=0; j<numFrameDates; ++j)
Expand Down Expand Up @@ -343,12 +343,12 @@ package starling.extensions.animate

private function getLayerData(layerIndex:int):Object
{
return _data.TIMELINE.LAYERS[layerIndex];
return _data.timeline.layers[layerIndex];
}

private function getFrameData(layerIndex:int, frameIndex:int):Object
{
var frames:Array = getLayerData(layerIndex).Frames;
var frames:Array = getLayerData(layerIndex).frames;
var numFrames:int = frames.length;

for (var i:int=0; i<numFrames; ++i)
Expand Down
14 changes: 14 additions & 0 deletions src/starling/extensions/animate/SymbolType.as
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,19 @@ package starling.extensions.animate
{
return value == GRAPHIC || value == MOVIE_CLIP || value == BUTTON;
}

public static function parse(value:String):String
{
switch (value)
{
case "G":
case GRAPHIC: return GRAPHIC;
case "MC":
case MOVIE_CLIP: return MOVIE_CLIP;
case "B":
case BUTTON: return BUTTON;
default: throw new ArgumentError("Invalid symbol type: " + value);
}
}
}
}

0 comments on commit f66354d

Please sign in to comment.