From b28ab2f5d1e3f3f7755da951c236153fc2798cff Mon Sep 17 00:00:00 2001 From: Jonah Bauer Date: Tue, 14 Dec 2021 12:55:18 +0100 Subject: [PATCH] improved CreateGameScreen --- .../libgdx/listeners/ResetErrorListener.java | 22 ++++ .../libgdx/screens/CreateGameScreen.java | 119 ++++++++++++------ .../main/resources/i18n/messages.properties | 8 +- .../resources/i18n/messages_de.properties | 8 +- .../core/src/main/resources/uiskin.json | 15 ++- .../uiskin/default-select-error.9.png | Bin 0 -> 468 bytes .../main/textures/uiskin/default-select.9.png | Bin 423 -> 481 bytes .../textures/uiskin/textfield-error.9.png | Bin 0 -> 236 bytes 8 files changed, 131 insertions(+), 41 deletions(-) create mode 100644 wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/listeners/ResetErrorListener.java create mode 100644 wizard-client/wizard-client-libgdx/core/src/main/textures/uiskin/default-select-error.9.png create mode 100644 wizard-client/wizard-client-libgdx/core/src/main/textures/uiskin/textfield-error.9.png diff --git a/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/listeners/ResetErrorListener.java b/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/listeners/ResetErrorListener.java new file mode 100644 index 0000000..57c9086 --- /dev/null +++ b/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/listeners/ResetErrorListener.java @@ -0,0 +1,22 @@ +package eu.jonahbauer.wizard.client.libgdx.listeners; + +import com.badlogic.gdx.scenes.scene2d.Actor; +import com.badlogic.gdx.scenes.scene2d.ui.SelectBox; +import com.badlogic.gdx.scenes.scene2d.ui.Skin; +import com.badlogic.gdx.scenes.scene2d.ui.TextField; +import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener; +import lombok.RequiredArgsConstructor; + +@RequiredArgsConstructor +public class ResetErrorListener extends ChangeListener { + private final Skin skin; + + @Override + public void changed(ChangeEvent event, Actor actor) { + if (event.getTarget() instanceof TextField textField) { + textField.setStyle(skin.get(TextField.TextFieldStyle.class)); + } else if (event.getTarget() instanceof SelectBox box) { + box.setStyle(skin.get(SelectBox.SelectBoxStyle.class)); + } + } +} diff --git a/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/screens/CreateGameScreen.java b/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/screens/CreateGameScreen.java index c811c82..fbef258 100644 --- a/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/screens/CreateGameScreen.java +++ b/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/screens/CreateGameScreen.java @@ -6,6 +6,7 @@ import com.badlogic.gdx.scenes.scene2d.ui.*; import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener; import com.badlogic.gdx.utils.Array; import eu.jonahbauer.wizard.client.libgdx.WizardGame; +import eu.jonahbauer.wizard.client.libgdx.listeners.ResetErrorListener; import eu.jonahbauer.wizard.client.libgdx.state.AwaitingJoinSession; import eu.jonahbauer.wizard.common.messages.client.CreateSessionMessage; import eu.jonahbauer.wizard.common.model.Configuration; @@ -18,10 +19,7 @@ public class CreateGameScreen extends MenuScreen { private TextField sessionName; private TextField playerName; private TextField timeOut; - - long selectedTimeOut; - String userName; - SelectBox configurations; + private SelectBox configurations; private final ChangeListener listener = new ChangeListener() { @Override @@ -30,24 +28,7 @@ public class CreateGameScreen extends MenuScreen { game.setScreen(new LobbyScreen(game)); sfxClick(); } else if (actor == buttonContinue) { - if (sessionName.getText() == null) { - System.out.println("Please select a session name."); - sessionName.clear(); - } - if (playerName.getText() == null) { - userName = "Mallory"; - System.out.println("Your name will be " + userName + "."); - } - try { - selectedTimeOut = Long.parseLong(timeOut.getText()); - } catch (NumberFormatException e) { - System.out.println("Please select a valid timeout."); - timeOut.clear(); - } - int selected = configurations.getSelectedIndex(); - Configuration config = Configuration.values()[selected]; - game.getClient().transition(new AwaitingJoinSession(null, sessionName.getText(), config, playerName.getText())); - game.getClient().send(new CreateSessionMessage(sessionName.getText(), playerName.getText(), selectedTimeOut, config)); + create(); } } }; @@ -60,51 +41,76 @@ public class CreateGameScreen extends MenuScreen { public void show() { super.show(); - buttonBack = new TextButton(game.messages.get("menu.connect.back"), game.data.skin); + buttonBack = new TextButton(game.messages.get("menu.create_game.back"), game.data.skin); buttonBack.setPosition(WizardGame.WIDTH * 0.275f, BUTTON_BAR_Y); - buttonContinue = new TextButton("Erstellen", game.data.skin); + buttonContinue = new TextButton(game.messages.get("menu.create_game.create"), game.data.skin); buttonContinue.setPosition(WizardGame.WIDTH * 0.725f - buttonContinue.getWidth(), BUTTON_BAR_Y); + var errorListener = new ResetErrorListener(game.data.skin); + sessionName = new TextField("", game.data.skin); sessionName.setPosition(WizardGame.WIDTH * 0.3f, WizardGame.HEIGHT * 0.5f); sessionName.setSize(0.4f * WizardGame.WIDTH, 64); + sessionName.addListener(errorListener); + sessionName.setProgrammaticChangeEvents(true); playerName = new TextField("", game.data.skin); playerName.setPosition(WizardGame.WIDTH * 0.3f, WizardGame.HEIGHT * 0.45f); playerName.setSize(0.4f * WizardGame.WIDTH, 64); + playerName.addListener(errorListener); + playerName.setTextFieldListener(new TextField.TextFieldListener() { + private final String format = game.messages.get("menu.create_game.session_name.default"); + private String oldName = ""; + + @Override + public void keyTyped(TextField textField, char c) { + if (textField == playerName) { + var player = playerName.getText(); + var session = sessionName.getText(); + if (session.isEmpty() || session.equals(format.formatted(oldName))) { + if (player.isEmpty()) { + sessionName.setText(""); + } else { + sessionName.setText(format.formatted(player)); + } + } + + oldName = player; + } + } + }); timeOut = new TextField("", game.data.skin); timeOut.setPosition(WizardGame.WIDTH * 0.3f, WizardGame.HEIGHT * 0.4f); timeOut.setSize(0.4f * WizardGame.WIDTH, 64); + timeOut.setTextFieldFilter(new TextField.TextFieldFilter.DigitsOnlyFilter()); + timeOut.addListener(errorListener); configurations = new SelectBox<>(game.data.skin); configurations.setSize(400, 64); configurations.setPosition(WizardGame.WIDTH * 0.3f, WizardGame.HEIGHT * 0.3f); + configurations.addListener(errorListener); Array values = new Array<>(); - for (Configuration value: Configuration.values() ) { + for (Configuration value : Configuration.values()) { values.add(value.toString()); } - configurations.setItems(values); var contentTable = new Table().center().left(); contentTable.columnDefaults(0).growX().width(0.4f * WizardGame.WIDTH - 20); contentTable.setSize(0.4f * WizardGame.WIDTH - 20, 400); + contentTable.setPosition(WizardGame.WIDTH * 0.3f, WizardGame.HEIGHT * 0.3f); - contentTable.add(new Label(game.messages.get("menu.lobby.player_name.label"), game.data.skin)).row(); + contentTable.add(new Label(game.messages.get("menu.create_game.player_name.label"), game.data.skin)).row(); contentTable.add(playerName).row(); - contentTable.add(new Label(game.messages.get("menu.lobby.session_name.label"), game.data.skin)).row(); + contentTable.add(new Label(game.messages.get("menu.create_game.session_name.label"), game.data.skin)).row(); contentTable.add(sessionName).row(); - contentTable.add(new Label(game.messages.get("menu.lobby.create_session.timeout.label"), game.data.skin)).row(); + contentTable.add(new Label(game.messages.get("menu.create_game.session_timeout.label"), game.data.skin)).row(); contentTable.add(timeOut).row(); - contentTable.add(new Label(game.messages.get("menu.lobby.session_configuration.label"), game.data.skin)).row(); + contentTable.add(new Label(game.messages.get("menu.create_game.session_configuration.label"), game.data.skin)).row(); contentTable.add(configurations).row(); - - contentTable.setPosition(WizardGame.WIDTH * 0.3f, WizardGame.HEIGHT * 0.3f); - - - + Gdx.input.setInputProcessor(game.data.stage); game.data.stage.addActor(buttonContinue); game.data.stage.addActor(contentTable); @@ -113,4 +119,47 @@ public class CreateGameScreen extends MenuScreen { buttonContinue.addListener(listener); buttonBack.addListener(listener); } + + private void create() { + boolean error = false; + + String sessionName = this.sessionName.getText(); + if (sessionName.isBlank()) { + System.out.println("Please select a session name."); + this.sessionName.setStyle(game.data.skin.get("error", TextField.TextFieldStyle.class)); + error = true; + } + + String playerName = this.playerName.getText(); + if (playerName.isBlank()) { + System.out.println("Please select a name."); + this.playerName.setStyle(game.data.skin.get("error", TextField.TextFieldStyle.class)); + error = true; + } + + long timeout = 0; + try { + timeout = Long.parseLong(this.timeOut.getText()); + } catch (NumberFormatException e) { + System.out.println("Please select a valid timeout."); + this.timeOut.setStyle(game.data.skin.get("error", TextField.TextFieldStyle.class)); + error = true; + } + + Configuration config = null; + try { + int selected = configurations.getSelectedIndex(); + config = Configuration.values()[selected]; + } catch (ArrayIndexOutOfBoundsException e) { + System.out.println("Please select a valid configuration."); + this.configurations.setStyle(game.data.skin.get("error", SelectBox.SelectBoxStyle.class)); + error = true; + } + + if (!error) { + var client = game.getClient(); + client.transition(new AwaitingJoinSession(null, sessionName, config, playerName)); + client.send(new CreateSessionMessage(sessionName, playerName, 1000 * timeout, config)); + } + } } diff --git a/wizard-client/wizard-client-libgdx/core/src/main/resources/i18n/messages.properties b/wizard-client/wizard-client-libgdx/core/src/main/resources/i18n/messages.properties index c9adbcf..6cd0d8c 100644 --- a/wizard-client/wizard-client-libgdx/core/src/main/resources/i18n/messages.properties +++ b/wizard-client/wizard-client-libgdx/core/src/main/resources/i18n/messages.properties @@ -14,7 +14,13 @@ menu.lobby.session_name.label=Session Name menu.lobby.session_player_count.label=Current Player Count menu.lobby.session_configuration.label=Configuration -menu.lobby.create_session.timeout.label=Timeout +menu.create_game.player_name.label=Player Name +menu.create_game.session_name.label=Session Name +menu.create_game.session_name.default=%s's Session +menu.create_game.session_timeout.label=Timeout (s) +menu.create_game.session_configuration.label=Configuration +menu.create_game.back=Back +menu.create_game.create=Create menu.loading.loading=Loading... menu.loading.back=Return To Main Menu diff --git a/wizard-client/wizard-client-libgdx/core/src/main/resources/i18n/messages_de.properties b/wizard-client/wizard-client-libgdx/core/src/main/resources/i18n/messages_de.properties index 4f6ad19..dc0000a 100644 --- a/wizard-client/wizard-client-libgdx/core/src/main/resources/i18n/messages_de.properties +++ b/wizard-client/wizard-client-libgdx/core/src/main/resources/i18n/messages_de.properties @@ -14,7 +14,13 @@ menu.lobby.session_name.label=Session Name menu.lobby.session_player_count.label=Spieleranzahl menu.lobby.session_configuration.label=Spielvariante -menu.lobby.create_session.timeout.label=Timeout +menu.create_game.player_name.label=Spielername +menu.create_game.session_name.label=Session Name +menu.create_game.session_name.default=%s's Session +menu.create_game.session_timeout.label=Timeout (s) +menu.create_game.session_configuration.label=Spielvariante +menu.create_game.back=Zurück +menu.create_game.create=Erstellen menu.loading.loading=Laden... menu.loading.back=Zurück zum Hauptmenü diff --git a/wizard-client/wizard-client-libgdx/core/src/main/resources/uiskin.json b/wizard-client/wizard-client-libgdx/core/src/main/resources/uiskin.json index e241865..3790e16 100644 --- a/wizard-client/wizard-client-libgdx/core/src/main/resources/uiskin.json +++ b/wizard-client/wizard-client-libgdx/core/src/main/resources/uiskin.json @@ -81,8 +81,12 @@ "scrollStyle": "default", "listStyle": { "font": "default-font", - "selection": "default-select-selection" + "selection": "selection" } + }, + "error": { + "parent": "default", + "background": "default-select-error" } }, "SplitPaneStyle": { @@ -136,9 +140,8 @@ "fontColor": "gold" }, "textfield": { - "background": "textfield", - "font": "default-font", - "fontColor": "white" + "parent": "default", + "background": "textfield" }, "handwritten": { "font": "handwritten", @@ -153,6 +156,10 @@ "fontColor": "white", "messageFontColor": "light_gray", "cursor": "cursor" + }, + "error": { + "parent": "default", + "background": "textfield-error" } }, "CheckBoxStyle": { diff --git a/wizard-client/wizard-client-libgdx/core/src/main/textures/uiskin/default-select-error.9.png b/wizard-client/wizard-client-libgdx/core/src/main/textures/uiskin/default-select-error.9.png new file mode 100644 index 0000000000000000000000000000000000000000..219d0bb02af19df25289a8868ad7b9a9f0cba68b GIT binary patch literal 468 zcmV;_0W1EAP)zwLY00001b5ch_0Itp) z=>Px#1ZP1_K>z@;j|==^1poj532;bRa{vGi!vFvd!vV){sAK>D0clA@K~!i%?V3%J zf-n$937431H?#5>avHc3r_p1$a(2|5BIAcGHG`<+r@M{SZ@jdJSm5TG;S0Olu zDc(^ReQL=t&wCMQnlOu3VZR@5=P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?f%o8d8y}yP?=kxi`A=24wR*BVW1*g-gkg{ipbUK|@X20LV?RLw7b7grVolGXR zxL&UiMNtCwm&;{omPp6raV<8RO(xy#b}zF;IvR~?b37iw_x-oIT&2Oma5(%5EMf05 zIVpbccDoItC}v2K)F|C;k8W- R#V7y(002ovPDHLkV1m{o#$f;e diff --git a/wizard-client/wizard-client-libgdx/core/src/main/textures/uiskin/textfield-error.9.png b/wizard-client/wizard-client-libgdx/core/src/main/textures/uiskin/textfield-error.9.png new file mode 100644 index 0000000000000000000000000000000000000000..1264633f80d4c867808adda10b881be5250624e8 GIT binary patch literal 236 zcmeAS@N?(olHy`uVBq!ia0vp^k|4~%1|*NXY)uAIjKx9jP7LeL$-D$|SkfJR9T^xl z_H+M9WCij$3p^r=85sBuf-vKbiP>*~f{mUojv*f2Z?78iH7M{n1fEjmne7+Xy!f!; zoD$`Ei`a4^3N|b;`09IO!TlpFjDo!E4*OUe_3A5??|t@?QJivJYvS)c8CvN#ubBn4 zIlcP4PpR5#%i8-7V|pEW5;E@;XuH`-ayG<>$F7>jW!({@-(H7RTAA dS5E{Svk7ev7MdC}BO2%!22WQ%mvv4FO#qzJR!sl^ literal 0 HcmV?d00001