1 /*
2  * Copyright (C) 2015 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 
17 package com.android.performanceLaunch.helper;
18 
19 import android.content.Context;
20 import android.opengl.GLSurfaceView;
21 import android.view.MotionEvent;
22 
23 /**
24  * A view container where OpenGL ES graphics can be drawn on screen. This view can also be used to
25  * capture touch events, such as a user interacting with drawn objects.
26  * Source : development/samples/OpenGL/HelloOpenGLES20
27  */
28 public class SimpleGLSurfaceView extends GLSurfaceView {
29 
30     private final SimpleGLRenderer mRenderer;
31 
SimpleGLSurfaceView(Context context)32     public SimpleGLSurfaceView(Context context) {
33         super(context);
34 
35         // Create an OpenGL ES 2.0 context.
36         setEGLContextClientVersion(2);
37 
38         // Set the Renderer for drawing on the GLSurfaceView
39         mRenderer = new SimpleGLRenderer();
40         setRenderer(mRenderer);
41 
42         // Render the view only when there is a change in the drawing data
43         setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
44     }
45 
46     private final float TOUCH_SCALE_FACTOR = 180.0f / 320;
47     private float mPreviousX;
48     private float mPreviousY;
49 
50     @Override
onTouchEvent(MotionEvent e)51     public boolean onTouchEvent(MotionEvent e) {
52         // MotionEvent reports input details from the touch screen
53         // and other input controls. In this case, you are only
54         // interested in events where the touch position changed.
55 
56         float x = e.getX();
57         float y = e.getY();
58 
59         switch (e.getAction()) {
60             case MotionEvent.ACTION_MOVE:
61 
62                 float dx = x - mPreviousX;
63                 float dy = y - mPreviousY;
64 
65                 // reverse direction of rotation above the mid-line
66                 if (y > getHeight() / 2) {
67                     dx = dx * -1;
68                 }
69 
70                 // reverse direction of rotation to left of the mid-line
71                 if (x < getWidth() / 2) {
72                     dy = dy * -1;
73                 }
74 
75                 mRenderer.setAngle(
76                         mRenderer.getAngle() +
77                                 ((dx + dy) * TOUCH_SCALE_FACTOR)); // = 180.0f / 320
78                 requestRender();
79         }
80 
81         mPreviousX = x;
82         mPreviousY = y;
83         return true;
84     }
85 
86 }
87