From 2e415d714bc28109512e0aa928f19d6df559f8aa Mon Sep 17 00:00:00 2001 From: Pavel Braginskiy Date: Wed, 10 Jul 2024 16:41:25 -0700 Subject: [PATCH] Add record sheet display to preview tab --- .../megameklab/printing/PrintRecordSheet.java | 4 +- .../megameklab/ui/generalUnit/PreviewTab.java | 4 + .../generalUnit/RecordSheetPreviewPanel.java | 85 +++++++++++++++++++ .../src/megameklab/util/UnitPrintManager.java | 2 +- 4 files changed, 92 insertions(+), 3 deletions(-) create mode 100644 megameklab/src/megameklab/ui/generalUnit/RecordSheetPreviewPanel.java diff --git a/megameklab/src/megameklab/printing/PrintRecordSheet.java b/megameklab/src/megameklab/printing/PrintRecordSheet.java index c2a9eaaef..92e02bce2 100644 --- a/megameklab/src/megameklab/printing/PrintRecordSheet.java +++ b/megameklab/src/megameklab/printing/PrintRecordSheet.java @@ -304,7 +304,7 @@ private void shadeTableRows() { /** * @return true if the document was created successfully, otherwise false */ - protected boolean createDocument(int pageIndex, PageFormat pageFormat, boolean addMargin) { + public boolean createDocument(int pageIndex, PageFormat pageFormat, boolean addMargin) { setSVGDocument(loadTemplate(pageIndex, pageFormat)); if (getSVGDocument() == null) { return false; @@ -386,7 +386,7 @@ public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) { return new ByteArrayInputStream(output.toByteArray()); } - protected GraphicsNode build() { + public GraphicsNode build() { GVTBuilder builder = new GVTBuilder(); BridgeContext ctx = new BridgeContext(new UserAgentAdapter() { @Override diff --git a/megameklab/src/megameklab/ui/generalUnit/PreviewTab.java b/megameklab/src/megameklab/ui/generalUnit/PreviewTab.java index 284868f4d..2c5da59e0 100644 --- a/megameklab/src/megameklab/ui/generalUnit/PreviewTab.java +++ b/megameklab/src/megameklab/ui/generalUnit/PreviewTab.java @@ -42,6 +42,7 @@ public class PreviewTab extends ITab { private final ConfigurableMechViewPanel panelMekView = new ConfigurableMechViewPanel(); private final MechViewPanel panelTROView = new MechViewPanel(); private final ConfigurableASCardPanel cardPanel = new ConfigurableASCardPanel(null); + private final RecordSheetPreviewPanel rsPanel = new RecordSheetPreviewPanel(); public PreviewTab(EntitySource eSource) { super(eSource); @@ -50,6 +51,7 @@ public PreviewTab(EntitySource eSource) { panPreview.addTab("Summary", panelMekView); panPreview.addTab("TRO", panelTROView); panPreview.addTab("AS Card", cardPanel); + panPreview.addTab("Record Sheet", rsPanel); add(panPreview, BorderLayout.CENTER); addComponentListener(refreshOnShow); } @@ -74,10 +76,12 @@ public void update() { } else { cardPanel.setASElement(null); } + rsPanel.setEntity(selectedUnit); } else { panelMekView.reset(); panelTROView.reset(); cardPanel.setASElement(null); + rsPanel.setEntity(null); } } diff --git a/megameklab/src/megameklab/ui/generalUnit/RecordSheetPreviewPanel.java b/megameklab/src/megameklab/ui/generalUnit/RecordSheetPreviewPanel.java new file mode 100644 index 000000000..4e916df5c --- /dev/null +++ b/megameklab/src/megameklab/ui/generalUnit/RecordSheetPreviewPanel.java @@ -0,0 +1,85 @@ +/* + * Copyright (c) 2024 - The MegaMek Team. All Rights Reserved. + * + * This file is part of MegaMek. + * + * MegaMek is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * MegaMek is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with MegaMek. If not, see . + */ + +package megameklab.ui.generalUnit; + +import megamek.common.Entity; +import megameklab.printing.PrintRecordSheet; +import megameklab.printing.RecordSheetOptions; +import megameklab.util.UnitPrintManager; +import org.apache.batik.gvt.GraphicsNode; + +import javax.swing.*; +import java.awt.*; +import java.awt.geom.AffineTransform; +import java.awt.print.PageFormat; +import java.util.List; + + +/** + * @author pavelbraginskiy + * + * Simply fills itself with the record sheet for the given unit. + */ +public class RecordSheetPreviewPanel extends JPanel { + private Entity entity; + + public void setEntity(Entity entity) { + this.entity = entity; + } + + @Override + public void paintComponent(Graphics g) { + super.paintComponent(g); + Graphics2D g2d = (Graphics2D) g; + + // Set render quality, antialiasing is extremely necessary + RenderingHints rh = new RenderingHints(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); + rh.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); + rh.put(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); + g2d.setRenderingHints(rh); + + // White background for image + g2d.setColor(Color.WHITE); + g2d.clearRect(0, 0, getWidth(), getHeight()); + + + if (entity != null) { + RecordSheetOptions options = new RecordSheetOptions(); + PrintRecordSheet sheet = UnitPrintManager.createSheets(List.of(entity), true, options).get(0); + + // 5-pixel margin around rs + PageFormat pf = new PageFormat(); + pf.setPaper(options.getPaperSize().createPaper(5, 5, 5, 5)); + sheet.createDocument(0, pf, true); + + GraphicsNode gn = sheet.build(); + + // Scale record sheet to the size of the panel, taking into account the 10 pixels taken up by margin + var bounds = gn.getBounds(); + var yscale = (getHeight() - 10) / bounds.getHeight(); + var xscale = (getWidth() - 10) / bounds.getWidth(); + var scale = Math.min(yscale, xscale); + gn.setTransform(AffineTransform.getScaleInstance(scale, scale)); + + // Draw the completed record sheet SVG to the panel + gn.paint(g2d); + } + } +} diff --git a/megameklab/src/megameklab/util/UnitPrintManager.java b/megameklab/src/megameklab/util/UnitPrintManager.java index b09c7c479..a70b6c2ad 100644 --- a/megameklab/src/megameklab/util/UnitPrintManager.java +++ b/megameklab/src/megameklab/util/UnitPrintManager.java @@ -113,7 +113,7 @@ private static File getExportFile(Frame parent, String suggestedFileName) { return f.getSelectedFile(); } - private static List createSheets(List entities, boolean singlePrint, + public static List createSheets(List entities, boolean singlePrint, RecordSheetOptions options) { List sheets = new ArrayList<>(); List infList = new ArrayList<>();