diff --git a/src/main/java/com/faforever/client/game/GenerateMapController.java b/src/main/java/com/faforever/client/game/GenerateMapController.java index 3ebd1979a2..d438666db3 100644 --- a/src/main/java/com/faforever/client/game/GenerateMapController.java +++ b/src/main/java/com/faforever/client/game/GenerateMapController.java @@ -12,6 +12,9 @@ import com.faforever.client.notification.NotificationService; import com.faforever.client.preferences.GeneratorPrefs; import com.google.common.annotations.VisibleForTesting; +import javafx.beans.binding.BooleanBinding; +import javafx.beans.property.BooleanProperty; +import javafx.beans.property.SimpleBooleanProperty; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.collections.transformation.FilteredList; @@ -89,9 +92,16 @@ public class GenerateMapController extends NodeController { IntStream.range(2, 17).boxed().collect(Collectors.toList())); private final FilteredList selectableSpawnCounts = new FilteredList<>(validSpawnCount); public Spinner numTeamsSpinner; + private final BooleanProperty disableCustomization = new SimpleBooleanProperty(); @Override protected void onInitialize() { + disableCustomization.bind(previousMapName.textProperty() + .isNotEmpty() + .or(generationTypeComboBox.valueProperty() + .isNotEqualTo(GenerationType.CASUAL)) + .or(commandLineArgsText.textProperty().isNotEmpty())); + JavaFxUtil.bindManagedToVisible(commandLineLabel, commandLineArgsText); initCommandlineArgs(); initGenerationTypeComboBox(); @@ -102,8 +112,10 @@ protected void onInitialize() { initSpawnCountSpinner(); initMapSizeSpinner(); initSeedField(); - bindCustomStyleDisabledProperty(terrainComboBox, biomeComboBox, resourcesComboBox, propsComboBox, - resourcesDensitySlider, reclaimDensitySlider); + + bindCustomStyleDisabledPropertyNonSliders(terrainComboBox, biomeComboBox, resourcesComboBox, propsComboBox); + bindCustomStyleDisabledPropertySlider(resourcesDensitySlider, resourcesComboBox); + bindCustomStyleDisabledPropertySlider(reclaimDensitySlider, propsComboBox); } private StringConverter getGenerationTypeConverter() { @@ -218,39 +230,21 @@ private void initMapSizeSpinner() { } private void initSymmetryComboBox() { - symmetryComboBox.disableProperty() - .bind(previousMapName.textProperty() - .isNotEmpty() - .or(generationTypeComboBox.valueProperty().isNotEqualTo(GenerationType.CASUAL)) - .or(commandLineArgsText.textProperty().isNotEmpty())); + symmetryComboBox.disableProperty().bind(disableCustomization); } private void initMapStyleComboBox() { - mapStyleComboBox.disableProperty() - .bind(previousMapName.textProperty() - .isNotEmpty() - .or(generationTypeComboBox.valueProperty().isNotEqualTo(GenerationType.CASUAL)) - .or(commandLineArgsText.textProperty().isNotEmpty()) - .or(customStyleCheckBox.selectedProperty())); + mapStyleComboBox.disableProperty().bind(disableCustomization + .or(customStyleCheckBox.selectedProperty())); } private void initCustomStyleOptions() { customStyleCheckBox.setSelected(generatorPrefs.getCustomStyle()); generatorPrefs.customStyleProperty().bind(customStyleCheckBox.selectedProperty()); - customStyleCheckBox.disableProperty() - .bind(previousMapName.textProperty() - .isNotEmpty() - .or(generationTypeComboBox.valueProperty() - .isNotEqualTo(GenerationType.CASUAL)) - .or(commandLineArgsText.textProperty().isNotEmpty())); + customStyleCheckBox.disableProperty().bind(disableCustomization); fixedSeedCheckBox.setSelected(generatorPrefs.getFixedSeed()); generatorPrefs.fixedSeedProperty().bind(fixedSeedCheckBox.selectedProperty()); - fixedSeedCheckBox.disableProperty() - .bind(previousMapName.textProperty() - .isNotEmpty() - .or(generationTypeComboBox.valueProperty() - .isNotEqualTo(GenerationType.CASUAL)) - .or(commandLineArgsText.textProperty().isNotEmpty())); + fixedSeedCheckBox.disableProperty().bind(disableCustomization); reclaimDensitySlider.setLabelFormatter( new LessMoreStringConverter(reclaimDensitySlider.getMin(), reclaimDensitySlider.getMax())); @@ -269,31 +263,27 @@ private void initCustomStyleOptions() { private void initSeedField() { seedTextField.setText(String.valueOf(generatorPrefs.getSeed())); generatorPrefs.seedProperty().bind(seedTextField.textProperty()); - seedTextField.disableProperty() - .bind(previousMapName.textProperty() - .isNotEmpty() - .or(generationTypeComboBox.valueProperty().isNotEqualTo(GenerationType.CASUAL)) - .or(commandLineArgsText.textProperty().isNotEmpty()) - .or(fixedSeedCheckBox.selectedProperty().not())); - seedRerollButton.disableProperty() - .bind(previousMapName.textProperty() - .isNotEmpty() - .or(generationTypeComboBox.valueProperty().isNotEqualTo(GenerationType.CASUAL)) - .or(commandLineArgsText.textProperty().isNotEmpty()) - .or(fixedSeedCheckBox.selectedProperty().not())); - } - - private void bindCustomStyleDisabledProperty(Node... nodes) { + + BooleanBinding customizationAllowedOrNoFixedSeed = disableCustomization.or( + fixedSeedCheckBox.selectedProperty().not()); + + seedTextField.disableProperty().bind(customizationAllowedOrNoFixedSeed); + seedRerollButton.disableProperty().bind(customizationAllowedOrNoFixedSeed); + } + + private void bindCustomStyleDisabledPropertyNonSliders(Node... nodes) { for (Node node : nodes) { - node.disableProperty() - .bind(previousMapName.textProperty() - .isNotEmpty() - .or(generationTypeComboBox.valueProperty().isNotEqualTo(GenerationType.CASUAL)) - .or(commandLineArgsText.textProperty().isNotEmpty()) - .or(customStyleCheckBox.selectedProperty().not())); + node.disableProperty().bind(disableCustomization.or(customStyleCheckBox.selectedProperty().not())); } } + private void bindCustomStyleDisabledPropertySlider(RangeSlider slider, ComboBox relatedComboBox) { + slider.disableProperty() + .bind(disableCustomization.or(customStyleCheckBox.selectedProperty().not()) + .or(relatedComboBox.valueProperty() + .isEqualTo(MapGeneratorService.GENERATOR_RANDOM_OPTION))); + } + private GeneratorOptions getGeneratorOptions() { GeneratorOptions.GeneratorOptionsBuilder optionsBuilder = GeneratorOptions.builder(); if (!commandLineArgsText.getText().isBlank()) { diff --git a/src/test/java/com/faforever/client/game/GenerateMapControllerTest.java b/src/test/java/com/faforever/client/game/GenerateMapControllerTest.java index 6c4b4c536a..d7011b0e89 100644 --- a/src/test/java/com/faforever/client/game/GenerateMapControllerTest.java +++ b/src/test/java/com/faforever/client/game/GenerateMapControllerTest.java @@ -532,5 +532,64 @@ public void testGetGenerateMapNoNameMapStyle() { assertNull(result.resourceDensity()); assertNull(result.reclaimDensity()); } + + @Test + public void testResourceComboRandomSliderDisabled(){ + generatorPrefs.setCustomStyle(true); + instance.resourcesComboBox.setItems(FXCollections.observableList(List.of("RANDOM"))); + instance.resourcesComboBox.getSelectionModel().selectFirst(); + instance.propsComboBox.setItems(FXCollections.observableList(List.of("OPTIMUS"))); + instance.propsComboBox.getSelectionModel().selectFirst(); + + runOnFxThreadAndWait(() -> reinitialize(instance)); + + ArgumentCaptor captor = ArgumentCaptor.forClass(GeneratorOptions.class); + + runOnFxThreadAndWait(() -> instance.onGenerateMap()); + + verify(mapGeneratorService).generateMap(captor.capture()); + + GeneratorOptions result = captor.getValue(); + assertEquals("RANDOM", result.resourceStyle()); + assertEquals("OPTIMUS", result.propStyle()); + assertTrue(instance.resourcesDensitySlider.isDisabled()); + assertFalse(instance.reclaimDensitySlider.isDisabled()); + } + + @Test + public void testPropComboRandomSliderDisabled(){ + generatorPrefs.setCustomStyle(true); + instance.propsComboBox.setItems(FXCollections.observableList(List.of("RANDOM"))); + instance.propsComboBox.getSelectionModel().selectFirst(); + instance.resourcesComboBox.setItems(FXCollections.observableList(List.of("OPTIMUS"))); + instance.resourcesComboBox.getSelectionModel().selectFirst(); + + runOnFxThreadAndWait(() -> reinitialize(instance)); + + ArgumentCaptor captor = ArgumentCaptor.forClass(GeneratorOptions.class); + + runOnFxThreadAndWait(() -> instance.onGenerateMap()); + + verify(mapGeneratorService).generateMap(captor.capture()); + + GeneratorOptions result = captor.getValue(); + assertEquals("RANDOM", result.propStyle()); + assertEquals("OPTIMUS", result.resourceStyle()); + assertTrue(instance.reclaimDensitySlider.isDisabled()); + assertFalse(instance.resourcesDensitySlider.isDisabled()); + } + + @Test + public void testCustomStyleFalseAndPropAndResourceComboNotRandomSlidersDisabled(){ + + runOnFxThreadAndWait(() -> reinitialize(instance)); + instance.customStyleCheckBox.setSelected(false); + instance.setPropStyles(new ArrayList<>(List.of("Maximus"))); + instance.setResourceStyles(new ArrayList<>(List.of("Decimus"))); + + + assertTrue(instance.reclaimDensitySlider.isDisabled()); + assertTrue(instance.resourcesDensitySlider.isDisabled()); + } }