gui.screen.HelpScreen.java Source code

Java tutorial

Introduction

Here is the source code for gui.screen.HelpScreen.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 gui.screen;

import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL30;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;
import com.badlogic.gdx.utils.GdxRuntimeException;
import com.badlogic.gdx.utils.viewport.StretchViewport;
import com.gdx.bomberman.Constants;

import java.util.ArrayList;

import client.Client;
import gui.AudioManager;
import gui.TextureManager;
import inputHandling.InputHandler;
import server.Server;

import static gui.TextureManager.backSkin;
import static gui.TextureManager.loadTexture;
import static gui.TextureManager.roundSkin;

/**
 *
 * @author qubasa
 */
public class HelpScreen extends Screens implements Screen {
    //Objects
    private Stage stage;
    private Table rootTable = new Table();
    private InputHandler inputHandler = new InputHandler();

    //Buttons
    private TextButton slideRight;
    private TextButton slideLeft;
    private TextButton backButton;

    private ArrayList<Texture> helpScreens = new ArrayList<>();
    private int currentBackground = 0;

    /**------------------------CONSTRUCTOR------------------------**/
    public HelpScreen(final Game game, final Client client, final Server server) {
        super(game, client, server);

        /**------------------------GET HELP BACKGROUND INTO ARRAY------------------------**/
        // Indexes all help backgrounds till not found exception
        try {
            for (int i = 1; true; i++) {
                helpScreens.add(loadTexture("menu/help" + i + ".png"));
            }
        } catch (GdxRuntimeException e) {

        }

        /**------------------------SETUP------------------------**/
        //Set input and viewpoint
        stage = new Stage(new StretchViewport(Constants.SCREENWIDTH, Constants.SCREENHEIGHT));
        inputHandler.setInputSource(stage);

        //Set background
        rootTable.background(new TextureRegionDrawable(new TextureRegion(helpScreens.get(0))));
        rootTable.setFillParent(true);
        stage.addActor(rootTable);

        //Initialise Font
        FreeTypeFontGenerator.FreeTypeFontParameter fontOptions = new FreeTypeFontGenerator.FreeTypeFontParameter();
        fontOptions.size = 11;
        BitmapFont font = TextureManager.menuFont.generateFont(fontOptions);

        /**------------------------BACK BUTTON------------------------**/
        TextButton.TextButtonStyle textButtonStyleBack = new TextButton.TextButtonStyle();
        textButtonStyleBack.font = font;
        textButtonStyleBack.up = backSkin.getDrawable("button_up");
        textButtonStyleBack.down = backSkin.getDrawable("button_down");
        textButtonStyleBack.over = backSkin.getDrawable("button_checked");

        /**------------------------ROUND BUTTON------------------------**/
        TextButton.TextButtonStyle textButtonStyleRound = new TextButton.TextButtonStyle();
        textButtonStyleRound.font = font;
        textButtonStyleRound.up = roundSkin.getDrawable("button_up");
        textButtonStyleRound.down = roundSkin.getDrawable("button_down");
        textButtonStyleRound.over = roundSkin.getDrawable("button_checked");

        /**------------------------ALL BUTTONS------------------------**/
        // Back button
        backButton = new TextButton("", textButtonStyleBack);
        backButton.setPosition(0, Constants.SCREENHEIGHT - backButton.getHeight() + 7);
        stage.addActor(backButton);

        //Slide right button
        slideRight = new TextButton(">>>", textButtonStyleRound);
        slideRight.setPosition(420, 20);
        stage.addActor(slideRight);

        //Slide right button
        slideLeft = new TextButton("<<<", textButtonStyleRound);
        slideLeft.setPosition(340, 20);
        stage.addActor(slideLeft);

        //Add click listener --> Back button
        backButton.addListener(new ChangeListener() {
            @Override
            public void changed(ChangeListener.ChangeEvent event, Actor actor) {
                //Add click musik
                AudioManager.playClickSound();

                // Wait till sound is done
                try {
                    Thread.sleep(100);

                } catch (InterruptedException ex) {

                }

                game.setScreen(new MenuScreen(game, client, server));
            }
        });

        //Add click listener --> Slide right
        slideRight.addListener(new ChangeListener() {
            @Override
            public void changed(ChangeListener.ChangeEvent event, Actor actor) {
                //Add click musik
                AudioManager.playClickSound();

                //Wait till sound is done
                try {
                    Thread.sleep(100);

                } catch (InterruptedException ex) {

                }

                nextBackground();
            }
        });

        //Add click listener --> Slide left
        slideLeft.addListener(new ChangeListener() {
            @Override
            public void changed(ChangeListener.ChangeEvent event, Actor actor) {
                //Add click musik
                AudioManager.playClickSound();

                //Wait till sound is done
                try {
                    Thread.sleep(100);

                } catch (InterruptedException ex) {

                }

                previousBackground();
            }
        });

    }

    private void nextBackground() {
        if (currentBackground < this.helpScreens.size() - 1) {
            currentBackground++;
            rootTable.background(new TextureRegionDrawable(new TextureRegion(helpScreens.get(currentBackground))));
        }
    }

    private void previousBackground() {
        if (currentBackground + 1 > 1) {
            currentBackground--;
            rootTable.background(new TextureRegionDrawable(new TextureRegion(helpScreens.get(currentBackground))));
        }
    }

    /**------------------------RENDER------------------------**/
    @Override
    public void render(float f) {
        //Debug
        //stage.setDebugAll(true);

        //Clear Screen
        Gdx.gl.glClearColor(0.2f, 0.2f, 0.2f, 1);
        Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT);

        //Draw stage
        stage.act(Constants.DELTATIME);
        stage.draw();

        /*------------------SWITCH TO FULLSCREEN AND BACK------------------*/
        super.changeToFullScreenOnF12();

        /*------------------QUIT GAME------------------*/
        if (Gdx.input.isKeyPressed(Input.Keys.ESCAPE)) {
            game.setScreen(new MenuScreen(game, client, server));
        }
    }

    /**------------------------RESIZE------------------------**/
    @Override
    public void resize(int width, int height) {
        stage.getViewport().update(width, height, false);
    }

    /**------------------------DISPOSE------------------------**/
    @Override
    public void dispose() {
        stage.dispose();
        backSkin.dispose();
    }

    /**------------------------OTHER------------------------**/
    @Override
    public void pause() {
    }

    @Override
    public void resume() {
    }

    @Override
    public void show() {
    }

    @Override
    public void hide() {
    }

}