AndroidInputHandler.java 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. * Copyright (c) 2009-2012 jMonkeyEngine
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are
  7. * met:
  8. *
  9. * * Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. *
  12. * * Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. *
  16. * * Neither the name of 'jMonkeyEngine' nor the names of its contributors
  17. * may be used to endorse or promote products derived from this software
  18. * without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  22. * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  23. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  24. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  25. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  26. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  27. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  28. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  29. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  30. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. */
  32. package com.jme3.input.android;
  33. import android.opengl.GLSurfaceView;
  34. import android.view.GestureDetector;
  35. import android.view.InputDevice;
  36. import android.view.KeyEvent;
  37. import android.view.MotionEvent;
  38. import android.view.ScaleGestureDetector;
  39. import android.view.View;
  40. import com.jme3.input.JoyInput;
  41. import com.jme3.input.TouchInput;
  42. import com.jme3.system.AppSettings;
  43. import java.util.logging.Level;
  44. import java.util.logging.Logger;
  45. /**
  46. * <code>AndroidInput</code> is the main class that connects the Android system
  47. * inputs to jME. It receives the inputs from the Android View and passes them
  48. * to the appropriate classes based on the source of the input.</br>
  49. * This class is to be extended when new functionality is released in Android.
  50. *
  51. * @author iwgeric
  52. */
  53. public class AndroidInputHandler implements View.OnTouchListener,
  54. View.OnKeyListener {
  55. private static final Logger logger = Logger.getLogger(AndroidInputHandler.class.getName());
  56. protected GLSurfaceView view;
  57. protected AndroidTouchInput touchInput;
  58. protected AndroidJoyInput joyInput;
  59. public AndroidInputHandler() {
  60. touchInput = new AndroidTouchInput(this);
  61. joyInput = new AndroidJoyInput(this);
  62. }
  63. public void setView(View view) {
  64. if (this.view != null && view != null && this.view.equals(view)) {
  65. return;
  66. }
  67. if (this.view != null) {
  68. removeListeners(this.view);
  69. }
  70. this.view = (GLSurfaceView)view;
  71. if (this.view != null) {
  72. addListeners(this.view);
  73. }
  74. joyInput.setView((GLSurfaceView)view);
  75. }
  76. public View getView() {
  77. return view;
  78. }
  79. protected void removeListeners(GLSurfaceView view) {
  80. view.setOnTouchListener(null);
  81. view.setOnKeyListener(null);
  82. touchInput.setGestureDetector(null);
  83. touchInput.setScaleDetector(null);
  84. }
  85. protected void addListeners(GLSurfaceView view) {
  86. view.setOnTouchListener(this);
  87. view.setOnKeyListener(this);
  88. AndroidGestureProcessor gestureHandler = new AndroidGestureProcessor(touchInput);
  89. touchInput.setGestureDetector(new GestureDetector(
  90. view.getContext(), gestureHandler));
  91. touchInput.setScaleDetector(new ScaleGestureDetector(
  92. view.getContext(), gestureHandler));
  93. }
  94. public void loadSettings(AppSettings settings) {
  95. touchInput.loadSettings(settings);
  96. }
  97. public TouchInput getTouchInput() {
  98. return touchInput;
  99. }
  100. public JoyInput getJoyInput() {
  101. return joyInput;
  102. }
  103. /*
  104. * Android input events include the source from which the input came from.
  105. * We must look at the source of the input event to determine which type
  106. * of jME input it belongs to.
  107. * If the input is from a gamepad or joystick source, the event is sent
  108. * to the JoyInput class to convert the event into jME joystick events.
  109. * </br>
  110. * If the input is from a touchscreen source, the event is sent to the
  111. * TouchProcessor to convert the event into touch events.
  112. * The TouchProcessor also converts the events into Mouse and Key events
  113. * if AppSettings is set to simulate Mouse or Keyboard events.
  114. *
  115. * Android reports the source as a bitmask as shown below.</br>
  116. *
  117. * InputDevice Sources
  118. * 0000 0000 0000 0000 0000 0000 0000 0000 - 32 bit bitmask
  119. *
  120. * 0000 0000 0000 0000 0000 0000 1111 1111 - SOURCE_CLASS_MASK (0x000000ff)
  121. * 0000 0000 0000 0000 0000 0000 0000 0000 - SOURCE_CLASS_NONE (0x00000000)
  122. * 0000 0000 0000 0000 0000 0000 0000 0001 - SOURCE_CLASS_BUTTON (0x00000001)
  123. * 0000 0000 0000 0000 0000 0000 0000 0010 - SOURCE_CLASS_POINTER (0x00000002)
  124. * 0000 0000 0000 0000 0000 0000 0000 0100 - SOURCE_CLASS_TRACKBALL (0x00000004)
  125. * 0000 0000 0000 0000 0000 0000 0000 1000 - SOURCE_CLASS_POSITION (0x00000008)
  126. * 0000 0000 0000 0000 0000 0000 0001 0000 - SOURCE_CLASS_JOYSTICK (0x00000010)
  127. *
  128. * 1111 1111 1111 1111 1111 1111 0000 0000 - Source_Any (0xffffff00)
  129. * 0000 0000 0000 0000 0000 0000 0000 0000 - SOURCE_UNKNOWN (0x00000000)
  130. * 0000 0000 0000 0000 0000 0001 0000 0001 - SOURCE_KEYBOARD (0x00000101)
  131. * 0000 0000 0000 0000 0000 0010 0000 0001 - SOURCE_DPAD (0x00000201)
  132. * 0000 0000 0000 0000 0000 0100 0000 0001 - SOURCE_GAMEPAD (0x00000401)
  133. * 0000 0000 0000 0000 0001 0000 0000 0010 - SOURCE_TOUCHSCREEN (0x00001002)
  134. * 0000 0000 0000 0000 0010 0000 0000 0010 - SOURCE_MOUSE (0x00002002)
  135. * 0000 0000 0000 0000 0100 0000 0000 0010 - SOURCE_STYLUS (0x00004002)
  136. * 0000 0000 0000 0001 0000 0000 0000 0100 - SOURCE_TRACKBALL (0x00010004)
  137. * 0000 0000 0001 0000 0000 0000 0000 1000 - SOURCE_TOUCHPAD (0x00100008)
  138. * 0000 0000 0010 0000 0000 0000 0000 0000 - SOURCE_TOUCH_NAVIGATION (0x00200000)
  139. * 0000 0001 0000 0000 0000 0000 0001 0000 - SOURCE_JOYSTICK (0x01000010)
  140. * 0000 0010 0000 0000 0000 0000 0000 0001 - SOURCE_HDMI (0x02000001)
  141. *
  142. * Example values reported by Android for Source
  143. * 4,098 = 0x00001002 =
  144. * 0000 0000 0000 0000 0001 0000 0000 0010 - SOURCE_CLASS_POINTER
  145. * SOURCE_TOUCHSCREEN
  146. * 1,281 = 0x00000501 =
  147. * 0000 0000 0000 0000 0000 0101 0000 0001 - SOURCE_CLASS_BUTTON
  148. * SOURCE_KEYBOARD
  149. * SOURCE_GAMEPAD
  150. * 16,777,232 = 0x01000010 =
  151. * 0000 0001 0000 0000 0000 0000 0001 0000 - SOURCE_CLASS_JOYSTICK
  152. * SOURCE_JOYSTICK
  153. *
  154. * 16,778,513 = 0x01000511 =
  155. * 0000 0001 0000 0000 0000 0101 0001 0001 - SOURCE_CLASS_BUTTON
  156. * SOURCE_CLASS_JOYSTICK
  157. * SOURCE_GAMEPAD
  158. * SOURCE_KEYBOARD
  159. * SOURCE_JOYSTICK
  160. *
  161. * 257 = 0x00000101 =
  162. * 0000 0000 0000 0000 0000 0001 0000 0001 - SOURCE_CLASS_BUTTON
  163. * SOURCE_KEYBOARD
  164. *
  165. *
  166. *
  167. */
  168. @Override
  169. public boolean onTouch(View view, MotionEvent event) {
  170. if (view != getView()) {
  171. return false;
  172. }
  173. boolean consumed = false;
  174. int source = event.getSource();
  175. // logger.log(Level.INFO, "onTouch source: {0}", source);
  176. boolean isTouch = ((source & InputDevice.SOURCE_TOUCHSCREEN) == InputDevice.SOURCE_TOUCHSCREEN);
  177. // logger.log(Level.INFO, "onTouch source: {0}, isTouch: {1}",
  178. // new Object[]{source, isTouch});
  179. if (isTouch && touchInput != null) {
  180. // send the event to the touch processor
  181. consumed = touchInput.onTouch(event);
  182. }
  183. return consumed;
  184. }
  185. @Override
  186. public boolean onKey(View view, int keyCode, KeyEvent event) {
  187. if (view != getView()) {
  188. return false;
  189. }
  190. boolean consumed = false;
  191. int source = event.getSource();
  192. // logger.log(Level.INFO, "onKey source: {0}", source);
  193. boolean isTouch =
  194. ((source & InputDevice.SOURCE_TOUCHSCREEN) == InputDevice.SOURCE_TOUCHSCREEN) ||
  195. ((source & InputDevice.SOURCE_KEYBOARD) == InputDevice.SOURCE_KEYBOARD);
  196. // logger.log(Level.INFO, "onKey source: {0}, isTouch: {1}",
  197. // new Object[]{source, isTouch});
  198. if (touchInput != null) {
  199. consumed = touchInput.onKey(event);
  200. }
  201. return consumed;
  202. }
  203. }