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

[3222] Disabling sliders when set to random #3268

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
87 changes: 41 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,7 @@
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.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.collections.transformation.FilteredList;
Expand Down Expand Up @@ -102,8 +103,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 +221,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(getSharedBooleanBinding());
}

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(getSharedBooleanBinding()
.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(getSharedBooleanBinding());
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(getSharedBooleanBinding());

reclaimDensitySlider.setLabelFormatter(
new LessMoreStringConverter(reclaimDensitySlider.getMin(), reclaimDensitySlider.getMax()));
Expand All @@ -269,31 +254,41 @@ 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) {
seedTextField.disableProperty().bind(getExtendedFixedSeedNotBooleanBinding());
seedRerollButton.disableProperty().bind(getExtendedFixedSeedNotBooleanBinding());
}

private BooleanBinding getSharedBooleanBinding() {
return previousMapName.textProperty().isNotEmpty()
.or(generationTypeComboBox.valueProperty().isNotEqualTo(GenerationType.CASUAL))
.or(commandLineArgsText.textProperty().isNotEmpty());
}
sulikdan marked this conversation as resolved.
Show resolved Hide resolved

private BooleanBinding getExtendedFixedSeedNotBooleanBinding() {
return getSharedBooleanBinding()
.or(fixedSeedCheckBox.selectedProperty().not());
}
sulikdan marked this conversation as resolved.
Show resolved Hide resolved

private BooleanBinding getExtendedCustomStyleNotBooleanBinding() {
return getSharedBooleanBinding()
.or(customStyleCheckBox.selectedProperty().not());
}
sulikdan marked this conversation as resolved.
Show resolved Hide resolved

private BooleanBinding getExtendedSliderBooleanBinding(ComboBox<String> comboBox) {
return getExtendedCustomStyleNotBooleanBinding()
.or(comboBox.valueProperty().isEqualTo(MapGeneratorService.GENERATOR_RANDOM_OPTION));
}
sulikdan marked this conversation as resolved.
Show resolved Hide resolved

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(getExtendedCustomStyleNotBooleanBinding());
}
}

private void bindCustomStyleDisabledPropertySlider(RangeSlider slider, ComboBox<String> relatedComboBox) {
slider.disableProperty().bind(getExtendedSliderBooleanBinding(relatedComboBox));
}

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());
}
}

Loading