| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 
 | import javafx.beans.property.*;
import javafx.geometry.BoundingBox;
import javafx.geometry.Rectangle2D;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.input.ScrollEvent;
import javafx.scene.layout.Region;
 
import java.util.List;
import java.util.Objects;
 
public final class ImagePanView extends Region {
    private final ImageView content = new ImageView();
 
    public ImagePanView() {
        this(null);
    }
 
    public ImagePanView(final Image image) {
        super();
        setId("imagePanView");
        getStyleClass().add("image-pan-view");
        content.setId("content");
        content.getStyleClass().add("content");
        content.setPreserveRatio(true);
        content.imageProperty().bind(imageProperty());
        content.setMouseTransparent(true);
        getChildren().addAll(content);
        imageProperty().addListener((observable, oldValue, newValue) -> {
            offsetX = (newValue == null) ? 0 : Math.min(offsetX, newValue.getWidth());
            offsetY = (newValue == null) ? 0 : Math.min(offsetY, newValue.getHeight());
            computeView();
        });
        widthProperty().addListener((observable, oldValue, newValue) -> computeView());
        heightProperty().addListener((observable, oldValue, newValue) -> computeView());
        setImage(image);
        //
        addEventFilter(MouseEvent.MOUSE_PRESSED, this::handleMousePressed);
        addEventFilter(MouseEvent.MOUSE_DRAGGED, this::handleMouseDragged);
        addEventFilter(ScrollEvent.SCROLL, this::handleZoomByScroll);
    }
 
    @Override
    protected void layoutChildren() {
        super.layoutChildren();
        final double width = getWidth();
        final double height = getHeight();
        final var insets = getInsets();
        final double insetsLeft = insets.getLeft();
        final double insetsRight = insets.getRight();
        final double insetsTop = insets.getTop();
        final double insetsBot = insets.getBottom();
        final double viewPortX = insetsLeft;
        final double viewPortY = insetsTop;
        final double viewPortW = Math.max(0, width - (insetsLeft + insetsRight));
        final double viewPortH = Math.max(0, height - (insetsTop + insetsBot));
        content.setLayoutX(viewPortX);
        content.setLayoutY(viewPortY);
        content.setFitWidth(viewPortW);
        content.setFitHeight(viewPortH);
    }
 
    private static final BoundingBox EMPTY_BOUNDS = new BoundingBox(0, 0, 0, 0);
    private static final Rectangle2D EMPTY_CONTENT_VIEWPORT = new Rectangle2D(0, 0, 0, 0);
 
    private void computeView() {
        System.out.println("computeView()");
        final var image = getImage();
        if (Objects.isNull(image)) {
            viewport.set(EMPTY_BOUNDS);
            content.setViewport(EMPTY_CONTENT_VIEWPORT);
            return;
        }
        final double width = getWidth();
        final double height = getHeight();
        final var insets = getInsets();
        final double insetsLeft = insets.getLeft();
        final double insetsRight = insets.getRight();
        final double insetsTop = insets.getTop();
        final double insetsBot = insets.getBottom();
        final double viewPortX = insetsLeft;
        final double viewPortY = insetsTop;
        final double viewPortW = Math.max(0, width - (insetsLeft + insetsRight));
        final double viewPortH = Math.max(0, height - (insetsTop + insetsBot));
        if (viewPortW == 0 || viewPortH == 0) {
            viewport.set(EMPTY_BOUNDS);
            content.setViewport(EMPTY_CONTENT_VIEWPORT);
            return;
        }
        final double zoom = getZoom();
        final double imageW = image.getWidth();
        final double imageH = image.getHeight();
        final double viewX = clamp(0, offsetX, image.getWidth());
        final double viewY = clamp(0, offsetY, image.getHeight());
        final double viewW = Math.min(imageW - viewX, viewPortW / zoom);
        final double viewH = Math.min(imageH - viewY, viewPortH / zoom);
        final var oldViewport = getViewport();
        if (oldViewport.isEmpty() || oldViewport.getMinX() != viewX || oldViewport.getMinY() != viewY || oldViewport.getWidth() != viewW || oldViewport.getHeight() != viewH) {
            viewport.set(new BoundingBox(viewX, viewY, viewW, viewH));
            content.setViewport(new Rectangle2D(viewX, viewY, viewW, viewH));
        }
    }
 
    private final ObjectProperty<Image> image = new SimpleObjectProperty<>(this, "image");
 
    public final Image getImage() {
        return image.get();
    }
 
    public final void setImage(final Image value) {
        image.set(value);
    }
 
    public final ObjectProperty<Image> imageProperty() {
        return image;
    }
 
    private final ReadOnlyObjectWrapper<BoundingBox> viewport = new ReadOnlyObjectWrapper<>(this, "viewport", EMPTY_BOUNDS);
 
    public final BoundingBox getViewport() {
        return viewport.get();
    }
 
    public final ReadOnlyObjectProperty<BoundingBox> viewportProperty() {
        return viewport.getReadOnlyProperty();
    }
 
    private double clamp(double min, double value, double max) {
        double result = Math.max(min, value);
        result = Math.min(result, max);
        return result;
    }
 
    private double offsetX;
    private double offsetY;
 
    private final ReadOnlyDoubleWrapper zoom = new ReadOnlyDoubleWrapper(this, "zoom", 1);
 
    public final double getZoom() {
        return zoom.get();
    }
 
    public final void setZoom(final double value) {
        if (value > 0) {
            zoom.set(value);
            computeView();
        }
    }
 
    public final ReadOnlyDoubleProperty zoomProperty() {
        return zoom.getReadOnlyProperty();
    }
 
    private double startX;
    private double startY;
 
    private void handleMousePressed(final MouseEvent event) {
        startX = event.getX();
        startY = event.getX();
    }
 
    private void handleMouseDragged(final MouseEvent event) {
        final var image = getImage();
        if (image == null) {
            return;
        }
        final double zoom = getZoom();
        final double rawDragX = startX - event.getX();
        final double rawDragY = startY - event.getY();
        startX = event.getX();
        startY = event.getY();
        final double dragX = rawDragX / zoom;
        final double dragY = rawDragY / zoom;
        System.out.printf("DRAG %f %f%n", dragX, dragY);
        final var viewport = getViewport();
        final double newOffsetX = clamp(0, offsetX + dragX, image.getWidth() - viewport.getWidth());
        final double newOffsetY = clamp(0, offsetY + dragY, image.getHeight() - viewport.getWidth());
        System.out.printf("%f -> %f%n", offsetX, newOffsetX);
        System.out.printf("%f -> %f%n", offsetY, newOffsetY);
        offsetX = newOffsetX;
        offsetY = newOffsetY;
        computeView();
    }
 
    final List<Double> ZOOM_FACTORS = List.of(0.25, 0.5, 1d, 2d, 4d, 8d, 16d);
 
    private void handleZoomByScroll(final ScrollEvent event) {
        final double deltaY = event.getDeltaY();
        final double oldZoom = getZoom();
        int zoomIndex = ZOOM_FACTORS.indexOf(oldZoom);
        // User provided zoom.
        if (zoomIndex < 0) {
            zoomIndex = ZOOM_FACTORS.stream()
                    .filter(value -> value < oldZoom)
                    .findFirst()
                    .map(ZOOM_FACTORS::indexOf)
                    .orElse(0);
        }
        int newZoomIndex = zoomIndex + ((deltaY > 0) ? -1 : 1);
        newZoomIndex = (int) clamp(0, newZoomIndex, ZOOM_FACTORS.size() - 1);
        final double zoom = ZOOM_FACTORS.get(newZoomIndex);
        setZoom(zoom);
    }
} |