Export to GitHub

splinkresource - ActionscriptAPI.wiki


First we need an IResourceProcessor implementation to process the configuration. As the configuration exists in the form of an external xml file we use the ExternalResourceProcessor and pass the path to our configuration xml file as first argument. The IResourceProcessor loads and evaluates the given xml file and then it loads all resources for the given locale. If no locale has been supplied splinkresource chooses the configured defaultLocale. As the IResourceProcessor interface extends IQable it can be put into any queue.

var processor : IResourceProcessor = new ExternalResourceProcessor("resourcebundles.xml"); processor.register(QEvent.ERROR, onError); processor.register(QEvent.COMPLETE, onComplete); processor.register(ResourceProcessorEvent.PROGRESS, onProgress); processor.start();

When the IResourceProcessor completes processing it distributes a QEvent.COMPLETE. At the time this event arrives all resources are loaded and ready to use.

The ResourceProvider serves as central repository from where you can access all resources which belong to the current locale:

``` private function onComplete(e:QEvent) : void { var provider : IResourceProvider = ResourceProvider.instance();

trace("resources for: " + provider.currentLocale); 

var asset : DisplayObject = provider.getAssetById("sample"));

var font : FontData = provider.getFontDataByType("normal");

var t : TextField = new TextField();
t.autoSize = TextFieldAutoSize.LEFT;
var tf : TextFormat = new TextFormat();
tf.font = data.id;
tf.size = 20 + font.sizeOffset;
tf.x = font.xOffset;
tf.y = font.xOffset;
t.htmlText = "Hello <i>world</i> <b>!</b>";
t.embedFonts = !font.isSystemFont;
t.setTextFormat(tf);

} ```

If an error happens, for instance because a file can't be found, a QEvent.ERROR event is distributed. If minor errors occur (like a missing file), the processor skips the faulty resources and continues processing.

Throughout the processing the IResourceProcessor distributes a special ResourceProcessorEvent event which contains detailled information on the resource loading progress: ``` private function onProgress(e : ResourceProcessorEvent) : void { var progress : ResourceProgress = e.resourceProgress;

trace("current resource: " + progress.id + "(" + progress.num + " of " + progress.totalItems + ")");
trace("current resource progress: " + progress.itemPercent + "%"); 
trace("current resource bytes: " + progress.itemLoadedBytes + " of " + progress.itemTotalBytes); 
trace("overall bytes:" + progress.totalLoadedBytes + " of " + progress.totalBytes); 
trace("overall progress:" + progress.totalPercent + "%"); 

```

You might wonder how the information on the overall progress is obtained. If you remember the configuration xml contains nodes which define the resources to load for a resourcebundle. These nodes have a filesize attribute which specifies the size of the resource in bytes. Because of that the ResourceProcessor knows the total amount of bytes to load upfront and it can calculate the overall processing progress and all the other details provided via ResourceProcessorEvent. Now you might say that it's quite cumbersome to keep the filesize attribute values in the configuration xml updated. This is where the SizeInjectorTask ant task comes to the rescue.

To switch the locale you can look up what locales are available and start ResourceProcessor with the locale to switch to. var to : String = "en_EN"; var locales : Array = ResourceProvider.instance().availableLocales; if(locales.indexOf(to) != -1) { var processor : IResourceProcessor = new ExternalResourceProcessor("resourcebundles.xml", to); ... when ResourceProcessor eventually distributes the QEvent.COMPLETE event, the assets and fonts for the new locale are ready to be used.