ZenGL.java 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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.EGLConfig;
  26. import javax.microedition.khronos.opengles.GL10;
  27. import android.app.Activity;
  28. import android.content.Context;
  29. import android.opengl.GLSurfaceView;
  30. import android.text.InputType;
  31. import android.view.*;
  32. import android.view.inputmethod.BaseInputConnection;
  33. import android.view.inputmethod.EditorInfo;
  34. import android.view.inputmethod.InputConnection;
  35. import android.view.inputmethod.InputMethodManager;
  36. public class ZenGL extends GLSurfaceView
  37. {
  38. private native void Main();
  39. private native void zglNativeInit( String AppDirectory, String HomeDirectory );
  40. private native void zglNativeDestroy();
  41. private native void zglNativeSurfaceCreated();
  42. private native void zglNativeSurfaceChanged( int width, int height );
  43. private native void zglNativeDrawFrame();
  44. private native void zglNativeActivate( boolean Activate );
  45. private native boolean zglNativeCloseQuery();
  46. private native void zglNativeTouch( int ID, float X, float Y, float Pressure );
  47. private native void zglNativeInputText( String Text );
  48. private native void zglNativeBackspace();
  49. private zglCRenderer Renderer;
  50. private String SourceDir;
  51. private String DataDir;
  52. private InputMethodManager InputManager;
  53. public ZenGL( Context context, String appName, String appSourceDir )
  54. {
  55. super( context );
  56. System.loadLibrary( "zenjpeg" );
  57. System.loadLibrary( "openal" );
  58. System.loadLibrary( "ogg" );
  59. System.loadLibrary( "vorbis" );
  60. System.loadLibrary( "theoradec" );
  61. System.loadLibrary( "chipmunk" );
  62. System.loadLibrary( appName );
  63. SourceDir = appSourceDir;
  64. DataDir = context.getFilesDir().getAbsolutePath();
  65. Renderer = new zglCRenderer();
  66. setRenderer( Renderer );
  67. InputManager = (InputMethodManager)context.getSystemService( Context.INPUT_METHOD_SERVICE );
  68. setFocusableInTouchMode( true );
  69. ((Activity)context).getWindow().setSoftInputMode( WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN );
  70. zglNativeInit( SourceDir, DataDir );
  71. Main();
  72. }
  73. public Boolean onCloseQuery()
  74. {
  75. return zglNativeCloseQuery();
  76. }
  77. @Override
  78. public void onPause()
  79. {
  80. if ( InputManager.isAcceptingText() )
  81. HideKeyboard();
  82. super.onPause();
  83. zglNativeActivate( false );
  84. }
  85. @Override
  86. public void onResume()
  87. {
  88. super.onResume();
  89. zglNativeActivate( true );
  90. }
  91. @Override
  92. public boolean onTouchEvent( MotionEvent event )
  93. {
  94. int action = event.getAction();
  95. int actionType = action & MotionEvent.ACTION_MASK;
  96. switch ( actionType )
  97. {
  98. case MotionEvent.ACTION_DOWN:
  99. {
  100. int count = event.getPointerCount();
  101. for ( int i = 0; i < count; i++ )
  102. {
  103. int pointerID = event.getPointerId( i );
  104. zglNativeTouch( pointerID, event.getX( i ), event.getY( i ), event.getPressure( i ) );
  105. }
  106. break;
  107. }
  108. case MotionEvent.ACTION_UP:
  109. {
  110. int count = event.getPointerCount();
  111. for ( int i = 0; i < count; i++ )
  112. {
  113. int pointerID = event.getPointerId( i );
  114. zglNativeTouch( pointerID, event.getX( i ), event.getY( i ), 0 );
  115. }
  116. break;
  117. }
  118. case MotionEvent.ACTION_MOVE:
  119. {
  120. int count = event.getPointerCount();
  121. for ( int i = 0; i < count; i++ )
  122. {
  123. int pointerID = event.getPointerId( i );
  124. zglNativeTouch( pointerID, event.getX( i ), event.getY( i ), event.getPressure( i ) );
  125. }
  126. break;
  127. }
  128. case MotionEvent.ACTION_POINTER_DOWN:
  129. {
  130. int pointerID = ( action & MotionEvent.ACTION_POINTER_ID_MASK ) >> MotionEvent.ACTION_POINTER_ID_SHIFT;
  131. int pointerIndex = event.getPointerId( pointerID );
  132. if ( pointerID >= 0 && pointerID < event.getPointerCount() )
  133. zglNativeTouch( pointerIndex, event.getX( pointerID ), event.getY( pointerID ), event.getPressure( pointerID ) );
  134. break;
  135. }
  136. case MotionEvent.ACTION_POINTER_UP:
  137. {
  138. int pointerID = ( action & MotionEvent.ACTION_POINTER_ID_MASK ) >> MotionEvent.ACTION_POINTER_ID_SHIFT;
  139. int pointerIndex = event.getPointerId( pointerID );
  140. if ( pointerID >= 0 && pointerID < event.getPointerCount() )
  141. zglNativeTouch( pointerIndex, event.getX( pointerID ), event.getY( pointerID ), 0 );
  142. break;
  143. }
  144. }
  145. return true;
  146. }
  147. public void Finish()
  148. {
  149. zglNativeDestroy();
  150. ((Activity)getContext()).finish();
  151. System.exit( 0 );
  152. }
  153. public void ShowKeyboard()
  154. {
  155. InputManager.toggleSoftInput( InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_NOT_ALWAYS );
  156. }
  157. public void HideKeyboard()
  158. {
  159. InputManager.hideSoftInputFromWindow( this.getWindowToken(), 0 );
  160. }
  161. @Override
  162. public InputConnection onCreateInputConnection( EditorInfo outAttrs )
  163. {
  164. outAttrs.actionLabel = "";
  165. outAttrs.hintText = "";
  166. outAttrs.initialCapsMode = 0;
  167. outAttrs.initialSelEnd = outAttrs.initialSelStart = -1;
  168. outAttrs.label = "";
  169. outAttrs.imeOptions = EditorInfo.IME_ACTION_DONE | EditorInfo.IME_FLAG_NO_EXTRACT_UI;
  170. outAttrs.inputType = InputType.TYPE_NULL;
  171. return new zglInputConnection( this, false );
  172. }
  173. @Override
  174. public boolean onCheckIsTextEditor()
  175. {
  176. return true;
  177. }
  178. @Override
  179. public boolean onKeyDown( int keyCode, KeyEvent event )
  180. {
  181. if ( keyCode == KeyEvent.KEYCODE_ENTER )
  182. HideKeyboard();
  183. else if ( keyCode == KeyEvent.KEYCODE_DEL )
  184. zglNativeBackspace();
  185. else if ( keyCode >= KeyEvent.KEYCODE_0 && keyCode <= KeyEvent.KEYCODE_9 )
  186. zglNativeInputText( ((Integer)(keyCode - 7)).toString() );
  187. return super.onKeyDown( keyCode, event );
  188. }
  189. public boolean onBackPressed()
  190. {
  191. return zglNativeCloseQuery();
  192. }
  193. class zglCRenderer implements Renderer
  194. {
  195. public void onSurfaceCreated( GL10 gl, EGLConfig config )
  196. {
  197. zglNativeSurfaceCreated();
  198. }
  199. public void onSurfaceChanged( GL10 gl, int width, int height )
  200. {
  201. zglNativeSurfaceChanged( width, height );
  202. }
  203. public void onDrawFrame( GL10 gl )
  204. {
  205. zglNativeDrawFrame();
  206. }
  207. }
  208. class zglInputConnection extends BaseInputConnection
  209. {
  210. public zglInputConnection( View targetView, boolean fullEditor )
  211. {
  212. super( targetView, fullEditor );
  213. }
  214. @Override
  215. public boolean commitText( CharSequence text, int newCursorPosition )
  216. {
  217. zglNativeInputText( (String)text );
  218. return true;
  219. }
  220. }
  221. }