es.eucm.ead.mockup.core.control.ScreenController.java Source code

Java tutorial

Introduction

Here is the source code for es.eucm.ead.mockup.core.control.ScreenController.java

Source

/**
 * eAdventure is a research project of the
 *    e-UCM research group.
 *
 *    Copyright 2005-2013 e-UCM research group.
 *
 *    You can access a list of all the contributors to eAdventure at:
 *          http://e-adventure.e-ucm.es/contributors
 *
 *    e-UCM is a research group of the Department of Software Engineering
 *          and Artificial Intelligence at the Complutense University of Madrid
 *          (School of Computer Science).
 *
 *          C Profesor Jose Garcia Santesmases sn,
 *          28040 Madrid (Madrid), Spain.
 *
 *          For more info please visit:  <http://e-adventure.e-ucm.es> or
 *          <http://www.e-ucm.es>
 *
 * ****************************************************************************
 *
 *  This file is part of eAdventure
 *
 *      eAdventure is free software: you can redistribute it and/or modify
 *      it under the terms of the GNU Lesser General Public License as published by
 *      the Free Software Foundation, either version 3 of the License, or
 *      (at your option) any later version.
 *
 *      eAdventure 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 Lesser General Public License for more details.
 *
 *      You should have received a copy of the GNU Lesser General Public License
 *      along with eAdventure.  If not, see <http://www.gnu.org/licenses/>.
 */
package es.eucm.ead.mockup.core.control;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.InputAdapter;
import com.badlogic.gdx.InputMultiplexer;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.GLCommon;

import es.eucm.ead.mockup.core.control.screens.AbstractScreen;

/**
 * Controlls the handlers, the input...
 */
public class ScreenController extends InputAdapter {

    /**
     * Change this color to change the color wich the screen is cleared with.
     */
    private float r = .8f;
    private float g = .9f;
    private float b = 1f;
    private float a = 1f;
    private Color auxClearColor;

    /**
     * Used to cap the fps so we don't suffer stuttering after pause/resume
     * events.
     */
    private final float MIN_FPS = 30F, MIN_DELTA_TIME = 1F / MIN_FPS;

    private AbstractScreen currentScreen;
    private InputMultiplexer multiplexer;

    public void create() {
        auxClearColor = new Color(r, g, b, a);
        this.multiplexer = new InputMultiplexer(AbstractScreen.stage, this);
        Gdx.input.setInputProcessor(this.multiplexer);
    }

    /**
     * Updates the current renderer.
     * 
     * @param delta
     *            Elapsed time since the game last updated.
     */
    public void act(float delta) {
        this.currentScreen.act(Math.min(delta, MIN_DELTA_TIME));
    }

    /**
     * Clears the screen and draws the current renderer.
     */
    public void draw() {
        clearColor();
        this.currentScreen.draw();
    }

    private void clearColor() {
        GLCommon gl = Gdx.gl20;
        gl.glClearColor(r, g, b, a);
        gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    }

    /**
     * Changes the current handler to the next one. Triggers the hide() and
     * show() events.
     * 
     * @param next
     *            The next handler.
     */
    public void changeTo(AbstractScreen next) {
        this.currentScreen.hide();
        this.currentScreen = next;
        this.currentScreen.show();
    }

    public void pause() {
        this.currentScreen.pause();
    }

    public void resume() {
        this.currentScreen.resume();
    }

    @Override
    public boolean keyDown(int keycode) {
        if (isBack(keycode)) {
            this.currentScreen.onBackKeyPressed();
        }
        return true;
    }

    private boolean isBack(int keycode) {
        return keycode == Keys.BACK || keycode == Keys.BACKSPACE;
    }

    public void setCurrentScreen(AbstractScreen currentCtr) {
        this.currentScreen = currentCtr;
    }

    public AbstractScreen getCurrentScreen() {
        return this.currentScreen;
    }

    public void changeClearColor(Color newColor) {

        r = newColor.r;
        g = newColor.g;
        b = newColor.b;
        a = newColor.a;
        auxClearColor.set(r, g, b, a);
    }

    public Color getClearColor() {
        return this.auxClearColor;
    }
}