Skip to content

Commit

Permalink
Merge pull request #4844 from SJuliez/MekView_FontHandler
Browse files Browse the repository at this point in the history
Entity View speedup and small corrections
  • Loading branch information
SJuliez authored Oct 20, 2023
2 parents 0dc1f39 + b2429cf commit 42fcd1f
Show file tree
Hide file tree
Showing 13 changed files with 205 additions and 112 deletions.
1 change: 1 addition & 0 deletions megamek/i18n/megamek/client/messages.properties
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ tglForceClose.toolTipText=Force the secondary skill, normally Piloting, to be eq
### EntityViewPane Class
Summary.title=Summary
TRO.title=TRO
ASCard.title=AS Card



Expand Down
34 changes: 3 additions & 31 deletions megamek/src/megamek/MegaMek.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import megamek.client.ui.preferences.SuitePreferences;
import megamek.client.ui.swing.ButtonOrderPreferences;
import megamek.client.ui.swing.MegaMekGUI;
import megamek.client.ui.swing.util.FontHandler;
import megamek.common.annotations.Nullable;
import megamek.common.commandline.AbstractCommandLineParser;
import megamek.common.commandline.ClientServerCommandLineParser;
Expand All @@ -29,7 +30,6 @@
import org.apache.logging.log4j.LogManager;

import javax.swing.*;
import java.awt.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
Expand Down Expand Up @@ -340,7 +340,7 @@ public static File getQuickSaveFile()
*/
public static void initializeSuiteGraphicalSetups(final String currentProject) {
// Setup Fonts
parseFontDirectory(new File(MMConstants.FONT_DIRECTORY));
FontHandler.initialize();

// Setup Themes
UIManager.installLookAndFeel("Flat Light", "com.formdev.flatlaf.FlatLightLaf");
Expand All @@ -356,32 +356,4 @@ public static void initializeSuiteGraphicalSetups(final String currentProject) {
// Setup Button Order Preferences
ButtonOrderPreferences.getInstance().setButtonPriorities();
}

/**
* Recursively search the provided directory, attempting to create and then register truetype
* fonts from .ttf files
* @param directory the directory to parse
*/
private static void parseFontDirectory(final File directory) {
final String[] filenames = directory.list();
if (filenames == null) {
return;
}

for (final String filename : filenames) {
if (filename.toLowerCase().endsWith(MMConstants.TRUETYPE_FONT)) {
try (InputStream fis = new FileInputStream(directory.getPath() + '/' + filename)) {
GraphicsEnvironment.getLocalGraphicsEnvironment().registerFont(
Font.createFont(Font.TRUETYPE_FONT, fis));
} catch (Exception ex) {
LogManager.getLogger().error("Failed to parse font", ex);
}
} else {
final File file = new File(directory, filename);
if (file.isDirectory()) {
parseFontDirectory(file);
}
}
}
}
}
}
26 changes: 13 additions & 13 deletions megamek/src/megamek/client/ui/dialogs/SBFStatsDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import megamek.client.ui.swing.GUIPreferences;
import megamek.client.ui.swing.MMToggleButton;
import megamek.client.ui.swing.SBFStatsTablePanel;
import megamek.client.ui.swing.util.FontHandler;
import megamek.client.ui.swing.util.UIUtil;
import megamek.codeUtilities.StringUtility;
import megamek.common.Game;
Expand All @@ -40,6 +41,7 @@
import java.awt.print.PrinterJob;
import java.util.Collection;
import java.util.Objects;
import java.util.Vector;
import java.util.stream.Collectors;

/**
Expand All @@ -57,9 +59,9 @@ public class SBFStatsDialog extends AbstractDialog {
private final JButton clipBoardButton = new JButton(Messages.getString("SBFStatsDialog.copy"));
private final JButton printButton = new JButton(Messages.getString("SBFStatsDialog.print"));
private final JLabel headerFontLabel = new JLabel(Messages.getString("SBFStatsDialog.headerFont"));
private final JComboBox<String> headerFontChooser = new JComboBox<>();
private JComboBox<String> headerFontChooser;
private final JLabel valueFontLabel = new JLabel(Messages.getString("SBFStatsDialog.valueFont"));
private final JComboBox<String> valueFontChooser = new JComboBox<>();
private JComboBox<String> valueFontChooser;
private final JScrollPane scrollPane = new JScrollPane();
private final JPanel centerPanel = new JPanel();
private SBFStatsTablePanel statsPanel;
Expand Down Expand Up @@ -91,6 +93,15 @@ protected Container createCenterPane() {
optionsPanel.add(elementsToggle);
optionsPanel.add(clipBoardButton);

headerFontChooser = new JComboBox<>(new Vector<>(FontHandler.getAvailableNonSymbolFonts()));
headerFontChooser.addItem("");
valueFontChooser = new JComboBox<>(new Vector<>(FontHandler.getAvailableNonSymbolFonts()));
valueFontChooser.addItem("");
headerFontChooser.setSelectedItem(GUIPreferences.getInstance().getSbfSheetHeaderFont());
valueFontChooser.setSelectedItem(GUIPreferences.getInstance().getSbfSheetValueFont());
headerFontChooser.setFont(UIUtil.getScaledFont());
valueFontChooser.setFont(UIUtil.getScaledFont());

var printPanel = new UIUtil.FixedYPanel(new FlowLayout(FlowLayout.LEFT));
printPanel.add(Box.createHorizontalStrut(25));
printPanel.add(printButton);
Expand All @@ -110,17 +121,6 @@ protected Container createCenterPane() {
headerFontLabel.setFont(UIUtil.getScaledFont());
valueFontLabel.setFont(UIUtil.getScaledFont());

headerFontChooser.addItem("");
valueFontChooser.addItem("");
for (String family : GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames()) {
headerFontChooser.addItem(family);
valueFontChooser.addItem(family);
}
headerFontChooser.setSelectedItem(GUIPreferences.getInstance().getSbfSheetHeaderFont());
valueFontChooser.setSelectedItem(GUIPreferences.getInstance().getSbfSheetValueFont());
headerFontChooser.setFont(UIUtil.getScaledFont());
valueFontChooser.setFont(UIUtil.getScaledFont());

scrollPane.getVerticalScrollBar().setUnitIncrement(16);
centerPanel.add(Box.createVerticalStrut(15));
centerPanel.add(optionsPanel);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@
*/
package megamek.client.ui.displayWrappers;

import megamek.client.ui.swing.util.FontHandler;
import megamek.common.annotations.Nullable;

import java.awt.*;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
* FontDisplay is a display wrapper around a Font, primarily to be used in ComboBoxes. This is
Expand Down Expand Up @@ -54,9 +54,7 @@ public Font getFont() {
//endregion Getters/Setters

public static List<FontDisplay> getSortedFontDisplays() {
return Stream.of(GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames())
.map(FontDisplay::new)
.collect(Collectors.toList());
return FontHandler.getAvailableFonts().stream().map(FontDisplay::new).collect(Collectors.toList());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import megamek.client.ui.WrapLayout;
import megamek.client.ui.swing.GUIPreferences;
import megamek.client.ui.swing.MechViewPanel;
import megamek.client.ui.swing.util.FontHandler;
import megamek.client.ui.swing.util.UIUtil;
import megamek.common.Entity;
import megamek.common.MechView;
Expand All @@ -32,15 +33,15 @@
import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.util.Locale;
import java.util.Vector;

/**
* This class wraps the MechView / MechViewPanel and gives it a toolbar to choose font, open the MUL
* and copy the contents.
*/
public class ConfigurableMechViewPanel extends JPanel {

private final JComboBox<String> fontChooser = new JComboBox<>();
private final JComboBox<String> fontChooser;
private final JButton copyHtmlButton = new JButton(Messages.getString("CMVPanel.copyHTML"));
private final JButton copyTextButton = new JButton(Messages.getString("CMVPanel.copyText"));
private final JButton mulButton = new JButton(Messages.getString("CMVPanel.MUL"));
Expand All @@ -56,13 +57,8 @@ public class ConfigurableMechViewPanel extends JPanel {
public ConfigurableMechViewPanel(@Nullable Entity entity) {
setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));

fontChooser = new JComboBox<>(new Vector<>(FontHandler.getAvailableNonSymbolFonts()));
fontChooser.addItem("");
for (String family : GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames(Locale.US)) {
Font f = Font.decode(family);
if (f.canDisplayUpTo("anzANZ150/[]") == -1) {
fontChooser.addItem(family);
}
}
fontChooser.addActionListener(ev -> updateFont());
fontChooser.setSelectedItem(GUIPreferences.getInstance().getSummaryFont());

Expand Down
69 changes: 32 additions & 37 deletions megamek/src/megamek/client/ui/panes/EntityViewPane.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 - The MegaMek Team. All Rights Reserved.
* Copyright (c) 2021-2023 - The MegaMek Team. All Rights Reserved.
*
* This file is part of MegaMek.
*
Expand All @@ -23,6 +23,9 @@
import megamek.client.ui.swing.alphaStrike.ConfigurableASCardPanel;
import megamek.client.ui.swing.calculationReport.FlexibleCalculationReport;
import megamek.common.Entity;
import megamek.common.GunEmplacement;
import megamek.common.alphaStrike.ASCardDisplayable;
import megamek.common.alphaStrike.AlphaStrikeElement;
import megamek.common.alphaStrike.conversion.ASConverter;
import megamek.common.annotations.Nullable;
import megamek.common.templates.TROView;
Expand All @@ -33,8 +36,8 @@
* The EntityViewPane displays the entity summary, TRO and AS card panels within a TabbedPane.
*/
public class EntityViewPane extends AbstractTabbedPane {
private ConfigurableMechViewPanel entityPanel;
private MechViewPanel troPanel;
private final ConfigurableMechViewPanel summaryPanel = new ConfigurableMechViewPanel();
private final MechViewPanel troPanel = new MechViewPanel();
private final ConfigurableASCardPanel cardPanel = new ConfigurableASCardPanel(getFrame());

public EntityViewPane(final JFrame frame, final @Nullable Entity entity) {
Expand All @@ -43,57 +46,49 @@ public EntityViewPane(final JFrame frame, final @Nullable Entity entity) {
updateDisplayedEntity(entity);
}

public ConfigurableMechViewPanel getEntityPanel() {
return entityPanel;
}

public void setEntityPanel(final ConfigurableMechViewPanel entityPanel) {
this.entityPanel = entityPanel;
}

public MechViewPanel getTROPanel() {
return troPanel;
}

public void setTROPanel(final MechViewPanel troPanel) {
this.troPanel = troPanel;
}

/**
* This purposefully does not set preferences, as it may be used on differing panes for
* differing uses and thus you don't want to remember the selected tab between the different
* locations.
*/
@Override
protected void initialize() {
setEntityPanel(new ConfigurableMechViewPanel());
getEntityPanel().setName("entityPanel");
addTab(resources.getString("Summary.title"), getEntityPanel());
summaryPanel.setName("entityPanel");
troPanel.setName("troPanel");

setTROPanel(new MechViewPanel());
getTROPanel().setName("troPanel");
addTab(resources.getString("TRO.title"), getTROPanel());

addTab("AS Card", cardPanel);
addTab(resources.getString("Summary.title"), summaryPanel);
addTab(resources.getString("TRO.title"), troPanel);
addTab(resources.getString("ASCard.title"), cardPanel);
}

/**
* Updates the pane's currently displayed entity.
* Updates the pane's currently displayed entity in all tabs. Performs Alpha Strike conversion if possible.
*
* @param entity the entity to update to, or null if the panels are to be emptied.
*/
public void updateDisplayedEntity(final @Nullable Entity entity) {
if (entity == null) {
getEntityPanel().reset();
getTROPanel().reset();
} else {
getEntityPanel().setEntity(entity);
getTROPanel().setMech(entity, TROView.createView(entity, true));
}
AlphaStrikeElement asUnit = null;
if (ASConverter.canConvert(entity)) {
cardPanel.setASElement(ASConverter.convert(entity, new FlexibleCalculationReport()));
asUnit = ASConverter.convert(entity, new FlexibleCalculationReport());
}
updateDisplayedEntity(entity, asUnit);
}

/**
* Updates the pane's currently displayed entity / AS unit to the respective given units. The method
* assumes that asUnit corresponds to entity and does no conversion. When the AS Element or MechSummary
* is available, passing it in as asUnit saves the time for AS conversion.
*
* @param entity the entity to update to, or null if the panels are to be emptied.
* @param asUnit the Alpha Strike unit corresponding to entity (may be a MechSummary)
*/
public void updateDisplayedEntity(final @Nullable Entity entity, @Nullable ASCardDisplayable asUnit) {
if (entity == null) {
troPanel.reset();
} else {
cardPanel.setASElement(null);
troPanel.setMech(entity, TROView.createView(entity, true));
}
summaryPanel.setEntity(entity);
cardPanel.setASElement(ASConverter.canConvert(entity) ? asUnit : null);
}
}
8 changes: 3 additions & 5 deletions megamek/src/megamek/client/ui/swing/CommonSettingsDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import megamek.client.ui.baseComponents.MMComboBox;
import megamek.client.ui.swing.StatusBarPhaseDisplay.PhaseCommand;
import megamek.client.ui.swing.unitDisplay.UnitDisplay;
import megamek.client.ui.swing.util.FontHandler;
import megamek.client.ui.swing.util.KeyCommandBind;
import megamek.client.ui.swing.util.PlayerColour;
import megamek.client.ui.swing.util.UIUtil;
Expand Down Expand Up @@ -199,7 +200,7 @@ private <T> void moveElement(DefaultListModel<T> srcModel, int srcIndex, int trg
private ColourSelectorButton csbMoveBackColor;
private ColourSelectorButton csbMoveSprintColor;

private final JComboBox<String> fontTypeChooserMoveFont = new JComboBox<>();
private JComboBox<String> fontTypeChooserMoveFont = new JComboBox<>();
private JTextField moveFontSize;
private final JComboBox<String> fontStyleChooserMoveFont = new JComboBox<>();

Expand Down Expand Up @@ -674,10 +675,7 @@ private JPanel getGameBoardPanel() {

addLineSpacer(comps);

for (String family : GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames()) {
fontTypeChooserMoveFont.addItem(family);
}

fontTypeChooserMoveFont = new JComboBox<>(new Vector<>(FontHandler.getAvailableNonSymbolFonts()));
fontTypeChooserMoveFont.setSelectedItem(GUIP.getMoveFontType());

JLabel moveFontTypeLabel = new JLabel(Messages.getString("CommonSettingsDialog.moveFontType"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import megamek.client.ui.WrapLayout;
import megamek.client.ui.dialogs.ASConversionInfoDialog;
import megamek.client.ui.swing.GUIPreferences;
import megamek.client.ui.swing.util.FontHandler;
import megamek.client.ui.swing.util.UIUtil;
import megamek.common.alphaStrike.ASCardDisplayable;
import megamek.common.alphaStrike.ASStatsExporter;
Expand All @@ -34,6 +35,7 @@
import java.awt.*;
import java.awt.datatransfer.*;
import java.util.List;
import java.util.Vector;

/**
* This is a JPanel that displays an AlphaStrike unit card and elements to configure the display of
Expand All @@ -42,7 +44,7 @@
*/
public class ConfigurableASCardPanel extends JPanel {

private final JComboBox<String> fontChooser = new JComboBox<>();
private final JComboBox<String> fontChooser;
private final JComboBox<Float> sizeChooser = new JComboBox<>();
private final JButton copyButton = new JButton(Messages.getString("CASCardPanel.copyCard"));
private final JButton copyStatsButton = new JButton(Messages.getString("CASCardPanel.copyStats"));
Expand All @@ -63,10 +65,8 @@ public ConfigurableASCardPanel(@Nullable ASCardDisplayable element, JFrame paren
this.parent = parent;
setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));

fontChooser = new JComboBox<>(new Vector<>(FontHandler.getAvailableNonSymbolFonts()));
fontChooser.addItem("");
for (String family : GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames()) {
fontChooser.addItem(family);
}
fontChooser.addActionListener(ev -> updateFont());
fontChooser.setSelectedItem(GUIPreferences.getInstance().getAsCardFont());

Expand Down
Loading

0 comments on commit 42fcd1f

Please sign in to comment.