Skip to content

Commit

Permalink
added showChoiceBox, closes #1094
Browse files Browse the repository at this point in the history
  • Loading branch information
AlmasB committed Nov 25, 2021
1 parent 7213fe5 commit 0d50a63
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 0 deletions.
5 changes: 5 additions & 0 deletions fxgl-samples/src/main/java/intermediate/DialogsSample.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ protected void initUI() {

dialogs.put("Error", () -> getDialogService().showErrorBox("This is a scary error box!", () -> {}));

dialogs.put("Choice with 1", () -> getDialogService().showChoiceBox("Choose wisely!", answer -> System.out.println("Chosen: " + answer), "Hello"));
dialogs.put("Choice with 2", () -> getDialogService().showChoiceBox("Choose wisely!", answer -> System.out.println("Chosen: " + answer), "Hello", "World"));
dialogs.put("Choice with 3", () -> getDialogService().showChoiceBox("Choose wisely!", answer -> System.out.println("Chosen: " + answer), "Hello", "World", "FXGL"));
dialogs.put("Choice with 4", () -> getDialogService().showChoiceBox("Choose wisely!", answer -> System.out.println("Chosen: " + answer), "Hello", "World", "FXGL", "JavaFX"));

dialogs.put("Confirmation", () -> getDialogService().showConfirmationBox("This is a confirmation box. Agree?", answer -> System.out.println("You pressed yes? " + answer)));

dialogs.put("Input", () -> getDialogService().showInputBox("This is an input box. You can type stuff...", answer -> System.out.println("You typed: "+ answer)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public abstract class DialogFactoryService extends EngineService {

public abstract Pane confirmationDialog(String message, Consumer<Boolean> callback);

public abstract <T> Pane choiceDialog(String message, Consumer<T> resultCallback, T firstOption, T... options);

public abstract Pane inputDialog(String message, Consumer<String> callback);

public abstract Pane inputDialog(String message, Predicate<String> filter, Consumer<String> callback);
Expand Down
12 changes: 12 additions & 0 deletions fxgl-scene/src/main/java/com/almasb/fxgl/ui/DialogService.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,18 @@ public abstract class DialogService extends EngineService {
*/
public abstract void showConfirmationBox(String message, Consumer<Boolean> resultCallback);

/**
* Shows a blocking message box with given choices.
* The callback is invoked with the user answer as parameter.
*
* @param message message to show
* @param resultCallback the function to be called
* @param firstOption the first option
* @param options any other options
* @param <T> type of options
*/
public abstract <T> void showChoiceBox(String message, Consumer<T> resultCallback, T firstOption, T... options);

/**
* Shows a blocking (stops game execution, method returns normally) message box with OK button and input field. The callback
* is invoked with the field text as parameter.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import com.almasb.fxgl.localization.LocalizationService
import javafx.beans.binding.StringBinding
import javafx.beans.property.ReadOnlyDoubleProperty
import javafx.beans.value.ChangeListener
import javafx.collections.FXCollections
import javafx.geometry.Insets
import javafx.geometry.Pos
import javafx.scene.Node
Expand Down Expand Up @@ -89,6 +90,46 @@ class FXGLDialogFactoryServiceProvider : DialogFactoryService() {
return wrap(vbox)
}

override fun <T : Any> choiceDialog(message: String, resultCallback: Consumer<T>, firstOption: T, vararg options: T): Pane {
val text = createMessage(message)

val choices = options.toMutableList()
choices.add(0, firstOption)

val hbox = HBox()

if (choices.size > 3) {

val choiceBox = uiFactory.newChoiceBox(FXCollections.observableArrayList(choices))
choiceBox.selectionModel.selectFirst()

val btn = uiFactory.newButton("Select")
btn.setOnAction {
resultCallback.accept(choiceBox.value)
}

hbox.children += choiceBox
hbox.children += btn

} else {
choices.forEach { option ->
val btn = uiFactory.newButton(option.toString())
btn.setOnAction {
resultCallback.accept(option)
}

hbox.children += btn
}
}

hbox.alignment = Pos.CENTER

val vbox = VBox(50.0, text, hbox)
vbox.setAlignment(Pos.CENTER)

return wrap(vbox)
}

override fun inputDialog(message: String, callback: Consumer<String>): Pane {
return inputDialog(message, Predicate { true }, callback)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,15 @@ class FXGLDialogService : DialogService() {
show("Confirm", dialog)
}

override fun <T : Any> showChoiceBox(message: String, callback: Consumer<T>, firstOption: T, vararg options: T) {
val dialog = dialogFactory.choiceDialog(message, { result ->
close()
callback.accept(result)
}, firstOption, *options)

show("Choice", dialog)
}

override fun showInputBox(message: String, resultCallback: Consumer<String>) {
showInputBox(message, Predicate { true }, resultCallback)
}
Expand Down
3 changes: 3 additions & 0 deletions fxgl/src/test/kotlin/com/almasb/fxgl/app/MockDialogService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ object MockDialogService : DialogService() {
override fun showConfirmationBox(message: String?, resultCallback: Consumer<Boolean>?) {
}

override fun <T : Any?> showChoiceBox(message: String?, resultCallback: Consumer<T>?, firstOption: T, vararg options: T) {
}

override fun showInputBoxWithCancel(message: String?, filter: Predicate<String>?, resultCallback: Consumer<String>?) {
}

Expand Down

0 comments on commit 0d50a63

Please sign in to comment.