XMLDisplayManager 004

February 7, 2011 by fMajakovskij

Since it is going to be an ongoing mini-project with a lot of updates, please refer to this link to see further update.

Here a quick update of the XMLDisplayManager with the addition of a couple of useful methods.
This update could be an excuse because there is also a first draft of a front-end, which could be named XMLDisplayViewer.
The following Flex application loads a static SWF file and parse it to generate an xml descriptor file for all of its DisplayObject, then you can browse and select one of them to see all the available properties:

This movie requires Flash Player 9

Now I would explain the added functions which could be useful even in other projects.

The following function simply check the datatype of a property and return the right datatype from a String value.
This is extremely useful when you get values from String data, like and xml, and want to update correctly accessors of your classes.
This is possible thanks to the power of flash.utils.describeType function which return a full descriptor XML file with all the properties and methods present in that class:

public  function validateDataType(data:*, value:Object):*
{
	var desc:XML = describeType(data)
	var type:String = desc.@name;
	switch(type)
	{
		case "Array":
			return value.split(",");
			break;

		case "String":
			return value.toString();
			break;

		case "int":
			return Number(value);
			break;

		case "Number":
			return Number(value);
			break;

		case "Boolean":
			return (value=="true")?true:false;
			break;
	}

	return value;
}

The following function return all the available, editable properties of a passed object:

public  function getAccessorsFromDisplayObject(_obj:DisplayObject):XMLList
{
	var def:XML = describeType(_obj);
	var acc:XMLList = def.accessor.(@access=="readwrite" && (@type == "Number" || @type == "int" || @type == "Boolean" || @type == "uint" || @type == "String"));
	return acc;
}

i.e.:

var sp:Sprite = new Sprite();
trace( getAccessorsFromDisplayObject(sp) );

// trace:
<accessor name="x" access="readwrite" type="Number" declaredBy="flash.display::DisplayObject"/>
<accessor name="y" access="readwrite" type="Number" declaredBy="flash.display::DisplayObject"/>
<accessor name="visible" access="readwrite" type="Boolean" declaredBy="flash.display::DisplayObject"/>
....

And the last one is a addon of the previous, which extends the ‘accessors’ XMLList give it the value for each property take from the passed object:

public static function fillAccessorsWithValues(_obj:DisplayObject):XMLList
{
	var accessors:XMLList = getAccessorsFromDisplayObject(_obj);
	for(var i:int=0; i<accessors.length(); ++i)
	{
		var access:XML = accessors[i];
		var prp:String = access.@name;
		if(_obj.hasOwnProperty(prp))
		{
			access.@value = _obj[prp]
		}
	}
	return accessors;
}

i.e.:

var sp:Sprite = new Sprite();
trace( getAccessorsFromDisplayObject(sp) );

// trace:
<accessor name="x" access="readwrite" type="Number" declaredBy="flash.display::DisplayObject" value="0"/>
<accessor name="y" access="readwrite" type="Number" declaredBy="flash.display::DisplayObject" value="0"/>
<accessor name="visible" access="readwrite" type="Boolean" declaredBy="flash.display::DisplayObject" value="true"/>
....

You can download the updated [download id="23"] class, v. 004:

That’s all for now, see in the next post

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>