com.flaiker.reaktio.screens.AbstractScreen.java Source code

Java tutorial

Introduction

Here is the source code for com.flaiker.reaktio.screens.AbstractScreen.java

Source

/***********************************************************************************
 * The MIT License (MIT)                                                           *
 *                                                                                 *
 * Copyright (c) 2015 Fabian Lupa                                                  *
 *                                                                                 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy    *
 * of this software and associated documentation files (the "Software"), to deal   *
 * in the Software without restriction, including without limitation the rights    *
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell       *
 * copies of the Software, and to permit persons to whom the Software is           *
 * furnished to do so, subject to the following conditions:                        *
 *                                                                                 *
 * The above copyright notice and this permission notice shall be included in all  *
 * copies or substantial portions of the Software.                                 *
 *                                                                                 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR      *
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,        *
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE     *
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER          *
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,   *
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE   *
 * SOFTWARE.                                                                       *
 ***********************************************************************************/

package com.flaiker.reaktio.screens;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.utils.viewport.StretchViewport;
import com.flaiker.reaktio.Reaktio;

public abstract class AbstractScreen implements Screen {
    private static final String LOG = AbstractScreen.class.getSimpleName();

    protected static final float SCREEN_WIDTH = 480;
    protected static final float SCREEN_HEIGHT = 800;

    protected final Stage uiStage;
    protected final Skin skin;
    protected final Reaktio reaktio;
    protected final SpriteBatch batch;
    protected OrthographicCamera camera;

    private Label fpsLabel;
    private Image backgroundImage;

    public AbstractScreen(Reaktio reaktio, Skin skin) {
        this.batch = new SpriteBatch();
        this.reaktio = reaktio;
        this.uiStage = new Stage(new StretchViewport(SCREEN_WIDTH, SCREEN_HEIGHT));
        this.skin = skin != null ? skin : new Skin(Gdx.files.internal("skin/uiskin.json"));
        this.fpsLabel = new Label("", this.skin, "arial", Color.WHITE);
        this.backgroundImage = new Image(new Texture((Gdx.files.internal("bg.png"))));
        camera = new OrthographicCamera();
        camera.setToOrtho(false, SCREEN_WIDTH, SCREEN_HEIGHT);
    }

    protected abstract String getName();

    @Override
    public void show() {
        Gdx.app.log(LOG, "Showing screen: " + getName());
        //FPS Label
        fpsLabel.setFontScale(0.5f);
        fpsLabel.setPosition(uiStage.getWidth() - fpsLabel.getPrefWidth(),
                uiStage.getHeight() - fpsLabel.getHeight());
        if (reaktio.getPreferencesManager().isFpsCounterEnabled())
            uiStage.addActor(fpsLabel);
    }

    @Override
    public void resize(int width, int height) {
        Gdx.app.log(LOG, "Resizing screen: " + getName() + " to: " + width + " x " + height);
        uiStage.getViewport().update(width, height, true);
    }

    @Override
    public void hide() {
        Gdx.app.log(LOG, "Hiding screen: " + getName());
        dispose();
    }

    @Override
    public void pause() {
        Gdx.app.log(LOG, "Pausing screen: " + getName());
    }

    @Override
    public void resume() {
        Gdx.app.log(LOG, "Resuming screen: " + getName());
    }

    @Override
    public void dispose() {
        Gdx.app.log(LOG, "Disposing screen: " + getName());
        uiStage.dispose();
        //skin.dispose();
        //reaktio.dispose();
        batch.dispose();
    }

    @Override
    public void render(float delta) {
        Gdx.gl.glClearColor(0.1f, 0.1f, 0.1f, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        camera.update();
        batch.setProjectionMatrix(camera.combined);
        batch.begin();

        backgroundImage.draw(batch, 1);
        preUIrender(delta);

        batch.end();

        // Update FPS-Counter
        fpsLabel.setText("FPS: " + String.valueOf(Gdx.graphics.getFramesPerSecond()));
        fpsLabel.setPosition(uiStage.getWidth() - fpsLabel.getPrefWidth(),
                uiStage.getHeight() - fpsLabel.getPrefHeight() / 2);

        uiStage.act(delta);
        uiStage.draw();

    }

    protected abstract void preUIrender(float delta);
}