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

Loading