package aurelienribon.bodyeditor.canvas.rigidbodies.input;

import aurelienribon.bodyeditor.Ctx;
import aurelienribon.bodyeditor.canvas.Canvas;
import aurelienribon.bodyeditor.canvas.rigidbodies.RigidBodiesScreen;
import aurelienribon.bodyeditor.models.RigidBodyModel;
import com.badlogic.gdx.Input.Buttons;
import com.badlogic.gdx.InputAdapter;
import com.badlogic.gdx.math.Matrix3;
import com.badlogic.gdx.math.Vector2;

/**
 * @author Aurelien Ribon | http://www.aurelienribon.com/
 */
public class TestInputProcessor extends InputAdapter {
	private final Canvas canvas;
	private final RigidBodiesScreen screen;
	private boolean touchDown = false;

	public TestInputProcessor(Canvas canvas, RigidBodiesScreen screen) {
		this.canvas = canvas;
		this.screen = screen;
	}

	@Override
	public boolean touchDown(int x, int y, int pointer, int button) {
		touchDown = button == Buttons.LEFT;
		if (!touchDown) return false;

		RigidBodyModel model = Ctx.bodies.getSelectedModel();
		if (model == null) return false;

		Vector2 p = canvas.screenToWorld(x, y);
		screen.ballThrowP1 = p;
		screen.ballThrowP2 = p;
		return false;
	}

	@Override
	public boolean touchUp(int x, int y, int pointer, int button) {
		if (!touchDown) return false;
		touchDown = false;

		RigidBodyModel model = Ctx.bodies.getSelectedModel();
		if (model == null) return false;

		Vector2 p1 = screen.ballThrowP1;
		Vector2 p2 = screen.ballThrowP2;
		Vector2 delta = new Vector2(p2).sub(p1);
		screen.fireBall(p1, delta.mul(new Matrix3(new float[]{3f,3f,3f})));//TODO possible bug

		screen.ballThrowP1 = null;
		screen.ballThrowP2 = null;
		return false;
	}

	@Override
	public boolean touchDragged(int x, int y, int pointer) {
		if (!touchDown) return false;

		RigidBodyModel model = Ctx.bodies.getSelectedModel();
		if (model == null) return false;

		Vector2 p = canvas.screenToWorld(x, y);
		screen.ballThrowP2 = p;
		return false;
	}
}

@@ -343,46 +343,8 @@
 er;%0A
-import com.badlogic.gdx.math.Matrix3;%0A
 impo
@@ -1451,68 +1451,17 @@
 lta.
-mul(new Matrix3(new float%5B%5D%7B3f,3f,3f%7D)));//TODO possible bug
+scl(3f));
 %0A%0A%09%09

Fix one ~possible~ actual bug, make test mode work.