Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add discord-formatted export support #5533

Merged
merged 2 commits into from
Jun 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import megamek.client.ui.swing.util.UIUtil;
import megamek.common.Entity;
import megamek.common.MechView;
import megamek.common.ViewFormatting;
import megamek.common.annotations.Nullable;

import javax.swing.*;
Expand Down Expand Up @@ -62,8 +63,10 @@ public ConfigurableMechViewPanel(@Nullable Entity entity) {
fontChooser.addActionListener(ev -> updateFont());
fontChooser.setSelectedItem(GUIPreferences.getInstance().getSummaryFont());

copyHtmlButton.addActionListener(ev -> copyToClipboard(true));
copyTextButton.addActionListener(ev -> copyToClipboard(false));
copyHtmlButton.addActionListener(ev -> copyToClipboard(ViewFormatting.HTML));
copyTextButton.addActionListener(ev -> copyToClipboard(ViewFormatting.NONE));
// todo: create a copyDiscordButton
// The implementer of the Discord export cared only about the MML UI.

mulButton.addActionListener(ev -> UIUtil.showMUL(mulId, this));
mulButton.setToolTipText("Show the Master Unit List entry for this unit. Opens a browser window.");
Expand Down Expand Up @@ -123,9 +126,9 @@ public void reset() {
mechViewPanel.reset();
}

private void copyToClipboard(boolean asHtml) {
private void copyToClipboard(ViewFormatting formatting) {
if (entity != null) {
MechView mechView = new MechView(entity, false, false, asHtml);
MechView mechView = new MechView(entity, false, false, formatting);
StringSelection stringSelection = new StringSelection(mechView.getMechReadout());
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents(stringSelection, null);
Expand Down
4 changes: 2 additions & 2 deletions megamek/src/megamek/client/ui/panes/EntityViewPane.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
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.ViewFormatting;
import megamek.common.alphaStrike.ASCardDisplayable;
import megamek.common.alphaStrike.AlphaStrikeElement;
import megamek.common.alphaStrike.conversion.ASConverter;
Expand Down Expand Up @@ -86,7 +86,7 @@ public void updateDisplayedEntity(final @Nullable Entity entity, @Nullable ASCar
if (entity == null) {
troPanel.reset();
} else {
troPanel.setMech(entity, TROView.createView(entity, true));
troPanel.setMech(entity, TROView.createView(entity, ViewFormatting.HTML));
}
summaryPanel.setEntity(entity);
cardPanel.setASElement(ASConverter.canConvert(entity) ? asUnit : null);
Expand Down
15 changes: 10 additions & 5 deletions megamek/src/megamek/common/Entity.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import megamek.common.planetaryconditions.PlanetaryConditions;
import megamek.common.planetaryconditions.Wind;
import megamek.common.preference.PreferenceManager;
import megamek.common.util.DiscordFormat;
import megamek.common.weapons.*;
import megamek.common.weapons.bayweapons.AR10BayWeapon;
import megamek.common.weapons.bayweapons.BayWeapon;
Expand Down Expand Up @@ -8897,7 +8898,7 @@ public void resetTransporter() {

@Override
public String getUnusedString() {
return getUnusedString(false);
return getUnusedString(ViewFormatting.NONE);
}

@Override
Expand Down Expand Up @@ -8928,8 +8929,8 @@ public double getUnused(Entity e) {
*
* @return A <code>String</code> meant for a human.
*/
public String getUnusedString(boolean ishtml) {
StringBuffer result = new StringBuffer();
public String getUnusedString(ViewFormatting formatting) {
StringBuilder result = new StringBuilder();

// Walk through this entity's transport components;
// add all of their string to ours.
Expand All @@ -8942,10 +8943,14 @@ public String getUnusedString(boolean ishtml) {
if ((next instanceof DockingCollar) && ((DockingCollar) next).isDamaged()) {
continue;
}
if (ishtml && (next instanceof Bay) && (((Bay) next).getBayDamage() > 0)) {
if (formatting == ViewFormatting.HTML && (next instanceof Bay) && (((Bay) next).getBayDamage() > 0)) {
result.append("<font color='red'>")
.append(next.getUnusedString())
.append("</font>");
} else if (formatting == ViewFormatting.DISCORD && (next instanceof Bay) && (((Bay) next).getBayDamage() > 0)) {
result.append(DiscordFormat.RED.format())
.append(next.getUnusedString())
.append(DiscordFormat.RESET.format());
} else {
result.append(next.getUnusedString());
}
Expand All @@ -8959,7 +8964,7 @@ public String getUnusedString(boolean ishtml) {
}
// Add a newline character between strings.
if (iter.hasMoreElements()) {
if (ishtml) {
if (formatting == ViewFormatting.HTML) {
result.append("<br>");
} else {
result.append("\n");
Expand Down
Loading
Loading