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

Fix for request 5333 - Add Gunnery / Pilot BV values to the unit selection screen. #5334

Merged
merged 4 commits into from
Apr 20, 2024
Merged
Changes from 2 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 @@ -24,6 +24,7 @@
import megamek.client.ui.swing.util.UIUtil;
import megamek.common.*;
import megamek.common.annotations.Nullable;
import megamek.common.battlevalue.BVCalculator;
import megamek.common.options.GameOptions;
import megamek.common.options.OptionsConstants;
import megamek.common.util.sorter.NaturalOrderComparator;
Expand Down Expand Up @@ -84,6 +85,8 @@
protected JLabel labelImage = new JLabel(""); //inline to avoid potential null pointer issues
protected JTable tableUnits;
protected JTextField textFilter;
protected JTextField textGunnery;
protected JTextField textPilot;
protected EntityViewPane panePreview;
private JPanel selectionPanel;
private JSplitPane splitPane;
Expand Down Expand Up @@ -293,6 +296,12 @@
gridBagConstraintsWest.gridy = 0;
panelFilterButtons.add(comboUnitType, gridBagConstraintsWest);

JLabel labelFilter = new JLabel(Messages.getString("MechSelectorDialog.m_labelFilter"));
labelFilter.setName("labelFilter");
gridBagConstraintsWest.gridx = 0;
gridBagConstraintsWest.gridy = 3;
panelFilterButtons.add(labelFilter, gridBagConstraintsWest);

textFilter = new JTextField("");
textFilter.setName("textFilter");
textFilter.getDocument().addDocumentListener(new DocumentListener() {
Expand All @@ -317,12 +326,56 @@
panelFilterButtons.add(textFilter, gridBagConstraintsWest);
gridBagConstraintsWest.fill = GridBagConstraints.NONE;

JLabel labelFilter = new JLabel(Messages.getString("MechSelectorDialog.m_labelFilter"));
labelFilter.setName("labelFilter");
/** Add the Gunnery and Piloting entry boxes and labels to the filter panel in the UI **/

JLabel lblGun = new JLabel("Gunnery");
lblGun.setName("lblGun");
gridBagConstraintsWest.gridx = 0;
gridBagConstraintsWest.gridy = 3;
panelFilterButtons.add(labelFilter, gridBagConstraintsWest);
gridBagConstraintsWest.gridy = 4;
panelFilterButtons.add(lblGun,gridBagConstraintsWest);

textGunnery = new JTextField("4");
textGunnery.setName("textGunnery");
textGunnery.getDocument().addDocumentListener(new DocumentListener() {
/** Set the table to refresh when the gunnery is changed **/
@Override
public void changedUpdate (DocumentEvent e) { sorter.allRowsChanged(); }

@Override
public void insertUpdate(DocumentEvent e) { sorter.allRowsChanged(); }

@Override
public void removeUpdate(DocumentEvent e) { sorter.allRowsChanged(); }

});
gridBagConstraintsWest.gridx = 1;
gridBagConstraintsWest.gridy = 4;
panelFilterButtons.add(textGunnery,gridBagConstraintsWest);

JLabel lblPilot = new JLabel("Piloting");
lblGun.setName("lblPilot");
gridBagConstraintsWest.gridx = 0;
gridBagConstraintsWest.gridy = 5;
panelFilterButtons.add(lblPilot,gridBagConstraintsWest);

textPilot = new JTextField("5");
textPilot.setName("textPilot");
textPilot.getDocument().addDocumentListener(new DocumentListener() {
/** Set the table to refresh when the piloting is changed **/
@Override
public void changedUpdate (DocumentEvent e) { sorter.allRowsChanged(); }

@Override
public void insertUpdate(DocumentEvent e) { sorter.allRowsChanged(); }

@Override
public void removeUpdate(DocumentEvent e) { sorter.allRowsChanged(); }

});
gridBagConstraintsWest.gridx = 1;
gridBagConstraintsWest.gridy = 5;
panelFilterButtons.add(textPilot,gridBagConstraintsWest);

labelImage.setHorizontalAlignment(SwingConstants.CENTER);
labelImage.setName("labelImage");
gridBagConstraints = new GridBagConstraints();
Expand All @@ -333,7 +386,7 @@
gridBagConstraints.weightx = 1.0;
gridBagConstraints.weighty = 1.0;
panelFilterButtons.add(labelImage, gridBagConstraints);

gridBagConstraints = new GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 0;
Expand All @@ -342,7 +395,7 @@
gridBagConstraints.weightx = 0.0;
gridBagConstraints.insets = new Insets(10, 10, 5, 0);
selectionPanel.add(panelFilterButtons, gridBagConstraints);

JPanel panelSearchButtons = new JPanel(new GridBagLayout());

buttonAdvancedSearch = new JButton(Messages.getString("MechSelectorDialog.AdvSearch"));
Expand Down Expand Up @@ -816,6 +869,7 @@
private static final int COL_COST = 6;
private static final int COL_LEVEL = 7;
private static final int N_COL = 8;
private int modified_bv = 0;

private MechSummary[] data = new MechSummary[0];

Expand Down Expand Up @@ -901,9 +955,53 @@
}
return ms.getTons();
} else if (col == COL_BV) {
return ms.getBV();
/** This code allows for Gunnery and BV to be read from the UI, and update the BV values in the table as a result **/
int gunnery = 4;
int piloting = 5;
if (textGunnery.getText().matches("\\d+")) {
gunnery = Integer.parseInt(textGunnery.getText());
github-advanced-security[bot] marked this conversation as resolved.
Fixed
Show resolved Hide resolved

Check notice

Code scanning / CodeQL

Missing catch of NumberFormatException Note

Potential uncaught 'java.lang.NumberFormatException'.
if (gunnery > 8) {
gunnery = 4;
};
};
if (textPilot.getText().matches("\\d+")) {
piloting = Integer.parseInt(textPilot.getText());
github-advanced-security[bot] marked this conversation as resolved.
Fixed
Show resolved Hide resolved

Check notice

Code scanning / CodeQL

Missing catch of NumberFormatException Note

Potential uncaught 'java.lang.NumberFormatException'.
if (piloting > 8) {
piloting = 5;
};
};

double gp_multiply = BVCalculator.bvSkillMultiplier(gunnery,piloting);
int modified_bv = (int) Math.round(ms.getBV() * gp_multiply);
return modified_bv;
} else if (col == COL_PV) {
return ms.getPointValue();
/** This code allows for Gunnery to be read from the UI, and update the PV values in the table as a result
* It uses Gunnery as the skill **/
int gunnery = 4;
double modifier = 1;
if (textGunnery.getText().matches("\\d+")) {
gunnery = Integer.parseInt(textGunnery.getText());

Check notice

Code scanning / CodeQL

Missing catch of NumberFormatException Note

Potential uncaught 'java.lang.NumberFormatException'.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The .matches line above it is a validation that it is a number before it goes to the parsing

};
if (gunnery == 4) {
modifier = 1;
} else if (gunnery == 3) {
modifier = 1.2;
} else if (gunnery == 2) {
modifier = 1.4;
} else if (gunnery == 1) {
modifier = 1.6;
} else if (gunnery == 0) {
modifier = 1.8;
} else if (gunnery == 5) {
modifier = 0.9;
} else if (gunnery == 6) {
modifier = 0.8;
} else if (gunnery == 7) {
modifier = 0.7;
} else {
modifier = 1;
}
return (int) Math.round(ms.getPointValue() * modifier);
} else if (col == COL_YEAR) {
return ms.getYear();
} else if (col == COL_COST) {
Expand Down Expand Up @@ -996,4 +1094,4 @@
if (!e.getValueIsAdjusting()) UIUtil.updateRowHeightsForEqualHeights(tableUnits);
}
};
}
}
Loading