com.binarytenshi.nopassing.core.InputHandler.java Source code

Java tutorial

Introduction

Here is the source code for com.binarytenshi.nopassing.core.InputHandler.java

Source

/*
 * Copyright:
 * This file was created by BinaryTENSHi and distributed
 * as part of NoPassing.
 *
 * NoPassing lies under a license which can be
 * found in the LICENSE file in the root directory
 * File created @ [26.12.2013, 19:04:40 CH timezone]
 */
package com.binarytenshi.nopassing.core;

import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.math.Vector3;
import com.binarytenshi.nopassing.core.environment.StreetHandler;
import com.binarytenshi.nopassing.lib.TextureLib;

public class InputHandler implements InputProcessor {
    public static InputHandler instance;
    private Vector2 startTouchDown;
    private boolean dragged;

    public InputHandler() {
        instance = this;
    }

    @Override
    public boolean keyDown(int keycode) {
        // TODO hold down

        switch (keycode) {
        case Keys.A:
        case Keys.LEFT:
            CameraHandler.move(-10, 0, 0);
            return true;

        case Keys.D:
        case Keys.RIGHT:
            CameraHandler.move(10, 0, 0);
            return true;

        case Keys.W:
        case Keys.UP:
            CameraHandler.move(0, 10, 0);
            return true;

        case Keys.S:
        case Keys.DOWN:
            CameraHandler.move(0, -10, 0);
            return true;
        }

        return false;
    }

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

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

    @Override
    public boolean mouseMoved(int screenX, int screenY) {
        Vector2 tileVector = getTileVector(screenX, screenY);

        //StreetHandler.highlightAt((int) tileVector.x, (int) tileVector.y);

        return false;
    }

    @Override
    public boolean scrolled(int amount) {
        CameraHandler.zoom(amount);

        return false;
    }

    @Override
    public boolean touchDown(int screenX, int screenY, int pointer, int button) {
        if (!SplineHandler.hasSpline()) {
            SplineHandler.beginSpline();
        }

        /*
        startTouchDown = new Vector2(screenX, screenY);
        */

        dragged = false;
        return false;
    }

    @Override
    public boolean touchDragged(int screenX, int screenY, int pointer) {
        dragged = true;

        SplineHandler.addPoint(getTileVector(screenX, screenY));

        /*Vector2 newTouch = new Vector2(screenX, screenY);
        Vector2 moveVector = startTouchDown.sub(newTouch);
        moveVector.y = -moveVector.y;
        moveVector.scl(CameraHandler.getCamera().zoom / (Gdx.graphics.getWidth() / CameraHandler.SCALE));
            
        CameraHandler.move(moveVector.x, moveVector.y, 0);
            
        startTouchDown = newTouch;
        */
        return false;
    }

    @Override
    public boolean touchUp(int screenX, int screenY, int pointer, int button) {
        if (SplineHandler.hasSpline()) {
            Spline spline = SplineHandler.endSpline();
            for (Vector2 v : spline.getPoints()) {
                StreetHandler.addRoadAt((int) v.x, (int) v.y);
            }
        }

        /*
        if (!dragged) {
        Vector2 tileVector = getTileVector(screenX, screenY);
            
        if (button == Input.Buttons.LEFT) {
            StreetHandler.addRoadAt((int) tileVector.x, (int) tileVector.y);
        } else if (button == Input.Buttons.RIGHT) {
            StreetHandler.removeRoadAt((int) tileVector.x, (int) tileVector.y);
        }
        }*/

        return false;
    }

    private Vector2 getTileVector(int screenX, int screenY) {
        Rectangle viewport = CameraHandler.getViewport();
        Vector3 touchVector = new Vector3(screenX, screenY, 0);
        CameraHandler.getCamera().unproject(touchVector, viewport.x, viewport.y, viewport.width, viewport.height);
        return new Vector2((int) (touchVector.x / TextureLib.tileSide),
                (int) (touchVector.y / TextureLib.tileSide));
    }
}