ZenGL.java 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /*
  2. * Copyright (c) 2012 Andrey Kemka
  3. *
  4. * This software is provided 'as-is', without any express or
  5. * implied warranty. In no event will the authors be held
  6. * liable for any damages arising from the use of this software.
  7. *
  8. * Permission is granted to anyone to use this software for any purpose,
  9. * including commercial applications, and to alter it and redistribute
  10. * it freely, subject to the following restrictions:
  11. *
  12. * 1. The origin of this software must not be misrepresented;
  13. * you must not claim that you wrote the original software.
  14. * If you use this software in a product, an acknowledgment
  15. * in the product documentation would be appreciated but
  16. * is not required.
  17. *
  18. * 2. Altered source versions must be plainly marked as such,
  19. * and must not be misrepresented as being the original software.
  20. *
  21. * 3. This notice may not be removed or altered from any
  22. * source distribution.
  23. */
  24. package zengl.android;
  25. import javax.microedition.khronos.egl.EGL10;
  26. import javax.microedition.khronos.egl.EGLContext;
  27. import javax.microedition.khronos.egl.EGLDisplay;
  28. import javax.microedition.khronos.egl.EGLSurface;
  29. import javax.microedition.khronos.egl.EGLConfig;
  30. import javax.microedition.khronos.opengles.GL10;
  31. import android.app.Activity;
  32. import android.content.Context;
  33. import android.opengl.GLSurfaceView;
  34. import android.text.InputType;
  35. import android.view.*;
  36. import android.view.inputmethod.BaseInputConnection;
  37. import android.view.inputmethod.EditorInfo;
  38. import android.view.inputmethod.InputConnection;
  39. import android.view.inputmethod.InputMethodManager;
  40. public class ZenGL extends GLSurfaceView
  41. {
  42. private native void Main();
  43. private native void zglNativeInit( String AppDirectory, String HomeDirectory );
  44. private native void zglNativeDestroy();
  45. private native void zglNativeSurfaceCreated();
  46. private native void zglNativeSurfaceChanged( int width, int height );
  47. private native void zglNativeDrawFrame();
  48. private native void zglNativeActivate( boolean Activate );
  49. private native boolean zglNativeCloseQuery();
  50. private native void zglNativeTouch( int ID, float X, float Y, float Pressure );
  51. private native void zglNativeInputText( String Text );
  52. private native void zglNativeBackspace();
  53. private zglCRenderer Renderer;
  54. private String SourceDir;
  55. private String DataDir;
  56. private InputMethodManager InputManager;
  57. public ZenGL( Context context, String appName, String appSourceDir )
  58. {
  59. super( context );
  60. System.loadLibrary( "zenjpeg" );
  61. System.loadLibrary( "openal" );
  62. System.loadLibrary( "ogg" );
  63. System.loadLibrary( "vorbis" );
  64. System.loadLibrary( "theoradec" );
  65. System.loadLibrary( "chipmunk" );
  66. System.loadLibrary( "GLU" );
  67. System.loadLibrary( appName );
  68. SourceDir = appSourceDir;
  69. DataDir = context.getFilesDir().getAbsolutePath();
  70. Renderer = new zglCRenderer();
  71. setRenderer( Renderer );
  72. InputManager = (InputMethodManager)context.getSystemService( Context.INPUT_METHOD_SERVICE );
  73. setFocusableInTouchMode( true );
  74. ((Activity)context).getWindow().setSoftInputMode( WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN );
  75. zglNativeInit( SourceDir, DataDir );
  76. Main();
  77. }
  78. public Boolean onCloseQuery()
  79. {
  80. return zglNativeCloseQuery();
  81. }
  82. @Override
  83. public void onPause()
  84. {
  85. if ( InputManager.isAcceptingText() )
  86. HideKeyboard();
  87. super.onPause();
  88. zglNativeActivate( false );
  89. }
  90. @Override
  91. public void onResume()
  92. {
  93. super.onResume();
  94. zglNativeActivate( true );
  95. }
  96. @Override
  97. public boolean onTouchEvent( MotionEvent event )
  98. {
  99. int action = event.getAction();
  100. int actionType = action & MotionEvent.ACTION_MASK;
  101. switch ( actionType )
  102. {
  103. case MotionEvent.ACTION_DOWN:
  104. {
  105. int count = event.getPointerCount();
  106. for ( int i = 0; i < count; i++ )
  107. {
  108. int pointerID = event.getPointerId( i );
  109. zglNativeTouch( pointerID, event.getX( i ), event.getY( i ), event.getPressure( i ) );
  110. }
  111. break;
  112. }
  113. case MotionEvent.ACTION_UP:
  114. {
  115. int count = event.getPointerCount();
  116. for ( int i = 0; i < count; i++ )
  117. {
  118. int pointerID = event.getPointerId( i );
  119. zglNativeTouch( pointerID, event.getX( i ), event.getY( i ), 0 );
  120. }
  121. break;
  122. }
  123. case MotionEvent.ACTION_MOVE:
  124. {
  125. int count = event.getPointerCount();
  126. for ( int i = 0; i < count; i++ )
  127. {
  128. int pointerID = event.getPointerId( i );
  129. zglNativeTouch( pointerID, event.getX( i ), event.getY( i ), event.getPressure( i ) );
  130. }
  131. break;
  132. }
  133. case MotionEvent.ACTION_POINTER_DOWN:
  134. {
  135. int pointerID = ( action & MotionEvent.ACTION_POINTER_ID_MASK ) >> MotionEvent.ACTION_POINTER_ID_SHIFT;
  136. int pointerIndex = event.getPointerId( pointerID );
  137. if ( pointerID >= 0 && pointerID < event.getPointerCount() )
  138. zglNativeTouch( pointerIndex, event.getX( pointerID ), event.getY( pointerID ), event.getPressure( pointerID ) );
  139. break;
  140. }
  141. case MotionEvent.ACTION_POINTER_UP:
  142. {
  143. int pointerID = ( action & MotionEvent.ACTION_POINTER_ID_MASK ) >> MotionEvent.ACTION_POINTER_ID_SHIFT;
  144. int pointerIndex = event.getPointerId( pointerID );
  145. if ( pointerID >= 0 && pointerID < event.getPointerCount() )
  146. zglNativeTouch( pointerIndex, event.getX( pointerID ), event.getY( pointerID ), 0 );
  147. break;
  148. }
  149. }
  150. return true;
  151. }
  152. public void Finish()
  153. {
  154. zglNativeDestroy();
  155. ((Activity)getContext()).finish();
  156. System.exit( 0 );
  157. }
  158. public void SwapBuffers()
  159. {
  160. try {
  161. EGL10 currEGL = (EGL10)EGLContext.getEGL();
  162. EGLDisplay currDisplay = currEGL.eglGetCurrentDisplay();
  163. if ( currDisplay == EGL10.EGL_NO_DISPLAY ) return;
  164. EGLSurface currSurface = currEGL.eglGetCurrentSurface( EGL10.EGL_DRAW );
  165. if ( currSurface == EGL10.EGL_NO_SURFACE ) return;
  166. currEGL.eglSwapBuffers( currDisplay, currSurface);
  167. } catch ( Exception e ) { }
  168. }
  169. public void ShowKeyboard()
  170. {
  171. InputManager.toggleSoftInput( InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_NOT_ALWAYS );
  172. }
  173. public void HideKeyboard()
  174. {
  175. InputManager.hideSoftInputFromWindow( this.getWindowToken(), 0 );
  176. }
  177. @Override
  178. public InputConnection onCreateInputConnection( EditorInfo outAttrs )
  179. {
  180. outAttrs.actionLabel = "";
  181. outAttrs.hintText = "";
  182. outAttrs.initialCapsMode = 0;
  183. outAttrs.initialSelEnd = outAttrs.initialSelStart = -1;
  184. outAttrs.label = "";
  185. outAttrs.imeOptions = EditorInfo.IME_ACTION_DONE | EditorInfo.IME_FLAG_NO_EXTRACT_UI;
  186. outAttrs.inputType = InputType.TYPE_NULL;
  187. return new zglInputConnection( this, false );
  188. }
  189. @Override
  190. public boolean onCheckIsTextEditor()
  191. {
  192. return true;
  193. }
  194. @Override
  195. public boolean onKeyDown( int keyCode, KeyEvent event )
  196. {
  197. if ( keyCode == KeyEvent.KEYCODE_ENTER )
  198. HideKeyboard();
  199. else if ( keyCode == KeyEvent.KEYCODE_DEL )
  200. zglNativeBackspace();
  201. else if ( keyCode >= KeyEvent.KEYCODE_0 && keyCode <= KeyEvent.KEYCODE_9 )
  202. zglNativeInputText( ((Integer)(keyCode - 7)).toString() );
  203. return super.onKeyDown( keyCode, event );
  204. }
  205. public boolean onBackPressed()
  206. {
  207. return zglNativeCloseQuery();
  208. }
  209. class zglCRenderer implements Renderer
  210. {
  211. public void onSurfaceCreated( GL10 gl, EGLConfig config )
  212. {
  213. zglNativeSurfaceCreated();
  214. }
  215. public void onSurfaceChanged( GL10 gl, int width, int height )
  216. {
  217. zglNativeSurfaceChanged( width, height );
  218. }
  219. public void onDrawFrame( GL10 gl )
  220. {
  221. zglNativeDrawFrame();
  222. }
  223. }
  224. class zglInputConnection extends BaseInputConnection
  225. {
  226. public zglInputConnection( View targetView, boolean fullEditor )
  227. {
  228. super( targetView, fullEditor );
  229. }
  230. @Override
  231. public boolean commitText( CharSequence text, int newCursorPosition )
  232. {
  233. zglNativeInputText( (String)text );
  234. return true;
  235. }
  236. }
  237. }