Last active
December 21, 2015 12:39
-
-
Save rayyee/6307353 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.rayyee.reflect | |
{ | |
import flash.display.DisplayObject; | |
import flash.display.DisplayObjectContainer; | |
import flash.display.MovieClip; | |
import flash.utils.describeType; | |
import flash.utils.getDefinitionByName; | |
import flash.utils.getQualifiedClassName; | |
/** | |
* 映射美术资源实例到程序对象 | |
* @usage [Map] public var close_btn:MovieClip; | |
* @usage [Map(skin="flash.display::MovieClip")] public var close_btn:MovieButton; | |
* @author rayyee | |
*/ | |
public class ArtSymbolMapper | |
{ | |
public function ArtSymbolMapper() | |
{ | |
//static class | |
} | |
public static function createToMap(container:DisplayObjectContainer, artCls:Class):void | |
{ | |
var _assets:MovieClip = new artCls as MovieClip; | |
map(_assets, container); | |
container.addChild(_assets); | |
} | |
public static function map(art:DisplayObjectContainer, mapped:Object):void | |
{ | |
var mappedDescribe:XML = describeType(mapped); | |
var child:DisplayObject, childClassName:String; | |
for each(var node:XML in mappedDescribe..variable.metadata.(@name == "Map")) | |
{ | |
child = searchChild(art, node.parent().@name); | |
childClassName = getQualifiedClassName(child); | |
if (childClassName == node.parent().@type) | |
mapped[node.parent().@name] = child; | |
else if (node.arg.(@key == "skin").@value == childClassName) | |
mapped[node.parent().@name] = new (getDefinitionByName(node.parent().@type) as Class)(child); | |
//else | |
//trace("ArtSymbolMapper::no map object ", childClassName, node, node.parent().@name); | |
} | |
} | |
private static function searchChild(p:DisplayObjectContainer, name:String):DisplayObject | |
{ | |
var i:int = 0, len:int = p.numChildren, child:DisplayObject; | |
for ( ; i < len; i += 1) | |
{ | |
child = p.getChildAt(i); | |
if (child.name == name) | |
{ | |
return child; | |
} | |
if (child is DisplayObjectContainer) | |
{ | |
child = searchChild(child as DisplayObjectContainer, name); | |
if (child) return child; | |
} | |
} | |
return null; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment