From 312e56e5a4c84dcfb5e5d5beca35e5a4e48dbb73 Mon Sep 17 00:00:00 2001 From: cwspain Date: Thu, 16 Nov 2023 15:07:26 -0600 Subject: [PATCH 1/2] Add size spinners for variable-size manipulators. --- .../ui/battleArmor/BAStructureTab.java | 86 +++++++++++++++++-- 1 file changed, 77 insertions(+), 9 deletions(-) diff --git a/megameklab/src/megameklab/ui/battleArmor/BAStructureTab.java b/megameklab/src/megameklab/ui/battleArmor/BAStructureTab.java index 43ceb32b3..412ff1b3f 100644 --- a/megameklab/src/megameklab/ui/battleArmor/BAStructureTab.java +++ b/megameklab/src/megameklab/ui/battleArmor/BAStructureTab.java @@ -33,16 +33,19 @@ import org.apache.logging.log4j.LogManager; import javax.swing.*; +import javax.swing.event.ChangeEvent; +import javax.swing.event.ChangeListener; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.List; +import java.util.Optional; import java.util.stream.Collectors; /** * @author jtighe (torren@users.sourceforge.net) */ -public class BAStructureTab extends ITab implements ActionListener, BABuildListener, ArmorAllocationListener { +public class BAStructureTab extends ITab implements ActionListener, ChangeListener, BABuildListener, ArmorAllocationListener { private RefreshListener refresh; Dimension labelSize = new Dimension(110, 25); @@ -54,8 +57,14 @@ public class BAStructureTab extends ITab implements ActionListener, BABuildListe private BAEnhancementView panEnhancements; // Manipulator Panel - private CustomComboBox leftManipSelect = new CustomComboBox<>(this::manipulatorDisplayName); - private CustomComboBox rightManipSelect = new CustomComboBox<>(this::manipulatorDisplayName); + private final CustomComboBox leftManipSelect = new CustomComboBox<>(this::manipulatorDisplayName); + private final CustomComboBox rightManipSelect = new CustomComboBox<>(this::manipulatorDisplayName); + + private final SpinnerNumberModel spnLeftManipulatorSizeModel = new SpinnerNumberModel(0.5, 0.5, Double.MAX_VALUE, 0.5); + private final SpinnerNumberModel spnRightManipulatorSizeModel = new SpinnerNumberModel(0.5, 0.5, Double.MAX_VALUE, 0.5); + private final JSpinner spnLeftManipulatorSize = new JSpinner(spnLeftManipulatorSizeModel); + private final JSpinner spnRightManipulatorSize = new JSpinner(spnRightManipulatorSizeModel); + private final JLabel lblSize = createLabel("Size:", labelSize); private PreviewTab previewTab; @@ -81,6 +90,7 @@ public void setUpPanels() { panEnhancements = new BAEnhancementView(panBasicInfo); GridBagConstraints gbc = new GridBagConstraints(); Dimension comboSize = new Dimension(250, 25); + Dimension spinnerSize = new Dimension(100, 25); gbc.gridx = 0; gbc.gridy = 0; @@ -93,11 +103,20 @@ public void setUpPanels() { gbc.gridx = 1; gbc.gridy = 0; manipPanel.add(leftManipSelect, gbc); + gbc.gridx = 2; + manipPanel.add(lblSize, gbc); + gbc.gridx = 3; + manipPanel.add(spnLeftManipulatorSize, gbc); + gbc.gridx = 1; gbc.gridy = 1; manipPanel.add(rightManipSelect, gbc); + gbc.gridx = 3; + manipPanel.add(spnRightManipulatorSize, gbc); setFieldSize(leftManipSelect, comboSize); setFieldSize(rightManipSelect, comboSize); + setFieldSize(spnLeftManipulatorSize, spinnerSize); + setFieldSize(spnRightManipulatorSize, spinnerSize); panBasicInfo.setBorder(BorderFactory.createTitledBorder("Basic Information")); panChassis.setBorder(BorderFactory.createTitledBorder("Chassis")); @@ -164,19 +183,45 @@ public void refresh() { rightManipSelect.addItem(et.getName()); } } - int manipType = BAManipulator.getManipulator( - getBattleArmor().getLeftManipulatorName()).type; + BAManipulator manipulator = BAManipulator.getManipulator( + getBattleArmor().getLeftManipulatorName()); leftManipSelect.setSelectedItem( - BattleArmor.MANIPULATOR_NAME_STRINGS[manipType]); - manipType = BAManipulator.getManipulator( - getBattleArmor().getRightManipulatorName()).type; + BattleArmor.MANIPULATOR_NAME_STRINGS[manipulator.type]); + manipulator = BAManipulator.getManipulator( + getBattleArmor().getRightManipulatorName()); rightManipSelect.setSelectedItem( - BattleArmor.MANIPULATOR_NAME_STRINGS[manipType]); + BattleArmor.MANIPULATOR_NAME_STRINGS[manipulator.type]); + refreshManipulatorSizes(BattleArmor.MOUNT_LOC_LARM, spnLeftManipulatorSize, spnLeftManipulatorSizeModel); + // For variable-sized pair-mounted manipulators, we'll only use one spinner + spnRightManipulatorSize.setEnabled(!manipulator.pairMounted); + refreshManipulatorSizes(BattleArmor.MOUNT_LOC_RARM, spnRightManipulatorSize, spnRightManipulatorSizeModel); + lblSize.setVisible(spnLeftManipulatorSize.isVisible() || spnRightManipulatorSize.isVisible()); + refreshPreview(); addAllListeners(); } + /** + * Sets values for the size control if the manipulator has a variable size; otherwise hides it. + * @param mountLoc The mount location + * @param spinner The spinner to show/hide + * @param model The spinner's number model + */ + private void refreshManipulatorSizes(int mountLoc, JSpinner spinner, SpinnerNumberModel model) { + Optional mounted = getBattleArmor().getMisc().stream() + .filter(m -> m.getType().hasFlag(MiscType.F_BA_MANIPULATOR) && (m.getBaMountLoc() == mountLoc)) + .findFirst(); + if (mounted.isPresent() && mounted.get().getType().isVariableSize()) { + model.setValue(mounted.get().getSize()); + model.setStepSize(mounted.get().getType().variableStepSize()); + model.setMinimum(mounted.get().getType().variableStepSize()); + spinner.setVisible(true); + } else { + spinner.setVisible(false); + } + } + public ITechManager getTechManager() { return panBasicInfo; } @@ -191,6 +236,8 @@ public void setTechFaction(int techFaction) { public void addAllListeners() { leftManipSelect.addActionListener(this); rightManipSelect.addActionListener(this); + spnLeftManipulatorSize.addChangeListener(this); + spnRightManipulatorSize.addChangeListener(this); panBasicInfo.addListener(this); panChassis.addListener(this); @@ -202,6 +249,8 @@ public void addAllListeners() { public void removeAllListeners() { leftManipSelect.removeActionListener(this); rightManipSelect.removeActionListener(this); + spnLeftManipulatorSize.removeChangeListener(this); + spnRightManipulatorSize.removeChangeListener(this); panBasicInfo.removeListener(this); panChassis.removeListener(this); @@ -306,6 +355,25 @@ public void actionPerformed(ActionEvent e) { refresh.refreshAll(); } + @Override + public void stateChanged(ChangeEvent evt) { + if (evt.getSource() == spnLeftManipulatorSize) { + setManipulatorSize(BattleArmor.MOUNT_LOC_LARM, spnLeftManipulatorSizeModel.getNumber().doubleValue()); + if (BAManipulator.getManipulator(getBattleArmor().getLeftManipulatorName()).pairMounted) { + spnRightManipulatorSizeModel.setValue(spnLeftManipulatorSizeModel.getValue()); + } + } else if (evt.getSource() == spnRightManipulatorSize) { + setManipulatorSize(BattleArmor.MOUNT_LOC_RARM, spnRightManipulatorSizeModel.getNumber().doubleValue()); + } + } + + private void setManipulatorSize(int mountLoc, double size) { + Optional mounted = getBattleArmor().getMisc().stream() + .filter(m -> m.getType().hasFlag(MiscType.F_BA_MANIPULATOR) && (m.getBaMountLoc() == mountLoc)) + .findFirst(); + mounted.ifPresent(value -> value.setSize(size)); + } + /** * Extracts the actual name of the manipulator from the full equipment name */ From 1681c2c1efcd7b7a4a71d1a7c5659e568012ed99 Mon Sep 17 00:00:00 2001 From: cwspain Date: Fri, 17 Nov 2023 13:08:46 -0600 Subject: [PATCH 2/2] Refresh after changing manipulator size. --- megameklab/src/megameklab/ui/battleArmor/BAStructureTab.java | 1 + 1 file changed, 1 insertion(+) diff --git a/megameklab/src/megameklab/ui/battleArmor/BAStructureTab.java b/megameklab/src/megameklab/ui/battleArmor/BAStructureTab.java index 412ff1b3f..cd8cac35d 100644 --- a/megameklab/src/megameklab/ui/battleArmor/BAStructureTab.java +++ b/megameklab/src/megameklab/ui/battleArmor/BAStructureTab.java @@ -365,6 +365,7 @@ public void stateChanged(ChangeEvent evt) { } else if (evt.getSource() == spnRightManipulatorSize) { setManipulatorSize(BattleArmor.MOUNT_LOC_RARM, spnRightManipulatorSizeModel.getNumber().doubleValue()); } + refresh.refreshAll(); } private void setManipulatorSize(int mountLoc, double size) {