package de.eightbitboy.ecorealms;

import com.badlogic.gdx.Input.Buttons;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.InputAdapter;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.graphics.PerspectiveCamera;

public class Control extends InputAdapter implements InputProcessor {
	private static final float SENSITIVITY = 0.2f;

	private PerspectiveCamera camera;
	private int cameraX = 0;
	private int cameraY = 0;

	private boolean lmbDown = false;
	private boolean rmbDown = false;
	private int mouseScreenX = 0;
	private int mouseScreenY = 0;

	Control(PerspectiveCamera camera) {
		this.camera = camera;
	}

	@Override
	public boolean keyDown(int keycode) {
		Logger.debug("Key: " + Keys.toString(keycode));

		switch (keycode) {
			case Keys.W:
				cameraX = +1;
				cameraY = +1;
				break;
			case Keys.A:
				cameraX = -1;
				cameraY = +1;
				break;
			case Keys.S:
				cameraX = -1;
				cameraY = -1;
				break;
			case Keys.D:
				cameraX = +1;
				cameraY = -1;
				break;
		}

		return false;
	}

	@Override
	public boolean keyUp(int keycode) {
		switch (keycode) {
			case Keys.W:
				cameraX = 0;
				cameraY = 0;
				break;
			case Keys.A:
				cameraX = 0;
				cameraY = 0;
				break;
			case Keys.S:
				cameraX = 0;
				cameraY = 0;
				break;
			case Keys.D:
				cameraX = 0;
				cameraY = 0;
				break;
		}
		return false;
	}

	@Override
	public boolean touchDown(int screenX, int screenY, int pointer, int button) {
		mouseScreenX = screenX;
		mouseScreenY = screenY;

		switch (button) {
			case Buttons.LEFT:
				lmbDown = true;
				Logger.debug("Key: LMB");
				break;
			case Buttons.RIGHT:
				rmbDown = true;
				Logger.debug("Key: RMB");
				break;
		}

		Logger.debug("Screen: " + screenX + "," + screenY);
		
		return super.touchDown(screenX, screenY, pointer, button);
	}

	@Override
	public boolean touchUp(int screenX, int screenY, int pointer, int button) {
		switch (button) {
			case Buttons.LEFT:
				lmbDown = false;
				break;
			case Buttons.RIGHT:
				rmbDown = false;
				break;
		}
		return super.touchUp(screenX, screenY, pointer, button);
	}

	void updateCamera() {
		camera.position.x += cameraX * SENSITIVITY;
		camera.position.y += cameraY * SENSITIVITY;
	}
}

@@ -232,16 +232,176 @@
 eCamera;
+%0Aimport com.badlogic.gdx.math.Intersector;%0Aimport com.badlogic.gdx.math.Plane;%0Aimport com.badlogic.gdx.math.Vector3;%0Aimport com.badlogic.gdx.math.collision.Ray;
 %0A%0Apublic
@@ -1924,16 +1924,52 @@
 enY);%0A%09%09
+showClickInWorld(screenX, screenY);%0A
 %0A%09%09retur
@@ -2427,11 +2427,306 @@
 VITY;%0A%09%7D
+%0A%0A%09private void showClickInWorld(int screenX, int screenY) %7B%0A%09%09Plane plane = new Plane(new Vector3(0, 0, 1), 0);%0A%09%09Ray ray = camera.getPickRay(screenX, screenY);%0A%0A%09%09Vector3 point = new Vector3();%0A%09%09if (Intersector.intersectRayPlane(ray, plane, point)) %7B%0A%09%09%09Logger.debug(point.toString());%0A%09%09%7D%0A%09%7D
 %0A%7D%0A

Get the intersection point