Skip to content

Commit 0d50a63

Browse files
committed
added showChoiceBox, closes #1094
1 parent 7213fe5 commit 0d50a63

6 files changed

Lines changed: 72 additions & 0 deletions

File tree

fxgl-samples/src/main/java/intermediate/DialogsSample.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ protected void initUI() {
3737

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

40+
dialogs.put("Choice with 1", () -> getDialogService().showChoiceBox("Choose wisely!", answer -> System.out.println("Chosen: " + answer), "Hello"));
41+
dialogs.put("Choice with 2", () -> getDialogService().showChoiceBox("Choose wisely!", answer -> System.out.println("Chosen: " + answer), "Hello", "World"));
42+
dialogs.put("Choice with 3", () -> getDialogService().showChoiceBox("Choose wisely!", answer -> System.out.println("Chosen: " + answer), "Hello", "World", "FXGL"));
43+
dialogs.put("Choice with 4", () -> getDialogService().showChoiceBox("Choose wisely!", answer -> System.out.println("Chosen: " + answer), "Hello", "World", "FXGL", "JavaFX"));
44+
4045
dialogs.put("Confirmation", () -> getDialogService().showConfirmationBox("This is a confirmation box. Agree?", answer -> System.out.println("You pressed yes? " + answer)));
4146

4247
dialogs.put("Input", () -> getDialogService().showInputBox("This is an input box. You can type stuff...", answer -> System.out.println("You typed: "+ answer)));

fxgl-scene/src/main/java/com/almasb/fxgl/ui/DialogFactoryService.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ public abstract class DialogFactoryService extends EngineService {
2626

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

29+
public abstract <T> Pane choiceDialog(String message, Consumer<T> resultCallback, T firstOption, T... options);
30+
2931
public abstract Pane inputDialog(String message, Consumer<String> callback);
3032

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

fxgl-scene/src/main/java/com/almasb/fxgl/ui/DialogService.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,18 @@ public abstract class DialogService extends EngineService {
4747
*/
4848
public abstract void showConfirmationBox(String message, Consumer<Boolean> resultCallback);
4949

50+
/**
51+
* Shows a blocking message box with given choices.
52+
* The callback is invoked with the user answer as parameter.
53+
*
54+
* @param message message to show
55+
* @param resultCallback the function to be called
56+
* @param firstOption the first option
57+
* @param options any other options
58+
* @param <T> type of options
59+
*/
60+
public abstract <T> void showChoiceBox(String message, Consumer<T> resultCallback, T firstOption, T... options);
61+
5062
/**
5163
* Shows a blocking (stops game execution, method returns normally) message box with OK button and input field. The callback
5264
* is invoked with the field text as parameter.

fxgl-scene/src/main/kotlin/com/almasb/fxgl/ui/FXGLDialogFactoryServiceProvider.kt

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import com.almasb.fxgl.localization.LocalizationService
1111
import javafx.beans.binding.StringBinding
1212
import javafx.beans.property.ReadOnlyDoubleProperty
1313
import javafx.beans.value.ChangeListener
14+
import javafx.collections.FXCollections
1415
import javafx.geometry.Insets
1516
import javafx.geometry.Pos
1617
import javafx.scene.Node
@@ -89,6 +90,46 @@ class FXGLDialogFactoryServiceProvider : DialogFactoryService() {
8990
return wrap(vbox)
9091
}
9192

93+
override fun <T : Any> choiceDialog(message: String, resultCallback: Consumer<T>, firstOption: T, vararg options: T): Pane {
94+
val text = createMessage(message)
95+
96+
val choices = options.toMutableList()
97+
choices.add(0, firstOption)
98+
99+
val hbox = HBox()
100+
101+
if (choices.size > 3) {
102+
103+
val choiceBox = uiFactory.newChoiceBox(FXCollections.observableArrayList(choices))
104+
choiceBox.selectionModel.selectFirst()
105+
106+
val btn = uiFactory.newButton("Select")
107+
btn.setOnAction {
108+
resultCallback.accept(choiceBox.value)
109+
}
110+
111+
hbox.children += choiceBox
112+
hbox.children += btn
113+
114+
} else {
115+
choices.forEach { option ->
116+
val btn = uiFactory.newButton(option.toString())
117+
btn.setOnAction {
118+
resultCallback.accept(option)
119+
}
120+
121+
hbox.children += btn
122+
}
123+
}
124+
125+
hbox.alignment = Pos.CENTER
126+
127+
val vbox = VBox(50.0, text, hbox)
128+
vbox.setAlignment(Pos.CENTER)
129+
130+
return wrap(vbox)
131+
}
132+
92133
override fun inputDialog(message: String, callback: Consumer<String>): Pane {
93134
return inputDialog(message, Predicate { true }, callback)
94135
}

fxgl/src/main/kotlin/com/almasb/fxgl/app/services/FXGLDialogService.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,15 @@ class FXGLDialogService : DialogService() {
133133
show("Confirm", dialog)
134134
}
135135

136+
override fun <T : Any> showChoiceBox(message: String, callback: Consumer<T>, firstOption: T, vararg options: T) {
137+
val dialog = dialogFactory.choiceDialog(message, { result ->
138+
close()
139+
callback.accept(result)
140+
}, firstOption, *options)
141+
142+
show("Choice", dialog)
143+
}
144+
136145
override fun showInputBox(message: String, resultCallback: Consumer<String>) {
137146
showInputBox(message, Predicate { true }, resultCallback)
138147
}

fxgl/src/test/kotlin/com/almasb/fxgl/app/MockDialogService.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ object MockDialogService : DialogService() {
2828
override fun showConfirmationBox(message: String?, resultCallback: Consumer<Boolean>?) {
2929
}
3030

31+
override fun <T : Any?> showChoiceBox(message: String?, resultCallback: Consumer<T>?, firstOption: T, vararg options: T) {
32+
}
33+
3134
override fun showInputBoxWithCancel(message: String?, filter: Predicate<String>?, resultCallback: Consumer<String>?) {
3235
}
3336

0 commit comments

Comments
 (0)