001/****************************************************************************** 002 Copyright 2011 See AUTHORS file. 003 004 Licensed under the Apache License, Version 2.0 (the "License"); 005 you may not use this file except in compliance with the License. 006 You may obtain a copy of the License at 007 008 http://www.apache.org/licenses/LICENSE-2.0 009 010 Unless required by applicable law or agreed to in writing, software 011 distributed under the License is distributed on an "AS IS" BASIS, 012 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 See the License for the specific language governing permissions and 014 limitations under the License. 015 */ 016 017package squidpony.squidgrid.gui.gdx; 018 019import com.badlogic.gdx.graphics.Color; 020import com.badlogic.gdx.math.Interpolation; 021import com.badlogic.gdx.scenes.scene2d.Actor; 022import com.badlogic.gdx.scenes.scene2d.actions.Actions; 023import com.badlogic.gdx.scenes.scene2d.actions.TemporalAction; 024 025/** Sets the actor's color (or a specified color), from the current to the new color. Note this action transitions from the color 026 * at the time the action starts to the specified color. 027 * @author Nathan Sweet */ 028public class PackedColorAction extends TemporalAction { 029 private float start; 030 private Color color; 031 private float end; 032 033 protected void begin () { 034 if (color == null) color = target.getColor(); 035 start = color.toFloatBits(); 036 } 037 038 protected void update (float percent) { 039 Color.abgr8888ToColor(color, SColor.lerpFloatColors(start, end, percent)); 040 } 041 042 public void reset () { 043 super.reset(); 044 color = null; 045 } 046 047 public Color getColor () { 048 return color; 049 } 050 051 /** Sets the color to modify. If null (the default), the {@link #getActor() actor's} {@link Actor#getColor() color} will be 052 * used. */ 053 public void setColor (Color color) { 054 this.color = color; 055 } 056 057 public float getEndColor () { 058 return end; 059 } 060 061 /** Sets the color to transition to. Required. */ 062 public void setEndColor (float color) { 063 end = color; 064 } 065 /** Sets the actor's color instantly. */ 066 static public PackedColorAction color (float color) { 067 return color(color, 0, null); 068 } 069 070 /** Transitions from the color at the time this action starts to the specified color. */ 071 static public PackedColorAction color (float color, float duration) { 072 return color(color, duration, null); 073 } 074 075 /** Transitions from the color at the time this action starts to the specified color. */ 076 static public PackedColorAction color (float color, float duration, Interpolation interpolation) { 077 PackedColorAction action = Actions.action(PackedColorAction.class); 078 action.setEndColor(color); 079 action.setDuration(duration); 080 action.setInterpolation(interpolation); 081 return action; 082 } 083 084}