diff --git a/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/screens/ErrorScreen.java b/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/screens/ErrorScreen.java new file mode 100644 index 0000000..1874f2f --- /dev/null +++ b/wizard-client/wizard-client-libgdx/core/src/main/java/eu/jonahbauer/wizard/client/libgdx/screens/ErrorScreen.java @@ -0,0 +1,68 @@ +package eu.jonahbauer.wizard.client.libgdx.screens; + +import com.badlogic.gdx.scenes.scene2d.Actor; +import com.badlogic.gdx.scenes.scene2d.ui.Label; +import com.badlogic.gdx.scenes.scene2d.ui.TextButton; +import com.badlogic.gdx.scenes.scene2d.ui.VerticalGroup; +import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener; +import eu.jonahbauer.wizard.client.libgdx.WizardGame; +import eu.jonahbauer.wizard.client.libgdx.listeners.KeyboardFocusManager; +import eu.jonahbauer.wizard.client.libgdx.state.Session; +import eu.jonahbauer.wizard.common.messages.server.NackMessage; + +public class ErrorScreen extends MenuScreen { + private final String labelText; + private TextButton buttonBack; + private final MenuScreen prevScreen; + + private final ChangeListener listener = new ChangeListener() { + @Override + public void changed(ChangeEvent event, Actor actor) { + if (actor == buttonBack) { + game.setScreen(prevScreen); + sfxClick(); + } + } + }; + + public ErrorScreen(WizardGame game, MenuScreen prevScreen, NackMessage message) { + super(game); + this.prevScreen = prevScreen; + labelText = switch (message.getCode()) { + case NackMessage.GAME_ALREADY_STARTED -> messages.get("menu.error.game_already_started"); + case NackMessage.SESSION_FULL -> messages.get("menu.error.session_full"); + case NackMessage.SESSION_NOT_FOUND -> messages.get("menu.error.session_not_found"); + case NackMessage.PLAYER_NAME_TAKEN -> messages.get("menu.error.player_name_taken"); + case NackMessage.PLAYER_NAME_NOT_ALLOWED -> messages.get("menu.error.player_name_not_allowed"); + case NackMessage.SESSION_NAME_TAKEN -> messages.get("menu.error.session_name_taken"); + case NackMessage.SESSION_NAME_NOT_ALLOWED -> messages.get("menu.error.session_name_not_allowed"); + case NackMessage.MALFORMED_MESSAGE -> messages.get("menu.error.malformed_message"); + case NackMessage.UNEXPECTED_MESSAGE -> messages.get("menu.error.unexpected_message"); + case NackMessage.ILLEGAL_ARGUMENT -> messages.get("menu.error.illegal_argument"); + case NackMessage.NOT_FOUND -> messages.get("menu.error.not_found"); + case NackMessage.ALREADY_CONNECTED -> messages.get("menu.error.already_connected"); + case NackMessage.GAME_NOT_YET_STARTED -> messages.get("menu.error.game_not_yet_started"); + case NackMessage.PLAYER_NOT_FOUND -> messages.get("menu.error.player_not_found"); + case NackMessage.ILLEGAL_STATE -> messages.get("menu.error.illegal_state"); + case NackMessage.BAD_REQUEST -> messages.get("menu.error.bad_request"); + default -> "Something went terribly wrong :("; + }; + } + + @Override + public void show() { + super.show(); + + var buttonBack = new TextButton(messages.get("menu.error.back"), skin); + var label = new Label(labelText, skin); + + var content = new VerticalGroup(); + content.setPosition(WizardGame.WIDTH * 0.5f, WizardGame.HEIGHT*0.5f); + content.addActor(label); + content.addActor(buttonBack); + + stage.addActor(content); + stage.addCaptureListener(new KeyboardFocusManager(buttonBack)); + } + +}