com.theosirian.ppioo.controllers.Controller.java Source code

Java tutorial

Introduction

Here is the source code for com.theosirian.ppioo.controllers.Controller.java

Source

/*
 * The MIT License (MIT)
 * Copyright (c) 2015-2016, theosirian
 *
 * 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.theosirian.ppioo.controllers;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.InputMultiplexer;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.theosirian.ppioo.Puno;
import com.theosirian.ppioo.util.ScreenshotFactory;

/**
 * Created by theosirian on 07/01/2016.
 */
public abstract class Controller implements InputProcessor {
    public Table rootLayout;
    protected Puno game;
    protected boolean invalid;

    protected Controller(Puno game) {
        this.game = game;
    }

    public void start() {
        InputMultiplexer inputMultiplexer = new InputMultiplexer();
        inputMultiplexer.addProcessor(game.stage);
        inputMultiplexer.addProcessor(this);
        createComponents();
        Gdx.input.setInputProcessor(inputMultiplexer);

        game.stage.addActor(rootLayout);
        buildUI();
    }

    protected abstract void createComponents();

    protected abstract void buildUI();

    public void update(float deltaTime) {
        if (game.stage != null) {
            game.stage.act(deltaTime);
        }
        if (isInvalid()) {
            buildUI();
            onRefreshUI();
            invalid = false;
        }
    }

    protected void onRefreshUI() {
    }

    public void draw(SpriteBatch batch) {
        if (game.stage != null) {
            game.stage.draw();
        }
    }

    public void dispose() {

    }

    @Override
    public boolean keyDown(int keycode) {
        if (keycode == Input.Keys.PLUS) {
            game.data.changeMoney(500);
            invalidate();
        }
        if (keycode == Keys.P) {
            ScreenshotFactory.saveScreenshot();
        }
        return false;
    }

    @Override
    public boolean keyUp(int keycode) {
        return false;
    }

    @Override
    public boolean keyTyped(char character) {
        return false;
    }

    @Override
    public boolean touchDown(int screenX, int screenY, int pointer, int button) {
        return false;
    }

    @Override
    public boolean touchUp(int screenX, int screenY, int pointer, int button) {
        return false;
    }

    @Override
    public boolean touchDragged(int screenX, int screenY, int pointer) {
        return false;
    }

    @Override
    public boolean mouseMoved(int screenX, int screenY) {
        return false;
    }

    @Override
    public boolean scrolled(int amount) {
        return false;
    }

    public boolean isInvalid() {
        return invalid;
    }

    public void invalidate() {
        this.invalid = true;
    }
}