From de0c29b586782f7b37a1181ca65d85e3e2d39022 Mon Sep 17 00:00:00 2001 From: saschawillems Date: Sun, 15 May 2016 11:13:14 +0200 Subject: [PATCH] Pass Android gamepad key press to virtual key function of example --- base/vulkanexamplebase.cpp | 38 ++++++++++++++++++++++++++++++++++++++ base/vulkanexamplebase.h | 28 +++++++++++++++++++--------- 2 files changed, 57 insertions(+), 9 deletions(-) diff --git a/base/vulkanexamplebase.cpp b/base/vulkanexamplebase.cpp index 1551be2a..251c428f 100644 --- a/base/vulkanexamplebase.cpp +++ b/base/vulkanexamplebase.cpp @@ -1062,6 +1062,44 @@ int32_t VulkanExampleBase::handleAppInput(struct android_app* app, AInputEvent* } return 1; } + + if (AInputEvent_getType(event) == AINPUT_EVENT_TYPE_KEY) + { + int32_t keyCode = AKeyEvent_getKeyCode((const AInputEvent*)event); + int32_t action = AKeyEvent_getAction((const AInputEvent*)event); + int32_t button = 0; + + if (action == AKEY_EVENT_ACTION_UP) + return 0; + + switch (keyCode) + { + case AKEYCODE_BUTTON_A: + vulkanExample->keyPressed(GAMEPAD_BUTTON_A); + break; + case AKEYCODE_BUTTON_B: + vulkanExample->keyPressed(GAMEPAD_BUTTON_B); + break; + case AKEYCODE_BUTTON_X: + vulkanExample->keyPressed(GAMEPAD_BUTTON_X); + break; + case AKEYCODE_BUTTON_Y: + vulkanExample->keyPressed(GAMEPAD_BUTTON_Y); + break; + case AKEYCODE_BUTTON_L1: + vulkanExample->keyPressed(GAMEPAD_BUTTON_L1); + break; + case AKEYCODE_BUTTON_R1: + vulkanExample->keyPressed(GAMEPAD_BUTTON_R1); + break; + case AKEYCODE_BUTTON_START: + vulkanExample->keyPressed(GAMEPAD_BUTTON_START); + break; + }; + + LOGD("Button %d pressed", keyCode); + } + return 0; } diff --git a/base/vulkanexamplebase.h b/base/vulkanexamplebase.h index 31035d2e..6386dce1 100644 --- a/base/vulkanexamplebase.h +++ b/base/vulkanexamplebase.h @@ -40,6 +40,14 @@ #include "vulkanTextureLoader.hpp" #include "vulkanMeshLoader.hpp" +#define GAMEPAD_BUTTON_A 0x1000 +#define GAMEPAD_BUTTON_B 0x1001 +#define GAMEPAD_BUTTON_X 0x1002 +#define GAMEPAD_BUTTON_Y 0x1003 +#define GAMEPAD_BUTTON_L1 0x1004 +#define GAMEPAD_BUTTON_R1 0x1005 +#define GAMEPAD_BUTTON_START 0x1006 + class VulkanExampleBase { private: @@ -158,15 +166,8 @@ public: VkImageView view; } depthStencil; - // OS specific -#if defined(_WIN32) - HWND window; - HINSTANCE windowInstance; -#elif defined(__ANDROID__) - android_app* androidApp; - // true if application has focused, false if moved to background - bool focused = false; - // Gamepad state (only one) + // Gamepad state (only one pad supported) + struct { struct @@ -177,6 +178,15 @@ public: float rz = 0.0f; } axes; } gamePadState; + + // OS specific +#if defined(_WIN32) + HWND window; + HINSTANCE windowInstance; +#elif defined(__ANDROID__) + android_app* androidApp; + // true if application has focused, false if moved to background + bool focused = false; #elif defined(__linux__) struct { bool left = false;