org.saltosion.pixelprophecy.input.GameInputAdapter.java Source code

Java tutorial

Introduction

Here is the source code for org.saltosion.pixelprophecy.input.GameInputAdapter.java

Source

/*
 * Copyright (C) 2016 Saltosion
 *
 * 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 3 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
 * GNUss 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 <http://www.gnu.org/licenses/>.
 */
package org.saltosion.pixelprophecy.input;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputAdapter;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.math.Vector2;
import org.saltosion.pixelprophecy.states.GameState;
import org.saltosion.pixelprophecy.util.Globals;

public class GameInputAdapter extends InputAdapter {

    GameState gameState;

    public GameInputAdapter(GameState gameState) {
        this.gameState = gameState;
    }

    @Override
    public boolean keyDown(int keycode) {
        if (!gameState.isActive()) {
            return false;
        }
        if (InputManager.isMapped(keycode)) {
            InputManager.get(keycode).pressed();
            return true;
        }
        return false;
    }

    @Override
    public boolean keyUp(int keycode) {
        if (!gameState.isActive()) {
            return false;
        }
        if (InputManager.isMapped(keycode)) {
            InputManager.get(keycode).released();
            return true;
        }
        return false;
    }

    @Override
    public boolean touchDown(int screenX, int screenY, int pointer, int button) {
        Vector2 screenSize = new Vector2(Globals.VIEWPORT_HEIGHT * Globals.ASPECT_RATIO, Globals.VIEWPORT_HEIGHT);
        Vector2 pos = new Vector2(screenX * screenSize.x / Gdx.graphics.getWidth(),
                (Gdx.graphics.getHeight() - screenY) * screenSize.y / Gdx.graphics.getHeight());
        OrthographicCamera cam = gameState.getGameCamera();
        pos.add(cam.position.x, cam.position.y);
        pos.sub(screenSize.scl(.5f));
        System.out.println(pos);
        return true;
    }

}