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 0000000..219d0bb Binary files /dev/null and b/wizard-client/wizard-client-libgdx/core/src/main/textures/uiskin/default-select-error.9.png differ diff --git a/wizard-client/wizard-client-libgdx/core/src/main/textures/uiskin/default-select.9.png b/wizard-client/wizard-client-libgdx/core/src/main/textures/uiskin/default-select.9.png index 38b951a..7c741f5 100644 Binary files a/wizard-client/wizard-client-libgdx/core/src/main/textures/uiskin/default-select.9.png and b/wizard-client/wizard-client-libgdx/core/src/main/textures/uiskin/default-select.9.png differ 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 0000000..1264633 Binary files /dev/null and b/wizard-client/wizard-client-libgdx/core/src/main/textures/uiskin/textfield-error.9.png differ