From 48f42959c4602d8e9e9182dafde3f0d096a0f022 Mon Sep 17 00:00:00 2001 From: Isaac Date: Thu, 25 Jan 2018 18:49:34 +0100 Subject: [PATCH] Disable layer field right click, start to add animation system #3, add R key to reset zoom and scroll, add Ctrl + X --- .../editor/animation/FrameRepeat.java | 24 +++++++ .../fr/minuskube/editor/layer/LayersPane.java | 6 ++ .../java/fr/minuskube/editor/scene/Scene.java | 32 +++++++++ .../editor/scene/object/SceneAnimation.java | 69 +++++++++++++++++++ .../scene/object/SceneAnimationFrame.java | 24 +++++++ 5 files changed, 155 insertions(+) create mode 100644 src/main/java/fr/minuskube/editor/animation/FrameRepeat.java create mode 100644 src/main/java/fr/minuskube/editor/scene/object/SceneAnimation.java create mode 100644 src/main/java/fr/minuskube/editor/scene/object/SceneAnimationFrame.java diff --git a/src/main/java/fr/minuskube/editor/animation/FrameRepeat.java b/src/main/java/fr/minuskube/editor/animation/FrameRepeat.java new file mode 100644 index 0000000..707e877 --- /dev/null +++ b/src/main/java/fr/minuskube/editor/animation/FrameRepeat.java @@ -0,0 +1,24 @@ +package fr.minuskube.editor.animation; + +public class FrameRepeat { + + private int start; + private int end; + private int times; + + private int currentTimes = 0; + + public FrameRepeat(int start, int end, int times) { + this.start = start; + this.end = end; + this.times = times; + } + + public int getStart() { return start; } + public int getEnd() { return end; } + public int getTimes() { return times; } + + public int getCurrentTimes() { return currentTimes; } + public void setCurrentTimes(int currentTimes) { this.currentTimes = currentTimes; } + +} diff --git a/src/main/java/fr/minuskube/editor/layer/LayersPane.java b/src/main/java/fr/minuskube/editor/layer/LayersPane.java index aa4e476..1d417d0 100644 --- a/src/main/java/fr/minuskube/editor/layer/LayersPane.java +++ b/src/main/java/fr/minuskube/editor/layer/LayersPane.java @@ -10,6 +10,7 @@ import javafx.fxml.FXML; import javafx.fxml.FXMLLoader; import javafx.geometry.Pos; +import javafx.scene.control.ContextMenu; import javafx.scene.control.ListView; import javafx.scene.control.MultipleSelectionModel; import javafx.scene.control.ScrollPane; @@ -18,6 +19,7 @@ import javafx.scene.control.TextField; import javafx.scene.image.ImageView; import javafx.scene.input.KeyCode; +import javafx.scene.input.MouseButton; import javafx.scene.layout.BorderPane; import javafx.scene.layout.HBox; import javafx.scene.layout.VBox; @@ -95,8 +97,12 @@ private HBox newImage(SceneImage image) { TextField field = new TextField(image.getName()); field.setEditable(false); + field.setContextMenu(new ContextMenu()); field.setOnMouseClicked(event -> { + if(event.getButton() != MouseButton.PRIMARY) + return; + if(event.getClickCount() >= 2) field.setEditable(true); else if(event.getClickCount() == 1) { diff --git a/src/main/java/fr/minuskube/editor/scene/Scene.java b/src/main/java/fr/minuskube/editor/scene/Scene.java index 47909bd..9ce90ed 100644 --- a/src/main/java/fr/minuskube/editor/scene/Scene.java +++ b/src/main/java/fr/minuskube/editor/scene/Scene.java @@ -98,11 +98,43 @@ else if(event.getCode() == KeyCode.LEFT) else if(event.getCode() == KeyCode.RIGHT) moveSelectedObjects(distance, 0); + else if(event.getCode() == KeyCode.R) { + zoom = 1; + + scrollX = ((canvas.getWidth() - width) / 2) / getWidth(); + scrollY = ((canvas.getHeight() - height) / 2) / getHeight(); + } + else if(event.getCode() == KeyCode.DELETE) { objects.removeIf(SceneObject::isSelected); selectedObjects.clear(); } + else if(event.isControlDown() && event.getCode() == KeyCode.X) { + ClipboardContent content = new ClipboardContent(); + + List selected = new ArrayList<>(); + List files = new ArrayList<>(); + + for(SceneObject object : selectedObjects) { + if(object instanceof SceneImage) { + SceneImage image = (SceneImage) object; + + files.add(image.getSource()); + selected.add(new SceneImage(image)); + } + } + + content.put(SceneImage.DATA_FORMAT, selected); + content.putFiles(files); + + Clipboard clipboard = Clipboard.getSystemClipboard(); + clipboard.setContent(content); + + objects.removeIf(SceneObject::isSelected); + selectedObjects.clear(); + } + else if(event.isControlDown() && event.getCode() == KeyCode.C) { ClipboardContent content = new ClipboardContent(); diff --git a/src/main/java/fr/minuskube/editor/scene/object/SceneAnimation.java b/src/main/java/fr/minuskube/editor/scene/object/SceneAnimation.java new file mode 100644 index 0000000..3b50984 --- /dev/null +++ b/src/main/java/fr/minuskube/editor/scene/object/SceneAnimation.java @@ -0,0 +1,69 @@ +package fr.minuskube.editor.scene.object; + +import fr.minuskube.editor.animation.FrameRepeat; +import javafx.collections.FXCollections; +import javafx.collections.ObservableList; + +import java.util.ArrayList; +import java.util.List; + +public class SceneAnimation { + + private ObservableList frames; + private List repeats; + + private boolean playing = false; + private boolean looping = false; + + private int width, height; + private boolean autoCrop; + private float speed = 1; + + private int currentFrame = 0; + private float currentFrameState = 0; + + public SceneAnimation() { + this.frames = FXCollections.observableArrayList(); + this.repeats = new ArrayList<>(); + } + + public void update() { + SceneAnimationFrame frame = frames.get(currentFrame); + + currentFrameState += (1 / 60f) * speed; // TODO: Replace by deltaTime + + if(currentFrameState >= frame.getDuration()) { + currentFrame++; + + for(FrameRepeat repeat : repeats) { + if(repeat.getTimes() != -1 && repeat.getTimes() >= repeat.getCurrentTimes()) + continue; + + if(currentFrame > repeat.getEnd()) + currentFrame = repeat.getStart(); + + repeat.setCurrentTimes(repeat.getCurrentTimes() + 1); + } + + currentFrameState = 0; + } + } + + public ObservableList getFrames() { return frames; } + public List getRepeats() { return repeats; } + + public boolean isPlaying() { return playing; } + + public boolean isLooping() { return looping; } + public void setLooping(boolean looping) { this.looping = looping; } + + public int getWidth() { return width; } + public void setWidth(int width) { this.width = width; } + + public int getHeight() { return height; } + public void setHeight(int height) { this.height = height; } + + public float getSpeed() { return speed; } + public void setSpeed(float speed) { this.speed = speed; } + +} diff --git a/src/main/java/fr/minuskube/editor/scene/object/SceneAnimationFrame.java b/src/main/java/fr/minuskube/editor/scene/object/SceneAnimationFrame.java new file mode 100644 index 0000000..c600ec0 --- /dev/null +++ b/src/main/java/fr/minuskube/editor/scene/object/SceneAnimationFrame.java @@ -0,0 +1,24 @@ +package fr.minuskube.editor.scene.object; + +import java.io.File; +import java.io.FileNotFoundException; + +public class SceneAnimationFrame extends SceneImage { + + private float duration; + private int offsetX, offsetY; + + public SceneAnimationFrame(File source) throws FileNotFoundException { + super(source); + } + + 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; } + +}