From 7151e4c06e5d782ef7da8e853f57ddbbb339ea60 Mon Sep 17 00:00:00 2001 From: Henrik Rydgard Date: Mon, 15 Jul 2013 17:37:16 +0200 Subject: [PATCH] Generalize the code mapping android key events into pad button bits, add support for keys needed to navigate UI. --- android/app-android.cpp | 61 ++++---------------------------------- base/BlackberryMain.cpp | 6 ++-- base/PCMain.cpp | 2 ++ input/input_state.cpp | 65 +++++++++++++++++++++++++++++++++++++++-- input/input_state.h | 32 +++++++++++++++----- ui/ui.cpp | 2 +- 6 files changed, 99 insertions(+), 69 deletions(-) diff --git a/android/app-android.cpp b/android/app-android.cpp index 48e55b2f2e..93863c0325 100644 --- a/android/app-android.cpp +++ b/android/app-android.cpp @@ -33,8 +33,6 @@ std::string frameCommandParam; const bool extraLog = true; -static uint32_t pad_buttons_async_set; -static uint32_t pad_buttons_async_clear; static float left_joystick_x_async; static float left_joystick_y_async; static float right_joystick_x_async; @@ -42,8 +40,6 @@ static float right_joystick_y_async; static float hat_joystick_x_async; static float hat_joystick_y_async; -static uint32_t pad_buttons_down; - int optimalFramesPerBuffer = 0; int optimalSampleRate = 0; @@ -142,9 +138,7 @@ extern "C" void Java_com_henrikrydgard_libnative_NativeApp_init renderer_inited = false; first_lost = true; - pad_buttons_down = 0; - pad_buttons_async_set = 0; - pad_buttons_async_clear = 0; + g_buttonTracker.Reset(); left_joystick_x_async = 0; left_joystick_y_async = 0; @@ -262,20 +256,13 @@ extern "C" void Java_com_henrikrydgard_libnative_NativeRenderer_displayRender(JN // TODO: Look into if these locks are a perf loss { lock_guard guard(input_state.lock); - pad_buttons_down |= pad_buttons_async_set; - pad_buttons_down &= ~pad_buttons_async_clear; - input_state.pad_buttons = pad_buttons_down; - - UpdateInputState(&input_state); - } - - { - lock_guard guard(input_state.lock); input_state.pad_lstick_x = left_joystick_x_async; input_state.pad_lstick_y = left_joystick_y_async; input_state.pad_rstick_x = right_joystick_x_async; input_state.pad_rstick_y = right_joystick_y_async; + + UpdateInputState(&input_state); } NativeUpdate(input_state); @@ -367,15 +354,6 @@ extern "C" void JNICALL Java_com_henrikrydgard_libnative_NativeApp_touch // ELOG("Touch Exit %i", pointerId); } -static inline void AsyncDown(int padbutton) { - pad_buttons_async_set |= padbutton; - pad_buttons_async_clear &= ~padbutton; -} - -static inline void AsyncUp(int padbutton) { - pad_buttons_async_set &= ~padbutton; - pad_buttons_async_clear |= padbutton; -} extern "C" void Java_com_henrikrydgard_libnative_NativeApp_keyDown(JNIEnv *, jclass, jint deviceId, jint key) { KeyInput keyInput; @@ -383,19 +361,7 @@ extern "C" void Java_com_henrikrydgard_libnative_NativeApp_keyDown(JNIEnv *, jcl keyInput.keyCode = key; keyInput.flags = KEY_DOWN; NativeKey(keyInput); - - // Pad mapping - switch (key) { - case KEYCODE_BACK: AsyncDown(PAD_BUTTON_BACK); break; // Back - case KEYCODE_MENU: AsyncDown(PAD_BUTTON_MENU); break; // Menu - case KEYCODE_BUTTON_CROSS_PS3: - AsyncDown(PAD_BUTTON_A); - break; - case KEYCODE_BUTTON_CIRCLE_PS3: - AsyncDown(PAD_BUTTON_B); - break; - default: break; - } + g_buttonTracker.Process(keyInput); } extern "C" void Java_com_henrikrydgard_libnative_NativeApp_keyUp(JNIEnv *, jclass, jint deviceId, jint key) { @@ -404,24 +370,7 @@ extern "C" void Java_com_henrikrydgard_libnative_NativeApp_keyUp(JNIEnv *, jclas keyInput.keyCode = key; keyInput.flags = KEY_UP; NativeKey(keyInput); - - // Pad mapping - switch (key) { - case KEYCODE_BACK: - AsyncUp(PAD_BUTTON_BACK); - break; // Back - case KEYCODE_MENU: - AsyncUp(PAD_BUTTON_MENU); - break; // Menu - case KEYCODE_BUTTON_CROSS_PS3: - AsyncUp(PAD_BUTTON_A); - break; - case KEYCODE_BUTTON_CIRCLE_PS3: - AsyncUp(PAD_BUTTON_B); - break; - default: - break; - } + g_buttonTracker.Process(keyInput); } extern "C" void Java_com_henrikrydgard_libnative_NativeApp_beginJoystickEvent( diff --git a/base/BlackberryMain.cpp b/base/BlackberryMain.cpp index 4e1e41e8d3..c4633abf98 100644 --- a/base/BlackberryMain.cpp +++ b/base/BlackberryMain.cpp @@ -51,7 +51,7 @@ void LaunchEmail(const char *email_address) InputState input_state; -// Input +// Input - urgh, these may collide with the android keycodes when we bring in input/keycodes.h const unsigned int buttonMappings[18] = { KEYCODE_K, //Cross KEYCODE_L, //Circle @@ -131,7 +131,7 @@ void BlackberryMain::handleInput(screen_event_t screen_event) if (value == buttonMappings[b] & 0xFF) { if (flags & KEY_DOWN) pad_buttons |= (1<second; key.deviceId = DEVICE_ID_KEYBOARD; NativeKey(key); + g_buttonTracker.Process(key); break; } case SDL_KEYUP: @@ -625,6 +626,7 @@ int main(int argc, char *argv[]) { key.keyCode = KeyMapRawSDLtoNative.find(k)->second; key.deviceId = DEVICE_ID_KEYBOARD; NativeKey(key); + g_buttonTracker.Process(key); break; } case SDL_MOUSEBUTTONDOWN: diff --git a/input/input_state.cpp b/input/input_state.cpp index 51a262c88d..9f1e8798db 100644 --- a/input/input_state.cpp +++ b/input/input_state.cpp @@ -1,5 +1,5 @@ -#include "input_state.h" -#include +#include "input/input_state.h" +#include "input/keycodes.h" const char *GetDeviceName(int deviceId) { switch (deviceId) { @@ -12,3 +12,64 @@ const char *GetDeviceName(int deviceId) { return "unknown"; } } + +// Default pad mapping to button bits. Used for UI and stuff that doesn't use PPSSPP's key mapping system. +int MapPadButtonFixed(int keycode) { + switch (keycode) { + case KEYCODE_BACK: return PAD_BUTTON_BACK; // Back + case KEYCODE_MENU: return PAD_BUTTON_MENU; // Menu + + case KEYCODE_Z: + case KEYCODE_SPACE: + case KEYCODE_BUTTON_1: + case KEYCODE_BUTTON_A: // same as KEYCODE_OUYA_BUTTON_O and KEYCODE_BUTTON_CROSS_PS3 + return PAD_BUTTON_A; + + case KEYCODE_ESCAPE: + case KEYCODE_BUTTON_2: + case KEYCODE_BUTTON_B: // same as KEYCODE_OUYA_BUTTON_A and KEYCODE_BUTTON_CIRCLE_PS3: + return PAD_BUTTON_B; + + case KEYCODE_DPAD_LEFT: return PAD_BUTTON_LEFT; + case KEYCODE_DPAD_RIGHT: return PAD_BUTTON_RIGHT; + case KEYCODE_DPAD_UP: return PAD_BUTTON_UP; + case KEYCODE_DPAD_DOWN: return PAD_BUTTON_DOWN; + + default: + return 0; + } +} + +void ButtonTracker::Update(InputState &input_state) { + pad_buttons_ |= pad_buttons_async_set; + pad_buttons_ &= ~pad_buttons_async_clear; + input_state.pad_buttons = pad_buttons_; +} + +void ButtonTracker::Process(const KeyInput &input) { + int btn = MapPadButtonFixed(input.keyCode); + if (btn == 0) + return; + + // For now, use a fixed mapping. Good enough for the basics on most platforms. + if (input.flags & KEY_DOWN) { + pad_buttons_async_set |= btn; + pad_buttons_async_clear &= ~btn; + } + if (input.flags & KEY_UP) { + pad_buttons_async_set &= ~btn; + pad_buttons_async_clear |= btn; + } +} + +ButtonTracker g_buttonTracker; + +void UpdateInputState(InputState *input) { + g_buttonTracker.Update(*input); + input->pad_buttons_down = (input->pad_last_buttons ^ input->pad_buttons) & input->pad_buttons; + input->pad_buttons_up = (input->pad_last_buttons ^ input->pad_buttons) & input->pad_last_buttons; +} + +void EndInputState(InputState *input) { + input->pad_last_buttons = input->pad_buttons; +} \ No newline at end of file diff --git a/input/input_state.h b/input/input_state.h index 1b2ebc84cb..735d81e0f5 100644 --- a/input/input_state.h +++ b/input/input_state.h @@ -109,14 +109,8 @@ private: DISALLOW_COPY_AND_ASSIGN(InputState); }; -inline void UpdateInputState(InputState *input) { - input->pad_buttons_down = (input->pad_last_buttons ^ input->pad_buttons) & input->pad_buttons; - input->pad_buttons_up = (input->pad_last_buttons ^ input->pad_buttons) & input->pad_last_buttons; -} - -inline void EndInputState(InputState *input) { - input->pad_last_buttons = input->pad_buttons; -} +void UpdateInputState(InputState *input); +void EndInputState(InputState *input); enum { TOUCH_MOVE = 1, @@ -160,3 +154,25 @@ struct AxisInput { float value; int flags; }; + + +class ButtonTracker { +public: + ButtonTracker() { Reset(); } + void Reset() { + pad_buttons_ = 0; + pad_buttons_async_set = 0; + pad_buttons_async_clear = 0; + } + void Process(const KeyInput &input); + void Update(InputState &input_state); + uint32_t GetPadButtons() const { return pad_buttons_; } + +private: + uint32_t pad_buttons_; + uint32_t pad_buttons_async_set; + uint32_t pad_buttons_async_clear; +}; + +// Platforms should call g_buttonTracker.Process(). +extern ButtonTracker g_buttonTracker; \ No newline at end of file diff --git a/ui/ui.cpp b/ui/ui.cpp index 9fd6769a06..80a1f5166c 100644 --- a/ui/ui.cpp +++ b/ui/ui.cpp @@ -294,7 +294,7 @@ int UITextureButton(UIContext *ctx, int id, const LayoutManager &layout, float w y += txOffset * 2; } ui_draw2d.DrawImage4Grid(drop_shadow, x - dropsize, y, x+w + dropsize, y+h+dropsize*1.5, - alphaMul(color,0.5f), 1.0f); + blackAlpha(0.5f), 1.0f); ui_draw2d.Flush(true); }