Simple actions - show/hide and swap region

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

Simple actions - show/hide and swap region

Postby Magnesus » Sun Oct 23, 2011 9:06 pm

This is based on Delay action source code, it's rough and needs some polishing (mostly removing things that are not needed), renaming etc. but it works, and maybe someone will find it useful.

1) ActionVisible - allows showing and hiding actors

Code: Select all
import com.badlogic.gdx.scenes.scene2d.Action;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.actions.ActionResetingPool;
import com.badlogic.gdx.scenes.scene2d.actors.Image;

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

        protected float taken;
        protected float duration;
        protected Actor actor;
        protected boolean listenerFired = false;
        protected boolean isVisible = false;

        public static MaGdxActionVisible $ (boolean isVisible, float duration) {
              MaGdxActionVisible delay = pool.obtain();
                delay.duration = duration;
                delay.isVisible = isVisible;
                return delay;
        }

        @Override public void reset () {
                listenerFired = false;
                super.reset();
        }

        @Override public void setTarget (Actor actor) {
                this.actor = actor;
                this.taken = 0;
        }

        @Override public void act (float delta) {
                taken += delta;
                if (taken > duration) {
                        if (!listenerFired && listener != null) {
                                listener.completed(this);
                                listenerFired = true;
                        }
                        Image i = (Image)getTarget();
                      i.visible = isVisible;
                }
        }

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

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

        @Override public Action copy () {
                return $(isVisible, duration);
        }

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



Usage example:

actor.action(Sequence.$(MaGdxActionVisible.$(true,0), MoveTo.$(10,10,100), MaGdxActionVisible.$(false,10), MaGdxActionVisible.$(true,10)));

2) ActionSwap - allows swaping region (could be used for animation - I use it to change the tile in my game to reversed image after rotating). It requires the Actor to be Image (so it's ugly - it will crash otherwise, sorry, needs fixing!).

Code: Select all
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.Action;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.actions.ActionResetingPool;
import com.badlogic.gdx.scenes.scene2d.actors.Image;

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

        protected TextureRegion newRegion;
        protected float taken;
        protected float duration;
        protected Actor actor;
        protected boolean listenerFired = false;

        public static MaGdxActionSwap $ (TextureRegion newRegion, float duration) {
              MaGdxActionSwap delay = pool.obtain();
                delay.duration = duration;
                delay.newRegion = newRegion;
                return delay;
        }

        @Override public void reset () {
                listenerFired = false;
                super.reset();
        }

        @Override public void setTarget (Actor actor) {
                this.actor = actor;
                this.taken = 0;
        }

        @Override public void act (float delta) {
                taken += delta;
                if (taken > duration) {
                        if (!listenerFired && listener != null) {
                                listener.completed(this);
                                listenerFired = true;
                        }
                        Image i = (Image)getTarget();
                      i.region = newRegion;
                }
        }

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

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

        @Override public Action copy () {
                return $(newRegion, duration);
        }

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


Usage example:

actor.action(Sequence.$(MoveTo.$(10,10,100), MaGdxActionSwap.$(newregion, 100));

The second argument - duration - is a delay before the change occurs.

PS. The licence is public domain (so do whatever you want with it).
Last edited by Magnesus on Thu Dec 08, 2011 8:03 pm, edited 1 time in total.
Magnesus
 
Posts: 1709
Joined: Sun Sep 25, 2011 3:50 pm

Re: Simple actions - show/hide and swap region

Postby radioking » Mon Oct 24, 2011 11:08 am

Hi Magnesus,

You might consider adding this to community wiki.
That is a good place to put and describe your howtos and code snippets.
Please "give sth. back" to community and contribute your knowledge to our libgdx-users project community wiki:
http://code.google.com/p/libgdx-users/
Please see Mario's note on User Wiki 2.0: http://www.badlogicgames.com/wordpress/?p=2411
radioking
 
Posts: 284
Joined: Wed Aug 03, 2011 10:28 am


Return to Libgdx Contributions

Who is online

Users browsing this forum: No registered users and 1 guest