From b481719277fd5d055b33ea4b4ac22b1b6aa2e1ab Mon Sep 17 00:00:00 2001 From: XProger Date: Sat, 25 Mar 2017 03:28:47 +0300 Subject: [PATCH] #15 android keyboard support; sensor landscape screen orientation --- src/platform/android/AndroidManifest.xml | 2 +- src/platform/android/jni/main.cpp | 22 ++++- .../org/xproger/openlara/MainActivity.java | 89 ++++++++----------- 3 files changed, 58 insertions(+), 55 deletions(-) diff --git a/src/platform/android/AndroidManifest.xml b/src/platform/android/AndroidManifest.xml index 0b50cb7..55df8d6 100644 --- a/src/platform/android/AndroidManifest.xml +++ b/src/platform/android/AndroidManifest.xml @@ -19,7 +19,7 @@ + android:screenOrientation="sensorLandscape"> diff --git a/src/platform/android/jni/main.cpp b/src/platform/android/jni/main.cpp index 9f6031b..d5f8528 100644 --- a/src/platform/android/jni/main.cpp +++ b/src/platform/android/jni/main.cpp @@ -106,14 +106,32 @@ int getPOV(int x, int y) { return 0; } +InputKey keyToInputKey(int code) { + int codes[] = { + 21, 22, 19, 20, 62, 66, 111, 59, 113, 57, + 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, + 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, + 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, + }; + + for (int i = 0; i < sizeof(codes) / sizeof(codes[0]); i++) + if (codes[i] == code) + return (InputKey)(ikLeft + i); + return ikNone; +} + JNI_METHOD(void, nativeTouch)(JNIEnv* env, jobject obj, jint id, jint state, jfloat x, jfloat y) { -// gamepad +// gamepad / keyboard if (state < 0) { switch (state) { case -3 : Input::setPos(ikJoyL, vec2(DeadZone(x), DeadZone(y))); break; case -4 : Input::setPos(ikJoyR, vec2(DeadZone(x), DeadZone(y))); break; case -5 : Input::setPos(ikJoyPOV, vec2(float(getPOV(sign(x), sign(y))), 0.0f)); break; - default : Input::setDown(InputKey(ikJoyA + (int)x), state != -1); + default : { + int btn = int(x); + InputKey key = btn <= 0 ? InputKey(ikJoyA - btn) : keyToInputKey(btn); + Input::setDown(key, state != -1); + } } return; } diff --git a/src/platform/android/src/org/xproger/openlara/MainActivity.java b/src/platform/android/src/org/xproger/openlara/MainActivity.java index c1b4e2e..08a6f31 100644 --- a/src/platform/android/src/org/xproger/openlara/MainActivity.java +++ b/src/platform/android/src/org/xproger/openlara/MainActivity.java @@ -31,7 +31,6 @@ public class MainActivity extends Activity implements OnTouchListener, OnGeneric private GLSurfaceView view; //private GvrLayout gvrLayout; private Wrapper wrapper; - private SparseIntArray joys = new SparseIntArray(); @Override protected void onCreate(Bundle savedInstanceState) { @@ -121,69 +120,55 @@ public class MainActivity extends Activity implements OnTouchListener, OnGeneric return true; } - private int getJoyIndex(InputDevice dev) { - int src = dev.getSources(); - if ((src & InputDevice.SOURCE_GAMEPAD) == InputDevice.SOURCE_GAMEPAD || - (src & InputDevice.SOURCE_JOYSTICK) == InputDevice.SOURCE_JOYSTICK) { - - int id = dev.getId(); - int index = joys.get(id, -1); - - if (index == -1) { - index = joys.size(); - joys.append(id, index); - } - return index; - } - return -1; - } - @Override public boolean onGenericMotion(View v, MotionEvent event) { - int index = getJoyIndex(event.getDevice()); - if (index == -1) return false; - - wrapper.onTouch(index, -3, event.getAxisValue(MotionEvent.AXIS_X), - event.getAxisValue(MotionEvent.AXIS_Y)); - - wrapper.onTouch(index, -4, event.getAxisValue(MotionEvent.AXIS_Z), - event.getAxisValue(MotionEvent.AXIS_RZ)); + int src = event.getDevice().getSources(); - wrapper.onTouch(index, -5, event.getAxisValue(MotionEvent.AXIS_HAT_X), - event.getAxisValue(MotionEvent.AXIS_HAT_Y)); + boolean isMouse = (src & (InputDevice.SOURCE_MOUSE)) != 0; + boolean isJoy = (src & (InputDevice.SOURCE_GAMEPAD | InputDevice.SOURCE_JOYSTICK)) != 0; + + if (isMouse) { + return true; + } + + if (isJoy) { + wrapper.onTouch(0, -3, event.getAxisValue(MotionEvent.AXIS_X), + event.getAxisValue(MotionEvent.AXIS_Y)); + + wrapper.onTouch(0, -4, event.getAxisValue(MotionEvent.AXIS_Z), + event.getAxisValue(MotionEvent.AXIS_RZ)); + + wrapper.onTouch(0, -5, event.getAxisValue(MotionEvent.AXIS_HAT_X), + event.getAxisValue(MotionEvent.AXIS_HAT_Y)); + } return true; } @Override public boolean onKey(View v, int keyCode, KeyEvent event) { - int index = getJoyIndex(event.getDevice()); - if (index == -1) - return false; - int btn; - + switch (keyCode) { - case KeyEvent.KEYCODE_BUTTON_A : btn = 0; break; - case KeyEvent.KEYCODE_BUTTON_B : btn = 1; break; - case KeyEvent.KEYCODE_BUTTON_X : btn = 2; break; - case KeyEvent.KEYCODE_BUTTON_Y : btn = 3; break; - case KeyEvent.KEYCODE_BUTTON_L1 : btn = 4; break; - case KeyEvent.KEYCODE_BUTTON_R1 : btn = 5; break; - case KeyEvent.KEYCODE_BUTTON_SELECT : btn = 6; break; - case KeyEvent.KEYCODE_BUTTON_START : btn = 7; break; - case KeyEvent.KEYCODE_BUTTON_THUMBL : btn = 8; break; - case KeyEvent.KEYCODE_BUTTON_THUMBR : btn = 9; break; - case KeyEvent.KEYCODE_BUTTON_L2 : btn = 10; break; - case KeyEvent.KEYCODE_BUTTON_R2 : btn = 11; break; - default : btn = -1; + case KeyEvent.KEYCODE_BUTTON_A : btn = -0; break; + case KeyEvent.KEYCODE_BUTTON_B : btn = -1; break; + case KeyEvent.KEYCODE_BUTTON_X : btn = -2; break; + case KeyEvent.KEYCODE_BUTTON_Y : btn = -3; break; + case KeyEvent.KEYCODE_BUTTON_L1 : btn = -4; break; + case KeyEvent.KEYCODE_BUTTON_R1 : btn = -5; break; + case KeyEvent.KEYCODE_BUTTON_SELECT : btn = -6; break; + case KeyEvent.KEYCODE_BUTTON_START : btn = -7; break; + case KeyEvent.KEYCODE_BUTTON_THUMBL : btn = -8; break; + case KeyEvent.KEYCODE_BUTTON_THUMBR : btn = -9; break; + case KeyEvent.KEYCODE_BUTTON_L2 : btn = -10; break; + case KeyEvent.KEYCODE_BUTTON_R2 : btn = -11; break; + case KeyEvent.KEYCODE_BACK : btn = KeyEvent.KEYCODE_ESCAPE; + default : btn = keyCode; } - - if (btn != -1) { - wrapper.onTouch(index, event.getAction() == KeyEvent.ACTION_DOWN ? -2 : -1, btn, 0); - return true; - } - return false; + + boolean isDown = event.getAction() == KeyEvent.ACTION_DOWN; + wrapper.onTouch(0, isDown ? -2 : -1, btn, 0); + return true; } static {