scenes.PauseScreen.java Source code

Java tutorial

Introduction

Here is the source code for scenes.PauseScreen.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.InputProcessor;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
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.InputListener;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
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.Align;
import com.circle.game.Config;
import networking.GameClient;

/**
 *
 * @author Martin Dorst <martin.drost@student.fontys.nl>
 */
public class PauseScreen extends ApplicationAdapter {

    private Stage stage; //done
    private TextureAtlas atlas; //done
    private Skin skin; //done
    private Table table; // Align all buttons
    private BitmapFont font60, font30, font25; //done
    private Label heading;
    private InputProcessor previouspProcessor;

    public PauseScreen() {
        create();

    }

    @Override
    public void create() {
        stage = new Stage();
        previouspProcessor = Gdx.input.getInputProcessor();
        Gdx.input.setInputProcessor(stage);

        stage.addListener(new InputListener() {
            @Override
            public boolean keyDown(InputEvent event, int keycode) {
                if (keycode == Input.Keys.ESCAPE) {
                    Gdx.input.setInputProcessor(previouspProcessor);
                    Config.overlay = null;
                }
                return true;
            }
        });
        atlas = new TextureAtlas("ui/buttonPack.pack");
        skin = new Skin(atlas);

        table = new Table(skin);
        table.setBounds(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());

        //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.5f, 0.5f, 0.5f, 255);
        generator.dispose(); // don't forget to dispose to avoid memory leaks!

        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;

        Label.LabelStyle headingStyle = new Label.LabelStyle(font60, Color.BLACK);

        //Add head
        table.add(new Label("", headingStyle)).width(300);
        table.row();
        table.row().padBottom(20);
        table.add(new Label("In-game menu", headingStyle)).align(Align.center);

        //Add return to game button
        TextButton returnToGame = new TextButton("Return to game", textButtonStyle);
        returnToGame.addListener(new ClickListener() {
            @Override
            public void clicked(InputEvent event, float x, float y) {
                Gdx.input.setInputProcessor(previouspProcessor);
                Config.overlay = null;
            }
        });
        table.row().padBottom(10);
        table.add(returnToGame).align(Align.center);

        //Add quit game button
        TextButton quitGame = new TextButton("Quit game", textButtonStyle);
        quitGame.addListener(new ClickListener() {
            @Override
            public void clicked(InputEvent event, float x, float y) {
                GameClient.leaveGames();
                Config.overlay = null;
                Config.page = new MainMenu();
            }
        });
        table.row().padBottom(10);
        table.add(quitGame).align(Align.center);

        //table.debug();
        stage.addActor(table);

    }

    @Override
    public void render() {
        stage.act();
        stage.draw();
    }
}