Skip to content

Commit

Permalink
Texture Viewer: Snappy resize if texture was fit into the viewport
Browse files Browse the repository at this point in the history
  • Loading branch information
ShadelessFox committed Jul 3, 2024
1 parent 4ded68f commit 355db0f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public class ImagePanel extends JComponent implements Scrollable {
private float lowRange;
private int mip;
private int slice;
private boolean snapZoom;
private Set<Channel> channels;

private BufferedImage filteredImage;
Expand Down Expand Up @@ -253,6 +254,10 @@ public void removeChannel(@NotNull Channel channel) {
}
}

public boolean isSnapZoom() {
return snapZoom;
}

public float getZoom() {
return zoom;
}
Expand All @@ -262,6 +267,7 @@ public void setZoom(float zoom) {
final float oldScale = this.zoom;

this.zoom = zoom;
this.snapZoom = false;

update();

Expand Down Expand Up @@ -304,18 +310,19 @@ public boolean isRangeAdjustable() {
}

public void fit() {
if (provider == null) {
return;
}
setZoom(computeFitZoom());
snapZoom = true;
}

public float computeFitZoom() {
final Container container = SwingUtilities.getAncestorOfClass(JViewport.class, this).getParent();
final Dimension viewport = container.getSize();
UIUtils.removeFrom(viewport, container.getInsets());

setZoom(UIUtils.getScalingFactor(
return UIUtils.getScalingFactor(
viewport.width, viewport.height,
image.getWidth(), image.getHeight()
));
);
}

private void reset(@Nullable ImageProvider provider) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

import javax.swing.*;
import java.awt.*;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;

public class ImagePanelViewport extends JViewport {
private static final int TILE_SIZE = 8;
Expand All @@ -18,6 +20,15 @@ public ImagePanelViewport(@NotNull ImagePanel panel) {

setView(inner);
setBackground(UIColor.TRANSPARENT);

addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
if (panel.isSnapZoom()) {
panel.fit();
}
}
});
}

@Override
Expand Down

0 comments on commit 355db0f

Please sign in to comment.