      1: package com.programmez.opengl.exercice1;

      3: import android.content.Context;
      4: import android.opengl.GLSurfaceView;
      5: import android.view.MotionEvent;

      7: public class CubeSurfaceView extends GLSurfaceView {
      8:         CubeRenderer mRenderer;
      9:         private final float TOUCH_SCALE_FACTOR = 180.0f / 320;
     10:         private final float TRACKBALL_SCALE_FACTOR = 36.0f;
     11:         private float mPreviousX = 0;
     12:         private float mPreviousY = 0;

     14:         public CubeSurfaceView(Context context) {
     15:                 super(context);
     16:                 mRenderer = new CubeRenderer(context);
     17:                 setRenderer(mRenderer);
     18:                 setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY);
     19:         }

     21:         @Override
     22:         public boolean onTrackballEvent(MotionEvent e) {
     23:                 mRenderer.setmAngleX(mRenderer.getmAngleX() + e.getX()
     24:                                 * TRACKBALL_SCALE_FACTOR);
     25:                 mRenderer.setmAngleY(mRenderer.getmAngleY() + e.getY()
     26:                                 * TRACKBALL_SCALE_FACTOR);
     27:                 return true;
     28:         }

     30:         @Override
     31:         public boolean onTouchEvent(MotionEvent e) {
     32:                 float x = e.getX();
     33:                 float y = e.getY();
     34:                 switch (e.getAction()) {
     35:                 case MotionEvent.ACTION_MOVE:
     36:                         float dx = x - mPreviousX;
     37:                         float dy = y - mPreviousY;
     38:                         mRenderer.setmAngleX(mRenderer.getmAngleX() + dy
     39:                                         * TOUCH_SCALE_FACTOR);
     40:                         mRenderer.setmAngleY(mRenderer.getmAngleY() + dx
     41:                                         * TOUCH_SCALE_FACTOR);
     42:                 }
     43:                 mPreviousX = x;
     44:                 mPreviousY = y;
     45:                 return true;
     46:         }
     47: }