/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *                                                                         *
 *   AndroidWorld Library, Copyright 2011 Bryan Chadwick                   *
 *                                                                         *
 *   FILE: ./android/image/OverlayXY.java                                  *
 *                                                                         *
 *   This file is part of AndroidWorld.                                    *
 *                                                                         *
 *   AndroidWorld is free software: you can redistribute it and/or         *
 *   modify it under the terms of the GNU General Public License           *
 *   as published by the Free Software Foundation, either version          *
 *   3 of the License, or (at your option) any later version.              *
 *                                                                         *
 *   AndroidWorld is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with AndroidWorld.  If not, see <http://www.gnu.org/licenses/>. *
 *                                                                         *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

package android.image;

import android.world.Posn;
import android.graphics.Canvas;

/**
 * <style type='text/css'><!--
 *    .com{ font-style: italic; color: #880000; }
 *    .keyw{ font-weight: bold; color: #000088; }
 *    .num{ color: #00AA00; }
 *    .str{ color: #CC00AB; }
 *    .prim{ color: #0000FF; }
 *    img.example{ padding-left: 50px; padding-bottom: 30px; }
 *  --></style>
 * 
 * Represents the Overlaying of two images with the given offset.
 *  
 * <pre>
 *    <span class='keyw'>new</span> OverlayXY(<span class='keyw'>new</span> Rectangle(<span class='num'>20</span>, <span class='num'>20</span>, <span class='str'>"outline"</span>, <span class='str'>"black"</span>),
 *                <span class='num'>20</span>, <span class='num'>0</span>, <span class='keyw'>new</span> Rectangle(<span class='num'>20</span>, <span class='num'>20</span>, <span class='str'>"outline"</span>, <span class='str'>"black"</span>))</pre>
 * <img class="example" src="test/images/overlayxy-1.png" />
 * <br />
 *  
 * <pre>
 *    <span class='keyw'>new</span> OverlayXY(<span class='keyw'>new</span> Rectangle(<span class='num'>20</span>, <span class='num'>20</span>, <span class='str'>"solid"</span>, <span class='str'>"red"</span>),
 *                <span class='num'>20</span>, <span class='num'>20</span>,
 *                <span class='keyw'>new</span> Rectangle(<span class='num'>20</span>, <span class='num'>20</span>, <span class='str'>"solid"</span>, <span class='str'>"black"</span>))</pre>
 * <img class="example" src="test/images/overlayxy-2.png" />
 * <br />
 *  
 * <pre>
 *    <span class='keyw'>new</span> OverlayXY(<span class='keyw'>new</span> Rectangle(<span class='num'>20</span>, <span class='num'>20</span>, <span class='str'>"solid"</span>, <span class='str'>"red"</span>),
 *                <span class='num'>-20</span>, <span class='num'>-20</span>,
 *                <span class='keyw'>new</span> Rectangle(<span class='num'>20</span>, <span class='num'>20</span>, <span class='str'>"solid"</span>, <span class='str'>"black"</span>))</pre>
 * <img class="example" src="test/images/overlayxy-3.png" />
 * <br />
 *  
 * <pre>
 *    <span class='keyw'>new</span> OverlayXY(
 *           <span class='keyw'>new</span> OverlayXY(<span class='keyw'>new</span> Ellipse(<span class='num'>40</span>, <span class='num'>40</span>, <span class='str'>"outline"</span>, <span class='str'>"black"</span>),
 *                 <span class='num'>10</span>, <span class='num'>15</span>, <span class='keyw'>new</span> Ellipse(<span class='num'>10</span>, <span class='num'>10</span>, <span class='str'>"solid"</span>, <span class='str'>"forestgreen"</span>)),
 *                 <span class='num'>20</span>, <span class='num'>15</span>, <span class='keyw'>new</span> Ellipse(<span class='num'>10</span>, <span class='num'>10</span>, <span class='str'>"solid"</span>, <span class='str'>"forestgreen"</span>))</pre>
 * <img class="example" src="test/images/overlayxy-4.png" />
 * <br />
 * <br />
 *  
 * Image overlays are also available as a method on <code>Image</code>s.
 *
 * <pre>
 *    <span class='keyw'>new</span> Rectangle(<span class='num'>20</span>, <span class='num'>20</span>, <span class='str'>"outline"</span>, <span class='str'>"black"</span>)
 *        .overlayxy(<span class='keyw'>new</span> Rectangle(<span class='num'>20</span>, <span class='num'>20</span>, <span class='str'>"outline"</span>, <span class='str'>"black"</span>), <span class='num'>20</span>, <span class='num'>0</span>)</pre>
 * <img class="example" src="test/images/overlayxy-1.2.png" />
 * <br />
 *  
 * <pre>
 *    <span class='keyw'>new</span> Rectangle(<span class='num'>20</span>, <span class='num'>20</span>, <span class='str'>"solid"</span>, <span class='str'>"black"</span>)
 *        .overlayxy(<span class='keyw'>new</span> Rectangle(<span class='num'>20</span>, <span class='num'>20</span>, <span class='str'>"solid"</span>, <span class='str'>"red"</span>), <span class='num'>20</span>, <span class='num'>20</span>)</pre>
 * <img class="example" src="test/images/overlayxy-2.2.png" />
 * <br />
 * 
 * <pre>
 *    <span class='keyw'>new</span> Rectangle(<span class='num'>20</span>, <span class='num'>20</span>, <span class='str'>"solid"</span>, <span class='str'>"black"</span>)
 *        .overlayxy(<span class='keyw'>new</span> Rectangle(<span class='num'>20</span>, <span class='num'>20</span>, <span class='str'>"solid"</span>, <span class='str'>"red"</span>), <span class='num'>-20</span>, <span class='num'>-20</span>)</pre>
 *  <img class="example" src="test/images/overlayxy-3.2.png" />
 *  <br />
 *     
 * <pre>
 *    <span class='keyw'>new</span> Ellipse(<span class='num'>10</span>, <span class='num'>10</span>, <span class='str'>"solid"</span>, <span class='str'>"forestgreen"</span>)
 *        .overlayxy(<span class='num'>20</span>, <span class='num'>15</span>, <span class='keyw'>new</span> Ellipse(<span class='num'>10</span>, <span class='num'>10</span>, <span class='str'>"solid"</span>, <span class='str'>"forestgreen"</span>)
 *                               .overlayxy(<span class='num'>10</span>, <span class='num'>15</span>, <span class='keyw'>new</span> Ellipse(<span class='num'>40</span>, <span class='num'>40</span>, <span class='str'>"outline"</span>, <span class='str'>"black"</span>))</pre>
 * <img class="example" src="test/images/overlayxy-4.2.png" />
 * <br />
 *   
 */
public class OverlayXY extends Overlay{
    private double dx, dy;
    private double tx, ty;
    
    /** Construct an OverlayXY from the two images and the offset (X,Y) */
    public OverlayXY(Image top, int x, int y, Image bot){
        this(top,(double)x,y,bot);
    }   
    /** Construct an OverlayXY from the two images and the Posn offset */
    public OverlayXY(Image top, Posn p, Image bot){
        this(top,p.x,p.y,bot);
    }   
    /** Construct an OverlayXY from the two images and the offset (X,Y) */
    public OverlayXY(Image top, double x, double y, Image bot){
        super(top,bot,
                calcwidth(top,bot,x),
                calcheight(top,bot,y));
        this.dx = x;
        this.dy = y;
        // Where to draw the Top image...
        this.tx = -pinholeX+top.pinholeX;
        if(top.width() > bot.width()+dx)
            this.tx = pinholeX-top.pinholeX;
        
        this.ty = -pinholeY+top.pinholeY;
        if(top.height() > bot.height()+dy)
            this.ty = pinholeY-top.pinholeY;
    }
    
    private static double calcwidth(Image t, Image b, double dx){
        return Math.max(t.width(), b.width()+dx)-((dx < 0)?dx:0);
    }
    private static double calcheight(Image t, Image b, double dy){
        return Math.max(t.height(), b.height()+dy)-((dy < 0)?dy:0);
    }
    
    /** Draw this OverlayXY image into a Graphics */
    public void paint(Canvas c, int xx, int yy){
        bot.paint(c, round(xx+tx+dx+bot.pinholeX-top.pinholeX),
                round(yy+ty+dy+bot.pinholeY-top.pinholeY));
        top.paint(c, round(xx+tx), round(yy+ty));
    }
}