2 // Getdown - application installer, patcher and launcher
3 // Copyright (C) 2004-2018 Getdown authors
4 // https://github.com/threerings/getdown/blob/master/LICENSE
6 package com.threerings.getdown.launcher;
11 import static com.threerings.getdown.Log.log;
13 public final class RotatingBackgrounds
15 public interface ImageLoader {
16 /** Loads and returns the image with the supplied path. */
17 public Image loadImage (String path);
21 * Creates a placeholder if there are no images. Just returns null from getImage every time.
23 public RotatingBackgrounds ()
28 /** Creates a single image background. */
29 public RotatingBackgrounds (Image background)
31 percentages = new int[] { 0 };
32 minDisplayTime = new int[] { 0 };
33 images = new Image[] { background };
34 errorImage = images[0];
38 * Create a sequence of images to be rotated through from <code>backgrounds</code>.
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.
46 public RotatingBackgrounds (List<String> backgrounds, String errorBackground, ImageLoader loader)
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 + "'");
59 images[ii] = loader.loadImage(pieces[0]);
61 minDisplayTime[ii] = Integer.parseInt(pieces[1]);
62 } catch (NumberFormatException e) {
63 log.warning("Unable to parse background image display time '" + background + "'");
67 percentages[ii] = (int)((ii/(float)backgrounds.size()) * 100);
69 if (errorBackground == null) {
70 errorImage = images[0];
72 errorImage = loader.loadImage(errorBackground);
77 * @return the image to display at the given progress or null if there aren't any.
79 public Image getImage (int progress)
81 if (images.length == 0) {
84 long now = System.currentTimeMillis();
85 if (current != images.length - 1
86 && (current == -1 || (progress >= percentages[current + 1] &&
87 (now - currentDisplayStart) / 1000 > minDisplayTime[current]))) {
89 currentDisplayStart = now;
91 return images[current];
95 * Returns the image to display if an error has caused getdown to fail.
97 public Image getErrorImage ()
103 * @return the number of images in this RotatingBackgrounds
105 public int getNumImages() {
106 return images.length;
109 protected void makeEmpty ()
111 percentages = new int[] {};
112 minDisplayTime = new int[] {};
113 images = new Image[] {};
116 /** Time at which the currently displayed image was first displayed in millis. */
117 protected long currentDisplayStart;
119 /** The index of the currently displayed image or -1 if we haven't displayed any. */
120 protected int current = -1;
122 protected Image[] images;
124 /** The image to display if getdown has failed due to an error. */
125 protected Image errorImage;
127 /** Percentage at which each image should be displayed. */
128 protected int[] percentages;
130 /** Time to show each image in seconds. */
131 protected int[] minDisplayTime;