From 13d0f97558e4cd2299bfcd10573eab09f1ded821 Mon Sep 17 00:00:00 2001 From: artdeell Date: Sat, 28 Dec 2024 20:59:59 +0300 Subject: [PATCH] Feat[gamepad_direct]: remove unnecessary strings, improve documentation --- .../customcontrols/gamepad/Gamepad.java | 5 +++++ .../gamepad/direct/DirectGamepad.java | 16 ++++++++++------ .../direct/DirectGamepadEnableHandler.java | 12 ++++++++++++ .../gamepad/direct/GamepadKeycodes.java | 1 - .../main/java/org/lwjgl/glfw/CallbackBridge.java | 3 ++- .../src/main/res/values/strings.xml | 2 -- 6 files changed, 29 insertions(+), 10 deletions(-) diff --git a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/customcontrols/gamepad/Gamepad.java b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/customcontrols/gamepad/Gamepad.java index f84cef74b4..fe97ec601f 100644 --- a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/customcontrols/gamepad/Gamepad.java +++ b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/customcontrols/gamepad/Gamepad.java @@ -454,6 +454,11 @@ public void handleGamepadInput(int keycode, float value) { break; } } + + /** + * Stops the Gamepad and removes all traces of the Gamepad from the view hierarchy. + * After this call, the Gamepad is not recoverable and a new one must be made. + */ public void removeSelf() { mRemoved = true; mMapProvider.detachGrabListener(this); diff --git a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/customcontrols/gamepad/direct/DirectGamepad.java b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/customcontrols/gamepad/direct/DirectGamepad.java index 2d600eec21..b99980c0fb 100644 --- a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/customcontrols/gamepad/direct/DirectGamepad.java +++ b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/customcontrols/gamepad/direct/DirectGamepad.java @@ -1,11 +1,8 @@ package net.kdt.pojavlaunch.customcontrols.gamepad.direct; -import static android.view.MotionEvent.AXIS_HAT_X; -import static android.view.MotionEvent.AXIS_HAT_Y; import static org.lwjgl.glfw.CallbackBridge.sGamepadAxisBuffer; import static org.lwjgl.glfw.CallbackBridge.sGamepadButtonBuffer; -import android.util.Log; import android.view.KeyEvent; import android.view.MotionEvent; @@ -38,12 +35,19 @@ public void handleGamepadInput(int keycode, float value) { case KeyEvent.KEYCODE_DPAD_DOWN: gKeycode = GamepadKeycodes.GLFW_GAMEPAD_BUTTON_DPAD_DOWN; break; case KeyEvent.KEYCODE_DPAD_LEFT: gKeycode = GamepadKeycodes.GLFW_GAMEPAD_BUTTON_DPAD_LEFT; break; case KeyEvent.KEYCODE_DPAD_RIGHT: gKeycode = GamepadKeycodes.GLFW_GAMEPAD_BUTTON_DPAD_RIGHT; break; - case KeyEvent.KEYCODE_DPAD_CENTER: break; // TODO + case KeyEvent.KEYCODE_DPAD_CENTER: + // Behave the same way as the Gamepad here, as GLFW doesn't have a keycode + // for the dpad center. + sGamepadButtonBuffer.put(GamepadKeycodes.GLFW_GAMEPAD_BUTTON_DPAD_UP, GamepadKeycodes.GLFW_RELEASE); + sGamepadButtonBuffer.put(GamepadKeycodes.GLFW_GAMEPAD_BUTTON_DPAD_DOWN, GamepadKeycodes.GLFW_RELEASE); + sGamepadButtonBuffer.put(GamepadKeycodes.GLFW_GAMEPAD_BUTTON_DPAD_LEFT, GamepadKeycodes.GLFW_RELEASE); + sGamepadButtonBuffer.put(GamepadKeycodes.GLFW_GAMEPAD_BUTTON_DPAD_RIGHT, GamepadKeycodes.GLFW_RELEASE); + return; case MotionEvent.AXIS_X: gAxis = GamepadKeycodes.GLFW_GAMEPAD_AXIS_LEFT_X; break; case MotionEvent.AXIS_Y: gAxis = GamepadKeycodes.GLFW_GAMEPAD_AXIS_LEFT_Y; break; case MotionEvent.AXIS_Z: gAxis = GamepadKeycodes.GLFW_GAMEPAD_AXIS_RIGHT_X; break; case MotionEvent.AXIS_RZ: gAxis = GamepadKeycodes.GLFW_GAMEPAD_AXIS_RIGHT_Y; break; - case AXIS_HAT_X: + case MotionEvent.AXIS_HAT_X: sGamepadButtonBuffer.put( GamepadKeycodes.GLFW_GAMEPAD_BUTTON_DPAD_LEFT, value < -0.85 ? GamepadKeycodes.GLFW_PRESS : GamepadKeycodes.GLFW_RELEASE @@ -53,7 +57,7 @@ public void handleGamepadInput(int keycode, float value) { value > 0.85 ? GamepadKeycodes.GLFW_PRESS : GamepadKeycodes.GLFW_RELEASE ); return; - case AXIS_HAT_Y: + case MotionEvent.AXIS_HAT_Y: sGamepadButtonBuffer.put( GamepadKeycodes.GLFW_GAMEPAD_BUTTON_DPAD_UP, value < -0.85 ? GamepadKeycodes.GLFW_PRESS : GamepadKeycodes.GLFW_RELEASE diff --git a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/customcontrols/gamepad/direct/DirectGamepadEnableHandler.java b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/customcontrols/gamepad/direct/DirectGamepadEnableHandler.java index b59a078338..5e6d7d87f4 100644 --- a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/customcontrols/gamepad/direct/DirectGamepadEnableHandler.java +++ b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/customcontrols/gamepad/direct/DirectGamepadEnableHandler.java @@ -1,5 +1,17 @@ package net.kdt.pojavlaunch.customcontrols.gamepad.direct; +/** + * Interface that is called once when the GLFW implementation requests to switch from + * the default gamepad implementation to the direct one. The implementor of this interface + * must take the necessary steps to disable the default gamepad implementation and replace + * it with an instance of DirectGamepad. + + * This is useful for switching from default to direct input after the user has already + * interacted with the gamepad. + */ public interface DirectGamepadEnableHandler { + /** + * Called once when GLFW requests switching the gamepad mode from default to direct. + */ void onDirectGamepadEnabled(); } diff --git a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/customcontrols/gamepad/direct/GamepadKeycodes.java b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/customcontrols/gamepad/direct/GamepadKeycodes.java index b175e3d662..2ed1a5a163 100644 --- a/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/customcontrols/gamepad/direct/GamepadKeycodes.java +++ b/app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/customcontrols/gamepad/direct/GamepadKeycodes.java @@ -3,7 +3,6 @@ public class GamepadKeycodes { public static final byte GLFW_RELEASE = 0; public static final byte GLFW_PRESS = 1; - public static int NUM_KEYCODES = 0; public static final short GLFW_GAMEPAD_BUTTON_A = 0; public static final short GLFW_GAMEPAD_BUTTON_B = 1; diff --git a/app_pojavlauncher/src/main/java/org/lwjgl/glfw/CallbackBridge.java b/app_pojavlauncher/src/main/java/org/lwjgl/glfw/CallbackBridge.java index af44e75cc8..e60966e600 100644 --- a/app_pojavlauncher/src/main/java/org/lwjgl/glfw/CallbackBridge.java +++ b/app_pojavlauncher/src/main/java/org/lwjgl/glfw/CallbackBridge.java @@ -22,7 +22,8 @@ public class CallbackBridge { public static final Choreographer sChoreographer = Choreographer.getInstance(); private static boolean isGrabbing = false; private static final ArrayList grabListeners = new ArrayList<>(); - private static WeakReference sDirectGamepadEnableHandler; + // Use a weak reference here to avoid possibly statically referencing a Context. + private static @Nullable WeakReference sDirectGamepadEnableHandler; public static final int CLIPBOARD_COPY = 2000; public static final int CLIPBOARD_PASTE = 2001; diff --git a/app_pojavlauncher/src/main/res/values/strings.xml b/app_pojavlauncher/src/main/res/values/strings.xml index 2aff163bf5..91104432dc 100644 --- a/app_pojavlauncher/src/main/res/values/strings.xml +++ b/app_pojavlauncher/src/main/res/values/strings.xml @@ -422,6 +422,4 @@ Unsuitable username The username must be between 3–16 characters long, and must only contain latin letters, arabic numerals and underscores. Quick settings - Use direct controller input - Disables keyboard/mouse emulation and lets the game use gamepad inputs directly.