package org.bobstuff.fishscreen.core;

import java.util.ArrayList;
import java.util.List;

import playn.core.Image;
import playn.core.ResourceCallback;

/**
 * @author bob
 *
 */
public class ProgressAssetWatcher implements ResourceCallback<Image> {
	
	private int loaded;
	private int errors;
	
	private boolean started;
	private final Listener listener;
	
	private List<Image> imagesToLoad;
	
	public ProgressAssetWatcher(Listener listener) {
		this.listener = listener;
		this.imagesToLoad = new ArrayList<Image>();
	}

	public void add(Image image) {
		imagesToLoad.add(image);
	}
	
	public boolean isDone() {
		return started && (loaded + errors == imagesToLoad.size());
	}

	public void start() {
		started = true;
		
		for (Image image : imagesToLoad) {
			image.addCallback(this);
		}
	}
	
	private void reportProgress() {
		if (listener != null) {
			int percentage = Math.round(((loaded + errors) / (float)imagesToLoad.size()) * 100);
			listener.progress(percentage);
		}
	}

	private void maybeDone() {
		reportProgress();
		if (isDone()) {
			if (listener != null) {
				listener.done();
			}
		}
	}
	
	@Override
	public void done(Image resource) {
		++loaded;
		maybeDone();
	}

	@Override
	public void error(Throwable e) {
		++errors;
		if (listener != null)
			listener.error(e);
		maybeDone();
	}
	
	public interface Listener {
		void done();
		
		void progress(final int percentageComplete);

		void error(Throwable e);
	}
	
}

@@ -109,16 +109,41 @@
 .Image;%0A
+import playn.core.Sound;%0A
 import p
@@ -171,16 +171,17 @@
 lback;%0A%0A
+%0A
 /**%0A * @
@@ -259,23 +259,16 @@
 Callback
-%3CImage%3E
  %7B%0A%09%0A%09pr
@@ -405,16 +405,51 @@
 ToLoad;%0A
+%09private List%3CSound%3E soundsToLoad;%0A
 %09%0A%09publi
@@ -562,24 +562,70 @@
 t%3CImage%3E();%0A
+%09%09this.soundsToLoad = new ArrayList%3CSound%3E();%0A
 %09%7D%0A%0A%09public 
@@ -673,24 +673,86 @@
 d(image);%0A%09%7D
+%0A%0A%09public void add(Sound sound)%7B%0A%09%09soundsToLoad.add(sound);%0A%09%7D
 %0A%09%0A%09public b
@@ -828,16 +828,38 @@
 d.size()
+ + soundsToLoad.size()
 );%0A%09%7D%0A%0A%09
@@ -966,24 +966,91 @@
 (this);%0A%09%09%7D%0A
+%09%09for(Sound sound : soundsToLoad)%7B%0A%09%09%09sound.addCallback(this);%0A%09%09%7D%0A
 %09%7D%0A%09%0A%09privat
@@ -1160,16 +1160,17 @@
  (float)
+(
 imagesTo
@@ -1180,16 +1180,39 @@
 d.size()
+ + soundsToLoad.size())
 ) * 100)
@@ -1412,21 +1412,22 @@
 id done(
-Image
+Object
  resourc

Update core/src/main/java/org/bobstuff/fishscreen/core/ProgressAssetWatcher.java