Skip to content

Commit

Permalink
Unify the status bars
Browse files Browse the repository at this point in the history
  • Loading branch information
SJuliez committed Dec 21, 2023
1 parent 37aa5ff commit 262fa97
Show file tree
Hide file tree
Showing 16 changed files with 400 additions and 1,339 deletions.
2 changes: 2 additions & 0 deletions megameklab/src/megameklab/ui/MegaMekLabMainUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -184,4 +184,6 @@ private void performRefresh() {
refreshAll();
}
}

public abstract JDialog getFloatingEquipmentDatabase();
}
152 changes: 25 additions & 127 deletions megameklab/src/megameklab/ui/battleArmor/BAStatusBar.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,153 +16,51 @@

package megameklab.ui.battleArmor;

import megamek.client.ui.WrapLayout;
import megamek.common.BattleArmor;
import megamek.common.verifier.EntityVerifier;
import megamek.common.verifier.TestBattleArmor;
import megameklab.ui.util.ITab;
import megameklab.ui.util.RefreshListener;
import megameklab.util.ImageHelper;
import megameklab.ui.generalUnit.StatusBar;
import megameklab.util.UnitUtil;

import javax.swing.*;
import java.awt.*;
import java.io.File;
import java.text.DecimalFormat;

public class BAStatusBar extends ITab {
public class BAStatusBar extends StatusBar {

private static final String MOVE_LABEL = "Movement: %d / %d";
private static final String WEIGHT_LABEL_SUIT = "Suit Weight: %,.0f kg/%,.0f kg";
private static final String REMAINDER = " (%,.0f kg Remaining)";

private final JPanel tonnagePanel = new JPanel();
private final JPanel movementPanel = new JPanel();
private final JPanel bvPanel = new JPanel();

private final JLabel move = new JLabel();
private final JLabel bvLabel = new JLabel();
private final JLabel tons = new JLabel();
private final JLabel cost = new JLabel();
private final JLabel invalid = new JLabel();
private final EntityVerifier entityVerifier = EntityVerifier.getInstance(new File(
"data/mechfiles/UnitVerifierOptions.xml"));
private final DecimalFormat formatter;
private final JFrame parentFrame;
private final JLabel suitWeight = new JLabel();

private RefreshListener refresh;
public BAStatusBar(final BAMainUI parent) {
super(parent);
parentFrame = parent;
formatter = new DecimalFormat();
JButton showEquipmentDatabase = new JButton("Show Equipment Database");
showEquipmentDatabase.addActionListener(evt -> parent.getFloatingEquipmentDatabase().setVisible(true));
JButton btnValidate = new JButton("Validate Unit");
btnValidate.addActionListener(evt -> UnitUtil.showValidation(getBattleArmor(), getParentFrame()));
JButton btnFluffImage = new JButton("Set Fluff Image");
btnFluffImage.addActionListener(evt -> getFluffImage());
invalid.setText("Invalid");
invalid.setForeground(Color.RED);
invalid.setVisible(false);

setLayout(new WrapLayout(FlowLayout.LEFT, 22, 5));
add(showEquipmentDatabase);
add(btnValidate);
add(btnFluffImage);
add(movementPanel());
add(bvPanel());
add(tonnagePanel());
add(invalid);
add(cost);
refresh();
}

public JPanel movementPanel() {
final int walk = getBattleArmor().getOriginalWalkMP();
final int jump = getBattleArmor().getOriginalJumpMP();

move.setText("Movement: " + walk + "/" + jump);
movementPanel.add(move);
return movementPanel;
add(move);
add(suitWeight);
}

public JPanel bvPanel() {
final int bv = getBattleArmor().calculateBattleValue();
bvLabel.setText("BV: " + bv);
bvPanel.add(bvLabel);

return bvPanel;
@Override
protected void additionalRefresh() {
refreshMovement();
refreshSuitWeight();
}

public JPanel tonnagePanel() {
tonnagePanel.add(tons);

return tonnagePanel;
public void refreshMovement() {
int walk = getBattleArmor().getOriginalWalkMP();
int jump = getBattleArmor().getOriginalJumpMP();
move.setText(String.format(MOVE_LABEL, walk, jump));
}

public void refresh() {

final int walk = getBattleArmor().getOriginalWalkMP();
final int jump = getBattleArmor().getOriginalJumpMP();
private void refreshSuitWeight() {
final double maxKilos = getBattleArmor().getTrooperWeight() * 1000;
double currentKilos;
final int bv = getBattleArmor().calculateBattleValue();

TestBattleArmor testBA = new TestBattleArmor(getBattleArmor(), entityVerifier.baOption,
null);
currentKilos = testBA.calculateWeight(BattleArmor.LOC_SQUAD);
double currentKilos = ((TestBattleArmor) getTestEntity()).calculateWeight(BattleArmor.LOC_SQUAD);
currentKilos += UnitUtil.getUnallocatedAmmoTonnage(getBattleArmor());
currentKilos *= 1000;

tons.setText(String.format("Suit Weight: %,.0f/%,.0f (%,.0f Remaining)", currentKilos, maxKilos, maxKilos - currentKilos));
tons.setToolTipText("This represents the weight of all squad-level " +
"equipment, it does not count individual equipment");
if (currentKilos > maxKilos) {
tons.setForeground(Color.red);
} else {
tons.setForeground(UIManager.getColor("Label.foreground"));
}

bvLabel.setText("BV: " + bv);
bvLabel.setToolTipText("BV 2.0");

move.setText("Movement: " + walk + "/" + jump);
move.setToolTipText("Walk/Jump MP");

cost.setText("Dry Cost: " + formatter.format(Math.round(getEntity().getCost(true))) + " C-bills");
cost.setToolTipText("The dry cost of the unit (without ammo). The unit's full cost is "
+ formatter.format(Math.round(getEntity().getCost(false))) + " C-bills.");

StringBuffer sb = new StringBuffer();
invalid.setVisible(!testBA.correctEntity(sb));
invalid.setToolTipText("<html>" + sb.toString().replaceAll("\n", "<br/>") + "</html>");
}

private void getFluffImage() {
// copied from structureTab
final FileDialog fDialog = new FileDialog(getParentFrame(), "Image Path",
FileDialog.LOAD);
fDialog.setDirectory(new File(ImageHelper.fluffPath).getAbsolutePath()
+ File.separatorChar + ImageHelper.imageMech
+ File.separatorChar);
fDialog.setLocationRelativeTo(this);

fDialog.setVisible(true);

if (fDialog.getFile() != null) {
String relativeFilePath = new File(fDialog.getDirectory()
+ fDialog.getFile()).getAbsolutePath();
relativeFilePath = "."
+ File.separatorChar
+ relativeFilePath
.substring(new File(System.getProperty("user.dir")).getAbsolutePath().length() + 1);
getBattleArmor().getFluff().setMMLImagePath(relativeFilePath);
String remaining = "";
if (maxKilos - currentKilos > 0) {
remaining = String.format(REMAINDER, maxKilos - currentKilos);
}
refresh.refreshPreview();
}

private JFrame getParentFrame() {
return parentFrame;
}

public void addRefreshedListener(final RefreshListener l) {
refresh = l;
suitWeight.setText(String.format(WEIGHT_LABEL_SUIT, currentKilos, maxKilos) + remaining);
suitWeight.setToolTipText("This represents the weight of all squad-level " +
"equipment, it does not count individual equipment");
}

}
2 changes: 1 addition & 1 deletion megameklab/src/megameklab/ui/combatVehicle/CVMainUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public void reloadTabs() {
equipmentTab.addRefreshedListener(this);
buildTab.addRefreshedListener(this);
fluffTab.setRefreshedListener(this);
statusbar.setRefreshListener(this);
statusbar.addRefreshedListener(this);

previewTab = new PreviewTab(this);

Expand Down
161 changes: 17 additions & 144 deletions megameklab/src/megameklab/ui/combatVehicle/CVStatusBar.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,173 +15,46 @@
*/
package megameklab.ui.combatVehicle;

import megamek.client.ui.swing.GUIPreferences;
import megamek.common.Tank;
import megamek.common.verifier.EntityVerifier;
import megamek.common.verifier.TestTank;
import megameklab.ui.util.ITab;
import megameklab.ui.util.RefreshListener;
import megamek.client.ui.WrapLayout;
import megameklab.util.ImageHelper;
import megameklab.util.UnitUtil;
import megameklab.ui.generalUnit.StatusBar;

import javax.swing.*;
import java.awt.*;
import java.io.File;
import java.text.DecimalFormat;

/**
* The Status Bar for Combat Vehicles
*/
public class CVStatusBar extends ITab {
public class CVStatusBar extends StatusBar {

private static final String MOVE_LABEL = "Movement: %d / %d / %d";
private static final String SLOTS_LABEL = "Free Slots: %d / %d";

private final JPanel slotsPanel = new JPanel();
private final JLabel move = new JLabel();
private final JLabel bvLabel = new JLabel();
private final JLabel tons = new JLabel();
private final JLabel slots = new JLabel();
private final JLabel cost = new JLabel();
private final JLabel invalid = new JLabel();
private final EntityVerifier entityVerifier = EntityVerifier.getInstance(new File(
"data/mechfiles/UnitVerifierOptions.xml"));
private TestTank testEntity;
private final DecimalFormat formatter;
private final JFrame parentFrame;

private RefreshListener refresh;

public CVStatusBar(CVMainUI parent) {
super(parent);
parentFrame = parent;

formatter = new DecimalFormat();
testEntity = new TestTank((Tank) parent.getEntity(), entityVerifier.tankOption,
null);
JButton showEquipmentDatabase = new JButton("Show Equipment Database");
showEquipmentDatabase.addActionListener(evt -> parent.getFloatingEquipmentDatabase().setVisible(true));
JButton btnValidate = new JButton("Validate Unit");
btnValidate.addActionListener(evt -> UnitUtil.showValidation(getTank(), getParentFrame()));
JButton btnFluffImage = new JButton("Set Fluff Image");
btnFluffImage.addActionListener(evt -> getFluffImage());
invalid.setText("Invalid");
invalid.setForeground(Color.RED);
invalid.setVisible(false);

setLayout(new WrapLayout(FlowLayout.LEFT, 22, 5));
add(showEquipmentDatabase);
add(btnValidate);
add(btnFluffImage);
add(tons);
add(movementLabel());
add(bvLabel());
add(bvLabel);
add(tonnageLabel());
add(slotsPanel());
add(invalid);
add(cost);
refresh();
add(move);
add(slots);
}

public void setRefreshListener(RefreshListener refresh) {
this.refresh = refresh;
@Override
protected void additionalRefresh() {
refreshMovement();
refreshSlots();
}

public JLabel movementLabel() {
public void refreshMovement() {
int walk = getTank().getOriginalWalkMP();
int run = getTank().getOriginalRunMP();
int jump = getTank().getOriginalJumpMP();

move.setText("Movement: " + walk + "/" + run + "/" + jump);
return move;
}

public JLabel bvLabel() {
int bv = getTank().calculateBattleValue();
bvLabel.setText("BV: " + bv);

return bvLabel;
move.setText(String.format(MOVE_LABEL, walk, run, jump));
}

public JLabel tonnageLabel() {
double tonnage = getTank().getWeight();
double currentTonnage;

currentTonnage = testEntity.calculateWeight();
currentTonnage += UnitUtil.getUnallocatedAmmoTonnage(getTank());

tons.setText("Tonnage: " + currentTonnage + "/" + tonnage);
return tons;
}

public JPanel slotsPanel() {
public void refreshSlots() {
Tank tank = getTank();
int currentSlots = tank.getTotalSlots() - tank.getFreeSlots();
slots.setText("Slots: "+currentSlots+"/"+tank.getTotalSlots());
slotsPanel.add(slots);
return slotsPanel;
}

public void refresh() {
int walk = getTank().getOriginalWalkMP();
int run = getTank().getRunMP();
int jump = getTank().getOriginalJumpMP();
double tonnage = getTank().getWeight();
double currentTonnage;
int bv = getTank().calculateBattleValue();

testEntity = new TestTank(getTank(), entityVerifier.tankOption,
null);

currentTonnage = testEntity.calculateWeight();

currentTonnage += UnitUtil.getUnallocatedAmmoTonnage(getTank());

tons.setText(String.format("Tonnage: %.1f/%.1f (%.1f Remaining)", currentTonnage, tonnage, tonnage - currentTonnage));
tons.setToolTipText("Current Tonnage/Max Tonnage");
if (currentTonnage > tonnage) {
tons.setForeground(Color.red);
} else {
tons.setForeground(UIManager.getColor("Label.foreground"));
}
Tank tank = getTank();
int currentSlots = tank.getTotalSlots() - tank.getFreeSlots();
slots.setText("Slots: "+currentSlots+"/"+tank.getTotalSlots());
if (currentSlots > tank.getTotalSlots()) {
slots.setForeground(Color.red);
} else {
slots.setForeground(UIManager.getColor("Label.foreground"));
}

bvLabel.setText("BV: " + bv);
bvLabel.setToolTipText("BV 2.0");

cost.setText("Dry Cost: " + formatter.format(Math.round(getEntity().getCost(true))) + " C-bills");
cost.setToolTipText("The dry cost of the unit (without ammo). The unit's full cost is "
+ formatter.format(Math.round(getEntity().getCost(false))) + " C-bills.");

move.setText("Movement: " + walk + "/" + run + "/" + jump);
move.setToolTipText("Walk/Run/Jump MP");
StringBuffer sb = new StringBuffer();
invalid.setVisible(!testEntity.correctEntity(sb));
invalid.setToolTipText("<html>" + sb.toString().replaceAll("\n", "<br/>") + "</html>");
}

private void getFluffImage() {
FileDialog fDialog = new FileDialog(getParentFrame(), "Image Path", FileDialog.LOAD);
fDialog.setDirectory(new File(ImageHelper.fluffPath).getAbsolutePath() + File.separatorChar + ImageHelper.imageMech + File.separatorChar);
fDialog.setLocationRelativeTo(this);
fDialog.setVisible(true);
if (fDialog.getFile() != null) {
String relativeFilePath = new File(fDialog.getDirectory() + fDialog.getFile()).getAbsolutePath();
relativeFilePath = "." + File.separatorChar + relativeFilePath
.substring(new File(System.getProperty("user.dir")).getAbsolutePath().length() + 1);
getTank().getFluff().setMMLImagePath(relativeFilePath);
}
if (refresh != null) {
refresh.refreshPreview();
}
}

private JFrame getParentFrame() {
return parentFrame;
slots.setText(String.format(SLOTS_LABEL, currentSlots, tank.getTotalSlots()));
slots.setForeground(currentSlots > tank.getTotalSlots() ? GUIPreferences.getInstance().getWarningColor() : null);
}
}
Loading

0 comments on commit 262fa97

Please sign in to comment.