improved CreateGameScreen
This commit is contained in:
parent
a0dac7f924
commit
54eeef1c0c
@ -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));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -6,6 +6,7 @@ import com.badlogic.gdx.scenes.scene2d.ui.*;
|
|||||||
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;
|
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;
|
||||||
import com.badlogic.gdx.utils.Array;
|
import com.badlogic.gdx.utils.Array;
|
||||||
import eu.jonahbauer.wizard.client.libgdx.WizardGame;
|
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.client.libgdx.state.AwaitingJoinSession;
|
||||||
import eu.jonahbauer.wizard.common.messages.client.CreateSessionMessage;
|
import eu.jonahbauer.wizard.common.messages.client.CreateSessionMessage;
|
||||||
import eu.jonahbauer.wizard.common.model.Configuration;
|
import eu.jonahbauer.wizard.common.model.Configuration;
|
||||||
@ -18,10 +19,7 @@ public class CreateGameScreen extends MenuScreen {
|
|||||||
private TextField sessionName;
|
private TextField sessionName;
|
||||||
private TextField playerName;
|
private TextField playerName;
|
||||||
private TextField timeOut;
|
private TextField timeOut;
|
||||||
|
private SelectBox<String> configurations;
|
||||||
long selectedTimeOut;
|
|
||||||
String userName;
|
|
||||||
SelectBox<String> configurations;
|
|
||||||
|
|
||||||
private final ChangeListener listener = new ChangeListener() {
|
private final ChangeListener listener = new ChangeListener() {
|
||||||
@Override
|
@Override
|
||||||
@ -30,24 +28,7 @@ public class CreateGameScreen extends MenuScreen {
|
|||||||
game.setScreen(new LobbyScreen(game));
|
game.setScreen(new LobbyScreen(game));
|
||||||
sfxClick();
|
sfxClick();
|
||||||
} else if (actor == buttonContinue) {
|
} else if (actor == buttonContinue) {
|
||||||
if (sessionName.getText() == null) {
|
create();
|
||||||
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));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -60,51 +41,76 @@ public class CreateGameScreen extends MenuScreen {
|
|||||||
public void show() {
|
public void show() {
|
||||||
super.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);
|
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);
|
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 = new TextField("", game.data.skin);
|
||||||
sessionName.setPosition(WizardGame.WIDTH * 0.3f, WizardGame.HEIGHT * 0.5f);
|
sessionName.setPosition(WizardGame.WIDTH * 0.3f, WizardGame.HEIGHT * 0.5f);
|
||||||
sessionName.setSize(0.4f * WizardGame.WIDTH, 64);
|
sessionName.setSize(0.4f * WizardGame.WIDTH, 64);
|
||||||
|
sessionName.addListener(errorListener);
|
||||||
|
sessionName.setProgrammaticChangeEvents(true);
|
||||||
|
|
||||||
playerName = new TextField("", game.data.skin);
|
playerName = new TextField("", game.data.skin);
|
||||||
playerName.setPosition(WizardGame.WIDTH * 0.3f, WizardGame.HEIGHT * 0.45f);
|
playerName.setPosition(WizardGame.WIDTH * 0.3f, WizardGame.HEIGHT * 0.45f);
|
||||||
playerName.setSize(0.4f * WizardGame.WIDTH, 64);
|
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 = new TextField("", game.data.skin);
|
||||||
timeOut.setPosition(WizardGame.WIDTH * 0.3f, WizardGame.HEIGHT * 0.4f);
|
timeOut.setPosition(WizardGame.WIDTH * 0.3f, WizardGame.HEIGHT * 0.4f);
|
||||||
timeOut.setSize(0.4f * WizardGame.WIDTH, 64);
|
timeOut.setSize(0.4f * WizardGame.WIDTH, 64);
|
||||||
|
timeOut.setTextFieldFilter(new TextField.TextFieldFilter.DigitsOnlyFilter());
|
||||||
|
timeOut.addListener(errorListener);
|
||||||
|
|
||||||
configurations = new SelectBox<>(game.data.skin);
|
configurations = new SelectBox<>(game.data.skin);
|
||||||
configurations.setSize(400, 64);
|
configurations.setSize(400, 64);
|
||||||
configurations.setPosition(WizardGame.WIDTH * 0.3f, WizardGame.HEIGHT * 0.3f);
|
configurations.setPosition(WizardGame.WIDTH * 0.3f, WizardGame.HEIGHT * 0.3f);
|
||||||
|
configurations.addListener(errorListener);
|
||||||
|
|
||||||
Array<String> values = new Array<>();
|
Array<String> values = new Array<>();
|
||||||
for (Configuration value: Configuration.values() ) {
|
for (Configuration value : Configuration.values()) {
|
||||||
values.add(value.toString());
|
values.add(value.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
configurations.setItems(values);
|
configurations.setItems(values);
|
||||||
|
|
||||||
var contentTable = new Table().center().left();
|
var contentTable = new Table().center().left();
|
||||||
contentTable.columnDefaults(0).growX().width(0.4f * WizardGame.WIDTH - 20);
|
contentTable.columnDefaults(0).growX().width(0.4f * WizardGame.WIDTH - 20);
|
||||||
contentTable.setSize(0.4f * WizardGame.WIDTH - 20, 400);
|
contentTable.setSize(0.4f * WizardGame.WIDTH - 20, 400);
|
||||||
|
|
||||||
contentTable.add(new Label(game.messages.get("menu.lobby.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(sessionName).row();
|
|
||||||
contentTable.add(new Label(game.messages.get("menu.lobby.create_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(configurations).row();
|
|
||||||
|
|
||||||
contentTable.setPosition(WizardGame.WIDTH * 0.3f, WizardGame.HEIGHT * 0.3f);
|
contentTable.setPosition(WizardGame.WIDTH * 0.3f, WizardGame.HEIGHT * 0.3f);
|
||||||
|
|
||||||
|
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.create_game.session_name.label"), game.data.skin)).row();
|
||||||
|
contentTable.add(sessionName).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.create_game.session_configuration.label"), game.data.skin)).row();
|
||||||
|
contentTable.add(configurations).row();
|
||||||
|
|
||||||
Gdx.input.setInputProcessor(game.data.stage);
|
Gdx.input.setInputProcessor(game.data.stage);
|
||||||
game.data.stage.addActor(buttonContinue);
|
game.data.stage.addActor(buttonContinue);
|
||||||
game.data.stage.addActor(contentTable);
|
game.data.stage.addActor(contentTable);
|
||||||
@ -113,4 +119,47 @@ public class CreateGameScreen extends MenuScreen {
|
|||||||
buttonContinue.addListener(listener);
|
buttonContinue.addListener(listener);
|
||||||
buttonBack.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));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -14,7 +14,13 @@ menu.lobby.session_name.label=Session Name
|
|||||||
menu.lobby.session_player_count.label=Current Player Count
|
menu.lobby.session_player_count.label=Current Player Count
|
||||||
menu.lobby.session_configuration.label=Configuration
|
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.loading=Loading...
|
||||||
menu.loading.back=Return To Main Menu
|
menu.loading.back=Return To Main Menu
|
||||||
|
@ -14,7 +14,13 @@ menu.lobby.session_name.label=Session Name
|
|||||||
menu.lobby.session_player_count.label=Spieleranzahl
|
menu.lobby.session_player_count.label=Spieleranzahl
|
||||||
menu.lobby.session_configuration.label=Spielvariante
|
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.loading=Laden...
|
||||||
menu.loading.back=Zurück zum Hauptmenü
|
menu.loading.back=Zurück zum Hauptmenü
|
||||||
|
@ -81,8 +81,12 @@
|
|||||||
"scrollStyle": "default",
|
"scrollStyle": "default",
|
||||||
"listStyle": {
|
"listStyle": {
|
||||||
"font": "default-font",
|
"font": "default-font",
|
||||||
"selection": "default-select-selection"
|
"selection": "selection"
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
"error": {
|
||||||
|
"parent": "default",
|
||||||
|
"background": "default-select-error"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"SplitPaneStyle": {
|
"SplitPaneStyle": {
|
||||||
@ -136,9 +140,8 @@
|
|||||||
"fontColor": "gold"
|
"fontColor": "gold"
|
||||||
},
|
},
|
||||||
"textfield": {
|
"textfield": {
|
||||||
"background": "textfield",
|
"parent": "default",
|
||||||
"font": "default-font",
|
"background": "textfield"
|
||||||
"fontColor": "white"
|
|
||||||
},
|
},
|
||||||
"handwritten": {
|
"handwritten": {
|
||||||
"font": "handwritten",
|
"font": "handwritten",
|
||||||
@ -153,6 +156,10 @@
|
|||||||
"fontColor": "white",
|
"fontColor": "white",
|
||||||
"messageFontColor": "light_gray",
|
"messageFontColor": "light_gray",
|
||||||
"cursor": "cursor"
|
"cursor": "cursor"
|
||||||
|
},
|
||||||
|
"error": {
|
||||||
|
"parent": "default",
|
||||||
|
"background": "textfield-error"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"CheckBoxStyle": {
|
"CheckBoxStyle": {
|
||||||
|
Binary file not shown.
After Width: | Height: | Size: 468 B |
Binary file not shown.
Before Width: | Height: | Size: 423 B After Width: | Height: | Size: 481 B |
Binary file not shown.
After Width: | Height: | Size: 236 B |
Loading…
x
Reference in New Issue
Block a user