From ea500923565f66326e0ace470184374f158781d8 Mon Sep 17 00:00:00 2001 From: victorfisac Date: Mon, 4 Jan 2016 21:00:20 +0100 Subject: [PATCH 1/3] Add Android physic buttons input detection - Added functions for detect when pressed, during down and when released. - Added defines for back, menu, volume up and down button numbers. --- src/core.c | 49 ++++++++++++++++++++++++++++++++++++++++++++++--- src/raylib.h | 9 +++++++++ 2 files changed, 55 insertions(+), 3 deletions(-) diff --git a/src/core.c b/src/core.c index 06260281d..6a4702405 100644 --- a/src/core.c +++ b/src/core.c @@ -122,6 +122,8 @@ static int ident, events; static bool windowReady = false; // Used to detect display initialization static bool appEnabled = true; // Used to detec if app is active static bool contextRebindRequired = false; // Used to know context rebind required +static int previousButtonState[512] = { 1 }; // Required to check if button pressed/released once +static int currentButtonState[512] = { 1 }; // Required to check if button pressed/released once #elif defined(PLATFORM_RPI) static EGL_DISPMANX_WINDOW_T nativeWindow; // Native window (graphic device) @@ -365,6 +367,13 @@ void InitWindow(int width, int height, struct android_app *state) TraceLog(INFO, "Android app initialized successfully"); + // Init button states values (default up) + for(int i = 0; i < 512; i++) + { + currentButtonState[i] = 1; + previousButtonState[i] = 1; + } + // Wait for window to be initialized (display and context) while (!windowReady) { @@ -1101,6 +1110,34 @@ Vector2 GetTouchPosition(void) return position; } + +bool IsButtonPressed(int button) +{ + bool pressed = false; + + if ((currentButtonState[button] != previousButtonState[button]) && (currentButtonState[button] == 0)) pressed = true; + else pressed = false; + + return pressed; +} + +// Detect if a button is being pressed (button held down) +bool IsButtonDown(int button) +{ + if (currentButtonState[button] == 0) return true; + else return false; +} + +// Detect if a button has been released once +bool IsButtonReleased(int button) +{ + bool released = false; + + if ((currentButtonState[button] != previousButtonState[button]) && (currentButtonState[button] == 1)) released = true; + else released = false; + + return released; +} #endif //---------------------------------------------------------------------------------- @@ -1648,12 +1685,14 @@ static int32_t AndroidInputCallback(struct android_app *app, AInputEvent *event) { int32_t keycode = AKeyEvent_getKeyCode(event); //int32_t AKeyEvent_getMetaState(event); + + currentButtonState[keycode] = AKeyEvent_getAction (event); // Down = 0, Up = 1 //if (keycode == AKEYCODE_HOME) { } - //if (keycode == AKEYCODE_POWER) { } - if (keycode == AKEYCODE_BACK) + if (keycode == AKEYCODE_POWER) { return 1; } + if ((keycode == AKEYCODE_BACK) || (keycode == AKEYCODE_MENU)) { - // Eat BACK_BUTTON, just do nothing... and don't let to be handled by OS! + // Eat BACK_BUTTON and AKEYCODE_MENU, just do nothing... and don't let to be handled by OS! return 1; } else if ((keycode == AKEYCODE_VOLUME_UP) || (keycode == AKEYCODE_VOLUME_DOWN)) @@ -1778,6 +1817,7 @@ static void PollInputEvents(void) // TODO: Remove this requirement... UpdateGestures(); + #endif #if defined(PLATFORM_DESKTOP) || defined(PLATFORM_WEB) @@ -1806,6 +1846,9 @@ static void PollInputEvents(void) glfwPollEvents(); // Register keyboard/mouse events... and window events! #elif defined(PLATFORM_ANDROID) + // Register previous keys states + for (int i = 0; i < 512; i++) previousButtonState[i] = currentButtonState[i]; + // Poll Events (registered events) // NOTE: Activity is paused if not enabled (appEnabled) while ((ident = ALooper_pollAll(appEnabled ? 0 : -1, NULL, &events,(void**)&source)) >= 0) diff --git a/src/raylib.h b/src/raylib.h index f12035370..641eac4bf 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -186,6 +186,12 @@ // TODO: Review Xbox360 USB Controller Buttons +// Android Physic Buttons +#define ANDROID_BACK 4 +#define ANDROID_MENU 82 +#define ANDROID_VOLUME_UP 24 +#define ANDROID_VOLUME_DOWN 25 + // Some Basic Colors // NOTE: Custom raylib color palette for amazing visuals on WHITE background #define LIGHTGRAY (Color){ 200, 200, 200, 255 } // Light Gray @@ -581,6 +587,9 @@ bool IsGamepadButtonUp(int gamepad, int button); // Detect if a gamepad b int GetTouchX(void); // Returns touch position X (relative to screen size) int GetTouchY(void); // Returns touch position Y (relative to screen size) Vector2 GetTouchPosition(void); // Returns touch position XY (relative to screen size) +bool IsButtonPressed(int button); // Detect if an android physic button has been pressed +bool IsButtonDown(int button); // Detect if an android physic button is being pressed +bool IsButtonReleased(int button); // Detect if an android physic button has been released //------------------------------------------------------------------------------------ // Gestures and Touch Handling Functions (Module: gestures) From 32508f6db1d3f3c070e80ea42e0a1244a4c88cab Mon Sep 17 00:00:00 2001 From: victorfisac Date: Tue, 5 Jan 2016 12:21:40 +0100 Subject: [PATCH 2/3] Fixed Android lock screen bug - Fixed Android locking screen bug. OnDestroy() was called when locking screen. - Power button input must be handled by OS. - AndroidManifest.xml is affected by configChanges="screenSize". - Updated library header for android template. --- src/core.c | 14 ++++++-- templates/android_project/AndroidManifest.xml | 2 +- .../android_project/jni/include/raylib.h | 36 +++++++++++++------ 3 files changed, 37 insertions(+), 15 deletions(-) diff --git a/src/core.c b/src/core.c index 96ce71768..40691403f 100644 --- a/src/core.c +++ b/src/core.c @@ -1686,11 +1686,19 @@ static int32_t AndroidInputCallback(struct android_app *app, AInputEvent *event) int32_t keycode = AKeyEvent_getKeyCode(event); //int32_t AKeyEvent_getMetaState(event); + // Save current button and its state currentButtonState[keycode] = AKeyEvent_getAction (event); // Down = 0, Up = 1 - //if (keycode == AKEYCODE_HOME) { } - if (keycode == AKEYCODE_POWER) { return 1; } - if ((keycode == AKEYCODE_BACK) || (keycode == AKEYCODE_MENU)) + if (keycode == AKEYCODE_POWER) + { + // Let the OS handle input to avoid app stuck. Behaviour: CMD_PAUSE -> CMD_SAVE_STATE -> CMD_STOP -> CMD_CONFIG_CHANGED -> CMD_LOST_FOCUS + // Resuming Behaviour: CMD_START -> CMD_RESUME -> CMD_CONFIG_CHANGED -> CMD_CONFIG_CHANGED -> CMD_GAINED_FOCUS + // It seems like locking mobile, screen size (CMD_CONFIG_CHANGED) is affected. + // NOTE: AndroidManifest.xml must have + // Before that change, activity was calling CMD_TERM_WINDOW and CMD_DESTROY when locking mobile, so that was not a normal behaviour + return 0; + } + else if ((keycode == AKEYCODE_BACK) || (keycode == AKEYCODE_MENU)) { // Eat BACK_BUTTON and AKEYCODE_MENU, just do nothing... and don't let to be handled by OS! return 1; diff --git a/templates/android_project/AndroidManifest.xml b/templates/android_project/AndroidManifest.xml index 8bb0534fb..f9f41b335 100644 --- a/templates/android_project/AndroidManifest.xml +++ b/templates/android_project/AndroidManifest.xml @@ -23,7 +23,7 @@ android:theme="@android:style/Theme.Holo.NoActionBar.Fullscreen" > diff --git a/templates/android_project/jni/include/raylib.h b/templates/android_project/jni/include/raylib.h index 864a240ad..641eac4bf 100644 --- a/templates/android_project/jni/include/raylib.h +++ b/templates/android_project/jni/include/raylib.h @@ -186,6 +186,12 @@ // TODO: Review Xbox360 USB Controller Buttons +// Android Physic Buttons +#define ANDROID_BACK 4 +#define ANDROID_MENU 82 +#define ANDROID_VOLUME_UP 24 +#define ANDROID_VOLUME_DOWN 25 + // Some Basic Colors // NOTE: Custom raylib color palette for amazing visuals on WHITE background #define LIGHTGRAY (Color){ 200, 200, 200, 255 } // Light Gray @@ -540,6 +546,9 @@ bool IsFileDropped(void); // Check if a file h char **GetDroppedFiles(int *count); // Retrieve dropped files into window void ClearDroppedFiles(void); // Clear dropped files paths buffer +void StorageSaveValue(int position, int value); // Storage save integer value (to defined position) +int StorageLoadValue(int position); // Storage load integer value (from defined position) + //------------------------------------------------------------------------------------ // Input Handling Functions (Module: core) //------------------------------------------------------------------------------------ @@ -578,6 +587,9 @@ bool IsGamepadButtonUp(int gamepad, int button); // Detect if a gamepad b int GetTouchX(void); // Returns touch position X (relative to screen size) int GetTouchY(void); // Returns touch position Y (relative to screen size) Vector2 GetTouchPosition(void); // Returns touch position XY (relative to screen size) +bool IsButtonPressed(int button); // Detect if an android physic button has been pressed +bool IsButtonDown(int button); // Detect if an android physic button is being pressed +bool IsButtonReleased(int button); // Detect if an android physic button has been released //------------------------------------------------------------------------------------ // Gestures and Touch Handling Functions (Module: gestures) @@ -793,21 +805,23 @@ void SetMaterialNormalDepth(Material *material, float depth); // Set n //---------------------------------------------------------------------------------- // Physics System Functions (engine-module: physics) //---------------------------------------------------------------------------------- -void InitPhysics(); // Initialize all internal physics values -void SetPhysics(Physics settings); // Set physics settings values using Physics data type to overwrite internal physics settings +void InitPhysics(); // Initialize all internal physics values +void SetPhysics(Physics settings); // Set physics settings values using Physics data type to overwrite internal physics settings -void AddRigidbody(int index, Rigidbody rigidbody); // Initialize a new rigidbody with parameters to internal index slot -void AddCollider(int index, Collider collider); // Initialize a new Collider with parameters to internal index slot +void AddRigidbody(int index, Rigidbody rigidbody); // Initialize a new rigidbody with parameters to internal index slot +void AddCollider(int index, Collider collider); // Initialize a new Collider with parameters to internal index slot -void ApplyPhysics(int index, Vector2 *position); // Apply physics to internal rigidbody, physics calculations are applied to position pointer parameter -void SetRigidbodyEnabled(int index, bool state); // Set enabled state to a defined rigidbody -void SetRigidbodyVelocity(int index, Vector2 velocity); // Set velocity of rigidbody (without considering of mass value) -void AddRigidbodyForce(int index, Vector2 force); // Set rigidbody force (considering mass value) +void ApplyPhysics(int index, Vector2 *position); // Apply physics to internal rigidbody, physics calculations are applied to position pointer parameter +void SetRigidbodyEnabled(int index, bool state); // Set enabled state to a defined rigidbody +void SetRigidbodyVelocity(int index, Vector2 velocity); // Set velocity of rigidbody (without considering of mass value) +void SetRigidbodyAcceleration(int index, Vector2 acceleration); // Set acceleration of rigidbody (without considering of mass value) +void AddRigidbodyForce(int index, Vector2 force); // Set rigidbody force (considering mass value) +void AddForceAtPosition(Vector2 position, float intensity, float radius); // Add a force to all enabled rigidbodies at a position -void SetColliderEnabled(int index, bool state); // Set enabled state to a defined collider +void SetColliderEnabled(int index, bool state); // Set enabled state to a defined collider -Rigidbody GetRigidbody(int index); // Returns the internal rigidbody data defined by index parameter -Collider GetCollider(int index); // Returns the internal collider data defined by index parameter +Rigidbody GetRigidbody(int index); // Returns the internal rigidbody data defined by index parameter +Collider GetCollider(int index); // Returns the internal collider data defined by index parameter //------------------------------------------------------------------------------------ // Audio Loading and Playing Functions (Module: audio) From fe0cf8f9a9d6cafa40a1baf8b3cdace0268ccb82 Mon Sep 17 00:00:00 2001 From: victorfisac Date: Tue, 5 Jan 2016 13:58:20 +0100 Subject: [PATCH 3/3] Added some comments and fixed spaces --- src/core.c | 1 + src/physac.c | 2 +- src/physac.h | 2 +- src/raylib.h | 4 ++-- templates/android_project/jni/include/raylib.h | 4 ++-- 5 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/core.c b/src/core.c index 40691403f..1d05edc63 100644 --- a/src/core.c +++ b/src/core.c @@ -1111,6 +1111,7 @@ Vector2 GetTouchPosition(void) return position; } +// Detect if a button has been pressed once bool IsButtonPressed(int button) { bool pressed = false; diff --git a/src/physac.c b/src/physac.c index 73ce7adc6..6e3b6e61c 100644 --- a/src/physac.c +++ b/src/physac.c @@ -61,7 +61,7 @@ static Vector2 Vector2Normalize(Vector2 vector); //---------------------------------------------------------------------------------- // Module Functions Definitions //---------------------------------------------------------------------------------- -void InitPhysics() +void InitPhysics(void) { for (int i = 0; i < MAX_ELEMENTS; i++) { diff --git a/src/physac.h b/src/physac.h index 7dbfe1fe7..558673ef3 100644 --- a/src/physac.h +++ b/src/physac.h @@ -76,7 +76,7 @@ extern "C" { // Prevents name mangling of functions //---------------------------------------------------------------------------------- // Module Functions Declarations //---------------------------------------------------------------------------------- -void InitPhysics(); // Initialize all internal physics values +void InitPhysics(void); // Initialize all internal physics values void SetPhysics(Physics settings); // Set physics settings values using Physics data type to overwrite internal physics settings void AddRigidbody(int index, Rigidbody rigidbody); // Initialize a new rigidbody with parameters to internal index slot diff --git a/src/raylib.h b/src/raylib.h index 641eac4bf..72211b59e 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -589,7 +589,7 @@ int GetTouchY(void); // Returns touch positio Vector2 GetTouchPosition(void); // Returns touch position XY (relative to screen size) bool IsButtonPressed(int button); // Detect if an android physic button has been pressed bool IsButtonDown(int button); // Detect if an android physic button is being pressed -bool IsButtonReleased(int button); // Detect if an android physic button has been released +bool IsButtonReleased(int button); // Detect if an android physic button has been released //------------------------------------------------------------------------------------ // Gestures and Touch Handling Functions (Module: gestures) @@ -805,7 +805,7 @@ void SetMaterialNormalDepth(Material *material, float depth); // Set n //---------------------------------------------------------------------------------- // Physics System Functions (engine-module: physics) //---------------------------------------------------------------------------------- -void InitPhysics(); // Initialize all internal physics values +void InitPhysics(void); // Initialize all internal physics values void SetPhysics(Physics settings); // Set physics settings values using Physics data type to overwrite internal physics settings void AddRigidbody(int index, Rigidbody rigidbody); // Initialize a new rigidbody with parameters to internal index slot diff --git a/templates/android_project/jni/include/raylib.h b/templates/android_project/jni/include/raylib.h index 641eac4bf..72211b59e 100644 --- a/templates/android_project/jni/include/raylib.h +++ b/templates/android_project/jni/include/raylib.h @@ -589,7 +589,7 @@ int GetTouchY(void); // Returns touch positio Vector2 GetTouchPosition(void); // Returns touch position XY (relative to screen size) bool IsButtonPressed(int button); // Detect if an android physic button has been pressed bool IsButtonDown(int button); // Detect if an android physic button is being pressed -bool IsButtonReleased(int button); // Detect if an android physic button has been released +bool IsButtonReleased(int button); // Detect if an android physic button has been released //------------------------------------------------------------------------------------ // Gestures and Touch Handling Functions (Module: gestures) @@ -805,7 +805,7 @@ void SetMaterialNormalDepth(Material *material, float depth); // Set n //---------------------------------------------------------------------------------- // Physics System Functions (engine-module: physics) //---------------------------------------------------------------------------------- -void InitPhysics(); // Initialize all internal physics values +void InitPhysics(void); // Initialize all internal physics values void SetPhysics(Physics settings); // Set physics settings values using Physics data type to overwrite internal physics settings void AddRigidbody(int index, Rigidbody rigidbody); // Initialize a new rigidbody with parameters to internal index slot