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

New methods play & playReverse of Animation class #1287

Open
wants to merge 5 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
17 changes: 17 additions & 0 deletions fxgl-core/src/main/kotlin/com/almasb/fxgl/animation/Animation.kt
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,23 @@ abstract class Animation<T>(
}
}

fun playReverse(){
if (!isAnimating) {
isReverse = true
play()
}
}

/**
* Plays Animation from current position in the direction indicated by rate.
*/
fun play(){
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm just wondering when this would be called. The current position in the animation can be controlled with pause/resume. isAnimating is always true when the animation is active. So the only way this is actually run is when the animation is fully stopped, meaning it matches the implementation of start. Does that make sense?

Copy link
Contributor Author

@chengenzhao chengenzhao Nov 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's say there is an AnimationChannel of wall clock starts from 0 to 12 clock
in the app, we want the clock animation starts from current time
so it would be nice to have

setTimeTo(currentTime)
play()

and later we may have

play(currentTime)//starts from currentTime
//or to more specific
playFrom(currentTime)

to replace the pure start() method
and later we may need playTo(specificPauseTime) method
and may have fluent method like

animation.setTimeTo(currentTime)
                .playTo(specificTime) //when it comes to specific time, it will pause
                .playReverseTo(anotherTimeEarlier) //like resume to another time
                .play(); //continue playing the rest animation
//or
animation.play(from: currentTime, to: specificTime) //play from current time to specific time, when it comes to specific time, animation will pause
                .playReverseTo(anotherTimeEarlier) //like resume to another time
                .play(); //continue playing the rest animation

if (!isAnimating) {
isAnimating = true
onProgress(animatedValue.getValue(if (isReverse) 1.0 else 0.0))
}
}

fun stop() {
if (isAnimating) {
isAnimating = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class AnimatedTexture(defaultChannel: AnimationChannel) : Texture(defaultChannel
private var currentFrame = 0

private lateinit var animation: Animation<Int>
private var animationList = mutableListOf<Animation<Int>>();

var animationChannel: AnimationChannel = defaultChannel
private set(value) {
Expand Down Expand Up @@ -65,8 +66,14 @@ class AnimatedTexture(defaultChannel: AnimationChannel) : Texture(defaultChannel
* The animation will stop on the last frame.
*/
fun playAnimationChannel(channel: AnimationChannel, startFrame: Int) {
playAnimationChannel(channel)
animationChannel = channel

jumpTo(startFrame)

animation.play()
}

fun jumpTo(startFrame: Int){
currentFrame = startFrame
animation.setTimeTo(currentFrame.toDouble() * animationChannel.frameDuration)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@
import javafx.animation.Interpolator;
import javafx.scene.control.Button;
import javafx.scene.control.Spinner;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.util.Duration;

import static com.almasb.fxgl.dsl.FXGL.*;
Expand Down Expand Up @@ -52,15 +54,26 @@ protected void initGame() {

@Override
protected void initUI() {
var frameSpinner = new Spinner<Integer>(0, 23, 0);
frameSpinner.setPrefWidth(100);
var startFrameSpinner = new Spinner<Integer>(0, 23, 0);
var stopFrameSpinner = new Spinner<Integer>(0, 23, 23);

startFrameSpinner.setPrefWidth(60);
stopFrameSpinner.prefWidthProperty().bind(startFrameSpinner.prefWidthProperty());

var btn = new Button("Play from frame");
btn.setOnAction(e -> {
spawnRobotForFrame(900, 700, frameSpinner.getValue());
spawnRobotForFrame(900, 700, startFrameSpinner.getValue());
});

var vbox = new VBox(10, frameSpinner, btn);
var from = new Text("from ");
var to = new Text(" to ");
from.setFont(Font.font(18));
from.setFill(Color.WHITE);
to.fontProperty().bind(from.fontProperty());
to.fillProperty().bind(from.fillProperty());

var hbox = new HBox(from,startFrameSpinner,to,stopFrameSpinner);
var vbox = new VBox(10, hbox, btn);

addUINode(vbox, 1230, 820);
}
Expand Down