Home | History | Annotate | Download | only in shaderstest
      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 
     17 package com.android.shaderstest;
     18 
     19 import android.content.Context;
     20 import android.renderscript.RSSurfaceView;
     21 import android.renderscript.RenderScriptGL;
     22 import android.view.MotionEvent;
     23 import android.view.ScaleGestureDetector;
     24 import android.view.SurfaceHolder;
     25 
     26 public class ShadersTestView extends RSSurfaceView {
     27 
     28     private RenderScriptGL mRS;
     29     private ShadersTestRS mRender;
     30 
     31     private ScaleGestureDetector mScaleDetector;
     32 
     33     private static final int INVALID_POINTER_ID = -1;
     34     private int mActivePointerId = INVALID_POINTER_ID;
     35 
     36     public ShadersTestView(Context context) {
     37         super(context);
     38         ensureRenderScript();
     39         mScaleDetector = new ScaleGestureDetector(context, new ScaleListener());
     40     }
     41 
     42     private void ensureRenderScript() {
     43         if (mRS == null) {
     44             RenderScriptGL.SurfaceConfig sc = new RenderScriptGL.SurfaceConfig();
     45             sc.setDepth(16, 24);
     46             mRS = createRenderScriptGL(sc);
     47             mRender = new ShadersTestRS();
     48             mRender.init(mRS, getResources());
     49         }
     50     }
     51 
     52     @Override
     53     protected void onAttachedToWindow() {
     54         super.onAttachedToWindow();
     55         ensureRenderScript();
     56     }
     57 
     58     @Override
     59     public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
     60         super.surfaceChanged(holder, format, w, h);
     61         mRender.surfaceChanged();
     62     }
     63 
     64     @Override
     65     protected void onDetachedFromWindow() {
     66         mRender = null;
     67         if (mRS != null) {
     68             mRS = null;
     69             destroyRenderScriptGL();
     70         }
     71     }
     72 
     73     @Override
     74     public boolean onTouchEvent(MotionEvent ev) {
     75         mScaleDetector.onTouchEvent(ev);
     76 
     77         boolean ret = false;
     78         float x = ev.getX();
     79         float y = ev.getY();
     80 
     81         final int action = ev.getAction();
     82 
     83         switch (action & MotionEvent.ACTION_MASK) {
     84         case MotionEvent.ACTION_DOWN: {
     85             mRender.onActionDown(x, y);
     86             mActivePointerId = ev.getPointerId(0);
     87             ret = true;
     88             break;
     89         }
     90         case MotionEvent.ACTION_MOVE: {
     91             if (!mScaleDetector.isInProgress()) {
     92                 mRender.onActionMove(x, y);
     93             }
     94             mRender.onActionDown(x, y);
     95             ret = true;
     96             break;
     97         }
     98 
     99         case MotionEvent.ACTION_UP: {
    100             mActivePointerId = INVALID_POINTER_ID;
    101             break;
    102         }
    103 
    104         case MotionEvent.ACTION_CANCEL: {
    105             mActivePointerId = INVALID_POINTER_ID;
    106             break;
    107         }
    108 
    109         case MotionEvent.ACTION_POINTER_UP: {
    110             final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK)
    111                     >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
    112             final int pointerId = ev.getPointerId(pointerIndex);
    113             if (pointerId == mActivePointerId) {
    114                 // This was our active pointer going up. Choose a new
    115                 // active pointer and adjust accordingly.
    116                 final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
    117                 x = ev.getX(newPointerIndex);
    118                 y = ev.getY(newPointerIndex);
    119                 mRender.onActionDown(x, y);
    120                 mActivePointerId = ev.getPointerId(newPointerIndex);
    121             }
    122             break;
    123         }
    124         }
    125 
    126         return ret;
    127     }
    128 
    129     private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
    130         @Override
    131         public boolean onScale(ScaleGestureDetector detector) {
    132             mRender.onActionScale(detector.getScaleFactor());
    133             return true;
    134         }
    135     }
    136 }
    137 
    138 
    139