package com.miviclin.droidengine2d.input;

import android.view.MotionEvent;

import com.miviclin.collections.PooledLinkedQueue;

/**
 * TouchInputController.
 * 
 * @author Miguel Vicente Linares
 * 
 */
public class TouchInputController {

	private final Object motionEventQueueLock = new Object();

	private TouchInputProcessor touchInputProcessor;
	private PooledLinkedQueue<MotionEvent> motionEventQueue;

	/**
	 * Creates a new TouchInputController.
	 */
	public TouchInputController() {
		this.touchInputProcessor = null;
		this.motionEventQueue = new PooledLinkedQueue<MotionEvent>(60);
	}

	/**
	 * Queues a copy of the specified MotionEvent for later processing. The event will be recycled when
	 * {@link TouchInputController#processTouchInput()} is called.
	 * 
	 * @param motionEvent MotionEvent.
	 */
	public void queueCopyOfMotionEvent(MotionEvent motionEvent) {
		if (motionEvent != null) {
			synchronized (motionEventQueueLock) {
				MotionEvent motionEventToQueue = MotionEvent.obtain(motionEvent);
				motionEventQueue.add(motionEventToQueue);
			}
		}
	}

	/**
	 * Processes touch input.<br>
	 * This method should be called when the game updates, before the update is processed.<br>
	 * {@link TouchInputProcessor#processMotionEvent(MotionEvent)} will be called once per MotionEvent queued in this
	 * TouchInputController. After that call, {@link MotionEvent#recycle()} will be called on the MotionEvent.
	 */
	public void processTouchInput() {
		if (touchInputProcessor != null) {
			MotionEvent motionEvent;
			synchronized (motionEventQueueLock) {
				while ((motionEvent = motionEventQueue.poll()) != null) {
					touchInputProcessor.processMotionEvent(motionEvent);
					motionEvent.recycle();
				}
			}
		}
	}

	/**
	 * Sets the {@link TouchInputProcessor} of this TouchInputController.
	 * 
	 * @param touchInputProcessor TouchListener.
	 */
	public void setTouchInputProcessor(TouchInputProcessor touchInputProcessor) {
		this.touchInputProcessor = touchInputProcessor;
	}

}

@@ -1504,50 +1504,58 @@
 %0A%09%09%09
-MotionEvent motionEvent;%0A%09%09%09synchronized
+synchronized (motionEventQueueLock) %7B%0A%09%09%09%09while
  (
+!
 moti
@@ -1570,28 +1570,39 @@
 ueue
-Lock) %7B%0A%09%09%09%09while ((
+.isEmpty()) %7B%0A%09%09%09%09%09MotionEvent 
 moti
@@ -1638,20 +1638,9 @@
 ll()
-) != null) %7B
+;
 %0A%09%09%09

Refactor TouchInputController.processTouchInput()