JAL-3130 adapted getdown src. attempt 2. first attempt failed due to cp'ed .git files
[jalview.git] / getdown / src / getdown / launcher / src / main / java / com / threerings / getdown / launcher / RotatingBackgrounds.java
1 //
2 // Getdown - application installer, patcher and launcher
3 // Copyright (C) 2004-2018 Getdown authors
4 // https://github.com/threerings/getdown/blob/master/LICENSE
5
6 package com.threerings.getdown.launcher;
7
8 import java.awt.Image;
9 import java.util.List;
10
11 import static com.threerings.getdown.Log.log;
12
13 public final class RotatingBackgrounds
14 {
15     public interface ImageLoader {
16         /** Loads and returns the image with the supplied path. */
17         public Image loadImage (String path);
18     }
19
20     /**
21      * Creates a placeholder if there are no images. Just returns null from getImage every time.
22      */
23     public RotatingBackgrounds ()
24     {
25         makeEmpty();
26     }
27
28     /** Creates a single image background. */
29     public RotatingBackgrounds (Image background)
30     {
31         percentages = new int[] { 0 };
32         minDisplayTime = new int[] { 0 };
33         images = new Image[] { background };
34         errorImage = images[0];
35     }
36
37     /**
38      * Create a sequence of images to be rotated through from <code>backgrounds</code>.
39      *
40      * Each String in backgrounds should be the path to the image, a semicolon, and the minimum
41      * amount of time to display the image in seconds. Each image will be active for an equal
42      * percentage of the download process, unless one hasn't been active for its minimum display
43      * time when the next should be shown. In that case, it's left up until its been there for its
44      * minimum display time and then the next one gets to come up.
45      */
46     public RotatingBackgrounds (List<String> backgrounds, String errorBackground, ImageLoader loader)
47     {
48         percentages = new int[backgrounds.size()];
49         minDisplayTime = new int[backgrounds.size()];
50         images = new Image[backgrounds.size()];
51         for (int ii = 0; ii < backgrounds.size(); ii++) {
52             String background = backgrounds.get(ii);
53             String[] pieces = background.split(";");
54             if (pieces.length != 2) {
55                 log.warning("Unable to parse background image '" + background + "'");
56                 makeEmpty();
57                 return;
58             }
59             images[ii] = loader.loadImage(pieces[0]);
60             try {
61                 minDisplayTime[ii] = Integer.parseInt(pieces[1]);
62             } catch (NumberFormatException e) {
63                 log.warning("Unable to parse background image display time '" + background + "'");
64                 makeEmpty();
65                 return;
66             }
67             percentages[ii] = (int)((ii/(float)backgrounds.size()) * 100);
68         }
69         if (errorBackground == null) {
70             errorImage = images[0];
71         } else {
72             errorImage = loader.loadImage(errorBackground);
73         }
74     }
75
76     /**
77      * @return the image to display at the given progress or null if there aren't any.
78      */
79     public Image getImage (int progress)
80     {
81         if (images.length == 0) {
82             return null;
83         }
84         long now = System.currentTimeMillis();
85         if (current != images.length - 1
86             && (current == -1 || (progress >= percentages[current + 1] &&
87                     (now - currentDisplayStart) / 1000 > minDisplayTime[current]))) {
88             current++;
89             currentDisplayStart = now;
90         }
91         return images[current];
92     }
93
94     /**
95      * Returns the image to display if an error has caused getdown to fail.
96      */
97     public Image getErrorImage ()
98     {
99         return errorImage;
100     }
101
102     /**
103      * @return the number of images in this RotatingBackgrounds
104      */
105     public int getNumImages() {
106         return images.length;
107     }
108
109     protected void makeEmpty ()
110     {
111         percentages = new int[] {};
112         minDisplayTime = new int[] {};
113         images = new Image[] {};
114     }
115
116     /** Time at which the currently displayed image was first displayed in millis. */
117     protected long currentDisplayStart;
118
119     /** The index of the currently displayed image or -1 if we haven't displayed any. */
120     protected int current = -1;
121
122     protected Image[] images;
123
124     /** The image to display if getdown has failed due to an error. */
125     protected Image errorImage;
126
127     /** Percentage at which each image should be displayed. */
128     protected int[] percentages;
129
130     /** Time to show each image in seconds. */
131     protected int[] minDisplayTime;
132 }