Merge pull request #3670 from JakubValtar/fx-framerate

FX - main drawing loop
This commit is contained in:
Ben Fry
2015-08-19 16:07:02 -04:00

View File

@@ -33,7 +33,9 @@ package processing.javafx;
import java.util.HashMap;
import java.util.Map;
import javafx.animation.AnimationTimer;
import javafx.animation.Animation;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.beans.value.ChangeListener;
@@ -50,6 +52,7 @@ import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;
import javafx.util.Duration;
import processing.core.*;
@@ -60,13 +63,31 @@ public class PSurfaceFX implements PSurface {
Stage stage;
Canvas canvas;
AnimationTimer timer;
final Animation animation;
public PSurfaceFX(PGraphicsFX2D graphics) {
fx = graphics;
canvas = new ResizableCanvas();
fx.context = canvas.getGraphicsContext2D();
{ // set up main drawing loop
KeyFrame keyFrame = new KeyFrame(Duration.millis(1000), e -> {
sketch.handleDraw();
if (sketch.exitCalled()) {
//sketch.exitActual(); // just calls System.exit()
Platform.exit(); // version for safe JavaFX shutdown
}
});
animation = new Timeline(keyFrame);
animation.setCycleCount(Timeline.INDEFINITE);
// key frame has duration of 1 second, so the rate of the animation
// should be set to frames per second
// setting rate to negative so that event fires at the start of
// the key frame and first frame is drawn immediately
animation.setRate(-60);
}
}
@@ -437,8 +458,9 @@ public class PSurfaceFX implements PSurface {
public void setFrameRate(float fps) {
// TODO Auto-generated method stub
// setting rate to negative so that event fires at the start of
// the key frame and first frame is drawn immediately
if (fps > 0) animation.setRate(-fps);
}
@@ -473,44 +495,28 @@ public class PSurfaceFX implements PSurface {
public void startThread() {
if (timer == null) {
timer = new AnimationTimer() {
@Override
public void handle(long now) {
//System.out.println("handle(" + now + ") calling handleDraw()");
sketch.handleDraw();
if (sketch.exitCalled()) {
//sketch.exitActual(); // just calls System.exit()
Platform.exit(); // version for safe JavaFX shutdown
}
}
};
timer.start();
}
animation.play();
}
public void pauseThread() {
// TODO Auto-generated method stub
animation.pause();
}
public void resumeThread() {
// TODO Auto-generated method stub
animation.play();
}
public boolean stopThread() {
// TODO Auto-generated method stub
return false;
animation.stop();
return true;
}
public boolean isStopped() {
// TODO Auto-generated method stub
return false;
return animation.getStatus() == Animation.Status.STOPPED;
}