/**
 * Copyright 2011 The PlayN Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package playn.tests.core;

import playn.core.CanvasImage;
import playn.core.Color;
import playn.core.GroupLayer;
import playn.core.Image;
import playn.core.ImageLayer;
import playn.core.ResourceCallback;
import playn.core.SurfaceLayer;
import static playn.core.PlayN.*;

public class ImageTypeTest extends Test {
  GroupLayer rootLayer;

  static float width = 100;
  static float height = 100;
  static int offset = 5;
  static String imageSrc = "images/imagetypetest.png";
  static String imageGroundTruthSrc = "images/imagetypetest_expected.png";

  Image image1;
  Image imageGroundTruth;
  ImageLayer imageLayer1;
  SurfaceLayer surfaceLayer1;
  ImageLayer canvasLayer1;
  ImageLayer imageLayer2;
  SurfaceLayer surfaceLayer2;
  ImageLayer canvasLayer2;
  ImageLayer groundTruthLayer;

  @Override
  public String getName() {
    return "ImageTypeTest";
  }

  @Override
  public String getDescription() {
    return "Test that image types display the same. Left-to-right: ImageLayer, SurfaceLayer, CanvasImage, ground truth (expected).";
  }

  @Override
  public void init() {
    rootLayer = graphics().rootLayer();
    // add a half white, half blue background
    SurfaceLayer bg = graphics().createSurfaceLayer((int) (4 * width), (int) (4 * height));
    bg.surface().setFillColor(Color.rgb(255, 255, 255));
    bg.surface().fillRect(0, 0, bg.surface().width(), bg.surface().height());
    bg.surface().setFillColor(Color.rgb(0, 0, 255));
    bg.surface().fillRect(0, bg.surface().width() / 2, bg.surface().width(),
        bg.surface().height() / 2);
    rootLayer.add(bg);

    image1 = assets().getImage(imageSrc);
    image1.addCallback(new ResourceCallback<Image>() {
      @Override
      public void done(Image image) {
        // once the image loads, create our layers
        imageLayer1 = graphics().createImageLayer(image);
        surfaceLayer1 = graphics().createSurfaceLayer(image.width(), image.height());
        surfaceLayer1.surface().drawImage(image, 0, 0);
        CanvasImage canvas1 = graphics().createImage(image.width(), image.height());
        canvas1.canvas().drawImage(image, 0, 0);
        canvasLayer1 = graphics().createImageLayer(canvas1);
        imageLayer2 = graphics().createImageLayer(image);
        surfaceLayer2 = graphics().createSurfaceLayer(image.width(), image.height());
        surfaceLayer2.surface().drawImage(image, 0, 0);
        CanvasImage canvas2 = graphics().createImage(image.width(), image.height());
        canvas2.canvas().drawImage(image, 0, 0);
        canvasLayer2 = graphics().createImageLayer(canvas2);

        // add layers to the rootLayer
        imageLayer1.transform().translate(offset, offset);
        rootLayer.add(imageLayer1);
        surfaceLayer1.transform().translate(offset + width, offset);
        rootLayer.add(surfaceLayer1);
        canvasLayer1.transform().translate(offset + 2 * width, offset);
        rootLayer.add(canvasLayer1);

        imageLayer2.transform().translate(offset, offset + 2 * height);
        rootLayer.add(imageLayer2);
        surfaceLayer2.transform().translate(offset + width, offset + 2 * height);
        rootLayer.add(surfaceLayer2);
        canvasLayer2.transform().translate(offset + 2 * width, offset + 2 * height);
        rootLayer.add(canvasLayer2);
      }

      @Override
      public void error(Throwable err) {
        log().error("Error loading image", err);
      }
    });

    // add ground truth image
    imageGroundTruth = assets().getImage(imageGroundTruthSrc);
    imageGroundTruth.addCallback(new ResourceCallback<Image>() {
      @Override
      public void done(Image image) {
        groundTruthLayer = graphics().createImageLayer(image);
        groundTruthLayer.transform().translate(3 * width, 0);
        rootLayer.add(groundTruthLayer);
      }

      @Override
      public void error(Throwable err) {
        log().error("Error loading image", err);
      }
    });
  }
}

@@ -1163,33 +1163,8 @@
 e1;%0A
-  Image imageGroundTruth;
 %0A  I
@@ -1235,32 +1235,33 @@
 r canvasLayer1;%0A
+%0A
   ImageLayer ima
@@ -1322,24 +1322,51 @@
 anvasLayer2;
+%0A%0A  Image imageGroundTruth;
 %0A  ImageLaye
@@ -1719,16 +1719,17 @@
 ayer();%0A
+%0A
     // a
@@ -3227,42 +3227,37 @@
     
-imageLayer1.transform().translate(
+rootLayer.addAt(imageLayer1, 
 offs
@@ -3294,173 +3294,89 @@
 .add
-(imageLayer1);%0A        surfaceLayer1.transform().translate(offset + width, offset);%0A        rootLayer.add(surfaceLayer1);%0A        canvasLayer1.transform().translate(
+At(surfaceLayer1, offset + width, offset);%0A        rootLayer.addAt(canvasLayer1, 
 offs
@@ -3392,32 +3392,33 @@
 width, offset);%0A
+%0A
         rootLaye
@@ -3426,296 +3426,169 @@
 .add
-(canvasLayer1);%0A%0A        imageLayer2.transform().translate(offset, offset + 2 * height);%0A        rootLayer.add(imageLayer2);%0A        surfaceLayer2.transform().translate(offset + width, offset + 2 * height);%0A        rootLayer.add(surfaceLayer2);%0A        canvasLayer2.transform().translate(
+At(imageLayer2, offset, offset + 2 * height);%0A        rootLayer.addAt(surfaceLayer2, offset + width, offset + 2 * height);%0A        rootLayer.addAt(canvasLayer2, 
 offs
@@ -3629,45 +3629,8 @@
 t);%0A
-        rootLayer.add(canvasLayer2);%0A
     
@@ -4036,70 +4036,8 @@
 e);%0A
-        groundTruthLayer.transform().translate(3 * width, 0);%0A
     
@@ -4053,16 +4053,18 @@
 ayer.add
+At
 (groundT
@@ -4072,16 +4072,30 @@
 uthLayer
+, 3 * width, 0
 );%0A     

Use GroupLayer.addAt, for great clarification.