Home | History | Annotate | Download | only in testapp
      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.testapp;
     18 
     19 import android.util.Log;
     20 import android.renderscript.Float3;
     21 import com.android.scenegraph.*;
     22 import com.android.scenegraph.CompoundTransform.RotateComponent;
     23 import com.android.scenegraph.CompoundTransform.TranslateComponent;
     24 
     25 public class TouchHandler {
     26     private static String TAG = "TouchHandler";
     27 
     28     float mLastX;
     29     float mLastY;
     30 
     31     float mRotateXValue;
     32     float mRotateYValue;
     33     Float3 mDistValue;
     34     Float3 mPosValue;
     35 
     36     CompoundTransform mCameraRig;
     37     RotateComponent mRotateX;
     38     RotateComponent mRotateY;
     39     TranslateComponent mDist;
     40     TranslateComponent mPosition;
     41     Camera mCamera;
     42 
     43     public void init(Scene scene) {
     44         // Some initial values for camera position
     45         mRotateXValue = -20;
     46         mRotateYValue = 0;
     47         mDistValue = new Float3(0, 0, 45);
     48         mPosValue = new Float3(0, 4, 0);
     49 
     50         // Make a camera transform we can manipulate
     51         mCameraRig = scene.appendNewCompoundTransform();
     52         mCameraRig.setName("CameraRig");
     53 
     54         mPosition = mCameraRig.addTranslate("Position", mPosValue);
     55         mRotateY  = mCameraRig.addRotate("RotateY", new Float3(0, 1, 0), mRotateYValue);
     56         mRotateX  = mCameraRig.addRotate("RotateX", new Float3(1, 0, 0), mRotateXValue);
     57         mDist     = mCameraRig.addTranslate("Distance", mDistValue);
     58 
     59         mCamera = scene.appendNewCamera();
     60         mCamera.setTransform(mCameraRig);
     61     }
     62 
     63     public Camera getCamera() {
     64         return mCamera;
     65     }
     66 
     67     public void onActionDown(float x, float y) {
     68         mLastX = x;
     69         mLastY = y;
     70     }
     71 
     72     public void onActionScale(float scale) {
     73         if (mDist == null) {
     74             return;
     75         }
     76         mDistValue.z *= 1.0f / scale;
     77         mDistValue.z = Math.max(10.0f, Math.min(mDistValue.z, 150.0f));
     78         mDist.setValue(mDistValue);
     79     }
     80 
     81     public void onActionMove(float x, float y) {
     82         if (mRotateX == null) {
     83             return;
     84         }
     85 
     86         float dx = mLastX - x;
     87         float dy = mLastY - y;
     88 
     89         if (Math.abs(dy) <= 2.0f) {
     90             dy = 0.0f;
     91         }
     92         if (Math.abs(dx) <= 2.0f) {
     93             dx = 0.0f;
     94         }
     95 
     96         mRotateYValue += dx * 0.25f;
     97         mRotateYValue %= 360.0f;
     98 
     99         mRotateXValue  += dy * 0.25f;
    100         mRotateXValue  = Math.max(mRotateXValue , -80.0f);
    101         mRotateXValue  = Math.min(mRotateXValue , 0.0f);
    102 
    103         mRotateX.setAngle(mRotateXValue);
    104         mRotateY.setAngle(mRotateYValue);
    105 
    106         mLastX = x;
    107         mLastY = y;
    108     }
    109 }
    110