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.graphics.Color;
     20 import com.badlogic.gdx.math.Interpolation;
     21 import com.badlogic.gdx.scenes.scene2d.Action;
     22 import com.badlogic.gdx.scenes.scene2d.Actor;
     23 import com.badlogic.gdx.scenes.scene2d.EventListener;
     24 import com.badlogic.gdx.scenes.scene2d.Touchable;
     25 import com.badlogic.gdx.utils.Pool;
     26 import com.badlogic.gdx.utils.Pools;
     27 
     28 /** Static convenience methods for using pooled actions, intended for static import.
     29  * @author Nathan Sweet */
     30 public class Actions {
     31 	/** Returns a new or pooled action of the specified type. */
     32 	static public <T extends Action> T action (Class<T> type) {
     33 		Pool<T> pool = Pools.get(type);
     34 		T action = pool.obtain();
     35 		action.setPool(pool);
     36 		return action;
     37 	}
     38 
     39 	static public AddAction addAction (Action action) {
     40 		AddAction addAction = action(AddAction.class);
     41 		addAction.setAction(action);
     42 		return addAction;
     43 	}
     44 
     45 	static public AddAction addAction (Action action, Actor targetActor) {
     46 		AddAction addAction = action(AddAction.class);
     47 		addAction.setTarget(targetActor);
     48 		addAction.setAction(action);
     49 		return addAction;
     50 	}
     51 
     52 	static public RemoveAction removeAction (Action action) {
     53 		RemoveAction removeAction = action(RemoveAction.class);
     54 		removeAction.setAction(action);
     55 		return removeAction;
     56 	}
     57 
     58 	static public RemoveAction removeAction (Action action, Actor targetActor) {
     59 		RemoveAction removeAction = action(RemoveAction.class);
     60 		removeAction.setTarget(targetActor);
     61 		removeAction.setAction(action);
     62 		return removeAction;
     63 	}
     64 
     65 	/** Moves the actor instantly. */
     66 	static public MoveToAction moveTo (float x, float y) {
     67 		return moveTo(x, y, 0, null);
     68 	}
     69 
     70 	static public MoveToAction moveTo (float x, float y, float duration) {
     71 		return moveTo(x, y, duration, null);
     72 	}
     73 
     74 	static public MoveToAction moveTo (float x, float y, float duration, Interpolation interpolation) {
     75 		MoveToAction action = action(MoveToAction.class);
     76 		action.setPosition(x, y);
     77 		action.setDuration(duration);
     78 		action.setInterpolation(interpolation);
     79 		return action;
     80 	}
     81 
     82 	static public MoveToAction moveToAligned (float x, float y, int alignment) {
     83 		return moveToAligned(x, y, alignment, 0, null);
     84 	}
     85 
     86 	static public MoveToAction moveToAligned (float x, float y, int alignment, float duration) {
     87 		return moveToAligned(x, y, alignment, duration, null);
     88 	}
     89 
     90 	static public MoveToAction moveToAligned (float x, float y, int alignment, float duration, Interpolation interpolation) {
     91 		MoveToAction action = action(MoveToAction.class);
     92 		action.setPosition(x, y, alignment);
     93 		action.setDuration(duration);
     94 		action.setInterpolation(interpolation);
     95 		return action;
     96 	}
     97 
     98 	/** Moves the actor instantly. */
     99 	static public MoveByAction moveBy (float amountX, float amountY) {
    100 		return moveBy(amountX, amountY, 0, null);
    101 	}
    102 
    103 	static public MoveByAction moveBy (float amountX, float amountY, float duration) {
    104 		return moveBy(amountX, amountY, duration, null);
    105 	}
    106 
    107 	static public MoveByAction moveBy (float amountX, float amountY, float duration, Interpolation interpolation) {
    108 		MoveByAction action = action(MoveByAction.class);
    109 		action.setAmount(amountX, amountY);
    110 		action.setDuration(duration);
    111 		action.setInterpolation(interpolation);
    112 		return action;
    113 	}
    114 
    115 	/** Sizes the actor instantly. */
    116 	static public SizeToAction sizeTo (float x, float y) {
    117 		return sizeTo(x, y, 0, null);
    118 	}
    119 
    120 	static public SizeToAction sizeTo (float x, float y, float duration) {
    121 		return sizeTo(x, y, duration, null);
    122 	}
    123 
    124 	static public SizeToAction sizeTo (float x, float y, float duration, Interpolation interpolation) {
    125 		SizeToAction action = action(SizeToAction.class);
    126 		action.setSize(x, y);
    127 		action.setDuration(duration);
    128 		action.setInterpolation(interpolation);
    129 		return action;
    130 	}
    131 
    132 	/** Sizes the actor instantly. */
    133 	static public SizeByAction sizeBy (float amountX, float amountY) {
    134 		return sizeBy(amountX, amountY, 0, null);
    135 	}
    136 
    137 	static public SizeByAction sizeBy (float amountX, float amountY, float duration) {
    138 		return sizeBy(amountX, amountY, duration, null);
    139 	}
    140 
    141 	static public SizeByAction sizeBy (float amountX, float amountY, float duration, Interpolation interpolation) {
    142 		SizeByAction action = action(SizeByAction.class);
    143 		action.setAmount(amountX, amountY);
    144 		action.setDuration(duration);
    145 		action.setInterpolation(interpolation);
    146 		return action;
    147 	}
    148 
    149 	/** Scales the actor instantly. */
    150 	static public ScaleToAction scaleTo (float x, float y) {
    151 		return scaleTo(x, y, 0, null);
    152 	}
    153 
    154 	static public ScaleToAction scaleTo (float x, float y, float duration) {
    155 		return scaleTo(x, y, duration, null);
    156 	}
    157 
    158 	static public ScaleToAction scaleTo (float x, float y, float duration, Interpolation interpolation) {
    159 		ScaleToAction action = action(ScaleToAction.class);
    160 		action.setScale(x, y);
    161 		action.setDuration(duration);
    162 		action.setInterpolation(interpolation);
    163 		return action;
    164 	}
    165 
    166 	/** Scales the actor instantly. */
    167 	static public ScaleByAction scaleBy (float amountX, float amountY) {
    168 		return scaleBy(amountX, amountY, 0, null);
    169 	}
    170 
    171 	static public ScaleByAction scaleBy (float amountX, float amountY, float duration) {
    172 		return scaleBy(amountX, amountY, duration, null);
    173 	}
    174 
    175 	static public ScaleByAction scaleBy (float amountX, float amountY, float duration, Interpolation interpolation) {
    176 		ScaleByAction action = action(ScaleByAction.class);
    177 		action.setAmount(amountX, amountY);
    178 		action.setDuration(duration);
    179 		action.setInterpolation(interpolation);
    180 		return action;
    181 	}
    182 
    183 	/** Rotates the actor instantly. */
    184 	static public RotateToAction rotateTo (float rotation) {
    185 		return rotateTo(rotation, 0, null);
    186 	}
    187 
    188 	static public RotateToAction rotateTo (float rotation, float duration) {
    189 		return rotateTo(rotation, duration, null);
    190 	}
    191 
    192 	static public RotateToAction rotateTo (float rotation, float duration, Interpolation interpolation) {
    193 		RotateToAction action = action(RotateToAction.class);
    194 		action.setRotation(rotation);
    195 		action.setDuration(duration);
    196 		action.setInterpolation(interpolation);
    197 		return action;
    198 	}
    199 
    200 	/** Rotates the actor instantly. */
    201 	static public RotateByAction rotateBy (float rotationAmount) {
    202 		return rotateBy(rotationAmount, 0, null);
    203 	}
    204 
    205 	static public RotateByAction rotateBy (float rotationAmount, float duration) {
    206 		return rotateBy(rotationAmount, duration, null);
    207 	}
    208 
    209 	static public RotateByAction rotateBy (float rotationAmount, float duration, Interpolation interpolation) {
    210 		RotateByAction action = action(RotateByAction.class);
    211 		action.setAmount(rotationAmount);
    212 		action.setDuration(duration);
    213 		action.setInterpolation(interpolation);
    214 		return action;
    215 	}
    216 
    217 	/** Sets the actor's color instantly. */
    218 	static public ColorAction color (Color color) {
    219 		return color(color, 0, null);
    220 	}
    221 
    222 	/** Transitions from the color at the time this action starts to the specified color. */
    223 	static public ColorAction color (Color color, float duration) {
    224 		return color(color, duration, null);
    225 	}
    226 
    227 	/** Transitions from the color at the time this action starts to the specified color. */
    228 	static public ColorAction color (Color color, float duration, Interpolation interpolation) {
    229 		ColorAction action = action(ColorAction.class);
    230 		action.setEndColor(color);
    231 		action.setDuration(duration);
    232 		action.setInterpolation(interpolation);
    233 		return action;
    234 	}
    235 
    236 	/** Sets the actor's alpha instantly. */
    237 	static public AlphaAction alpha (float a) {
    238 		return alpha(a, 0, null);
    239 	}
    240 
    241 	/** Transitions from the alpha at the time this action starts to the specified alpha. */
    242 	static public AlphaAction alpha (float a, float duration) {
    243 		return alpha(a, duration, null);
    244 	}
    245 
    246 	/** Transitions from the alpha at the time this action starts to the specified alpha. */
    247 	static public AlphaAction alpha (float a, float duration, Interpolation interpolation) {
    248 		AlphaAction action = action(AlphaAction.class);
    249 		action.setAlpha(a);
    250 		action.setDuration(duration);
    251 		action.setInterpolation(interpolation);
    252 		return action;
    253 	}
    254 
    255 	/** Transitions from the alpha at the time this action starts to an alpha of 0. */
    256 	static public AlphaAction fadeOut (float duration) {
    257 		return alpha(0, duration, null);
    258 	}
    259 
    260 	/** Transitions from the alpha at the time this action starts to an alpha of 0. */
    261 	static public AlphaAction fadeOut (float duration, Interpolation interpolation) {
    262 		AlphaAction action = action(AlphaAction.class);
    263 		action.setAlpha(0);
    264 		action.setDuration(duration);
    265 		action.setInterpolation(interpolation);
    266 		return action;
    267 	}
    268 
    269 	/** Transitions from the alpha at the time this action starts to an alpha of 1. */
    270 	static public AlphaAction fadeIn (float duration) {
    271 		return alpha(1, duration, null);
    272 	}
    273 
    274 	/** Transitions from the alpha at the time this action starts to an alpha of 1. */
    275 	static public AlphaAction fadeIn (float duration, Interpolation interpolation) {
    276 		AlphaAction action = action(AlphaAction.class);
    277 		action.setAlpha(1);
    278 		action.setDuration(duration);
    279 		action.setInterpolation(interpolation);
    280 		return action;
    281 	}
    282 
    283 	static public VisibleAction show () {
    284 		return visible(true);
    285 	}
    286 
    287 	static public VisibleAction hide () {
    288 		return visible(false);
    289 	}
    290 
    291 	static public VisibleAction visible (boolean visible) {
    292 		VisibleAction action = action(VisibleAction.class);
    293 		action.setVisible(visible);
    294 		return action;
    295 	}
    296 
    297 	static public TouchableAction touchable (Touchable touchable) {
    298 		TouchableAction action = action(TouchableAction.class);
    299 		action.setTouchable(touchable);
    300 		return action;
    301 	}
    302 
    303 	static public RemoveActorAction removeActor () {
    304 		return action(RemoveActorAction.class);
    305 	}
    306 
    307 	static public RemoveActorAction removeActor (Actor removeActor) {
    308 		RemoveActorAction action = action(RemoveActorAction.class);
    309 		action.setTarget(removeActor);
    310 		return action;
    311 	}
    312 
    313 	static public DelayAction delay (float duration) {
    314 		DelayAction action = action(DelayAction.class);
    315 		action.setDuration(duration);
    316 		return action;
    317 	}
    318 
    319 	static public DelayAction delay (float duration, Action delayedAction) {
    320 		DelayAction action = action(DelayAction.class);
    321 		action.setDuration(duration);
    322 		action.setAction(delayedAction);
    323 		return action;
    324 	}
    325 
    326 	static public TimeScaleAction timeScale (float scale, Action scaledAction) {
    327 		TimeScaleAction action = action(TimeScaleAction.class);
    328 		action.setScale(scale);
    329 		action.setAction(scaledAction);
    330 		return action;
    331 	}
    332 
    333 	static public SequenceAction sequence (Action action1) {
    334 		SequenceAction action = action(SequenceAction.class);
    335 		action.addAction(action1);
    336 		return action;
    337 	}
    338 
    339 	static public SequenceAction sequence (Action action1, Action action2) {
    340 		SequenceAction action = action(SequenceAction.class);
    341 		action.addAction(action1);
    342 		action.addAction(action2);
    343 		return action;
    344 	}
    345 
    346 	static public SequenceAction sequence (Action action1, Action action2, Action action3) {
    347 		SequenceAction action = action(SequenceAction.class);
    348 		action.addAction(action1);
    349 		action.addAction(action2);
    350 		action.addAction(action3);
    351 		return action;
    352 	}
    353 
    354 	static public SequenceAction sequence (Action action1, Action action2, Action action3, Action action4) {
    355 		SequenceAction action = action(SequenceAction.class);
    356 		action.addAction(action1);
    357 		action.addAction(action2);
    358 		action.addAction(action3);
    359 		action.addAction(action4);
    360 		return action;
    361 	}
    362 
    363 	static public SequenceAction sequence (Action action1, Action action2, Action action3, Action action4, Action action5) {
    364 		SequenceAction action = action(SequenceAction.class);
    365 		action.addAction(action1);
    366 		action.addAction(action2);
    367 		action.addAction(action3);
    368 		action.addAction(action4);
    369 		action.addAction(action5);
    370 		return action;
    371 	}
    372 
    373 	static public SequenceAction sequence (Action... actions) {
    374 		SequenceAction action = action(SequenceAction.class);
    375 		for (int i = 0, n = actions.length; i < n; i++)
    376 			action.addAction(actions[i]);
    377 		return action;
    378 	}
    379 
    380 	static public SequenceAction sequence () {
    381 		return action(SequenceAction.class);
    382 	}
    383 
    384 	static public ParallelAction parallel (Action action1) {
    385 		ParallelAction action = action(ParallelAction.class);
    386 		action.addAction(action1);
    387 		return action;
    388 	}
    389 
    390 	static public ParallelAction parallel (Action action1, Action action2) {
    391 		ParallelAction action = action(ParallelAction.class);
    392 		action.addAction(action1);
    393 		action.addAction(action2);
    394 		return action;
    395 	}
    396 
    397 	static public ParallelAction parallel (Action action1, Action action2, Action action3) {
    398 		ParallelAction action = action(ParallelAction.class);
    399 		action.addAction(action1);
    400 		action.addAction(action2);
    401 		action.addAction(action3);
    402 		return action;
    403 	}
    404 
    405 	static public ParallelAction parallel (Action action1, Action action2, Action action3, Action action4) {
    406 		ParallelAction action = action(ParallelAction.class);
    407 		action.addAction(action1);
    408 		action.addAction(action2);
    409 		action.addAction(action3);
    410 		action.addAction(action4);
    411 		return action;
    412 	}
    413 
    414 	static public ParallelAction parallel (Action action1, Action action2, Action action3, Action action4, Action action5) {
    415 		ParallelAction action = action(ParallelAction.class);
    416 		action.addAction(action1);
    417 		action.addAction(action2);
    418 		action.addAction(action3);
    419 		action.addAction(action4);
    420 		action.addAction(action5);
    421 		return action;
    422 	}
    423 
    424 	static public ParallelAction parallel (Action... actions) {
    425 		ParallelAction action = action(ParallelAction.class);
    426 		for (int i = 0, n = actions.length; i < n; i++)
    427 			action.addAction(actions[i]);
    428 		return action;
    429 	}
    430 
    431 	static public ParallelAction parallel () {
    432 		return action(ParallelAction.class);
    433 	}
    434 
    435 	static public RepeatAction repeat (int count, Action repeatedAction) {
    436 		RepeatAction action = action(RepeatAction.class);
    437 		action.setCount(count);
    438 		action.setAction(repeatedAction);
    439 		return action;
    440 	}
    441 
    442 	static public RepeatAction forever (Action repeatedAction) {
    443 		RepeatAction action = action(RepeatAction.class);
    444 		action.setCount(RepeatAction.FOREVER);
    445 		action.setAction(repeatedAction);
    446 		return action;
    447 	}
    448 
    449 	static public RunnableAction run (Runnable runnable) {
    450 		RunnableAction action = action(RunnableAction.class);
    451 		action.setRunnable(runnable);
    452 		return action;
    453 	}
    454 
    455 	static public LayoutAction layout (boolean enabled) {
    456 		LayoutAction action = action(LayoutAction.class);
    457 		action.setLayoutEnabled(enabled);
    458 		return action;
    459 	}
    460 
    461 	static public AfterAction after (Action action) {
    462 		AfterAction afterAction = action(AfterAction.class);
    463 		afterAction.setAction(action);
    464 		return afterAction;
    465 	}
    466 
    467 	static public AddListenerAction addListener (EventListener listener, boolean capture) {
    468 		AddListenerAction addAction = action(AddListenerAction.class);
    469 		addAction.setListener(listener);
    470 		addAction.setCapture(capture);
    471 		return addAction;
    472 	}
    473 
    474 	static public AddListenerAction addListener (EventListener listener, boolean capture, Actor targetActor) {
    475 		AddListenerAction addAction = action(AddListenerAction.class);
    476 		addAction.setTarget(targetActor);
    477 		addAction.setListener(listener);
    478 		addAction.setCapture(capture);
    479 		return addAction;
    480 	}
    481 
    482 	static public RemoveListenerAction removeListener (EventListener listener, boolean capture) {
    483 		RemoveListenerAction addAction = action(RemoveListenerAction.class);
    484 		addAction.setListener(listener);
    485 		addAction.setCapture(capture);
    486 		return addAction;
    487 	}
    488 
    489 	static public RemoveListenerAction removeListener (EventListener listener, boolean capture, Actor targetActor) {
    490 		RemoveListenerAction addAction = action(RemoveListenerAction.class);
    491 		addAction.setTarget(targetActor);
    492 		addAction.setListener(listener);
    493 		addAction.setCapture(capture);
    494 		return addAction;
    495 	}
    496 }
    497