001package squidpony.squidgrid.gui.gdx; 002 003import com.badlogic.gdx.graphics.Color; 004import com.badlogic.gdx.graphics.Texture; 005import com.badlogic.gdx.scenes.scene2d.ui.Image; 006 007import java.util.ArrayList; 008import java.util.Collections; 009import java.util.List; 010 011/** 012 * Created by Tommy Ettinger on 3/23/2016. 013 */ 014public class ColorChangeImage extends Image { 015 016 private List<Color> colors; 017 private float progress = 0f; 018 private float loopTime = 2f; 019 020 /** 021 * Creates an image stretched and aligned center, that will use the specified list of colors. 022 * 023 * @param texture the texture to use 024 * @param colors a List of Color, such as one returned by SquidColorCenter's gradient or rainbow methods 025 */ 026 public ColorChangeImage(Texture texture, List<Color> colors) { 027 super(texture); 028 029 if(colors == null || colors.isEmpty()) 030 this.colors = DefaultResources.getSCC().rainbow(12); 031 else 032 this.colors = colors; 033 } 034 035 /** 036 * Creates an image stretched and aligned center, that will use the specified list of colors. 037 * 038 * @param texture the texture to use 039 * @param colors a List of Color, such as one returned by SquidColorCenter's gradient or rainbow methods 040 */ 041 public ColorChangeImage(Texture texture, float loopTime, List<Color> colors) { 042 super(texture); 043 044 if(colors == null || colors.isEmpty()) 045 this.colors = DefaultResources.getSCC().rainbow(12); 046 else 047 this.colors = colors; 048 this.loopTime = loopTime; 049 } 050 /** 051 * Creates an image stretched and aligned center, that will use the specified list of colors. 052 * 053 * @param texture the texture to use 054 * @param colors a List of Color, such as one returned by SquidColorCenter's gradient or rainbow methods 055 */ 056 public ColorChangeImage(Texture texture, float loopTime, Color... colors) { 057 super(texture); 058 059 if(colors == null || colors.length == 0) 060 this.colors = DefaultResources.getSCC().rainbow(12); 061 else { 062 this.colors = new ArrayList<>(colors.length); 063 Collections.addAll(this.colors, colors); 064 } 065 this.loopTime = loopTime; 066 } 067 068 /** 069 * Returns the color the actor will be tinted when drawn. Takes the Color from the List of Color this was constructed 070 * with or assigned with setColors, not the normal Actor color assigned with setColor. 071 * @return the Color this will be drawn with 072 */ 073 @Override 074 public Color getColor() { 075 return colors.get((int)(progress * colors.size() / loopTime)); 076 } 077 078 /** 079 * Sets the list of colors this uses to choose what color it draws with. 080 * @param colors a List of Color, such as one returned by SquidColorCenter's gradient or rainbow methods 081 */ 082 public void setColors(List<Color> colors) 083 { 084 if(colors == null || colors.isEmpty()) 085 this.colors = DefaultResources.getSCC().rainbow(12); 086 else 087 this.colors = colors; 088 } 089 090 /** 091 * Sets the list of colors this uses to choose what color it draws with. 092 * @param colors an array or vararg of Color 093 */ 094 public void setColors(Color... colors) 095 { 096 if (colors == null || colors.length == 0) 097 this.colors = DefaultResources.getSCC().rainbow(12); 098 else { 099 this.colors = new ArrayList<>(colors.length); 100 Collections.addAll(this.colors, colors); 101 } 102 } 103 /** 104 * Updates the actor based on time. Typically this is called each frame by 105 * {@link com.badlogic.gdx.scenes.scene2d.Stage#act(float)}. 106 * <p> 107 * The default implementation calls 108 * {@link com.badlogic.gdx.scenes.scene2d.Action#act(float)} on each action and removes actions that are complete. 109 * 110 * @param delta Time in seconds since the last frame. 111 */ 112 @Override 113 public void act(float delta) { 114 super.act(delta); 115 progress = (progress + delta) % (loopTime - 0.001f); 116 } 117 118 /** 119 * Changes the amount of time this takes to loop through all colors, and also resets the current loop to its start. 120 * @param loopTime the amount of time, in seconds, it takes to loop through all the colors in the list 121 */ 122 public void resetLoopTime(float loopTime) 123 { 124 this.loopTime = loopTime; 125 progress = 0f; 126 } 127}