package com.miviclin.droidengine2ddemos.input.touch;

import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;

import com.miviclin.droidengine2d.Game;
import com.miviclin.droidengine2d.graphics.Color;
import com.miviclin.droidengine2d.graphics.Graphics;
import com.miviclin.droidengine2d.graphics.material.TextureMaterial;
import com.miviclin.droidengine2d.graphics.texture.TextureAtlas;
import com.miviclin.droidengine2d.graphics.texture.TexturePackerAtlas;
import com.miviclin.droidengine2d.input.KeyListener;
import com.miviclin.droidengine2d.input.TouchListener;
import com.miviclin.droidengine2d.screen.Screen;
import com.miviclin.droidengine2d.util.Transform;
import com.miviclin.droidengine2d.util.math.Vector2;
import com.miviclin.droidengine2d.util.time.TimeCounter;
import com.miviclin.droidengine2ddemos.util.Rectangle;

public class TouchDemoScreen extends Screen {

	private static final int MAX_VISIBLE_TIME_MILLIS = 200;

	private Color backgroundColor;
	private volatile Rectangle<TextureMaterial> rectangle;
	private volatile TimeCounter timeCounter;

	public TouchDemoScreen(float viewWidth, float viewHeight, Game game) {
		super(viewWidth, viewHeight, game);
	}

	@Override
	public void update(float delta) {
		timeCounter.update();
		getInputManager().processInput();
	}

	@Override
	public void draw(Graphics g) {
		g.setBackgroundColor(backgroundColor);
		if (timeCounter.getMilliseconds() <= MAX_VISIBLE_TIME_MILLIS) {
			g.drawRect(rectangle.getMaterial(), rectangle.getTransform());
		}
	}

	@Override
	public void onRegister() {
		backgroundColor = new Color(0.5f, 0.5f, 0.5f);

		TextureAtlas textureAtlas = new TexturePackerAtlas();
		textureAtlas.loadFromFile("textures/touch-icon-atlas.xml", getGame().getActivity());
		getGame().getTextureManager().addTextureAtlas(textureAtlas);

		Transform transform = new Transform(new Vector2(getWidth() / 2, getHeight() / 2), new Vector2(240, 240));
		rectangle = new Rectangle<TextureMaterial>(transform,
				new TextureMaterial(textureAtlas.getTextureRegion("touch-icon.png")));

		timeCounter = new TimeCounter();

		getInputManager().getTouchController().setTouchListener(new TouchListener() {

			@Override
			public boolean onTouch(MotionEvent motionEvent) {
				if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
					rectangle.getTransform().getPosition().set(motionEvent.getX(), motionEvent.getY());
					timeCounter.reset();
					Log.d("touch", "touch " + motionEvent.getAction());
				}
				return true;
			}
		});

		getInputManager().getKeyController().setKeyListener(new KeyListener() {

			@Override
			public boolean onKey(int keyCode, KeyEvent event) {
				return true;
			}
		});
	}

	@Override
	public void onActivation() {
	}

	@Override
	public void onDeactivation() {
	}

	@Override
	public void onPause() {
	}

	@Override
	public void onResume() {
	}

	@Override
	public void onDispose() {
	}

}

@@ -76,38 +76,8 @@
 og;%0A
-import android.view.KeyEvent;%0A
 impo
@@ -498,73 +498,26 @@
 put.
-KeyListener;%0Aimport com.miviclin.droidengine2d.input.TouchListene
+TouchInputProcesso
 r;%0Ai
@@ -2065,16 +2065,21 @@
 getTouch
+Input
 Controll
@@ -2095,33 +2095,45 @@
 ouch
-Listener(new TouchListene
+InputProcessor(new TouchInputProcesso
 r() 
@@ -2162,23 +2162,31 @@
 lic 
-boolean onTouch
+void processMotionEvent
 (Mot
@@ -2338,16 +2338,30 @@
 .getX(),
+ getHeight() -
  motionE
@@ -2467,197 +2467,8 @@
 %09%09%7D%0A
-%09%09%09%09return true;%0A%09%09%09%7D%0A%09%09%7D);%0A%0A%09%09getInputManager().getKeyController().setKeyListener(new KeyListener() %7B%0A%0A%09%09%09@Override%0A%09%09%09public boolean onKey(int keyCode, KeyEvent event) %7B%0A%09%09%09%09return true;%0A
 %09%09%09%7D

Fix errors in TouchDemoScreen