如何实现用于旋转组的撤消/重做功能



多亏了James_D,当我移动或调整我的组大小时,我有一个完美的撤消/重做功能。不幸的是,我也无法使其与旋转一起使用。

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

线程

"JavaFX 应用程序线程"中的异常

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

预期:测试。收到Model.Undo.UndoChange@8c4318ec:

测试。Model.Undo.UndoChange@35c7d8eb

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;
    }
}

事件流:

{
...
        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())));
    }

我发现了这个问题,我必须实现 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;
}

相关内容

  • 没有找到相关文章

最新更新