fix(io): scale the GUI map with HiDPI factor and live zoom

MapPanel had fixed pixel sizes and an 11pt font, so it ignored uiScale and
the Ctrl +/- zoom. It now multiplies all dimensions, strokes and the label
font by a scale set from uiScale x zoom in SwingIO.applyFonts().

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-05-31 21:14:31 +02:00
parent f1ec321e50
commit f6f62b66d8
2 changed files with 77 additions and 36 deletions

View File

@@ -18,14 +18,19 @@ import java.awt.RenderingHints;
import java.awt.Stroke; import java.awt.Stroke;
import java.util.List; import java.util.List;
/** Draws a {@link MapView} as outlined rooms linked by corridors (Graphics2D). */ /**
* Draws a {@link MapView} as outlined rooms linked by corridors (Graphics2D).
* All dimensions and the label font are multiplied by {@link #setScale(float)}
* so the map tracks the window's HiDPI scale and the live zoom.
*/
public class MapPanel extends JComponent { public class MapPanel extends JComponent {
private static final int CELL_W = 96; private static final int BASE_CELL_W = 96;
private static final int CELL_H = 46; private static final int BASE_CELL_H = 46;
private static final int GAP_X = 34; private static final int BASE_GAP_X = 34;
private static final int GAP_Y = 30; private static final int BASE_GAP_Y = 30;
private static final int PAD = 16; private static final int BASE_PAD = 16;
private static final float BASE_FONT = 11f;
private static final Color BG = new Color(0x0e, 0x12, 0x18); private static final Color BG = new Color(0x0e, 0x12, 0x18);
private static final Color CORRIDOR = new Color(0x46, 0x70, 0x8a); private static final Color CORRIDOR = new Color(0x46, 0x70, 0x8a);
@@ -33,47 +38,81 @@ public class MapPanel extends JComponent {
private static final Color CURRENT = new Color(0xe6, 0xc3, 0x4a); private static final Color CURRENT = new Color(0xe6, 0xc3, 0x4a);
private static final Color KNOWN = new Color(0x3a, 0x46, 0x55); private static final Color KNOWN = new Color(0x3a, 0x46, 0x55);
private final Font font; private final Font baseFont;
private float scale = 1f;
private MapView view = new MapView(List.of(), List.of()); private MapView view = new MapView(List.of(), List.of());
public MapPanel(Font font) { public MapPanel(Font font) {
this.font = font.deriveFont(11f); this.baseFont = font;
setBackground(BG); setBackground(BG);
setOpaque(true); setOpaque(true);
} }
/** Sets the rendering scale (HiDPI × zoom) and re-lays-out the panel. */
public void setScale(float scale) {
SwingUtilities.invokeLater(() -> {
this.scale = Math.max(0.4f, scale);
recomputeSize();
repaint();
});
}
/** Replaces the rendered map and scrolls the current room into view. */ /** Replaces the rendered map and scrolls the current room into view. */
public void show(MapView v) { public void show(MapView v) {
SwingUtilities.invokeLater(() -> { SwingUtilities.invokeLater(() -> {
this.view = v; this.view = v;
int maxX = 0; recomputeSize();
int maxY = 0;
for (RoomCell c : v.cells()) {
maxX = Math.max(maxX, c.x());
maxY = Math.max(maxY, c.y());
}
setPreferredSize(new Dimension(
PAD * 2 + (maxX + 1) * CELL_W + maxX * GAP_X,
PAD * 2 + (maxY + 1) * CELL_H + maxY * GAP_Y));
revalidate();
repaint(); repaint();
v.cells().stream().filter(c -> c.state() == CellState.CURRENT).findFirst() v.cells().stream().filter(c -> c.state() == CellState.CURRENT).findFirst()
.ifPresent(c -> scrollRectToVisible(boundsExpanded(c))); .ifPresent(c -> scrollRectToVisible(boundsExpanded(c)));
}); });
} }
private int cellW() {
return Math.round(BASE_CELL_W * scale);
}
private int cellH() {
return Math.round(BASE_CELL_H * scale);
}
private int gapX() {
return Math.round(BASE_GAP_X * scale);
}
private int gapY() {
return Math.round(BASE_GAP_Y * scale);
}
private int pad() {
return Math.round(BASE_PAD * scale);
}
private void recomputeSize() {
int maxX = 0;
int maxY = 0;
for (RoomCell c : view.cells()) {
maxX = Math.max(maxX, c.x());
maxY = Math.max(maxY, c.y());
}
setPreferredSize(new Dimension(
pad() * 2 + (maxX + 1) * cellW() + maxX * gapX(),
pad() * 2 + (maxY + 1) * cellH() + maxY * gapY()));
revalidate();
}
private Rectangle boundsExpanded(RoomCell c) { private Rectangle boundsExpanded(RoomCell c) {
int x = PAD + c.x() * (CELL_W + GAP_X); int x = pad() + c.x() * (cellW() + gapX());
int y = PAD + c.y() * (CELL_H + GAP_Y); int y = pad() + c.y() * (cellH() + gapY());
return new Rectangle(x - GAP_X, y - GAP_Y, CELL_W + 2 * GAP_X, CELL_H + 2 * GAP_Y); return new Rectangle(x - gapX(), y - gapY(), cellW() + 2 * gapX(), cellH() + 2 * gapY());
} }
private int cx(RoomCell c) { private int cx(RoomCell c) {
return PAD + c.x() * (CELL_W + GAP_X); return pad() + c.x() * (cellW() + gapX());
} }
private int cy(RoomCell c) { private int cy(RoomCell c) {
return PAD + c.y() * (CELL_H + GAP_Y); return pad() + c.y() * (cellH() + gapY());
} }
@Override @Override
@@ -82,22 +121,22 @@ public class MapPanel extends JComponent {
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.setColor(BG); g.setColor(BG);
g.fillRect(0, 0, getWidth(), getHeight()); g.fillRect(0, 0, getWidth(), getHeight());
g.setFont(font); g.setFont(baseFont.deriveFont(BASE_FONT * scale));
Stroke solid = new BasicStroke(2f); int cellW = cellW();
Stroke dashed = new BasicStroke(1.5f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, int cellH = cellH();
10f, new float[]{4f, 3f}, 0f); int arc = Math.round(12 * scale);
Stroke solid = new BasicStroke(2f * scale);
Stroke dashed = new BasicStroke(1.5f * scale, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER,
10f, new float[]{4f * scale, 3f * scale}, 0f);
// corridors first // corridors first
for (Connection cn : view.connections()) { for (Connection cn : view.connections()) {
boolean known = cn.from().state() == CellState.KNOWN || cn.to().state() == CellState.KNOWN; boolean known = cn.from().state() == CellState.KNOWN || cn.to().state() == CellState.KNOWN;
g.setStroke(known ? dashed : solid); g.setStroke(known ? dashed : solid);
g.setColor(known ? KNOWN : CORRIDOR); g.setColor(known ? KNOWN : CORRIDOR);
int x1 = cx(cn.from()) + CELL_W / 2; g.drawLine(cx(cn.from()) + cellW / 2, cy(cn.from()) + cellH / 2,
int y1 = cy(cn.from()) + CELL_H / 2; cx(cn.to()) + cellW / 2, cy(cn.to()) + cellH / 2);
int x2 = cx(cn.to()) + CELL_W / 2;
int y2 = cy(cn.to()) + CELL_H / 2;
g.drawLine(x1, y1, x2, y2);
} }
// rooms on top // rooms on top
@@ -112,18 +151,19 @@ public class MapPanel extends JComponent {
g.setColor(c.state() == CellState.CURRENT g.setColor(c.state() == CellState.CURRENT
? new Color(0x27, 0x21, 0x10) ? new Color(0x27, 0x21, 0x10)
: new Color(0x11, 0x16, 0x1f)); : new Color(0x11, 0x16, 0x1f));
g.fillRoundRect(x, y, CELL_W, CELL_H, 12, 12); g.fillRoundRect(x, y, cellW, cellH, arc, arc);
g.setStroke(c.state() == CellState.KNOWN g.setStroke(c.state() == CellState.KNOWN
? dashed ? dashed
: new BasicStroke(c.state() == CellState.CURRENT ? 2.5f : 1.5f)); : new BasicStroke((c.state() == CellState.CURRENT ? 2.5f : 1.5f) * scale));
g.setColor(stroke); g.setColor(stroke);
g.drawRoundRect(x, y, CELL_W, CELL_H, 12, 12); g.drawRoundRect(x, y, cellW, cellH, arc, arc);
String label = c.state() == CellState.KNOWN ? "?" : c.name(); String label = c.state() == CellState.KNOWN ? "?" : c.name();
int tw = g.getFontMetrics().stringWidth(label); int tw = g.getFontMetrics().stringWidth(label);
int ascent = g.getFontMetrics().getAscent();
g.setColor(c.state() == CellState.KNOWN ? KNOWN.brighter() : stroke); g.setColor(c.state() == CellState.KNOWN ? KNOWN.brighter() : stroke);
g.drawString(label, x + (CELL_W - tw) / 2, y + CELL_H / 2 + 4); g.drawString(label, x + (cellW - tw) / 2, y + (cellH + ascent) / 2 - 2);
} }
g.dispose(); g.dispose();
} }

View File

@@ -182,6 +182,7 @@ public class SwingIO implements GameIO {
output.setFont(baseFont.deriveFont(main)); output.setFont(baseFont.deriveFont(main));
input.setFont(baseFont.deriveFont(main)); input.setFont(baseFont.deriveFont(main));
hud.setFont(baseFont.deriveFont(small)); hud.setFont(baseFont.deriveFont(small));
map.setScale(uiScale * zoom);
frame.revalidate(); frame.revalidate();
frame.repaint(); frame.repaint();
} }