Skip to content

Commit

Permalink
[3222] Disabling sliders when set to random (#3268)
Browse files Browse the repository at this point in the history
* [3222] Disabling sliders when set to random

* [3222] Replacing function with shared/local property.

* [3222] Using SimpleBooleanProperty
  • Loading branch information
sulikdan authored Nov 4, 2024
1 parent f8a99b2 commit a53f5a9
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 46 deletions.
82 changes: 36 additions & 46 deletions src/main/java/com/faforever/client/game/GenerateMapController.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -89,9 +92,16 @@ public class GenerateMapController extends NodeController<Pane> {
IntStream.range(2, 17).boxed().collect(Collectors.toList()));
private final FilteredList<Integer> selectableSpawnCounts = new FilteredList<>(validSpawnCount);
public Spinner<Integer> 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();
Expand All @@ -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<GenerationType> getGenerationTypeConverter() {
Expand Down Expand Up @@ -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()));
Expand All @@ -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<String> 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()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<GeneratorOptions> 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<GeneratorOptions> 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());
}
}

0 comments on commit a53f5a9

Please sign in to comment.