From cf21aef0accf1b0aca8f261778bb04bab90b2396 Mon Sep 17 00:00:00 2001 From: Victor Date: Fri, 25 Aug 2017 11:33:44 +0300 Subject: [PATCH] Add nodes hierarchy --- .../main/java/com/annimon/hotarufx/Main.java | 9 ++++- .../hotarufx/visual/objects/CircleNode.java | 9 +++-- .../hotarufx/visual/objects/ObjectNode.java | 11 ++++-- .../visual/objects/PropertyConsumers.java | 5 +++ .../hotarufx/visual/objects/ShapeNode.java | 35 +++++++++++++++++++ 5 files changed, 64 insertions(+), 5 deletions(-) create mode 100644 app/src/main/java/com/annimon/hotarufx/visual/objects/ShapeNode.java diff --git a/app/src/main/java/com/annimon/hotarufx/Main.java b/app/src/main/java/com/annimon/hotarufx/Main.java index e96ebbc..9d99e39 100644 --- a/app/src/main/java/com/annimon/hotarufx/Main.java +++ b/app/src/main/java/com/annimon/hotarufx/Main.java @@ -24,7 +24,8 @@ public class Main extends Application { for (int y = -1; y <= 1; y++) { for (int x = -1; x <= 1; x++) { val node = new CircleNode(); - node.circle.setFill(colors[Math.abs(x * y)]); + val colorIndex = Math.abs(x * y); + node.circle.setFill(colors[colorIndex]); node.circle.setCenterX(x * halfWidth); node.circle.setCenterY(y * halfHeight); node.circle.setRadius(50); @@ -32,6 +33,9 @@ public class Main extends Application { .add(KeyFrame.of(30), 70) .add(KeyFrame.of(90), 20) .add(KeyFrame.of(300), 70); + node.fillProperty() + .add(KeyFrame.of(150), colors[1 - colorIndex]) + .add(KeyFrame.of(300), colors[colorIndex]); if (x == 0 && y == 0) { node.centerXProperty() .add(KeyFrame.of(60), 0) @@ -45,6 +49,9 @@ public class Main extends Application { .add(KeyFrame.of(300), 0); node.radiusProperty() .add(KeyFrame.of(320), 180); + node.fillProperty() + .add(KeyFrame.of(300), node.circle.getFill()) + .add(KeyFrame.of(320), Color.WHITE); } node.accept(renderVisitor, scene); } diff --git a/app/src/main/java/com/annimon/hotarufx/visual/objects/CircleNode.java b/app/src/main/java/com/annimon/hotarufx/visual/objects/CircleNode.java index a4ccc2f..50be055 100644 --- a/app/src/main/java/com/annimon/hotarufx/visual/objects/CircleNode.java +++ b/app/src/main/java/com/annimon/hotarufx/visual/objects/CircleNode.java @@ -6,14 +6,19 @@ import com.annimon.hotarufx.visual.TimeLine; import com.annimon.hotarufx.visual.visitors.NodeVisitor; import javafx.scene.shape.Circle; -public class CircleNode extends ObjectNode { +public class CircleNode extends ShapeNode { public final Circle circle; private PropertyTimelineHolder centerX, centerY, radius; public CircleNode() { - circle = new Circle(); + this(new Circle()); + } + + private CircleNode(Circle circle) { + super(circle); + this.circle = circle; centerX = PropertyTimelineHolder.empty(); centerY = PropertyTimelineHolder.empty(); radius = PropertyTimelineHolder.empty(); diff --git a/app/src/main/java/com/annimon/hotarufx/visual/objects/ObjectNode.java b/app/src/main/java/com/annimon/hotarufx/visual/objects/ObjectNode.java index 44edc08..2b3a8d2 100644 --- a/app/src/main/java/com/annimon/hotarufx/visual/objects/ObjectNode.java +++ b/app/src/main/java/com/annimon/hotarufx/visual/objects/ObjectNode.java @@ -2,12 +2,19 @@ package com.annimon.hotarufx.visual.objects; import com.annimon.hotarufx.visual.TimeLine; import com.annimon.hotarufx.visual.visitors.NodeVisitor; +import javafx.scene.Node; public abstract class ObjectNode { - public abstract R accept(NodeVisitor visitor, T input); + private final Node node; + + public ObjectNode(Node node) { + this.node = node; + } public void buildTimeline(TimeLine timeline) { - + } + + public abstract R accept(NodeVisitor visitor, T input); } diff --git a/app/src/main/java/com/annimon/hotarufx/visual/objects/PropertyConsumers.java b/app/src/main/java/com/annimon/hotarufx/visual/objects/PropertyConsumers.java index 28ca402..fbc088b 100644 --- a/app/src/main/java/com/annimon/hotarufx/visual/objects/PropertyConsumers.java +++ b/app/src/main/java/com/annimon/hotarufx/visual/objects/PropertyConsumers.java @@ -4,6 +4,7 @@ import com.annimon.hotarufx.visual.PropertyTimeline; import com.annimon.hotarufx.visual.TimeLine; import java.util.function.Consumer; import javafx.animation.KeyValue; +import javafx.scene.paint.Paint; public class PropertyConsumers { @@ -11,6 +12,10 @@ public class PropertyConsumers { return genericConsumer(timeline); } + public static Consumer> paintConsumer(TimeLine timeline) { + return genericConsumer(timeline); + } + public static Consumer> genericConsumer(TimeLine timeline) { return t -> { t.getKeyFrames().forEach((keyFrame, value) -> { diff --git a/app/src/main/java/com/annimon/hotarufx/visual/objects/ShapeNode.java b/app/src/main/java/com/annimon/hotarufx/visual/objects/ShapeNode.java new file mode 100644 index 0000000..98cecc8 --- /dev/null +++ b/app/src/main/java/com/annimon/hotarufx/visual/objects/ShapeNode.java @@ -0,0 +1,35 @@ +package com.annimon.hotarufx.visual.objects; + +import com.annimon.hotarufx.visual.PropertyTimeline; +import com.annimon.hotarufx.visual.PropertyTimelineHolder; +import com.annimon.hotarufx.visual.TimeLine; +import javafx.scene.paint.Paint; +import javafx.scene.shape.Shape; + +public abstract class ShapeNode extends ObjectNode { + + private final Shape shape; + + private PropertyTimelineHolder fill, stroke; + + public ShapeNode(Shape shape) { + super(shape); + this.shape = shape; + fill = PropertyTimelineHolder.empty(); + stroke = PropertyTimelineHolder.empty(); + } + + public PropertyTimeline fillProperty() { + return fill.setIfEmptyThenGet(shape::fillProperty); + } + + public PropertyTimeline strokeProperty() { + return stroke.setIfEmptyThenGet(shape::strokeProperty); + } + + public void buildTimeline(TimeLine timeline) { + super.buildTimeline(timeline); + fill.ifPresent(PropertyConsumers.paintConsumer(timeline)); + stroke.ifPresent(PropertyConsumers.paintConsumer(timeline)); + } +}