Home | History | Annotate | Download | only in opengl
      1 /*
      2  * Copyright (C) 2011 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 package com.example.android.opengl;
     17 
     18 import android.content.Context;
     19 import android.opengl.GLSurfaceView;
     20 import android.view.MotionEvent;
     21 
     22 /**
     23  * A view container where OpenGL ES graphics can be drawn on screen.
     24  * This view can also be used to capture touch events, such as a user
     25  * interacting with drawn objects.
     26  */
     27 public class MyGLSurfaceView extends GLSurfaceView {
     28 
     29     private final MyGLRenderer mRenderer;
     30 
     31     public MyGLSurfaceView(Context context) {
     32         super(context);
     33 
     34         // Create an OpenGL ES 2.0 context.
     35         setEGLContextClientVersion(2);
     36 
     37         // Set the Renderer for drawing on the GLSurfaceView
     38         mRenderer = new MyGLRenderer();
     39         setRenderer(mRenderer);
     40 
     41         // Render the view only when there is a change in the drawing data
     42         setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
     43     }
     44 
     45     private final float TOUCH_SCALE_FACTOR = 180.0f / 320;
     46     private float mPreviousX;
     47     private float mPreviousY;
     48 
     49     @Override
     50     public boolean onTouchEvent(MotionEvent e) {
     51         // MotionEvent reports input details from the touch screen
     52         // and other input controls. In this case, you are only
     53         // interested in events where the touch position changed.
     54 
     55         float x = e.getX();
     56         float y = e.getY();
     57 
     58         switch (e.getAction()) {
     59             case MotionEvent.ACTION_MOVE:
     60 
     61                 float dx = x - mPreviousX;
     62                 float dy = y - mPreviousY;
     63 
     64                 // reverse direction of rotation above the mid-line
     65                 if (y > getHeight() / 2) {
     66                     dx = dx * -1 ;
     67                 }
     68 
     69                 // reverse direction of rotation to left of the mid-line
     70                 if (x < getWidth() / 2) {
     71                     dy = dy * -1 ;
     72                 }
     73 
     74                 mRenderer.setAngle(
     75                         mRenderer.getAngle() +
     76                         ((dx + dy) * TOUCH_SCALE_FACTOR));  // = 180.0f / 320
     77                 requestRender();
     78         }
     79 
     80         mPreviousX = x;
     81         mPreviousY = y;
     82         return true;
     83     }
     84 
     85 }
     86