ColorTo action

Any community contributions to libgdx go here! Some may get included in the core API when permission is granted.

ColorTo action

Postby Magnesus » Sun Jan 01, 2012 11:31 am

It's not as nice as I thought it would be (changing brightness would be usefull too) but it works. Should also take interpolators, but I didn't test it.
Change the name and package to yours.
How to use? Just like FadeTo:

youractor.action(MaGdxActionColorTo.$(1.0f, 0.0f, 0.0f, 0.0f, 100));

Code: Select all
package magory.and;

import com.badlogic.gdx.scenes.scene2d.Action;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.AnimationAction;
import com.badlogic.gdx.scenes.scene2d.actions.ActionResetingPool;

public class MaGdxActionColorTo extends AnimationAction {
        static final ActionResetingPool<MaGdxActionColorTo> pool = new ActionResetingPool<MaGdxActionColorTo>(4, 100) {
                @Override protected MaGdxActionColorTo newObject () {
                        return new MaGdxActionColorTo();
                }
        };

        protected float r = 1.0f;
        protected float g = 1.0f;
        protected float b = 1.0f;
        protected float a = 1.0f;
       
        protected float startR = 1.0f;
        protected float startG = 1.0f;
        protected float startB = 1.0f;
        protected float startA = 1.0f;
       
        protected float deltaR = 1.0f;
        protected float deltaG = 1.0f;
        protected float deltaB = 1.0f;
        protected float deltaA = 1.0f;

        public static MaGdxActionColorTo $ (float r, float g, float b, float a, float duration) {
              MaGdxActionColorTo action = pool.obtain();
                action.duration = duration;
                action.invDuration = 1 / duration;
                action.r = Math.min(Math.max(r, 0.0f), 1.0f);
                action.g = Math.min(Math.max(g, 0.0f), 1.0f);
                action.b = Math.min(Math.max(b, 0.0f), 1.0f);
                action.a = Math.min(Math.max(a, 0.0f), 1.0f);       
                return action;
        }

        @Override public void setTarget (Actor actor) {
                this.target = actor;
                this.taken = 0;
                this.done = false;
                this.startR = actor.color.r;
                this.startG = actor.color.g;
                this.startB = actor.color.b;
                this.startA = actor.color.a;
                this.deltaR = r - actor.color.r;
                this.deltaG = g - actor.color.g;
                this.deltaB = b - actor.color.b;
                this.deltaA = a - actor.color.a;
        }

        @Override public void act (float delta) {
           float alpha = createInterpolatedAlpha(delta);
           //Actor t = getTarget();
            if (done) {
                    target.color.a = a;
                    target.color.r = r;
                    target.color.g = g;
                    target.color.b = b;
            } else {
                    float val = startA + deltaA * alpha;
                    target.color.a = Math.min(Math.max(val, 0.0f), 1.0f);
                   
                    val = startR + deltaR * alpha;
                    target.color.r = Math.min(Math.max(val, 0.0f), 1.0f);
                   
                    val = startG + deltaG * alpha;
                    target.color.g = Math.min(Math.max(val, 0.0f), 1.0f);
                   
                    val =startB + deltaB * alpha;
                    target.color.b = Math.min(Math.max(val, 0.0f), 1.0f);
            }
        }

        /*@Override public boolean isDone () {
                return taken > duration;
        }*/

        @Override public void finish () {
           super.finish();
            pool.free(this);
        }

        @Override public Action copy () {
               MaGdxActionColorTo colorTo = $(r, g, b, a, duration);
              if (interpolator != null) colorTo.setInterpolator(interpolator.copy());
                return colorTo;
        }

      /*@Override
      public Actor getTarget() {
         return actor;
      }*/
}


edit: could Color allow values greater than 1.0 for r, g and b? We could then brighten stuff that way. :)
Magnesus
 
Posts: 1709
Joined: Sun Sep 25, 2011 3:50 pm

Return to Libgdx Contributions

Who is online

Users browsing this forum: No registered users and 1 guest