Search in sources :

Example 1 with NewChatMessagEvent

use of de.gg.event.NewChatMessagEvent in project ProjektGG by eskalon.

the class LobbyScreen method initUI.

@Override
protected void initUI() {
    // backgroundTexture = assetManager.get(BACKGROUND_IMAGE_PATH);
    Sound clickSound = assetManager.get(BUTTON_SOUND);
    // mainTable.setBackground(skin.getDrawable("parchment-small"));
    ImageTextButton leaveButton = new ImageTextButton("Verlassen", skin);
    leaveButton.addListener(new InputListener() {

        public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
            clickSound.play(1F);
            game.getNetworkHandler().disconnect();
            game.pushScreen("mainMenu");
            return true;
        }
    });
    NetworkHandler netHandler = game.getNetworkHandler();
    readyUpLobbyButton = new ImageTextButton("Bereit", skin);
    if (netHandler.isHost()) {
        readyUpLobbyButton.setDisabled(true);
        readyUpLobbyButton.setText("Spiel starten");
    }
    readyUpLobbyButton.addListener(new InputListener() {

        public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
            clickSound.play(1F);
            getLocalPlayer().toggleReady();
            netHandler.onLocalPlayerChange(getLocalPlayer());
            updateLobbyUI();
            return true;
        }
    });
    Table playerTable = new Table();
    Table settingsTable = new Table();
    Table buttonTable = new Table();
    Table chatTable = new Table();
    // settings table + player table
    buttonTable.add(readyUpLobbyButton).bottom().padBottom(20).row();
    buttonTable.add(leaveButton);
    Table chatInputTable = new Table();
    ImageTextButton sendButton = new ImageTextButton("Senden", skin);
    TextField chatInputField = new TextField("", skin);
    chatInputField.setTextFieldListener(new TextFieldListener() {

        @Override
        public void keyTyped(TextField textField, char key) {
            if (!textField.getText().isEmpty() && (key == (char) 13)) {
                // Enter
                clickSound.play(1F);
                netHandler.sendChatMessage(chatInputField.getText());
                onNewChatMessage(new NewChatMessagEvent(localNetworkId, chatInputField.getText()));
                chatInputField.setText("");
            }
        }
    });
    sendButton.addListener(new InputListener() {

        public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
            if (!chatInputField.getText().isEmpty()) {
                clickSound.play(1F);
                netHandler.sendChatMessage(chatInputField.getText());
                onNewChatMessage(new NewChatMessagEvent(localNetworkId, chatInputField.getText()));
                chatInputField.setText("");
            }
            return true;
        }
    });
    messagesArea = new TextArea("", skin);
    messagesArea.setDisabled(true);
    ScrollPane messagesPane = new ScrollPane(messagesArea);
    chatInputTable.add(chatInputField).left().width(325).padRight(15);
    chatInputTable.add(sendButton);
    chatTable.debug();
    chatTable.add(messagesPane).height(125).width(425).top().row();
    chatTable.add(chatInputTable).left().padTop(10).width(425).bottom();
    playerSlots = new Table[6];
    playerSlots[0] = new Table();
    playerSlots[1] = new Table();
    playerSlots[2] = new Table();
    playerSlots[3] = new Table();
    playerSlots[4] = new Table();
    playerSlots[5] = new Table();
    playerTable.add(playerSlots[0]).height(25).width(425).row();
    playerTable.add(playerSlots[1]).height(25).width(425).row();
    playerTable.add(playerSlots[2]).height(25).width(425).row();
    playerTable.add(playerSlots[3]).height(25).width(425).row();
    playerTable.add(playerSlots[4]).height(25).width(425).row();
    playerTable.add(playerSlots[5]).height(25).width(425).row();
    mainTable.add(playerTable).width(425).height(155);
    mainTable.add(settingsTable).width(155).row();
    mainTable.add(chatTable).height(165).bottom();
    mainTable.add(buttonTable).height(165);
    mainTable.setDebug(true);
// updateLobbyUI();
}
Also used : ImageTextButton(com.badlogic.gdx.scenes.scene2d.ui.ImageTextButton) Table(com.badlogic.gdx.scenes.scene2d.ui.Table) TextArea(com.badlogic.gdx.scenes.scene2d.ui.TextArea) Sound(com.badlogic.gdx.audio.Sound) InputListener(com.badlogic.gdx.scenes.scene2d.InputListener) ScrollPane(com.badlogic.gdx.scenes.scene2d.ui.ScrollPane) NewChatMessagEvent(de.gg.event.NewChatMessagEvent) TextField(com.badlogic.gdx.scenes.scene2d.ui.TextField) NetworkHandler(de.gg.network.NetworkHandler) TextFieldListener(com.badlogic.gdx.scenes.scene2d.ui.TextField.TextFieldListener) InputEvent(com.badlogic.gdx.scenes.scene2d.InputEvent)

Example 2 with NewChatMessagEvent

use of de.gg.event.NewChatMessagEvent in project ProjektGG by eskalon.

the class NetworkHandler method setUpConnectionAsClient.

/**
 * Connects the client to the server. After it is finished a
 * {@link ConnectionEstablishedEvent} is posted on the
 * {@linkplain ProjektGG#getEventBus() event bus}.
 *
 * @param ip
 *            The server's ip.
 * @param port
 *            The server's port.
 */
public void setUpConnectionAsClient(String ip, int port) {
    client = new Client();
    client.start();
    NetworkRegisterer.registerClasses(client.getKryo());
    TypeListener listener = new TypeListener();
    // GAME SETUP MESSAGE (ON CLIENT CONNECT)
    listener.addTypeHandler(GameSetupMessage.class, (con, msg) -> {
        eventBus.post(new GameSessionSetupEvent(msg.getPlayers(), msg.getId(), msg.getSettings()));
        localClientId = msg.getId();
        Gdx.app.log("Client", "Netzwerk ID: " + localClientId);
    });
    // NEW CHAT MESSAGE
    listener.addTypeHandler(ChatMessageSentMessage.class, (con, msg) -> {
        eventBus.post(new NewChatMessagEvent(msg.getSenderId(), msg.getMessage()));
    });
    // PLAYER CHANGED
    listener.addTypeHandler(PlayerChangedMessage.class, (con, msg) -> {
        eventBus.post(new PlayerChangedEvent(msg.getId(), msg.getPlayer()));
    });
    // PLAYER JOINED
    listener.addTypeHandler(PlayerJoinedMessage.class, (con, msg) -> {
        eventBus.post(new PlayerConnectedEvent(msg.getId(), msg.getPlayer()));
    });
    // PLAYER LEFT
    listener.addTypeHandler(PlayerLeftMessage.class, (con, msg) -> {
        eventBus.post(new PlayerDisconnectedEvent(msg.getId()));
    });
    client.addListener(listener);
    Thread t = new Thread(new Runnable() {

        public void run() {
            IOException ex = null;
            try {
                client.connect(6000, ip, port);
                Gdx.app.log("Client", "Lobby beigetreten");
            } catch (IOException e) {
                ex = e;
                e.printStackTrace();
            }
            eventBus.post(new ConnectionEstablishedEvent(ex));
        }
    });
    t.start();
}
Also used : PlayerConnectedEvent(de.gg.event.PlayerConnectedEvent) ConnectionEstablishedEvent(de.gg.event.ConnectionEstablishedEvent) TypeListener(com.esotericsoftware.kryonet.Listener.TypeListener) GameSessionSetupEvent(de.gg.event.GameSessionSetupEvent) NewChatMessagEvent(de.gg.event.NewChatMessagEvent) PlayerChangedEvent(de.gg.event.PlayerChangedEvent) PlayerDisconnectedEvent(de.gg.event.PlayerDisconnectedEvent) IOException(java.io.IOException) Client(com.esotericsoftware.kryonet.Client)

Aggregations

NewChatMessagEvent (de.gg.event.NewChatMessagEvent)2 Sound (com.badlogic.gdx.audio.Sound)1 InputEvent (com.badlogic.gdx.scenes.scene2d.InputEvent)1 InputListener (com.badlogic.gdx.scenes.scene2d.InputListener)1 ImageTextButton (com.badlogic.gdx.scenes.scene2d.ui.ImageTextButton)1 ScrollPane (com.badlogic.gdx.scenes.scene2d.ui.ScrollPane)1 Table (com.badlogic.gdx.scenes.scene2d.ui.Table)1 TextArea (com.badlogic.gdx.scenes.scene2d.ui.TextArea)1 TextField (com.badlogic.gdx.scenes.scene2d.ui.TextField)1 TextFieldListener (com.badlogic.gdx.scenes.scene2d.ui.TextField.TextFieldListener)1 Client (com.esotericsoftware.kryonet.Client)1 TypeListener (com.esotericsoftware.kryonet.Listener.TypeListener)1 ConnectionEstablishedEvent (de.gg.event.ConnectionEstablishedEvent)1 GameSessionSetupEvent (de.gg.event.GameSessionSetupEvent)1 PlayerChangedEvent (de.gg.event.PlayerChangedEvent)1 PlayerConnectedEvent (de.gg.event.PlayerConnectedEvent)1 PlayerDisconnectedEvent (de.gg.event.PlayerDisconnectedEvent)1 NetworkHandler (de.gg.network.NetworkHandler)1 IOException (java.io.IOException)1