Home | History | Annotate | Download | only in actions
      1 /*******************************************************************************
      2  * Copyright 2011 See AUTHORS file.
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *   http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  ******************************************************************************/
     16 
     17 package com.badlogic.gdx.scenes.scene2d.actions;
     18 
     19 import com.badlogic.gdx.scenes.scene2d.Action;
     20 import com.badlogic.gdx.scenes.scene2d.Actor;
     21 import com.badlogic.gdx.utils.Array;
     22 
     23 /** Executes an action only after all other actions on the actor at the time this action's target was set have finished.
     24  * @author Nathan Sweet */
     25 public class AfterAction extends DelegateAction {
     26 	private Array<Action> waitForActions = new Array(false, 4);
     27 
     28 	public void setTarget (Actor target) {
     29 		if (target != null) waitForActions.addAll(target.getActions());
     30 		super.setTarget(target);
     31 	}
     32 
     33 	public void restart () {
     34 		super.restart();
     35 		waitForActions.clear();
     36 	}
     37 
     38 	protected boolean delegate (float delta) {
     39 		Array<Action> currentActions = target.getActions();
     40 		if (currentActions.size == 1) waitForActions.clear();
     41 		for (int i = waitForActions.size - 1; i >= 0; i--) {
     42 			Action action = waitForActions.get(i);
     43 			int index = currentActions.indexOf(action, true);
     44 			if (index == -1) waitForActions.removeIndex(i);
     45 		}
     46 		if (waitForActions.size > 0) return false;
     47 		return action.act(delta);
     48 	}
     49 }
     50