//
// 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.Canvas;
import playn.core.CanvasImage;
import playn.core.GroupLayer;
import playn.core.ImageLayer;
import playn.core.PlayN;
import playn.core.TextLayout;

/**
 * Handles the maintenance of a canvas image and layer for displaying a chunk of pre-rendered
 * graphics.
 */
public class Glyph
{
    public Glyph (GroupLayer parent) {
        _parent = parent;
    }

    /** Ensures that the canvas image is at least the specified dimensions and cleared to all
     * transparent pixels. Also creates and adds the image layer to the parent layer if
     * needed. */
    public void prepare (float width, float height) {
        // recreate our canvas if we need more room than we have (TODO: should we ever shrink it?)
        if (_image == null || _image.width() < width || _image.height() < height) {
            _image = PlayN.graphics().createImage(width, height);
            if (_layer != null) _layer.setImage(_image);
        } else {
            _image.canvas().clear();
        }
        if (_layer == null) _parent.add(_layer = PlayN.graphics().createImageLayer(_image));
        _preparedWidth = width;
        _preparedHeight = height;
    }

    /** Returns the layer that contains our glyph image. Valid after {@link #prepare}. */
    public ImageLayer layer () {
        return _layer;
    }

    /** Returns the canvas into which drawing may be done. Valid after {@link #prepare}. */
    public Canvas canvas () {
        return _image.canvas();
    }

    /** Destroys the layer and image, removing them from the containing widget. */
    public void destroy () {
        if (_layer != null) {
            _layer.destroy();
            _layer = null;
        }
        _image = null;
    }

    /**
     * Returns the width of the last call to {@link #prepare}, or zero if the glyph is not
     * prepared. The canvas should be at least this width, or null if the glyph is not prepared.
     */
    public float preparedWidth () {
        return _preparedWidth;
    }

    /**
     * Returns the height of the last call to {@link #prepare}, or zero if the glyph is not
     * prepared. The canvas should be at least this height, or null if the glyph is not prepared.
     */
    public float preparedHeight () {
        return _preparedHeight;
    }

    /**
     * Prepares the canvas and renders the supplied text at 0, 0 using the given config.
     */
    public void renderText (TextConfig config, String text) {
        renderText(config, config.layout(text));
    }

    /**
     * Prepares the canvas and renders the supplied layout at 0, 0 using the given config.
     */
    public void renderText (TextConfig config, TextLayout layout) {
        prepare(config.effect.adjustWidth(layout.width()),
            config.effect.adjustHeight(layout.height()));
        config.render(canvas(), layout, 0, 0);
    }

    protected final GroupLayer _parent;
    protected CanvasImage _image;
    protected ImageLayer _layer;
    protected float _preparedWidth, _preparedHeight;
}

@@ -583,24 +583,156 @@
 t = parent;%0A
+        _depth = null;%0A    %7D%0A%0A    public Glyph (GroupLayer parent, float depth) %7B%0A        _parent = parent;%0A        _depth = depth;%0A
     %7D%0A%0A    /
@@ -1382,20 +1382,22 @@
 ll) 
-_parent.add(
+%7B%0A            
 _lay
@@ -1442,18 +1442,117 @@
 (_image)
-)
 ;
+%0A            if (_depth != null) _layer.setDepth(_depth);%0A            _parent.add(_layer);%0A        %7D
 %0A       
@@ -3335,24 +3335,58 @@
 er _parent;%0A
+    protected final Float _depth;%0A
     protecte

Support setting the depth of the created image layer