如何实现撤消/重做功能来旋转组?

时间:2016-06-06 13:56:53

标签: javafx reactfx

感谢James_D,当我移动或调整我的群组时,我有一个完美的撤消/重做功能。 不幸的是,我也无法使用旋转功能。

编辑:我已经创建了一个自定义边界框,撤消/重做也适用于旋转。但每次撤消任何更改时都会抛出异常消息:

  

线程" JavaFX应用程序线程"

中的异常      

java.lang.IllegalArgumentException:收到意外更改。

     

预期:test.Model.Undo.UndoChange@8c4318ec收到:

     

test.Model.Undo.UndoChange@35c7d8eb at

     

org.fxmisc.undo.impl.UndoManagerImpl.changeObserved(UndoManagerImpl.java:185)

    public class CustomBoundingBox {

    private double minX;
    private double minY;
    private double width;
    private double height;
    private double rotation;
    private Paint fill;

    public CustomBoundingBox(double minX, double minY, double width, double height, double rotation, Paint fill) {
        this.minX=minX;
        this.minY=minY;
        this.width=width;
        this.height=height;
        this.rotation=rotation;
        this.fill=fill;
    }

    public double getMinX() {
        return minX;
    }

    public void setMinX(double minX) {
        this.minX = minX;
    }

    public double getMinY() {
        return minY;
    }

    public void setMinY(double minY) {
        this.minY = minY;
    }

    public double getWidth() {
        return width;
    }

    public void setWidth(double width) {
        this.width = width;
    }

    public double getHeight() {
        return height;
    }

    public void setHeight(double height) {
        this.height = height;
    }

    public double getRotation() {
        return rotation;
    }

    public void setRotation(double rotation) {
        this.rotation = rotation;
    }

    public Paint getFill() {
        return fill;
    }

    public void setColor(Paint fill) {
        this.fill = fill;
    }
}

EventStreamers:

{
...
        EventStream<UndoChange<CustomBoundingBox>> rotationChanges = makeEventStream(rect.rotateProperty(),
                rRect -> new CustomBoundingBox(rect.getRect().getX(), rect.getRect().getY(), rect.getRect().getWidth(), rect.getRect().getHeight(), rRect.doubleValue(), rect.getRect().getFill()));


        EventStream<UndoChange<CustomBoundingBox>> boundsChanges = EventStreams
                .merge(xChanges, yChanges, widthChanges, heightChanges, rotationChanges).reducible(UndoChange::merge)
                .suspendWhen(animationRunning);

        rect.undoManager = UndoManagerFactory.unlimitedHistoryUndoManager(boundsChanges, UndoChange::invert, c -> {
            rect.getRedoAnimation().getKeyFrames()
                    .setAll(new KeyFrame(RectangleModel.getRedoAnimationTime(), 
                            new KeyValue(rect.getRect().xProperty(), c.getNewValue().getMinX()),
                            new KeyValue(rect.getRect().yProperty(), c.getNewValue().getMinY()),
                            new KeyValue(rect.getRect().widthProperty(), c.getNewValue().getWidth()),
                            new KeyValue(rect.getRect().heightProperty(), c.getNewValue().getHeight()),
                            new KeyValue(rect.rotateProperty(), c.getNewValue().getRotation())));
            rect.getRedoAnimation().play();
        }, (c1, c2) -> Optional.of(c1.merge(c2)));

    }

    private <S, T> EventStream<UndoChange<T>> makeEventStream(ObservableValue<S> property, Function<S, T> objectSupplier) {
        return EventStreams.changesOf(property).map(
                c -> new UndoChange<>(objectSupplier.apply(c.getOldValue()), objectSupplier.apply(c.getNewValue())));
    }

1 个答案:

答案 0 :(得分:0)

我发现了这个问题,我必须实现equals()方法:

public boolean equals(Object obj) {
    if (obj == this) return true;
    if (obj instanceof CustomBoundingBox) {
        CustomBoundingBox other = (CustomBoundingBox) obj;
        return getMinX() == other.getMinX()
            && getMinY() == other.getMinY()
            && getWidth() == other.getWidth()
            && getHeight() == other.getHeight()
            && getRotation() == other.getRotation()
            && getFill() == other.getFill();
    } else return false;
}
相关问题