scenes.Chat.java Source code

Java tutorial

Introduction

Here is the source code for scenes.Chat.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package scenes;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.InputAdapter;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator;
import com.circle.game.Config;
import misc.CircleColors;
import networking.GameClient;

/**
 * 
 * @author 
 */
public class Chat extends ApplicationAdapter {
    private BitmapFont font21;
    private BitmapFont font21Blue;
    private SpriteBatch batch;
    private boolean isTyping;
    private String enteredMessage;
    private InputProcessor previousProcessor;

    public Chat() {
        create();

    }

    @Override
    public void create() {
        batch = new SpriteBatch();

        //initialize fonts
        FreeTypeFontGenerator generator = new FreeTypeFontGenerator(Gdx.files.internal("fonts/MunroSmall.ttf"));
        FreeTypeFontGenerator.FreeTypeFontParameter parameter = new FreeTypeFontGenerator.FreeTypeFontParameter();

        parameter.size = 21;
        font21 = generator.generateFont(parameter);
        font21.setColor(CircleColors.black);
        font21Blue = generator.generateFont(parameter);
        font21Blue.setColor(CircleColors.blue);
        generator.dispose(); // don't forget to dispose to avoid memory leaks!

        isTyping = false;
    }

    @Override
    public void render() {
        batch.begin();
        for (int i = 0; i <= 6; i++) {
            if (i >= GameClient.chatMessages.size())
                break;

            font21.draw(batch, GameClient.chatMessages.get(i).text, 30, 60 + 30 * i);
        }

        if (isTyping)
            font21Blue.draw(batch, Config.username + ": " + enteredMessage + "_", 30, 30);

        batch.end();

        if (Config.username != "" && !isTyping && Gdx.input.isKeyPressed(Input.Keys.T)) {
            openChatWindow();
        }
    }

    private void openChatWindow() {
        previousProcessor = Gdx.input.getInputProcessor();
        enteredMessage = "";
        isTyping = true;

        Gdx.input.setInputProcessor(new InputAdapter() {

            @Override
            public boolean keyDown(int keyCode) {
                if (keyCode == Input.Keys.ENTER) {
                    GameClient.sendChatMessage(enteredMessage);
                    enteredMessage = "";
                    isTyping = false;
                    Gdx.input.setInputProcessor(previousProcessor);
                }
                return true;
            }

            @Override
            public boolean keyTyped(char character) {
                if (character == '\b' && enteredMessage.length() > 0)
                    enteredMessage = enteredMessage.substring(0, enteredMessage.length() - 1);
                if (character == '\b' || character == '\t' || character == '\n' || character == '\r'
                        || character == '\f' || character == '`' || character == '\u0000'
                        || enteredMessage.length() >= 30)
                    return true;

                enteredMessage += character;
                return true;
            }
        });
    }
}