Java tutorial
/* * 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.graphics.Color; import com.badlogic.gdx.graphics.g2d.BitmapFont; import com.badlogic.gdx.graphics.g2d.GlyphLayout; import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.graphics.g2d.TextureAtlas; import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator; import com.badlogic.gdx.scenes.scene2d.InputEvent; import com.badlogic.gdx.scenes.scene2d.Stage; import com.badlogic.gdx.scenes.scene2d.ui.Skin; import com.badlogic.gdx.scenes.scene2d.ui.Table; import com.badlogic.gdx.scenes.scene2d.ui.TextButton; import com.badlogic.gdx.scenes.scene2d.utils.ClickListener; import com.badlogic.gdx.utils.Timer; import com.badlogic.gdx.utils.Timer.Task; import com.circle.game.Config; import java.util.TimerTask; import misc.CircleColors; import networking.GameClient; /** * * @author <a href="mailto:martin.drost@student.fontys.nl">Martin Dorst</a> */ public class EnterName extends ApplicationAdapter { private Stage stage; //done private TextureAtlas atlas; //done private Skin skin; //done private Table table; // Align all buttons private TextButton buttonStart; private BitmapFont font60; private BitmapFont errorFont; private BitmapFont font25; private BitmapFont font30; private BitmapFont font30Fade; private GlyphLayout layout; private SpriteBatch batch; private String enteredName; private int numberOfChars; private int maxNumberOfChars; private FontFade fade; private boolean usesSpecialChars; public EnterName() { create(); } @Override public void create() { usesSpecialChars = false; layout = new GlyphLayout(); batch = new SpriteBatch(); enteredName = Config.username; Config.username = ""; numberOfChars = enteredName.length(); maxNumberOfChars = 10; //initialize fonts FreeTypeFontGenerator generator = new FreeTypeFontGenerator(Gdx.files.internal("fonts/MunroSmall.ttf")); FreeTypeFontGenerator.FreeTypeFontParameter parameter = new FreeTypeFontGenerator.FreeTypeFontParameter(); parameter.size = 60; font60 = generator.generateFont(parameter); // font size 12 pixels font60.setColor(Color.BLACK); parameter.size = 40; font30 = generator.generateFont(parameter); // font size 12 pixels font30.setColor(0.38f, 0.38f, 0.38f, 1f); font30Fade = generator.generateFont(parameter); // font size 12 pixels font30Fade.setColor(0.38f, 0.38f, 0.38f, 1f); parameter.size = 25; errorFont = generator.generateFont(parameter); // font size 12 pixels errorFont.setColor(CircleColors.red); font25 = generator.generateFont(parameter); // font size 12 pixels font25.setColor(Color.BLACK); generator.dispose(); // don't forget to dispose to avoid memory leaks! fade = new FontFade(font30Fade); //prepare buttons stage = new Stage() { @Override public boolean keyDown(int keyCode) { if (keyCode == Input.Keys.BACKSPACE) { if (enteredName.length() > 0) enteredName = enteredName.substring(0, enteredName.length() - 1); } else if (keyCode == Input.Keys.ENTER && enteredName.length() != 0) { Config.username = enteredName; GameClient.registerClientName(enteredName); } else if (keyCode > 28 && keyCode < 55) { if (numberOfChars < maxNumberOfChars) enteredName += Input.Keys.toString(keyCode); } else { usesSpecialChars = true; Timer.schedule(new Task() { @Override public void run() { usesSpecialChars = false; } }, 1); } numberOfChars = enteredName.length(); return super.keyDown(keyCode); } }; Gdx.input.setInputProcessor(stage); atlas = new TextureAtlas("ui/buttonPack.pack"); skin = new Skin(atlas); table = new Table(skin); table.setBounds(0, -150, Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); final TextButton.TextButtonStyle textButtonStyle = new TextButton.TextButtonStyle(); textButtonStyle.downFontColor = new Color(0f, 0.57f, 0.92f, 1f); textButtonStyle.overFontColor = new Color(0f, 0.57f, 1f, 1f); textButtonStyle.font = font30; textButtonStyle.fontColor = Color.BLACK; buttonStart = new TextButton("Play", textButtonStyle); buttonStart.addListener(new ClickListener() { @Override public void clicked(InputEvent event, float x, float y) { if (enteredName.length() == 0) return; Config.username = enteredName; GameClient.registerClientName(enteredName); } }); buttonStart.pad(20); table.add(buttonStart); stage.addActor(table); } @Override public void render() { batch.begin(); //Write Circle layout.setText(font60, "Enter name"); int x = (int) (Gdx.graphics.getWidth() / 2 - layout.width / 2); int y = (int) (Gdx.graphics.getHeight() / 2 + 60); font60.draw(batch, "Enter name", x, y); //Write instrustions layout.setText(font25, "State your name (" + numberOfChars + "/" + maxNumberOfChars + ")"); x = (int) (Gdx.graphics.getWidth() / 2 - layout.width / 2); y = (int) (Gdx.graphics.getHeight() / 2 + 10); font25.draw(batch, "State your name (" + numberOfChars + "/" + maxNumberOfChars + ")", x, y); //Write entered name layout.setText(font30, enteredName + "_"); x = (int) (Gdx.graphics.getWidth() / 2 - layout.width / 2); y = (int) (Gdx.graphics.getHeight() / 2 - 30); font30.draw(batch, enteredName, x, y); //Write fading text trailer layout.setText(font30, enteredName); x = (int) (x + layout.width); y = (int) (Gdx.graphics.getHeight() / 2 - 30); font30Fade.draw(batch, "_", x, y); fade.update(); if (usesSpecialChars) { layout.setText(errorFont, "Can't use special characters"); x = (int) (Gdx.graphics.getWidth() / 2 - layout.width / 2); y = (int) (Gdx.graphics.getHeight() / 2 - 60); errorFont.draw(batch, "Can't use special characters", x, y); } batch.end(); stage.act(); stage.draw(); } }