Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

com.jme3.anim.tween.action.BaseAction: basic javadoc #2020

Merged
merged 12 commits into from
Jan 29, 2024
Merged
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 57 additions & 15 deletions jme3-core/src/main/java/com/jme3/anim/tween/action/BaseAction.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2009-2022 jMonkeyEngine
* Copyright (c) 2009-2023 jMonkeyEngine
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -35,14 +35,39 @@
import com.jme3.anim.tween.ContainsTweens;
import com.jme3.anim.tween.Tween;
import com.jme3.util.SafeArrayList;

import java.util.List;

/**
* A simple implementation for the abstract class {@link Action} to provide a wrapper for a {@link Tween}.
* Internally, it is used as a helper class for {@link Action} to extract and gather actions from a tween and interpolate it.
* <p>
* An example showing two clip actions running in parallel at 2x of their ordinary speed
* by the help of BaseAction on a new Animation Layer :
* <pre class="prettyprint">
* //create a base action from a tween.
* final BaseAction action = new BaseAction(Tweens.parallel(clipAction0, clipAction1));
* //set the action properties - utilized within the #{@link Action} class.
* baseAction.setSpeed(2f);
* //register the action as an observer to the animComposer control.
* animComposer.addAction("basicAction", action);
* //make a new Layer for a basic armature mask
* animComposer.makeLayer(ActionState.class.getSimpleName(), new ArmatureMask());
* //run the action within this layer
* animComposer.setCurrentAction("basicAction", ActionState.class.getSimpleName());
* </pre>
* </p>
* Created by Nehon.
*/
public class BaseAction extends Action {

final private Tween tween;
private boolean maskPropagationEnabled = true;

/**
* Instantiates an action from a tween.
*
* @param tween a tween to extract the actions from (not null).
*/
public BaseAction(Tween tween) {
this.tween = tween;
setLength(tween.getLength());
Expand All @@ -52,33 +77,33 @@ public BaseAction(Tween tween) {
subActions.toArray(actions);
}

private void gatherActions(Tween tween, List<Action> subActions) {
if (tween instanceof Action) {
subActions.add((Action) tween);
} else if (tween instanceof ContainsTweens) {
Tween[] tweens = ((ContainsTweens) tween).getTweens();
for (Tween t : tweens) {
gatherActions(t, subActions);
}
}
}

/**
* @return true if mask propagation to child actions is enabled else returns false
* Tests whether the animation mask is applied to the wrapped actions {@link BaseAction#actions}.
*
* @return true if mask propagation to child actions is enabled else returns false.
*/
public boolean isMaskPropagationEnabled() {
return maskPropagationEnabled;
}

/**
* Determines whether to apply the animation mask to the wrapped or child actions {@link BaseAction#actions}.
*
* @param maskPropagationEnabled If true, then mask set by AnimLayer will be
* forwarded to all child actions (Default=true)
* forwarded to all child actions (Default=true).
*/
public void setMaskPropagationEnabled(boolean maskPropagationEnabled) {
this.maskPropagationEnabled = maskPropagationEnabled;
}

/**
* Internal use only.
*
* Dispatched from {@link com.jme3.anim.AnimLayer#update(float)}. Applies an animation mask to this action,
* if the {@link BaseAction#maskPropagationEnabled} is enabled, the animation mask is propagated to the child actions (or wrapped actions).
*
pavly-gerges marked this conversation as resolved.
Show resolved Hide resolved
* @param mask an animation mask to be applied to this action.
*/
@Override
public void setMask(AnimationMask mask) {
super.setMask(mask);
Expand All @@ -94,4 +119,21 @@ public void setMask(AnimationMask mask) {
public boolean interpolate(double t) {
return tween.interpolate(t);
}

/**
* Extracts the actions from a tween into a list.
*
* @param tween the tween to extract the actions from (not null).
* @param subActions a collection to gather the extracted actions (not null).
*/
private void gatherActions(Tween tween, List<Action> subActions) {
if (tween instanceof Action) {
subActions.add((Action) tween);
} else if (tween instanceof ContainsTweens) {
Tween[] tweens = ((ContainsTweens) tween).getTweens();
for (Tween t : tweens) {
gatherActions(t, subActions);
}
}
}
}