• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2008 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.example.android.rs.balls;
18 
19 import java.io.Writer;
20 import java.util.ArrayList;
21 import java.util.concurrent.Semaphore;
22 
23 import android.renderscript.RSSurfaceView;
24 import android.renderscript.RenderScript;
25 import android.renderscript.RenderScriptGL;
26 
27 import android.content.Context;
28 import android.content.res.Resources;
29 import android.graphics.Bitmap;
30 import android.graphics.drawable.BitmapDrawable;
31 import android.graphics.drawable.Drawable;
32 import android.os.Handler;
33 import android.os.Message;
34 import android.util.AttributeSet;
35 import android.util.Log;
36 import android.view.Surface;
37 import android.view.SurfaceHolder;
38 import android.view.SurfaceView;
39 import android.view.KeyEvent;
40 import android.view.MotionEvent;
41 
42 public class BallsView extends RSSurfaceView {
43 
BallsView(Context context)44     public BallsView(Context context) {
45         super(context);
46         //setFocusable(true);
47     }
48 
49     private RenderScriptGL mRS;
50     private BallsRS mRender;
51 
surfaceChanged(SurfaceHolder holder, int format, int w, int h)52     public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
53         super.surfaceChanged(holder, format, w, h);
54         if (mRS == null) {
55             RenderScriptGL.SurfaceConfig sc = new RenderScriptGL.SurfaceConfig();
56             mRS = createRenderScriptGL(sc);
57             mRS.setSurface(holder, w, h);
58             mRender = new BallsRS();
59             mRender.init(mRS, getResources(), w, h);
60         }
61         mRender.updateProjectionMatrices();
62     }
63 
64     @Override
onDetachedFromWindow()65     protected void onDetachedFromWindow() {
66         if(mRS != null) {
67             mRS = null;
68             destroyRenderScriptGL();
69         }
70     }
71 
72 
73     @Override
onTouchEvent(MotionEvent ev)74     public boolean onTouchEvent(MotionEvent ev)
75     {
76         int act = ev.getActionMasked();
77         if (act == ev.ACTION_UP) {
78             mRender.newTouchPosition(0, 0, 0, ev.getPointerId(0));
79             return false;
80         } else if (act == MotionEvent.ACTION_POINTER_UP) {
81             // only one pointer going up, we can get the index like this
82             int pointerIndex = ev.getActionIndex();
83             int pointerId = ev.getPointerId(pointerIndex);
84             mRender.newTouchPosition(0, 0, 0, pointerId);
85             return false;
86         }
87         int count = ev.getHistorySize();
88         int pcount = ev.getPointerCount();
89 
90         for (int p=0; p < pcount; p++) {
91             int id = ev.getPointerId(p);
92             mRender.newTouchPosition(ev.getX(p),
93                                      ev.getY(p),
94                                      ev.getPressure(p),
95                                      id);
96 
97             for (int i=0; i < count; i++) {
98                 mRender.newTouchPosition(ev.getHistoricalX(p, i),
99                                          ev.getHistoricalY(p, i),
100                                          ev.getHistoricalPressure(p, i),
101                                          id);
102             }
103         }
104         return true;
105     }
106 
setAccel(float x, float y, float z)107     void setAccel(float x, float y, float z) {
108         if (mRender == null) {
109             return;
110         }
111         mRender.setAccel(x, -y);
112     }
113 
114 }
115 
116 
117