package de.markusfisch.android.shadereditor;

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.opengl.GLSurfaceView;
import android.util.AttributeSet;
import android.view.MotionEvent;

public class ShaderView
	extends GLSurfaceView
{
	public final ShaderRenderer renderer = new ShaderRenderer();

	private final AccelerometerListener accelerometerListener =
		new AccelerometerListener();
	private SensorManager sensorManager = null;
	private Sensor accelerometerSensor = null;
	private boolean listeningToAccelerometer = false;

	public ShaderView( Context context )
	{
		super( context );
		init();
	}

	public ShaderView( Context context, AttributeSet attrs )
	{
		super( context, attrs );
		init();
	}

	@Override
	public void onPause()
	{
		super.onPause();

		if( accelerometerSensor != null &&
			listeningToAccelerometer )
		{
			sensorManager.unregisterListener(
				accelerometerListener );

			listeningToAccelerometer = false;
		}
	}

	@Override
	public boolean onTouchEvent( MotionEvent e )
	{
		renderer.onTouch( e.getX(), e.getY() );

		return true;
	}

	public void registerAccelerometerListener()
	{
		if( !listeningToAccelerometer &&
			(sensorManager != null ||
				(sensorManager = (SensorManager)
					getContext().getSystemService(
						Context.SENSOR_SERVICE )) != null) &&
			(accelerometerSensor != null ||
				(accelerometerSensor = sensorManager.getDefaultSensor(
					Sensor.TYPE_ACCELEROMETER )) != null) )
			listeningToAccelerometer = sensorManager.registerListener(
				accelerometerListener,
				accelerometerSensor,
				SensorManager.SENSOR_DELAY_NORMAL );
	}

	private void init()
	{
		renderer.view = this;

		setEGLContextClientVersion( 2 );
		setRenderer( renderer );
		setRenderMode( GLSurfaceView.RENDERMODE_CONTINUOUSLY );
	}

	private class AccelerometerListener implements SensorEventListener
	{
		private long last = 0;

		@Override
		public final void onAccuracyChanged( Sensor sensor, int accuracy )
		{
		}

		@Override
		public final void onSensorChanged( SensorEvent event )
		{
			if( last > 0 )
			{
				final float t = event.timestamp;
				final float a = t/(t+(t-last));
				final float b = 1f-a;

				renderer.gravity[0] =
					a*renderer.gravity[0]+b*event.values[0];
				renderer.gravity[1] =
					a*renderer.gravity[1]+b*event.values[1];
				renderer.gravity[2] =
					a*renderer.gravity[2]+b*event.values[2];

				renderer.linear[0] =
					event.values[0]-renderer.gravity[0];
				renderer.linear[1] =
					event.values[1]-renderer.gravity[1];
				renderer.linear[2] =
					event.values[2]-renderer.gravity[2];
			}

			last = event.timestamp;
		}
	}
}

@@ -1581,16 +1581,55 @@
 null) )%0A
+%09%09%7B%0A%09%09%09accelerometerListener.reset();%0A%0A
 %09%09%09liste
@@ -1771,24 +1771,28 @@
 Y_NORMAL );%0A
+%09%09%7D%0A
 %09%7D%0A%0A%09private
@@ -2042,16 +2042,52 @@
 te long 
+last;%0A%0A%09%09public void reset()%0A%09%09%7B%0A%09%09%09
 last = 0
@@ -2087,16 +2087,20 @@
 ast = 0;
+%0A%09%09%7D
 %0A%0A%09%09@Ove
@@ -2432,16 +2432,22 @@
 vity%5B0%5D+
+%0A%09%09%09%09%09
 b*event.
@@ -2453,24 +2453,25 @@
 .values%5B0%5D;%0A
+%0A
 %09%09%09%09renderer
@@ -2511,16 +2511,22 @@
 vity%5B1%5D+
+%0A%09%09%09%09%09
 b*event.
@@ -2532,24 +2532,25 @@
 .values%5B1%5D;%0A
+%0A
 %09%09%09%09renderer
@@ -2590,16 +2590,22 @@
 vity%5B2%5D+
+%0A%09%09%09%09%09
 b*event.
@@ -2662,16 +2662,22 @@
 lues%5B0%5D-
+%0A%09%09%09%09%09
 renderer
@@ -2689,16 +2689,17 @@
 ity%5B0%5D;%0A
+%0A
 %09%09%09%09rend
@@ -2736,16 +2736,22 @@
 lues%5B1%5D-
+%0A%09%09%09%09%09
 renderer
@@ -2763,16 +2763,17 @@
 ity%5B1%5D;%0A
+%0A
 %09%09%09%09rend
@@ -2810,16 +2810,22 @@
 lues%5B2%5D-
+%0A%09%09%09%09%09
 renderer

Reset accelerometer timer