Tizen 2.1 base
[platform/upstream/libbullet.git] / Extras / PhysicsEffects / project / Android / PfxApp_5_Raycast / src / pfx / renderingsupport / PhysicsEffectsGLSurfaceView.java
1 /*\r
2  Applied Research Associates Inc. (c)2011\r
3 \r
4  Redistribution and use in source and binary forms,\r
5    with or without modification, are permitted provided that the\r
6    following conditions are met:\r
7     * Redistributions of source code must retain the above copyright\r
8       notice, this list of conditions and the following disclaimer.\r
9     * Redistributions in binary form must reproduce the above copyright\r
10       notice, this list of conditions and the following disclaimer in the\r
11       documentation and/or other materials provided with the distribution.\r
12     * Neither the name of the Applied Research Associates Inc nor the names\r
13       of its contributors may be used to endorse or promote products derived\r
14       from this software without specific prior written permission.\r
15 \r
16    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"\r
17    AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
18    IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
19    ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE\r
20    LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR\r
21    CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF\r
22    SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS\r
23    INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN\r
24    CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\r
25    ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\r
26    POSSIBILITY OF SUCH DAMAGE.\r
27 */\r
28 \r
29 package pfx.renderingsupport;\r
30 \r
31 import android.opengl.GLSurfaceView;\r
32 import android.content.Context;\r
33 import android.view.MotionEvent;\r
34 \r
35 /**\r
36  * Implementation of the OpenGL ES view window and creates/assigns a renderer.\r
37  * Handles touch events to allow the user to change scene on tap, move the camera\r
38  * around by touch based movement, and allows the user to zoom in and out by pinching.\r
39  * @author ahamilton\r
40  *\r
41  */\r
42 public class PhysicsEffectsGLSurfaceView extends GLSurfaceView {\r
43     public PhysicsEffectsGLSurfaceView(Context context) {\r
44         super(context);\r
45         peRenderer = new PhysicsEffectsRenderer();\r
46         setRenderer(peRenderer);\r
47     }\r
48     \r
49     float x1 = 0, x2, y1 = 0, y2;\r
50     long downTime;\r
51 \r
52         /**\r
53          * Function to process touch events for camera and scene control\r
54          *\r
55          */\r
56     public boolean onTouchEvent(final MotionEvent event) {                \r
57         switch(event.getAction()) {\r
58                 case(MotionEvent.ACTION_DOWN):\r
59                      x1 = event.getX();\r
60                      y1 = event.getY();\r
61                      downTime = event.getDownTime();\r
62                      break;\r
63                 case(MotionEvent.ACTION_UP):\r
64                         if(event.getEventTime() - downTime < 100) nativeSceneChange(); \r
65                         break;\r
66                 case(MotionEvent.ACTION_MOVE): {\r
67                         if(event.getPointerCount() == 1){\r
68                                 x2 = event.getX();\r
69                                 y2 = event.getY();\r
70                                 float dx = x2-x1;\r
71                                 float dy = y2-y1;\r
72 \r
73                                 // Use dx and dy to determine the direction\r
74                                 if(dx > 0) {\r
75                                         nativeCameraLeft();\r
76                                 } else {  \r
77                                         nativeCameraRight();\r
78                                 } \r
79                                 if(dy > 0){\r
80                                         nativeCameraUp();\r
81                                 } else {\r
82                                         nativeCameraDown();\r
83                                 }\r
84                                 x1=x2;\r
85                                 y1=y2;\r
86                         } else if(event.getPointerCount() == 2){\r
87                         y2 = event.getY();\r
88 \r
89                         float dy = y2-y1;\r
90 \r
91                         // Use dy to determine zoom\r
92                         if(dy > 0){\r
93                                 nativeCameraZoomIn();\r
94                         } else {\r
95                                 nativeCameraZoomOut();\r
96                         }\r
97                         y1=y2;\r
98                         }\r
99                 }\r
100         }\r
101         return true;\r
102     }\r
103 \r
104     PhysicsEffectsRenderer peRenderer;\r
105 \r
106     private static native void nativeCameraUp();\r
107     private static native void nativeCameraDown();\r
108     private static native void nativeCameraLeft();\r
109     private static native void nativeCameraRight();\r
110     private static native void nativeCameraZoomOut();\r
111     private static native void nativeCameraZoomIn();\r
112     private static native void nativeSceneChange();\r
113 }\r