Skip to content

Commit

Permalink
Fix SceneAnimation, some animation tests #3
Browse files Browse the repository at this point in the history
  • Loading branch information
MinusKube committed Jan 30, 2018
1 parent 417feb4 commit 258ce77
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 12 deletions.
47 changes: 45 additions & 2 deletions src/main/java/fr/minuskube/editor/scene/Scene.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<? super SceneObject>) change -> {
while(change.next()) {
for(SceneObject added : change.getAddedSubList()) {
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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);
Expand Down
30 changes: 27 additions & 3 deletions src/main/java/fr/minuskube/editor/scene/object/SceneAnimation.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<SceneAnimationFrame> frames;
private List<FrameRepeat> repeats;

private boolean playing = false;
private boolean looping = false;
private boolean playing = true;
private boolean looping = true;

private int width, height;
private boolean autoCrop;
Expand All @@ -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
Expand All @@ -46,6 +61,15 @@ public void update() {
}

currentFrameState = 0;

if(currentFrame >= frames.size()) {
if(looping)
currentFrame = 0;
else {
currentFrame--;
playing = false;
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
public class SceneAnimationFrame extends SceneImage {

private float duration;
private int offsetX, offsetY;

public SceneAnimationFrame(File source) throws FileNotFoundException {
super(source);
Expand All @@ -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; }

}

0 comments on commit 258ce77

Please sign in to comment.