Skip to content

Commit 284de66

Browse files
committed
Added 'Can Use' Filter to Owned Unit Picker
- Moved unit population logic into a new `populateUnitChoiceCombo` method for better encapsulation and readability. - Updated `choiceOriginalUnit` initialization to use the new method instead of inline logic. - Added checks in `populateUnitChoiceCombo` to filter units based on the person's abilities (drive, gun, tech).
1 parent c07e034 commit 284de66

File tree

1 file changed

+39
-2
lines changed

1 file changed

+39
-2
lines changed

MekHQ/src/mekhq/gui/dialog/CustomizePersonDialog.java

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import megamek.client.ui.swing.DialogOptionComponent;
3636
import megamek.client.ui.swing.DialogOptionListener;
3737
import megamek.common.Crew;
38+
import megamek.common.Entity;
3839
import megamek.common.EquipmentType;
3940
import megamek.common.TechConstants;
4041
import megamek.common.enums.Gender;
@@ -787,8 +788,8 @@ public Component getListCellRendererComponent(JList<?> list,
787788
return this;
788789
}
789790
});
790-
choiceOriginalUnit.addItem(null);
791-
campaign.getHangar().forEachUnit(choiceOriginalUnit::addItem);
791+
populateUnitChoiceCombo();
792+
792793
if (null == person.getOriginalUnitId() || null == campaign.getUnit(person.getOriginalUnitId())) {
793794
choiceOriginalUnit.setSelectedItem(null);
794795
} else {
@@ -1067,6 +1068,42 @@ public Component getListCellRendererComponent(JList<?> list,
10671068
pack();
10681069
}
10691070

1071+
/**
1072+
* Populates a combo box with a list of units that the specified person can interact with,
1073+
* based on their abilities to drive, gun, or tech the corresponding entities.
1074+
*
1075+
* <p>The method adds eligible units from the campaign's unit list to the combo box
1076+
* {@code choiceOriginalUnit}, and starts by adding a {@code null} entry to represent no selection.</p>
1077+
*/
1078+
private void populateUnitChoiceCombo() {
1079+
choiceOriginalUnit.addItem(null); // Add a null entry as the initial option
1080+
1081+
// Iterate through all units in the campaign
1082+
for (Unit unit : campaign.getUnits()) {
1083+
Entity entity = unit.getEntity();
1084+
1085+
// Skip units without an associated entity
1086+
if (entity == null) {
1087+
continue;
1088+
}
1089+
1090+
// Add units to the combo box based on the person's capabilities
1091+
if (person.canDrive(entity)) {
1092+
choiceOriginalUnit.addItem(unit);
1093+
continue; // Skip further checks if already added
1094+
}
1095+
1096+
if (person.canGun(entity)) {
1097+
choiceOriginalUnit.addItem(unit);
1098+
continue; // Skip further checks if already added
1099+
}
1100+
1101+
if (person.canTech(entity)) {
1102+
choiceOriginalUnit.addItem(unit);
1103+
}
1104+
}
1105+
}
1106+
10701107
@Deprecated // These need to be migrated to the Suite Constants / Suite Options Setup
10711108
private void setUserPreferences() {
10721109
try {

0 commit comments

Comments
 (0)