A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/AlmasB/FXGL/commit/453f6f5f4 below:

added choice dialog box API for List and Enum options · AlmasB/FXGL@453f6f5 · GitHub

File tree Expand file treeCollapse file tree 6 files changed

+71

-5

lines changed

Filter options

Expand file treeCollapse file tree 6 files changed

+71

-5

lines changed Original file line number Diff line number Diff line change

@@ -15,6 +15,7 @@

15 15

import javafx.scene.paint.Color;

16 16 17 17

import java.util.LinkedHashMap;

18 +

import java.util.List;

18 19

import java.util.Map;

19 20 20 21

import static com.almasb.fxgl.dsl.FXGL.*;

@@ -26,6 +27,10 @@

26 27

*/

27 28

public class DialogsSample extends GameApplication {

28 29 30 +

private enum CustomEnum {

31 +

VALUE1, VALUE2, VALUE3

32 +

}

33 + 29 34

@Override

30 35

protected void initSettings(GameSettings settings) { }

31 36

@@ -42,6 +47,10 @@ protected void initUI() {

42 47

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

43 48

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

44 49 50 +

dialogs.put("Choice with List", () -> getDialogService().showChoiceBox("Choose wisely!", List.of("Item1", "Item2"), answer -> System.out.println("Chosen: " + answer)));

51 + 52 +

dialogs.put("Choice with Enum", () -> getDialogService().showChoiceBox("Choose wisely!", CustomEnum.class, answer -> System.out.println("Chosen: " + answer)));

53 + 45 54

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

46 55 47 56

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

Original file line number Diff line number Diff line change

@@ -12,6 +12,7 @@

12 12

import javafx.scene.control.Button;

13 13

import javafx.scene.layout.Pane;

14 14 15 +

import java.util.List;

15 16

import java.util.function.Consumer;

16 17

import java.util.function.Predicate;

17 18

@@ -28,6 +29,8 @@ public abstract class DialogFactoryService extends EngineService {

28 29 29 30

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

30 31 32 +

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

33 + 31 34

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

32 35 33 36

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

Original file line number Diff line number Diff line change

@@ -11,6 +11,7 @@

11 11

import javafx.scene.Node;

12 12

import javafx.scene.control.Button;

13 13 14 +

import java.util.List;

14 15

import java.util.function.Consumer;

15 16

import java.util.function.Predicate;

16 17

@@ -59,6 +60,28 @@ public abstract class DialogService extends EngineService {

59 60

*/

60 61

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

61 62 63 +

/**

64 +

* Shows a blocking message box with given choices.

65 +

* The callback is invoked with the user answer as parameter.

66 +

*

67 +

* @param message message to show

68 +

* @param options the list of options

69 +

* @param resultCallback the function to be called

70 +

* @param <T> type of options

71 +

*/

72 +

public abstract <T> void showChoiceBox(String message, List<T> options, Consumer<T> resultCallback);

73 + 74 +

/**

75 +

* Shows a blocking message box with given choices.

76 +

* The callback is invoked with the user answer as parameter.

77 +

*

78 +

* @param message message to show

79 +

* @param enumClass the Enum class

80 +

* @param resultCallback the function to be called

81 +

* @param <T> type of options

82 +

*/

83 +

public abstract <T extends Enum<T>> void showChoiceBox(String message, Class<T> enumClass, Consumer<T> resultCallback);

84 + 62 85

/**

63 86

* Shows a blocking (stops game execution, method returns normally) message box with OK button and input field. The callback

64 87

* is invoked with the field text as parameter.

Original file line number Diff line number Diff line change

@@ -9,6 +9,7 @@ package com.almasb.fxgl.ui

9 9

import com.almasb.fxgl.core.Inject

10 10

import com.almasb.fxgl.core.util.EmptyRunnable

11 11

import com.almasb.fxgl.localization.LocalizationService

12 +

import com.almasb.fxgl.logging.Logger

12 13

import javafx.beans.binding.StringBinding

13 14

import javafx.beans.property.ReadOnlyDoubleProperty

14 15

import javafx.beans.value.ChangeListener

@@ -95,16 +96,20 @@ class FXGLDialogFactoryServiceProvider : DialogFactoryService() {

95 96

}

96 97 97 98

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

98 -

val text = createMessage(message)

99 - 100 99

val choices = options.toMutableList()

101 100

choices.add(0, firstOption)

102 101 102 +

return choiceDialog(message, choices, resultCallback)

103 +

}

104 + 105 +

override fun <T : Any> choiceDialog(message: String, options: List<T>, resultCallback: Consumer<T>): Pane {

106 +

val text = createMessage(message)

107 + 103 108

val hbox = HBox()

104 109 105 -

if (choices.size > 3) {

110 +

if (options.size > 3) {

106 111 107 -

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

112 +

val choiceBox = uiFactory.newChoiceBox(FXCollections.observableArrayList(options))

108 113

choiceBox.selectionModel.selectFirst()

109 114 110 115

val btn = uiFactory.newButton("Select")

@@ -115,8 +120,15 @@ class FXGLDialogFactoryServiceProvider : DialogFactoryService() {

115 120

hbox.children += choiceBox

116 121

hbox.children += btn

117 122 123 +

} else if (options.size == 0) {

124 +

Logger.get<FXGLDialogFactoryServiceProvider>().warning("choiceDialog() called with an empty options list")

125 + 126 +

val btn = uiFactory.newButton("No options provided")

127 + 128 +

hbox.children += btn

129 + 118 130

} else {

119 -

choices.forEach { option ->

131 +

options.forEach { option ->

120 132

val btn = uiFactory.newButton(option.toString())

121 133

btn.setOnAction {

122 134

resultCallback.accept(option)

Original file line number Diff line number Diff line change

@@ -142,6 +142,19 @@ class FXGLDialogService : DialogService() {

142 142

show("Choice", dialog)

143 143

}

144 144 145 +

override fun <T : Any> showChoiceBox(message: String, options: List<T>, resultCallback: Consumer<T>) {

146 +

val dialog = dialogFactory.choiceDialog(message, options) { result ->

147 +

close()

148 +

resultCallback.accept(result)

149 +

}

150 + 151 +

show("Choice", dialog)

152 +

}

153 + 154 +

override fun <T : Enum<T>> showChoiceBox(message: String, enumClass: Class<T>, resultCallback: Consumer<T>) {

155 +

showChoiceBox(message, EnumSet.allOf(enumClass).toList(), resultCallback)

156 +

}

157 + 145 158

override fun showInputBox(message: String, resultCallback: Consumer<String>) {

146 159

showInputBox(message, Predicate { true }, resultCallback)

147 160

}

Original file line number Diff line number Diff line change

@@ -31,6 +31,12 @@ object MockDialogService : DialogService() {

31 31

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

32 32

}

33 33 34 +

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

35 +

}

36 + 37 +

override fun <T : Enum<T>?> showChoiceBox(message: String?, enumClass: Class<T>?, resultCallback: Consumer<T>?) {

38 +

}

39 + 34 40

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

35 41

}

36 42

You can’t perform that action at this time.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4