com.github.unluckyninja.defenseofhuman.MenuScreen.java Source code

Java tutorial

Introduction

Here is the source code for com.github.unluckyninja.defenseofhuman.MenuScreen.java

Source

/****************************************************************************************
 * Copyright (C) 2013 UnluckyNinja                                                      *
 *                                                                                      *
 * This program is free software; you can redistribute it and/or                        *
 * modify it under the terms of the GNU General Public License                          *
 * as published by the Free Software Foundation; either version 2                       *
 * of the License, or (at your option) any later version.                               *
 *                                                                                      *
 * This program is distributed in the hope that it will be useful,                      *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of                       *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                        *
 * GNU General Public License for more details.                                         *
 *                                                                                      *
 * You should have received a copy of the GNU General Public License                    *
 * along with this program; if not, see here:http://www.gnu.org/licenses/gpl-2.0.txt    *
 ****************************************************************************************/
package com.github.unluckyninja.defenseofhuman;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.g2d.*;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
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.ClickListener;
import com.badlogic.gdx.utils.Array;

import static com.github.unluckyninja.defenseofhuman.DefenseOfHuman.*;

/**
 * @author UnluckyNinja
 */
public class MenuScreen implements Screen {

    private SpriteBatch batch;

    private Stage stage;

    public MenuScreen(SpriteBatch batch) {
        this.batch = batch;
        setupStage();
        tempCreate();
    }

    Animation animation;

    private void tempCreate() {
        TextureAtlas atlas = new TextureAtlas(Gdx.files.internal("textures/characters/test.pack"));
        Array<TextureAtlas.AtlasRegion> regions = atlas.findRegions("running");
        animation = new Animation(0.15f, regions, Animation.LOOP);
    }

    private void setupStage() {
        stage = new Stage();

        Table table = new Table();
        table.setFillParent(true);
        stage.addActor(table);

        TextButton.TextButtonStyle style = new TextButton.TextButtonStyle();
        style.font = new BitmapFont();

        Actor actor = new TextButton("Play", style);
        actor.addListener(new ClickListener() {
            private int i;

            @Override
            public void clicked(InputEvent event, float x, float y) {
                DefenseOfHuman.game.changeScreen(State.PLAYING);
            }
        });
        table.add(actor);

        table.row();

        actor = new TextButton("Quit", style);
        actor.addListener(new ClickListener() {
            private int i;

            @Override
            public void clicked(InputEvent event, float x, float y) {
                Gdx.app.exit();
            }
        });
        table.add(actor);
    }

    float time;

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

        Table.drawDebug(stage);
        TextButton.drawDebug(stage);

        time += delta;
        TextureRegion region = animation.getKeyFrame(time);
        batch.begin();
        float width = region.getRegionWidth() / PIXELS_PERMETER;
        float height = region.getRegionHeight() / PIXELS_PERMETER;
        batch.draw(region, DefenseOfHuman.game.sceneInputController.cursorPosition.x - width / 2,
                DefenseOfHuman.game.sceneInputController.cursorPosition.y, width, height);
        batch.end();
    }

    @Override
    public void resize(int width, int height) {
        stage.setViewport(width, height, true);
        Vector3 temp = new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0);
        DefenseOfHuman.game.unproject(temp);
        DefenseOfHuman.game.sceneInputController.cursorPosition.set(temp.x, temp.y);
    }

    @Override
    public void show() {
        DefenseOfHuman.game.inputilexer.addProcessor(0, stage);
    }

    @Override
    public void hide() {
        DefenseOfHuman.game.inputilexer.removeProcessor(stage);
    }

    @Override
    public void pause() {
    }

    @Override
    public void resume() {
    }

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

}