Skip to content

Commit

Permalink
Merge pull request #4822 from SJuliez/MekView
Browse files Browse the repository at this point in the history
MekView updates
  • Loading branch information
SJuliez authored Oct 18, 2023
2 parents bda4af0 + f8126fc commit fc1fc74
Show file tree
Hide file tree
Showing 11 changed files with 349 additions and 141 deletions.
6 changes: 6 additions & 0 deletions megamek/i18n/megamek/client/messages.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4460,6 +4460,12 @@ CASCardPanel.conversionReport=Conversion Report
CASCardPanel.font=Font:
CASCardPanel.cardSize=Card Size:

#ConfigurableMechViewPanel
CMVPanel.copyHTML=Copy as HTML
CMVPanel.copyText=Copy as Text
CMVPanel.MUL=Open MUL
CMVPanel.font=Font:

#Gamemaster Menu Text
Gamemaster.Gamemaster=Gamemaster
Gamemaster.EditDamage=Edit Damage
Expand Down
3 changes: 3 additions & 0 deletions megamek/src/megamek/MMConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ public final class MMConstants extends SuiteConstants {
//region General Constants
public static final String PROJECT_NAME = "MegaMek";
public static final String MUL_URL_PREFIX = "http://www.masterunitlist.info/Unit/Details/";
public static final String BT_URL_SHRAPNEL = "https://bg.battletech.com/shrapnel/";
/** When this text is found in the source field, the Mek View will display a link to {@link #BT_URL_SHRAPNEL} */
public static final String SOURCE_TEXT_SHRAPNEL = "Shrapnel";
//endregion General Constants

//region GUI Constants
Expand Down
17 changes: 15 additions & 2 deletions megamek/src/megamek/client/ui/dialogs/EntityReadoutDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,13 @@
import megamek.client.ui.panes.EntityViewPane;
import megamek.client.ui.preferences.JTabbedPanePreference;
import megamek.client.ui.preferences.PreferencesNode;
import megamek.client.ui.swing.util.UIUtil;
import megamek.common.*;

/** A dialog showing the unit readout for a given unit. */
/**
* A dialog showing the unit readout for a given unit. It shows an {@link EntityViewPane} with the entity summary,
* TRO and AS card panels within a TabbedPane.
*/
public class EntityReadoutDialog extends AbstractDialog {

private final Entity entity;
Expand Down Expand Up @@ -57,4 +61,13 @@ protected void setCustomPreferences(final PreferencesNode preferences) throws Ex
super.setCustomPreferences(preferences);
preferences.manage(new JTabbedPanePreference(entityView));
}
}

@Override
public void setVisible(boolean visible) {
if (visible) {
UIUtil.adjustDialog(this, UIUtil.FONT_SCALE1);
}

super.setVisible(visible);
}
}
138 changes: 138 additions & 0 deletions megamek/src/megamek/client/ui/panes/ConfigurableMechViewPanel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
/*
* Copyright (c) 2023 - 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 megamek.client.ui.panes;

import megamek.MMConstants;
import megamek.client.ui.Messages;
import megamek.client.ui.WrapLayout;
import megamek.client.ui.swing.GUIPreferences;
import megamek.client.ui.swing.MechViewPanel;
import megamek.client.ui.swing.util.UIUtil;
import megamek.common.Entity;
import megamek.common.MechView;
import megamek.common.annotations.Nullable;

import javax.swing.*;
import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.util.Locale;

/**
* 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 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"));
private final MechViewPanel mechViewPanel = new MechViewPanel();
private int mulId;
private Entity entity;

/**
* Constructs a panel with the given unit to display.
*
* @param entity The Entity to display
*/
public ConfigurableMechViewPanel(@Nullable Entity entity) {
setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));

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());

copyHtmlButton.addActionListener(ev -> copyToClipboard(true));
copyTextButton.addActionListener(ev -> copyToClipboard(false));

mulButton.addActionListener(ev -> UIUtil.showMUL(mulId, this));
mulButton.setToolTipText("Show the Master Unit List entry for this unit. Opens a browser window.");

var chooserLine = new UIUtil.FixedYPanel(new WrapLayout(FlowLayout.LEFT, 15, 10));
JPanel fontChooserPanel = new JPanel();
fontChooserPanel.add(new JLabel(Messages.getString("CMVPanel.font")));
fontChooserPanel.add(fontChooser);
chooserLine.add(fontChooserPanel);
chooserLine.add(copyHtmlButton);
chooserLine.add(copyTextButton);
chooserLine.add(mulButton);

add(chooserLine);
add(mechViewPanel);
setEntity(entity);
}

/** Construct a new panel without a unit to display. */
public ConfigurableMechViewPanel() {
this(null);
}

/**
* Set the panel to display the given element.
*
* @param entity The Entity to display
*/
public void setEntity(@Nullable Entity entity) {
this.entity = entity;
mulId = (entity != null) ? entity.getMulId() : -1;
mulButton.setEnabled(mulId > 0);
copyTextButton.setEnabled(entity != null);
copyHtmlButton.setEnabled(entity != null);
if (entity != null) {
mechViewPanel.setMech(entity, GUIPreferences.getInstance().getSummaryFont());
} else {
mechViewPanel.reset();
}
}

/** Set the card to use a newly selected font. */
private void updateFont() {
if (entity != null) {
String selectedItem = (String) fontChooser.getSelectedItem();
if ((selectedItem == null) || selectedItem.isBlank()) {
mechViewPanel.setMech(entity, MMConstants.FONT_SANS_SERIF);
GUIPreferences.getInstance().setSummaryFont("");
} else {
mechViewPanel.setMech(entity, selectedItem);
GUIPreferences.getInstance().setSummaryFont(selectedItem);
}
}
}

public void reset() {
mechViewPanel.reset();
}

private void copyToClipboard(boolean asHtml) {
if (entity != null) {
MechView mechView = new MechView(entity, false, false, asHtml);
StringSelection stringSelection = new StringSelection(mechView.getMechReadout());
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents(stringSelection, null);
}
}
}
28 changes: 10 additions & 18 deletions megamek/src/megamek/client/ui/panes/EntityViewPane.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,29 +30,24 @@
import javax.swing.*;

/**
* The EntityViewPane displays the Entity Summary and the TRO panels within a Tabbed Pane.
* The EntityViewPane displays the entity summary, TRO and AS card panels within a TabbedPane.
*/
public class EntityViewPane extends AbstractTabbedPane {
//region Variable Declarations
private MechViewPanel entityPanel;
private ConfigurableMechViewPanel entityPanel;
private MechViewPanel troPanel;
private final ConfigurableASCardPanel cardPanel = new ConfigurableASCardPanel(getFrame());
//endregion Variable Declarations

//region Constructors
public EntityViewPane(final JFrame frame, final @Nullable Entity entity) {
super(frame, "EntityViewPane");
initialize();
updateDisplayedEntity(entity);
}
//endregion Constructors

//region Getters/Setters
public MechViewPanel getEntityPanel() {
public ConfigurableMechViewPanel getEntityPanel() {
return entityPanel;
}

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

Expand All @@ -63,17 +58,15 @@ public MechViewPanel getTROPanel() {
public void setTROPanel(final MechViewPanel troPanel) {
this.troPanel = troPanel;
}
//endregion Getters/Setters

//region Initialization
/**
* 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 MechViewPanel());
setEntityPanel(new ConfigurableMechViewPanel());
getEntityPanel().setName("entityPanel");
addTab(resources.getString("Summary.title"), getEntityPanel());

Expand All @@ -83,19 +76,18 @@ protected void initialize() {

addTab("AS Card", cardPanel);
}
//endregion Initialization

/**
* This updates the pane's currently displayed entity
* @param entity the entity to update to, or null if the panels are to be reset.
* Updates the pane's currently displayed entity.
*
* @param entity the entity to update to, or null if the panels are to be emptied.
*/
public void updateDisplayedEntity(final @Nullable Entity entity) {
// Null entity, which means to reset the panels
if (entity == null) {
getEntityPanel().reset();
getTROPanel().reset();
} else {
getEntityPanel().setMech(entity, false);
getEntityPanel().setEntity(entity);
getTROPanel().setMech(entity, TROView.createView(entity, true));
}
if (ASConverter.canConvert(entity)) {
Expand All @@ -104,4 +96,4 @@ public void updateDisplayedEntity(final @Nullable Entity entity) {
cardPanel.setASElement(null);
}
}
}
}
11 changes: 11 additions & 0 deletions megamek/src/megamek/client/ui/swing/GUIPreferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,8 @@ public class GUIPreferences extends PreferenceStoreProxy {
public static final String AS_CARD_SIZE = "AsCardSize";
public static final String SBFSHEET_HEADERFONT = "SBFSheetHeaderFont";
public static final String SBFSHEET_VALUEFONT = "SBFSheetValueFont";
public static final String SUMMARY_FONT = "SummaryCardFont";


// RAT dialog preferences
public static String RAT_TECH_LEVEL = "RATTechLevel";
Expand Down Expand Up @@ -718,6 +720,7 @@ protected GUIPreferences() {
setDefault(AS_CARD_SIZE, 0.75f);
setDefault(SBFSHEET_HEADERFONT, "");
setDefault(SBFSHEET_VALUEFONT, "");
setDefault(SUMMARY_FONT, "");
}

public void setDefault(String name, Color color) {
Expand Down Expand Up @@ -1438,6 +1441,10 @@ public String getAsCardFont() {
return store.getString(AS_CARD_FONT);
}

public String getSummaryFont() {
return store.getString(SUMMARY_FONT);
}

public String getSbfSheetHeaderFont() {
return store.getString(SBFSHEET_HEADERFONT);
}
Expand Down Expand Up @@ -2211,6 +2218,10 @@ public void setAsCardFont(String asCardFont) {
store.setValue(AS_CARD_FONT, asCardFont);
}

public void setSummaryFont(String summaryFont) {
store.setValue(SUMMARY_FONT, summaryFont);
}

public void setAsCardSize(float size) {
store.setValue(AS_CARD_SIZE, size);
}
Expand Down
25 changes: 16 additions & 9 deletions megamek/src/megamek/client/ui/swing/MechViewPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package megamek.client.ui.swing;

import megamek.client.ui.swing.util.FluffImageHelper;
import megamek.client.ui.swing.util.UIUtil;
import megamek.client.ui.swing.util.UIUtil.FixedXPanel;
import megamek.common.Entity;
import megamek.common.MechView;
Expand Down Expand Up @@ -60,18 +61,13 @@ public MechViewPanel(int width, int height, boolean noBorder) {
txtMek.setMinimumSize(new Dimension(width, height));
txtMek.setPreferredSize(new Dimension(width, height));
txtMek.addHyperlinkListener(e -> {
try {
if (HyperlinkEvent.EventType.ACTIVATED == e.getEventType()) {
if (Desktop.isDesktopSupported() && Desktop.getDesktop().isSupported(Desktop.Action.BROWSE)) {
Desktop.getDesktop().browse(e.getURL().toURI());
}
}
} catch (Exception ex) {
LogManager.getLogger().error("", ex);
JOptionPane.showMessageDialog(this, ex.getMessage(), "ERROR", JOptionPane.ERROR_MESSAGE);
if (HyperlinkEvent.EventType.ACTIVATED == e.getEventType()) {
UIUtil.browse(e.getURL().toString(), this);
}
});
scrMek = new JScrollPane(txtMek);
scrMek.getVerticalScrollBar().setUnitIncrement(16);

if (noBorder) {
scrMek.setBorder(null);
}
Expand Down Expand Up @@ -104,6 +100,12 @@ public void setMech(Entity entity, MechView mechView) {
setFluffImage(entity);
}

public void setMech(Entity entity, MechView mechView, String fontName) {
txtMek.setText(mechView.getMechReadout(fontName));
txtMek.setCaretPosition(0);
setFluffImage(entity);
}

public void setMech(Entity entity, TROView troView) {
txtMek.setText(troView.processTemplate());
txtMek.setCaretPosition(0);
Expand All @@ -115,6 +117,11 @@ public void setMech(Entity entity, boolean useAlternateCost) {
setMech(entity, mechView);
}

public void setMech(Entity entity, String fontName) {
MechView mechView = new MechView(entity, false, false);
setMech(entity, mechView, fontName);
}

private void setFluffImage(Entity entity) {
Image image = FluffImageHelper.getFluffImage(entity);
// Scale down to the default width if the image is wider than that
Expand Down
Loading

0 comments on commit fc1fc74

Please sign in to comment.