blob: 98cec55b278b87be645ab056ce7847acd6f013fe [file] [log] [blame]
Jason Samse5f2f662012-05-16 15:43:25 -07001/*
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
17package com.example.android.rs.fountain;
18
19import java.io.Writer;
20import java.util.ArrayList;
21import java.util.concurrent.Semaphore;
22
Stephen Hines0e1207e2013-05-06 18:18:57 -070023import android.renderscript.RSSurfaceView;
Jason Samse5f2f662012-05-16 15:43:25 -070024import android.renderscript.RenderScript;
25import android.renderscript.RenderScriptGL;
26
27import android.content.Context;
28import android.content.res.Resources;
29import android.graphics.Bitmap;
30import android.graphics.drawable.BitmapDrawable;
31import android.graphics.drawable.Drawable;
32import android.os.Handler;
33import android.os.Message;
34import android.util.AttributeSet;
35import android.util.Log;
36import android.view.Surface;
37import android.view.SurfaceHolder;
38import android.view.SurfaceView;
39import android.view.KeyEvent;
40import android.view.MotionEvent;
41
Stephen Hines0e1207e2013-05-06 18:18:57 -070042public class FountainView extends RSSurfaceView {
Jason Samse5f2f662012-05-16 15:43:25 -070043
44 public FountainView(Context context) {
45 super(context);
46 //setFocusable(true);
47 }
48
49 private RenderScriptGL mRS;
50 private FountainRS mRender;
51
Stephen Hines0e1207e2013-05-06 18:18:57 -070052 public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
53 super.surfaceChanged(holder, format, w, h);
Jason Samse5f2f662012-05-16 15:43:25 -070054 if (mRS == null) {
55 RenderScriptGL.SurfaceConfig sc = new RenderScriptGL.SurfaceConfig();
56 mRS = createRenderScriptGL(sc);
Stephen Hines0e1207e2013-05-06 18:18:57 -070057 mRS.setSurface(holder, w, h);
Jason Samse5f2f662012-05-16 15:43:25 -070058 mRender = new FountainRS();
59 mRender.init(mRS, getResources());
60 }
61 }
62
63 @Override
64 protected void onDetachedFromWindow() {
Jason Samse5f2f662012-05-16 15:43:25 -070065 if (mRS != null) {
66 mRS = null;
67 destroyRenderScriptGL();
68 }
69 }
70
71
72 @Override
73 public boolean onTouchEvent(MotionEvent ev)
74 {
75 int act = ev.getActionMasked();
76 if (act == ev.ACTION_UP) {
77 mRender.newTouchPosition(0, 0, 0, ev.getPointerId(0));
78 return false;
79 } else if (act == MotionEvent.ACTION_POINTER_UP) {
80 // only one pointer going up, we can get the index like this
81 int pointerIndex = ev.getActionIndex();
82 int pointerId = ev.getPointerId(pointerIndex);
83 mRender.newTouchPosition(0, 0, 0, pointerId);
84 }
85 int count = ev.getHistorySize();
86 int pcount = ev.getPointerCount();
87
88 for (int p=0; p < pcount; p++) {
89 int id = ev.getPointerId(p);
90 mRender.newTouchPosition(ev.getX(p),
91 ev.getY(p),
92 ev.getPressure(p),
93 id);
94
95 for (int i=0; i < count; i++) {
96 mRender.newTouchPosition(ev.getHistoricalX(p, i),
97 ev.getHistoricalY(p, i),
98 ev.getHistoricalPressure(p, i),
99 id);
100 }
101 }
102 return true;
103 }
104}
105
106