Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix map background dialog width #3869

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 55 additions & 22 deletions src/main/java/net/rptools/maptool/client/swing/ImagePanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public enum SelectionMode {

private ImagePanelModel model;

private int visibleRowCount = 0;
private int gridSize = 50;
private final Dimension gridPadding = new Dimension(9, 11);
private final int captionPadding = 5;
Expand Down Expand Up @@ -103,6 +104,22 @@ private void ensureFontHeight(Graphics2D g) {
}
}

private int getItemWidth() {
return gridSize + gridPadding.width;
}

private int getItemHeight() {
return gridSize + gridPadding.height + (showCaptions ? fontHeight + captionPadding : 0);
}

public void setVisibleRowCount(int visibleRowCount) {
this.visibleRowCount = Math.max(0, visibleRowCount);
}

public int getVisibleRowCount() {
return this.visibleRowCount;
}

public void setGridSize(int size) {
// Min
size = Math.max(25, size);
Expand Down Expand Up @@ -207,22 +224,28 @@ protected void paintComponent(Graphics gfx) {
}
imageBoundsMap.clear();

int x = gridPadding.width;
int y = gridPadding.height;
int itemsPerRow = calculateItemsPerRow();
int itemWidth = getItemWidth();
int itemHeight = getItemHeight();

int numToProcess = model.getImageCount();
String timerField = null;
if (timer.isEnabled()) {
timerField = "time to process " + numToProcess + " images";
timer.start(timerField);
}
for (int i = 0; i < numToProcess; i++) {
int row = i / itemsPerRow;
int column = i % itemsPerRow;

int x = gridPadding.width + column * itemWidth;
int y = gridPadding.height + row * itemHeight;

Image image;

Rectangle bounds = new Rectangle(x, y, gridSize, gridSize);
imageBoundsMap.put(
new Rectangle(
x, y, gridSize, gridSize + (showCaptions ? captionPadding + fontHeight : 0)),
i);
new Rectangle(x, y, itemWidth - gridPadding.width, itemHeight - gridPadding.height), i);

// Background
Paint paint = model.getBackground(i);
Expand Down Expand Up @@ -303,15 +326,6 @@ protected void paintComponent(Graphics gfx) {
g.setRenderingHints(savedRenderingHints);
}
}
// Line wrap
x += gridSize + gridPadding.width;
if ((x + gridSize) > (size.width - gridPadding.width)) {
x = gridPadding.width;
y += gridSize + gridPadding.height;
if (showCaptions) {
y += fontHeight;
}
}
}
g.setFont(savedFont);
if (timer.isEnabled()) {
Expand Down Expand Up @@ -378,6 +392,14 @@ private Dimension constrainSize(Image image, int size) {
return new Dimension(width, height);
}

private int calculateItemsPerRow() {
if (getItemWidth() <= 0) {
return 1;
}

return Math.max(1, getWidth() / getItemWidth());
}

@Override
public Dimension getPreferredSize() {
if (model == null || model.getImageCount() == 0) {
Expand All @@ -387,14 +409,13 @@ public Dimension getPreferredSize() {
ensureFontHeight(null);
int width = getWidth();

int itemWidth = gridSize + gridPadding.width;
int itemHeight =
gridSize + gridPadding.height + (showCaptions ? fontHeight + captionPadding : 0);
int itemWidth = getItemWidth();
int itemHeight = getItemHeight();
int rowCount;
if (width < gridSize + gridPadding.width * 2) {
if (width < itemWidth + gridPadding.width) {
rowCount = model.getImageCount();
} else {
int itemsPerRow = width / itemWidth;
int itemsPerRow = calculateItemsPerRow();
rowCount = (int) Math.ceil(model.getImageCount() / (float) itemsPerRow);
}
int height = rowCount * itemHeight;
Expand All @@ -403,7 +424,11 @@ public Dimension getPreferredSize() {

@Override
public Dimension getMinimumSize() {
return getPreferredSize();
ensureFontHeight(null);

int width = getItemWidth() + gridPadding.width;
int height = getItemHeight();
return new Dimension(width, height);
}

protected int getImageIndexAt(int x, int y) {
Expand All @@ -428,13 +453,21 @@ public String getToolTipText(MouseEvent event) {
// SCROLLABLE
@Override
public Dimension getPreferredScrollableViewportSize() {
return getPreferredSize();
if (visibleRowCount <= 0) {
return getPreferredSize();
}

ensureFontHeight(null);

int width = 3 * getItemWidth() + gridPadding.width;
int height = visibleRowCount * getItemHeight();
return new Dimension(width, height);
}

@Override
public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction) {
ensureFontHeight(null);
return ((gridSize + gridPadding.height * 2) + (showCaptions ? fontHeight + captionPadding : 0));
return getItemHeight() + gridPadding.height;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import javax.swing.colorchooser.AbstractColorChooserPanel;
import net.rptools.maptool.language.I18N;

@SuppressWarnings("serial")
public class PaintChooser extends JPanel {

private final JColorChooser swatchColorChooser;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import javax.swing.JComponent;
import javax.swing.JSplitPane;
import net.rptools.maptool.client.swing.AbstractPaintChooserPanel;
import net.rptools.maptool.client.swing.ImagePanel;
import net.rptools.maptool.client.swing.PaintChooser;
import net.rptools.maptool.client.ui.assetpanel.AssetPanel;
import net.rptools.maptool.client.ui.assetpanel.AssetPanelModel;
Expand All @@ -28,7 +27,6 @@
public class TextureChooserPanel extends AbstractPaintChooserPanel {

private PaintChooser paintChooser;
private ImagePanel imagePanel;

public TextureChooserPanel(PaintChooser paintChooser, AssetPanelModel model) {
this(paintChooser, model, "textureChooser");
Expand Down Expand Up @@ -60,7 +58,7 @@ private JComponent createImageExplorerPanel(AssetPanelModel model, String contro

paintChooser.setPaint(new AssetPaint(asset));
});
assetPanel.setThumbSize(100);
assetPanel.setThumbSize(50);

return assetPanel;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ private void createImagePanel() {
imagePanel.setShowCaptions(true);
imagePanel.setSelectionMode(SelectionMode.SINGLE);
imagePanel.setFont(new Font("Helvetica", 0, 10)); // XXX Overrides TinyLAF?
imagePanel.setVisibleRowCount(2);

imagePanel.addMouseWheelListener(
e -> {
Expand Down Expand Up @@ -182,9 +183,9 @@ private JPanel createSouthPanel() {
imagePanelProgressBar.setIndeterminate(false);
imagePanelProgressBar.setVisible(false);

panel.add(BorderLayout.NORTH, createFilterPanel());
panel.add(BorderLayout.NORTH, createFilterPanel());
panel.add(BorderLayout.WEST, getThumbnailPreviewSlider());

panel.add(BorderLayout.CENTER, new JScrollPane(imagePanel));
panel.add(BorderLayout.SOUTH, imagePanelProgressBar);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,6 @@ public void actionPerformed(ActionEvent e) {
TextureChooserPanel textureChooserPanel =
new TextureChooserPanel(paintChooser, model, "mapPropertiesTextureChooser");
paintChooser.addPaintChooser(textureChooserPanel);
paintChooser.setPreferredSize(new Dimension(450, 400));
mapSelectorDialog = new MapSelectorDialog();
getRootPane().setDefaultButton(getOKButton());
}
Expand Down Expand Up @@ -423,6 +422,7 @@ private void initMapButton() {
getMapButton()
.addActionListener(
e -> {
mapSelectorDialog.pack();
Asset asset = mapSelectorDialog.chooseAsset();
if (asset == null) {
return;
Expand Down Expand Up @@ -584,7 +584,6 @@ public MapSelectorDialog() {
add(BorderLayout.CENTER, createImageExplorerPanel());
add(BorderLayout.SOUTH, createButtonBar());
this.setTitle(I18N.getText("MapPropertiesDialog.label.image"));
setSize(500, 400);
}

@Override
Expand Down