001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.gui.animation;
003
004import java.awt.Color;
005import java.awt.Graphics;
006import java.awt.Point;
007import java.awt.Polygon;
008import java.util.Random;
009
010/**
011 * A star displayed when {@link ChristmasExtension} is active. Copied from Icedtea-Web.
012 * @author Jiri Vanek (Red Hat)
013 * @see <a href="http://icedtea.classpath.org/hg/icedtea-web/rev/87d3081ab573">Initial commit</a>
014 * @since 14581
015 */
016class Star implements IAnimObject {
017    private static final Random seed = new Random();
018
019    static final int averageStarWidth = 10;    // stars will be 5-15
020    static final int averageFallSpeed = 4;     // 2-6
021    static final int averageRotationSpeed = 2; // 1-3
022
023    private static final Color WATER_LIVE_COLOR = new Color(80, 131, 160);
024
025    private int w;
026    private int h;
027
028    private int radiusX;
029    private int radiusY;
030    private int maxRadiusX;
031    private int maxRadiusY;
032    private final Point center = new Point();
033    private final int fallSpeed;
034    private final boolean orientation;
035    private final int[] originalColor = new int[3];
036    private final int[] color = new int[originalColor.length];
037    private int direction;
038    private final boolean haveEight;
039
040    Star(int w, int h) {
041        this.w = w;
042        this.h = h;
043        createRadiuses();
044        haveEight = seed.nextBoolean();
045        center.x = seed.nextInt(w + 1);
046        center.y = seed.nextInt(h + 1);
047        fallSpeed = averageFallSpeed / 2 + seed.nextInt(averageFallSpeed / 2);
048        orientation = seed.nextBoolean();
049        direction = -(averageRotationSpeed / 2 + seed.nextInt(averageRotationSpeed / 2));
050        if (seed.nextInt(4) == 0) {
051            originalColor[0] = Color.yellow.getRed();
052            originalColor[1] = Color.yellow.getGreen();
053            originalColor[2] = Color.yellow.getBlue();
054        } else {
055            originalColor[0] = WATER_LIVE_COLOR.getRed();
056            originalColor[1] = WATER_LIVE_COLOR.getGreen();
057            originalColor[2] = WATER_LIVE_COLOR.getBlue();
058        }
059    }
060
061    @Override
062    public void paint(Graphics g) {
063        Color c = g.getColor();
064        g.setColor(new Color(color[0], color[1], color[2]));
065        Polygon p = createPolygon();
066        if (haveEight) {
067            int min1 = Math.min(radiusX, radiusY);
068            int min2 = min1 / 2;
069            g.fillRect(center.x - min2, center.y - min2, min1, min1);
070        }
071        g.fillPolygon(p);
072        g.setColor(c);
073    }
074
075    @Override
076    public void setExtend(int w, int h) {
077        this.w = w;
078        this.h = h;
079    }
080
081    @Override
082    public void animate() {
083        center.y += fallSpeed;
084        if (orientation) {
085            radiusX += direction;
086            if (radiusX <= -direction) {
087                direction = -direction;
088                radiusX = direction;
089            }
090            if (radiusX >= maxRadiusX) {
091                direction = -direction;
092                radiusX = maxRadiusX;
093            }
094            interpolateColors(radiusX, maxRadiusX);
095        } else {
096            radiusY += direction;
097            if (radiusY <= -direction) {
098                direction = -direction;
099                radiusY = direction;
100            }
101            if (radiusY >= maxRadiusY) {
102                direction = -direction;
103                radiusY = maxRadiusY;
104            }
105            interpolateColors(radiusY, maxRadiusY);
106        }
107        if (center.y > w + 1 || center.y > h + radiusY * 2) {
108            createRadiuses();
109            center.x = seed.nextInt(w + 1);
110            center.y = -radiusY * 2;
111        }
112    }
113
114    private static int createRadius() {
115        return averageStarWidth / 2 + seed.nextInt(averageStarWidth);
116    }
117
118    private Polygon createPolygon() {
119        int min = Math.min(radiusX, radiusY) / 3;
120        Polygon p = new Polygon();
121        p.addPoint(center.x - radiusX, center.y);
122        p.addPoint(center.x - min, center.y - min);
123        p.addPoint(center.x, center.y - radiusY);
124        p.addPoint(center.x + min, center.y - min);
125        p.addPoint(center.x + radiusX, center.y);
126        p.addPoint(center.x + min, center.y + min);
127        p.addPoint(center.x, center.y + radiusY);
128        p.addPoint(center.x - min, center.y + min);
129        return p;
130    }
131
132    private void interpolateColors(int is, int max) {
133        for (int i = 0; i < originalColor.length; i++) {
134            int fadeMin;
135            if (center.y < 0) {
136                fadeMin = 0;
137            } else if (center.y > h) {
138                fadeMin = 255;
139            } else {
140                fadeMin = (int) interpol(h, center.y, 255, 0); // from white to black
141            }
142            int fadeMax;
143            if (center.y < 0) {
144                fadeMax = 0;
145            } else if (center.y > h) {
146                fadeMax = originalColor[i];
147            } else {
148                fadeMax = (int) interpol(h, center.y, originalColor[i], 0); // from color to black
149            }
150            color[i] = (int) interpol(max, is, fadeMin, fadeMax);
151        }
152    }
153
154    private void createRadiuses() {
155        radiusX = createRadius();
156        radiusY = radiusX;
157        switch (seed.nextInt(3)) {
158            case 0:
159                radiusX = radiusX + (2 * radiusX) / 3;
160                break;
161            case 1:
162                radiusY = radiusY + (2 * radiusY) / 3;
163                break;
164            default:
165                break;
166        }
167        maxRadiusX = radiusX;
168        maxRadiusY = radiusY;
169    }
170
171    /**
172     * Interpolation is root ratio is r= (currentSize / origSize)
173     * then value to-from is interpolated from to to from according to ratio
174     *
175     * @param origSize original size
176     * @param currentSize current size
177     * @param from starting value
178     * @param to ending value
179     * @return interpolated value
180     */
181    static double interpol(double origSize, double currentSize, double from, double to) {
182        return (currentSize / origSize) * (to - from) + from;
183    }
184}