//
// Triple Play - utilities for use in PlayN-based games
// Copyright (c) 2011, Three Rings Design, Inc. - All rights reserved.
// http://github.com/threerings/tripleplay/blob/master/LICENSE

package tripleplay.util;

import playn.core.GroupLayer;
import playn.core.Layer;
import pythagoras.f.IPoint;
import pythagoras.f.Point;
import pythagoras.f.Rectangle;
import pythagoras.f.Transform;

/**
 * Provides utility functions for dealing with Layers
 */
public class Layers
{
    /**
     * Transforms a point from one Layer's coordinate system to another's
     */
    public static Point transform (IPoint p, Layer from, Layer to, Point result)
    {
        Layer.Util.layerToScreen(from, p, result);
        Layer.Util.screenToLayer(to, result, result);
        return result;
    }

    /**
     * Transforms a point from one Layer's coordinate system to another's
     */
    public static Point transform (IPoint p, Layer from, Layer to)
    {
        return transform(p, from, to, new Point());
    }

    /**
     * Computes the total bounds of the layer hierarchy rooted at <code>root</code>.
     * The returned Rectangle will be in <code>root</code>'s coordinate system.
     */
    public static Rectangle totalBounds (Layer root)
    {
        Rectangle r = new Rectangle();
        addBounds(root, root, r);
        return r;
    }

    /**
     * Helper function for totalBounds()
     */
    protected static void addBounds (Layer root, Layer l, Rectangle bounds)
    {
        if (l == root) {
            // initialize bounds
            float x = l.originX();
            float y = l.originY();
            // avoid weird "-0, -0" rectangle origin
            bounds.setBounds((x != 0 ? -x : 0), (y != 0 ? -y : 0), 0, 0);
        }

        if (l instanceof Layer.HasSize) {
            Layer.HasSize lhs = (Layer.HasSize) l;
            float w = lhs.width();
            float h = lhs.height();

            // grow bounds
            if (w != 0 || h != 0) {
                Transform t = l.transform();
                bounds.add(Layer.Util.layerToParent(l, root, t.tx(), t.ty()));
                bounds.add(Layer.Util.layerToParent(l, root, t.tx() + w, t.ty() + h));
            }
        }

        if (l instanceof GroupLayer) {
            GroupLayer group = (GroupLayer) l;
            for (int ii = 0, ll = group.size(); ii < ll; ++ii) {
                addBounds(root, group.get(ii), bounds);
            }
        }
    }
}

@@ -357,39 +357,8 @@
 gle;
-%0Aimport pythagoras.f.Transform;
 %0A%0A/*
@@ -1948,53 +1948,8 @@
 ) %7B%0A
-                Transform t = l.transform();%0A
     
@@ -2009,22 +2009,12 @@
 ot, 
-t.tx(), t.ty()
+0, 0
 ));%0A
@@ -2078,28 +2078,10 @@
 ot, 
-t.tx() + w, t.ty() +
+w,
  h))

Fix Layers.totalBounds.