summaryrefslogtreecommitdiffstats
path: root/tests/RenderScriptTests/SceneGraph/src/com/android/testapp/TouchHandler.java
blob: d0f979734c9e7890f3099e7ba7452565ae4b5da1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.testapp;

import android.util.Log;
import android.renderscript.Float3;
import com.android.scenegraph.*;
import com.android.scenegraph.CompoundTransform.RotateComponent;
import com.android.scenegraph.CompoundTransform.TranslateComponent;

public class TouchHandler {
    private static String TAG = "TouchHandler";

    float mLastX;
    float mLastY;

    float mRotateXValue;
    float mRotateYValue;
    Float3 mDistValue;
    Float3 mPosValue;

    CompoundTransform mCameraRig;
    RotateComponent mRotateX;
    RotateComponent mRotateY;
    TranslateComponent mDist;
    TranslateComponent mPosition;
    Camera mCamera;

    public void init(Scene scene) {
        // Some initial values for camera position
        mRotateXValue = -20;
        mRotateYValue = 0;
        mDistValue = new Float3(0, 0, 45);
        mPosValue = new Float3(0, 4, 0);

        // Make a camera transform we can manipulate
        mCameraRig = scene.appendNewCompoundTransform();
        mCameraRig.setName("CameraRig");

        mPosition = mCameraRig.addTranslate("Position", mPosValue);
        mRotateY  = mCameraRig.addRotate("RotateY", new Float3(0, 1, 0), mRotateYValue);
        mRotateX  = mCameraRig.addRotate("RotateX", new Float3(1, 0, 0), mRotateXValue);
        mDist     = mCameraRig.addTranslate("Distance", mDistValue);

        mCamera = scene.appendNewCamera();
        mCamera.setTransform(mCameraRig);
    }

    public Camera getCamera() {
        return mCamera;
    }

    public void onActionDown(float x, float y) {
        mLastX = x;
        mLastY = y;
    }

    public void onActionScale(float scale) {
        if (mDist == null) {
            return;
        }
        mDistValue.z *= 1.0f / scale;
        mDistValue.z = Math.max(10.0f, Math.min(mDistValue.z, 150.0f));
        mDist.setValue(mDistValue);
    }

    public void onActionMove(float x, float y) {
        if (mRotateX == null) {
            return;
        }

        float dx = mLastX - x;
        float dy = mLastY - y;

        if (Math.abs(dy) <= 2.0f) {
            dy = 0.0f;
        }
        if (Math.abs(dx) <= 2.0f) {
            dx = 0.0f;
        }

        mRotateYValue += dx * 0.25f;
        mRotateYValue %= 360.0f;

        mRotateXValue  += dy * 0.25f;
        mRotateXValue  = Math.max(mRotateXValue , -80.0f);
        mRotateXValue  = Math.min(mRotateXValue , 0.0f);

        mRotateX.setAngle(mRotateXValue);
        mRotateY.setAngle(mRotateYValue);

        mLastX = x;
        mLastY = y;
    }
}