I use ImageLoader to display image of a file(called it state).
I need to watch(monitor) this state file and when it is CHANGED I need to update the source of the ImageLoader.
While the state file is not available, the ImageLoader is source is a placeholder image.
For the file monitoring I use this lib:
http://www.mikechambers.com/blog/2009/03/11/monitoring-file-changes-in-adobe-air/
Works perfect, when the state file is created, the source is updated on the ImageLoader, but when the file is updated, the the ImageLoader is not updated.
here is the relevant source
package model.scene.state {
import com.adobe.air.filesystem.FileMonitor;
import com.adobe.air.filesystem.events.FileMonitorEvent;
import controller.base.BaseController;
import core.base.CoreBaseEventDispatcher;
import core.notification.CoreNotification;
import flash.filesystem.File;
import model.config.DynamicDungeons;
import model.scene.SceneModel;
[Bindable]
public class State extends CoreBaseEventDispatcher {
public static const NOTIFICATION_STATE_CREATED:String = 'state.notifications.state.created';
public static const NOTIFICATION_STATE_CHANGED:String = 'state.notifications.state.changed';
public var sceneModel:SceneModel;
private var monitor:FileMonitor;
public function State(sceneModel:SceneModel) {
super();
this.sceneModel = sceneModel;
this.monitor = new FileMonitor(this.resource);
this.monitor.addEventListener(FileMonitorEvent.CHANGE, this.onFileChange);
this.monitor.addEventListener(FileMonitorEvent.CREATE, this.onFileCreate);
this.monitor.watch();
}
public function get resource():File {
var pngSceneState:File = this.sceneModel.bundleFolder.resolvePath(DynamicDungeons.SCENE_STATES_FOLDER_NAME).resolvePath(this.sceneModel.id + ".png");
return pngSceneState;
}
private function onFileChange(event:FileMonitorEvent):void {
CoreNotification.createNotification(NOTIFICATION_STATE_CHANGED+this.sceneModel.id)
.addParam(BaseController.DATA, {resource: this.resource, sceneModel: this.sceneModel})
.send();
}
private function onFileCreate(event:FileMonitorEvent):void {
log("=====FILE CREATED=====");
CoreNotification.createNotification(NOTIFICATION_STATE_CREATED+this.sceneModel.id)
.addParam(BaseController.DATA, {resource: this.resource, sceneModel: this.sceneModel})
.send();
}
}
}
<?xml version="1.0"?>
<feathersui:GuiLayoutGroupListItemRenderer xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:feathersui="http://gotocore/feathersui"
xmlns:layout="library://ns.feathersui.com/mxml"
styleName="{Theme.STORY_TILE_LIST_ITEM_RENDERER}"
addedToStage="addedToStageHandler(event)"
removedFromStage="removedFromStageHandler(event)"
touchGroup="true" enterFrame="enterFrameHandler(event)"
>
<fx:Script><![CDATA[
import core.notification.CoreListener;
import core.notification.CoreNotification;
import feathers.controls.Alert;
import feathers.core.PopUpManager;
import model.config.DynamicDungeons;
import model.scene.SceneModel;
import model.scene.state.State;
import model.theme.Theme;
import starling.events.EnterFrameEvent;
import starling.events.Event;
import starling.rendering.Painter;
import starling.utils.ScaleMode;
[Bindable]
protected var sceneModel:SceneModel;
[Bindable]
private var source:Object;
protected function removedFromStageHandler(event:starling.events.Event):void {
CoreListener.unregister(State.NOTIFICATION_STATE_CREATED + this.sceneModel.id, this.stateCreatedListener);
CoreListener.unregister(State.NOTIFICATION_STATE_CHANGED + this.sceneModel.id, this.stateChangedListener);
}
protected function addedToStageHandler(event:starling.events.Event):void {
}
override public function set data(value:Object):void {
super.data = value;
if (!value)
return;
this.sceneModel = this.data as SceneModel;
this.sceneModel.addEventListener(starling.events.Event.CHANGE, sceneModel_changeHandler);
this.source = this.sceneModel.state.resource.exists ? this.sceneModel.state.resource.url : this.sceneModel.background.thumbnail;
CoreListener.unregister(State.NOTIFICATION_STATE_CREATED + this.sceneModel.id, this.stateCreatedListener);
CoreListener.unregister(State.NOTIFICATION_STATE_CHANGED + this.sceneModel.id, this.stateChangedListener);
CoreListener.register(State.NOTIFICATION_STATE_CREATED + this.sceneModel.id, this.stateCreatedListener);
CoreListener.register(State.NOTIFICATION_STATE_CHANGED + this.sceneModel.id, this.stateChangedListener);
}
protected function sceneModel_changeHandler(event:Event):void {
}
private function enterFrameHandler(event:EnterFrameEvent):void {
this.setRequiresRedraw();
}
private function stateCreatedListener(params:Array):void {
log("=====STATE "+this.sceneModel.id+" CREATED=====");
this.source = this.sceneModel.state.resource.url;
}
private function stateChangedListener(params:Array):void {
log("=====STATE "+this.sceneModel.id+" CHANGED=====");
this.source = this.sceneModel.state.resource.url;
}
]]></fx:Script>
<feathersui:layout>
<layout:AnchorLayout/>
</feathersui:layout>
<feathersui:GuiImageLoader id="thumbnail" source="{this.source}" scaleMode="noBorder" errorTexture="{null}" textureCache="{null}"
>
<feathersui:layoutData>
<layout:AnchorLayoutData percentHeight="100" percentWidth="100"/>
</feathersui:layoutData>
</feathersui:GuiImageLoader>
</feathersui:GuiLayoutGroupListItemRenderer>