diff --git a/src/main/java/fr/minuskube/editor/scene/Scene.java b/src/main/java/fr/minuskube/editor/scene/Scene.java index 10b6215..09342f8 100644 --- a/src/main/java/fr/minuskube/editor/scene/Scene.java +++ b/src/main/java/fr/minuskube/editor/scene/Scene.java @@ -1,8 +1,11 @@ package fr.minuskube.editor.scene; import fr.minuskube.editor.control.DrawableCanvas; +import fr.minuskube.editor.scene.object.SceneAnimation; +import fr.minuskube.editor.scene.object.SceneAnimationFrame; import fr.minuskube.editor.scene.object.SceneImage; import fr.minuskube.editor.scene.object.SceneObject; +import javafx.animation.AnimationTimer; import javafx.collections.FXCollections; import javafx.collections.ListChangeListener; import javafx.collections.ObservableList; @@ -54,6 +57,20 @@ public Scene(Stage stage, Canvas canvas, int width, int height) { } public void init() { + AnimationTimer updateTimer = new AnimationTimer() { + @Override + public void handle(long now) { + canvas.redraw(); + + try { + Thread.sleep((long) ((1 / 60.0) * 1000)); + } catch(InterruptedException e) { + e.printStackTrace(); + } + } + }; + updateTimer.start(); + objects.addListener((ListChangeListener) change -> { while(change.next()) { for(SceneObject added : change.getAddedSubList()) { @@ -181,6 +198,34 @@ else if(event.isControlDown() && event.getCode() == KeyCode.V) { }); resetPosition(); + + // XXX: Some animation tests + try { + SceneAnimation anim = new SceneAnimation(); + + int maxWidth = 0; + int maxHeight = 0; + + for(int i = 1; i <= 12; i++) { + String num = i < 10 ? ("0" + i) : String.valueOf(i); + + SceneAnimationFrame frame = new SceneAnimationFrame(new File("D:\\SVN\\2017-2018\\Sections\\PrepaJVA" + + "\\imontagne\\graph\\Cuphead\\Cagney Carnation\\Death_Frames\\Death_" + num + ".png")); + frame.setDuration(1 / 24f); + + maxWidth = Math.max(maxWidth, frame.getWidth()); + maxHeight = Math.max(maxHeight, frame.getHeight()); + + anim.getFrames().add(frame); + } + + anim.setWidth(maxWidth); + anim.setHeight(maxHeight); + + this.objects.add(anim); + } catch(IOException e) { + e.printStackTrace(); + } } public void resetPosition() { @@ -261,8 +306,6 @@ public void init(Scene scene) { context.setTransform(affine); context.save(); - System.out.println(scene.getScrollX()); - if(scene.getWidth() != 0 && scene.getHeight() != 0) { context.setFill(Color.BLACK); context.fillRect(-2, -2, scene.getWidth() + 4, scene.getHeight() + 4); diff --git a/src/main/java/fr/minuskube/editor/scene/object/SceneAnimation.java b/src/main/java/fr/minuskube/editor/scene/object/SceneAnimation.java index 3b50984..c218054 100644 --- a/src/main/java/fr/minuskube/editor/scene/object/SceneAnimation.java +++ b/src/main/java/fr/minuskube/editor/scene/object/SceneAnimation.java @@ -3,17 +3,18 @@ import fr.minuskube.editor.animation.FrameRepeat; import javafx.collections.FXCollections; import javafx.collections.ObservableList; +import javafx.scene.canvas.GraphicsContext; import java.util.ArrayList; import java.util.List; -public class SceneAnimation { +public class SceneAnimation extends SceneObject { private ObservableList frames; private List repeats; - private boolean playing = false; - private boolean looping = false; + private boolean playing = true; + private boolean looping = true; private int width, height; private boolean autoCrop; @@ -27,7 +28,21 @@ public SceneAnimation() { this.repeats = new ArrayList<>(); } + @Override + public void draw(GraphicsContext context) { + update(); // XXX: TEMPORARY + + if(!this.frames.isEmpty()) { + System.out.println("Draw frame: " + currentFrame); + + this.frames.get(currentFrame).draw(context); + } + } + public void update() { + if(!playing) + return; + SceneAnimationFrame frame = frames.get(currentFrame); currentFrameState += (1 / 60f) * speed; // TODO: Replace by deltaTime @@ -46,6 +61,15 @@ public void update() { } currentFrameState = 0; + + if(currentFrame >= frames.size()) { + if(looping) + currentFrame = 0; + else { + currentFrame--; + playing = false; + } + } } } diff --git a/src/main/java/fr/minuskube/editor/scene/object/SceneAnimationFrame.java b/src/main/java/fr/minuskube/editor/scene/object/SceneAnimationFrame.java index c600ec0..90fa4d2 100644 --- a/src/main/java/fr/minuskube/editor/scene/object/SceneAnimationFrame.java +++ b/src/main/java/fr/minuskube/editor/scene/object/SceneAnimationFrame.java @@ -6,7 +6,6 @@ public class SceneAnimationFrame extends SceneImage { private float duration; - private int offsetX, offsetY; public SceneAnimationFrame(File source) throws FileNotFoundException { super(source); @@ -15,10 +14,4 @@ public SceneAnimationFrame(File source) throws FileNotFoundException { public float getDuration() { return duration; } public void setDuration(float duration) { this.duration = duration; } - public int getOffsetX() { return offsetX; } - public void setOffsetX(int offsetX) { this.offsetX = offsetX; } - - public int getOffsetY() { return offsetY; } - public void setOffsetY(int offsetY) { this.offsetY = offsetY; } - }