|
| 1 | +package edu.wpi.grip.ui.preview; |
| 2 | + |
| 3 | +import javafx.beans.property.DoubleProperty; |
| 4 | +import javafx.beans.property.ObjectProperty; |
| 5 | +import javafx.beans.property.ReadOnlyDoubleProperty; |
| 6 | +import javafx.beans.property.SimpleDoubleProperty; |
| 7 | +import javafx.beans.property.SimpleObjectProperty; |
| 8 | +import javafx.geometry.Orientation; |
| 9 | +import javafx.scene.image.Image; |
| 10 | +import javafx.scene.layout.Background; |
| 11 | +import javafx.scene.layout.BackgroundImage; |
| 12 | +import javafx.scene.layout.BackgroundRepeat; |
| 13 | +import javafx.scene.layout.BackgroundSize; |
| 14 | +import javafx.scene.layout.Region; |
| 15 | + |
| 16 | +/** |
| 17 | + * A custom implementation of an image view that resizes to fit its parent container. |
| 18 | + */ |
| 19 | +public class ResizableImageView extends Region { |
| 20 | + |
| 21 | + private final ObjectProperty<Image> image = new SimpleObjectProperty<>(this, "image"); |
| 22 | + private final DoubleProperty ratio = new SimpleDoubleProperty(this, "ratio", 1); |
| 23 | + private static final BackgroundSize size = |
| 24 | + new BackgroundSize(BackgroundSize.AUTO, BackgroundSize.AUTO, false, false, true, false); |
| 25 | + |
| 26 | + /** |
| 27 | + * Creates a new resizable image view. |
| 28 | + */ |
| 29 | + public ResizableImageView() { |
| 30 | + super(); |
| 31 | + |
| 32 | + getStyleClass().add("resizable-image"); |
| 33 | + |
| 34 | + image.addListener((obs, old, img) -> { |
| 35 | + if (img == null) { |
| 36 | + setBackground(null); |
| 37 | + ratio.set(1); |
| 38 | + } else if (img != old) { |
| 39 | + // Only create a new background object when the image changes |
| 40 | + // Otherwise we would be creating a new background object for every frame of every preview |
| 41 | + Background background = createImageBackground(img); |
| 42 | + setBackground(background); |
| 43 | + ratio.set(img.getWidth() / img.getHeight()); |
| 44 | + setPrefHeight(img.getHeight()); |
| 45 | + setPrefWidth(USE_COMPUTED_SIZE); |
| 46 | + } |
| 47 | + }); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Creates a background that displays only the given image. |
| 52 | + * |
| 53 | + * @param img the image to create the background for |
| 54 | + */ |
| 55 | + private static Background createImageBackground(Image img) { |
| 56 | + BackgroundImage backgroundImage = new BackgroundImage( |
| 57 | + img, |
| 58 | + BackgroundRepeat.NO_REPEAT, |
| 59 | + BackgroundRepeat.NO_REPEAT, |
| 60 | + null, |
| 61 | + size |
| 62 | + ); |
| 63 | + return new Background(backgroundImage); |
| 64 | + } |
| 65 | + |
| 66 | + public Image getImage() { |
| 67 | + return image.get(); |
| 68 | + } |
| 69 | + |
| 70 | + public ObjectProperty<Image> imageProperty() { |
| 71 | + return image; |
| 72 | + } |
| 73 | + |
| 74 | + public void setImage(Image image) { |
| 75 | + this.image.set(image); |
| 76 | + } |
| 77 | + |
| 78 | + public double getRatio() { |
| 79 | + return ratio.get(); |
| 80 | + } |
| 81 | + |
| 82 | + public ReadOnlyDoubleProperty ratioProperty() { |
| 83 | + return ratio; |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public Orientation getContentBias() { |
| 88 | + return Orientation.VERTICAL; |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Computes the width of the displayed image for the given target height, maintaining the image's |
| 93 | + * intrinsic aspect ratio. |
| 94 | + * |
| 95 | + * @param height the target height of the image |
| 96 | + * @return the width of the image |
| 97 | + */ |
| 98 | + private double computeImageWidthForHeight(double height) { |
| 99 | + if (getImage() == null) { |
| 100 | + return 1; |
| 101 | + } |
| 102 | + return height * getRatio(); |
| 103 | + } |
| 104 | + |
| 105 | + @Override |
| 106 | + protected double computePrefWidth(double height) { |
| 107 | + return computeImageWidthForHeight(height); |
| 108 | + } |
| 109 | +} |
0 commit comments