1/*
2 * ProGuard -- shrinking, optimization, obfuscation, and preverification
3 *             of Java bytecode.
4 *
5 * Copyright (c) 2002-2013 Eric Lafortune (eric@graphics.cornell.edu)
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the Free
9 * Software Foundation; either version 2 of the License, or (at your option)
10 * any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21package proguard.gui.splash;
22
23import java.awt.*;
24
25/**
26 * This Sprite adds a drop shadow to another Sprite.
27 *
28 * @author Eric Lafortune
29 */
30public class ShadowedSprite implements Sprite
31{
32    private final VariableInt    xOffset;
33    private final VariableInt    yOffset;
34    private final VariableDouble alpha;
35    private final VariableInt    blur;
36    private final Sprite         sprite;
37
38    private float cachedAlpha = -1.0f;
39    private Color cachedColor;
40
41
42    /**
43     * Creates a new ShadowedSprite.
44     * @param xOffset the variable x-offset of the shadow, relative to the sprite itself.
45     * @param yOffset the variable y-offset of the shadow, relative to the sprite itself.
46     * @param alpha   the variable darkness of the shadow (between 0 and 1).
47     * @param blur    the variable blur of the shadow (0 for sharp shadows, 1 or
48     *                more for increasingly blurry shadows).
49     * @param sprite  the Sprite to be painted with its shadow.
50     */
51    public ShadowedSprite(VariableInt    xOffset,
52                          VariableInt    yOffset,
53                          VariableDouble alpha,
54                          VariableInt    blur,
55                          Sprite         sprite)
56    {
57        this.xOffset = xOffset;
58        this.yOffset = yOffset;
59        this.alpha   = alpha;
60        this.blur    = blur;
61        this.sprite  = sprite;
62    }
63
64
65    // Implementation for Sprite.
66
67    public void paint(Graphics graphics, long time)
68    {
69        double l = alpha.getDouble(time);
70        int    b = blur.getInt(time) + 1;
71
72        float a = 1.0f - (float)Math.pow(1.0 - l, 1.0/(b*b));
73        if (a != cachedAlpha)
74        {
75            cachedAlpha = a;
76            cachedColor = new Color(0f, 0f, 0f, a);
77        }
78
79        // Set up the shadow graphics.
80        //OverrideGraphics2D g = new OverrideGraphics2D((Graphics2D)graphics);
81        //g.setOverrideColor(cachedColor);
82
83        // Set the shadow color.
84        Color actualColor = graphics.getColor();
85        graphics.setColor(cachedColor);
86
87        int xo = xOffset.getInt(time) - b/2;
88        int yo = yOffset.getInt(time) - b/2;
89
90        // Draw the sprite's shadow.
91        for (int x = 0; x < b; x++)
92        {
93            for (int y = 0; y < b; y++)
94            {
95                int xt = xo + x;
96                int yt = yo + y;
97                graphics.translate(xt, yt);
98                sprite.paint(graphics, time);
99                graphics.translate(-xt, -yt);
100            }
101        }
102
103        // Restore the actual sprite color.
104        graphics.setColor(actualColor);
105
106        // Draw the sprite itself in the ordinary graphics.
107        sprite.paint(graphics, time);
108    }
109}
110