Skip to content

Commit

Permalink
Merge pull request #1553 from pavelbraginskiy/rs-preview
Browse files Browse the repository at this point in the history
Add record sheet display to preview tab
  • Loading branch information
IllianiCBT committed Jul 11, 2024
2 parents 07b4362 + 2e415d7 commit 3e752b4
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 3 deletions.
4 changes: 2 additions & 2 deletions megameklab/src/megameklab/printing/PrintRecordSheet.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions megameklab/src/megameklab/ui/generalUnit/PreviewTab.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
}
Expand All @@ -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);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
*/

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);
}
}
}
2 changes: 1 addition & 1 deletion megameklab/src/megameklab/util/UnitPrintManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ private static File getExportFile(Frame parent, String suggestedFileName) {
return f.getSelectedFile();
}

private static List<PrintRecordSheet> createSheets(List<? extends BTObject> entities, boolean singlePrint,
public static List<PrintRecordSheet> createSheets(List<? extends BTObject> entities, boolean singlePrint,
RecordSheetOptions options) {
List<PrintRecordSheet> sheets = new ArrayList<>();
List<Infantry> infList = new ArrayList<>();
Expand Down

0 comments on commit 3e752b4

Please sign in to comment.