Skip to content

Commit

Permalink
Add discord-formatted export support
Browse files Browse the repository at this point in the history
  • Loading branch information
pavelbraginskiy committed May 31, 2024
1 parent 03be022 commit 458baf8
Show file tree
Hide file tree
Showing 6 changed files with 250 additions and 60 deletions.
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.MechView.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: 3 additions & 1 deletion megamek/src/megamek/client/ui/panes/EntityViewPane.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import megamek.client.ui.swing.calculationReport.FlexibleCalculationReport;
import megamek.common.Entity;
import megamek.common.GunEmplacement;
import megamek.common.MechView;
import megamek.common.MechView.ViewFormatting;
import megamek.common.alphaStrike.ASCardDisplayable;
import megamek.common.alphaStrike.AlphaStrikeElement;
import megamek.common.alphaStrike.conversion.ASConverter;
Expand Down Expand Up @@ -86,7 +88,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
17 changes: 12 additions & 5 deletions megamek/src/megamek/common/Entity.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import megamek.client.ui.swing.calculationReport.CalculationReport;
import megamek.client.ui.swing.calculationReport.DummyCalculationReport;
import megamek.codeUtilities.StringUtility;
import megamek.common.MechView.ViewFormatting;
import megamek.common.MovePath.MoveStepType;
import megamek.common.actions.*;
import megamek.common.annotations.Nullable;
Expand All @@ -37,6 +38,8 @@
import megamek.common.planetaryconditions.PlanetaryConditions;
import megamek.common.planetaryconditions.Wind;
import megamek.common.preference.PreferenceManager;
import megamek.common.util.DiscordExportUtil;
import megamek.common.util.DiscordExportUtil.DiscordFormat;
import megamek.common.weapons.*;
import megamek.common.weapons.bayweapons.AR10BayWeapon;
import megamek.common.weapons.bayweapons.BayWeapon;
Expand Down Expand Up @@ -8897,7 +8900,7 @@ public void resetTransporter() {

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

@Override
Expand Down Expand Up @@ -8928,8 +8931,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 +8945,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 +8966,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

0 comments on commit 458baf8

Please sign in to comment.