se.theodor.quiz.screen.MainMenu.java Source code

Java tutorial

Introduction

Here is the source code for se.theodor.quiz.screen.MainMenu.java

Source

/********************************************************************************
 * 
 *   Copyright 2014 Theodor Angergard
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *
 *******************************************************************************/

package se.theodor.quiz.screen;

import static se.theodor.quiz.util.Objects.BATCH;
import static se.theodor.quiz.util.Objects.GREEN_TEXT_BUTTON_STYLE;

import se.theodor.quiz.Quiz;
import se.theodor.quiz.util.Objects;
import se.theodor.quiz.util.Values;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
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.Table;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;

public class MainMenu implements Screen {

    public MainMenu(Quiz quiz) {
        this.quiz = quiz;
    }

    private Quiz quiz;
    private Stage stage;

    @Override
    public void render(float delta) {
        stage.act(delta);
        BATCH.begin();
        stage.draw();
        BATCH.end();
    }

    @Override
    public void resize(int width, int height) {
    }

    @Override
    public void show() {
        TextButton play = new TextButton("Play", GREEN_TEXT_BUTTON_STYLE);
        play.addListener(new InputListener() {
            @Override
            public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
                return true;
            }

            @Override
            public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
                quiz.setScreen(new CreateOrLoadQuiz(quiz));
            }

        });

        TextButton create = new TextButton("Create a new Quiz", GREEN_TEXT_BUTTON_STYLE);
        create.addListener(new InputListener() {
            @Override
            public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
                return true;
            }

            @Override
            public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
                if (button == 0) {
                    quiz.setScreen(new CreateQuiz(quiz));
                }
            }

        });

        TextButton options = new TextButton("Settings", GREEN_TEXT_BUTTON_STYLE);
        options.addListener(new InputListener() {
            @Override
            public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
                return true;
            }

            @Override
            public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
                if (button == 0) {

                }
            }

        });

        TextButton exit = new TextButton("Exit", GREEN_TEXT_BUTTON_STYLE);
        exit.addListener(new InputListener() {
            @Override
            public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
                return true;
            }

            @Override
            public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
                if (button == 0) {
                    Gdx.app.exit();
                }
            }

        });

        Table table = new Table();
        table.setBounds(0, 0, quiz.getWidth(), quiz.getHeight());
        stage = Objects.STAGE;
        stage.clear();

        table.add(play).pad(Values.PAD);
        table.row();
        table.add(create).pad(Values.PAD);
        table.row();
        table.add(exit).pad(Values.PAD);
        stage.addActor(table);
    }

    @Override
    public void hide() {

    }

    @Override
    public void pause() {

    }

    @Override
    public void resume() {

    }

    @Override
    public void dispose() {
        stage.dispose();
    }

}