Skip to content
This repository was archived by the owner on Sep 23, 2025. It is now read-only.

Commit 21cfcdb

Browse files
authored
Feat[input]: GLFW direct controller input (#6604)
1 parent ffc74c5 commit 21cfcdb

File tree

21 files changed

+308
-39
lines changed

21 files changed

+308
-39
lines changed
Binary file not shown.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1687691695196
1+
1738839797928
Binary file not shown.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1692525087345
1+
1738839797913
Binary file not shown.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1732218529630
1+
1738839797921

app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/MinecraftGLSurface.java

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
import net.kdt.pojavlaunch.customcontrols.ControlLayout;
3030
import net.kdt.pojavlaunch.customcontrols.gamepad.DefaultDataProvider;
3131
import net.kdt.pojavlaunch.customcontrols.gamepad.Gamepad;
32+
import net.kdt.pojavlaunch.customcontrols.gamepad.direct.DirectGamepad;
33+
import net.kdt.pojavlaunch.customcontrols.gamepad.direct.DirectGamepadEnableHandler;
3234
import net.kdt.pojavlaunch.customcontrols.mouse.AbstractTouchpad;
3335
import net.kdt.pojavlaunch.customcontrols.mouse.AndroidPointerCapture;
3436
import net.kdt.pojavlaunch.customcontrols.mouse.InGUIEventProcessor;
@@ -40,15 +42,16 @@
4042

4143
import org.lwjgl.glfw.CallbackBridge;
4244

45+
import fr.spse.gamepad_remapper.GamepadHandler;
4346
import fr.spse.gamepad_remapper.RemapperManager;
4447
import fr.spse.gamepad_remapper.RemapperView;
4548

4649
/**
4750
* Class dealing with showing minecraft surface and taking inputs to dispatch them to minecraft
4851
*/
49-
public class MinecraftGLSurface extends View implements GrabListener {
52+
public class MinecraftGLSurface extends View implements GrabListener, DirectGamepadEnableHandler {
5053
/* Gamepad object for gamepad inputs, instantiated on need */
51-
private Gamepad mGamepad = null;
54+
private GamepadHandler mGamepadHandler;
5255
/* The RemapperView.Builder object allows you to set which buttons to remap */
5356
private final RemapperManager mInputManager = new RemapperManager(getContext(), new RemapperView.Builder(null)
5457
.remapA(true)
@@ -88,6 +91,7 @@ public MinecraftGLSurface(Context context) {
8891
public MinecraftGLSurface(Context context, AttributeSet attributeSet) {
8992
super(context, attributeSet);
9093
setFocusable(true);
94+
CallbackBridge.setDirectGamepadEnableHandler(this);
9195
}
9296

9397
@RequiresApi(api = Build.VERSION_CODES.O)
@@ -203,7 +207,11 @@ public boolean onTouchEvent(MotionEvent e) {
203207
}
204208

205209
private void createGamepad(View contextView, InputDevice inputDevice) {
206-
mGamepad = new Gamepad(contextView, inputDevice, DefaultDataProvider.INSTANCE, true);
210+
if(CallbackBridge.sGamepadDirectInput) {
211+
mGamepadHandler = new DirectGamepad();
212+
}else {
213+
mGamepadHandler = new Gamepad(contextView, inputDevice, DefaultDataProvider.INSTANCE, true);
214+
}
207215
}
208216

209217
/**
@@ -215,9 +223,9 @@ public boolean dispatchGenericMotionEvent(MotionEvent event) {
215223
int mouseCursorIndex = -1;
216224

217225
if(Gamepad.isGamepadEvent(event)){
218-
if(mGamepad == null) createGamepad(this, event.getDevice());
226+
if(mGamepadHandler == null) createGamepad(this, event.getDevice());
219227

220-
mInputManager.handleMotionEventInput(getContext(), event, mGamepad);
228+
mInputManager.handleMotionEventInput(getContext(), event, mGamepadHandler);
221229
return true;
222230
}
223231

@@ -287,9 +295,9 @@ public boolean processKeyEvent(KeyEvent event) {
287295
}
288296

289297
if(Gamepad.isGamepadEvent(event)){
290-
if(mGamepad == null) createGamepad(this, event.getDevice());
298+
if(mGamepadHandler == null) createGamepad(this, event.getDevice());
291299

292-
mInputManager.handleKeyEventInput(getContext(), event, mGamepad);
300+
mInputManager.handleKeyEventInput(getContext(), event, mGamepadHandler);
293301
return true;
294302
}
295303

@@ -411,6 +419,17 @@ private void updateGrabState(boolean isGrabbing) {
411419
}
412420
}
413421

422+
@Override
423+
public void onDirectGamepadEnabled() {
424+
post(()->{
425+
if(mGamepadHandler != null && mGamepadHandler instanceof Gamepad) {
426+
((Gamepad)mGamepadHandler).removeSelf();
427+
}
428+
// Force gamepad recreation on next event
429+
mGamepadHandler = null;
430+
});
431+
}
432+
414433
/** A small interface called when the listener is ready for the first time */
415434
public interface SurfaceReadyListener {
416435
void isReady();

app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/customcontrols/gamepad/DefaultDataProvider.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,9 @@ public boolean isGrabbing() {
3030
public void attachGrabListener(GrabListener grabListener) {
3131
CallbackBridge.addGrabListener(grabListener);
3232
}
33+
34+
@Override
35+
public void detachGrabListener(GrabListener grabListener) {
36+
CallbackBridge.removeGrabListener(grabListener);
37+
}
3338
}

app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/customcontrols/gamepad/Gamepad.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import net.kdt.pojavlaunch.GrabListener;
2727
import net.kdt.pojavlaunch.LwjglGlfwKeycode;
2828
import net.kdt.pojavlaunch.R;
29-
import net.kdt.pojavlaunch.prefs.LauncherPreferences;
3029
import net.kdt.pojavlaunch.utils.MCOptionUtils;
3130

3231
import org.lwjgl.glfw.CallbackBridge;
@@ -88,6 +87,8 @@ public class Gamepad implements GrabListener, GamepadHandler {
8887

8988
private final GamepadDataProvider mMapProvider;
9089

90+
private boolean mRemoved = false;
91+
9192
public Gamepad(View contextView, InputDevice inputDevice, GamepadDataProvider mapProvider, boolean showCursor){
9293
Settings.setDeadzoneScale(PREF_DEADZONE_SCALE);
9394

@@ -96,7 +97,7 @@ public Gamepad(View contextView, InputDevice inputDevice, GamepadDataProvider ma
9697
@Override
9798
public void doFrame(long frameTimeNanos) {
9899
tick(frameTimeNanos);
99-
mScreenChoreographer.postFrameCallback(this);
100+
if(!mRemoved) mScreenChoreographer.postFrameCallback(this);
100101
}
101102
};
102103
mScreenChoreographer.postFrameCallback(frameCallback);
@@ -453,4 +454,15 @@ public void handleGamepadInput(int keycode, float value) {
453454
break;
454455
}
455456
}
457+
458+
/**
459+
* Stops the Gamepad and removes all traces of the Gamepad from the view hierarchy.
460+
* After this call, the Gamepad is not recoverable and a new one must be made.
461+
*/
462+
public void removeSelf() {
463+
mRemoved = true;
464+
mMapProvider.detachGrabListener(this);
465+
ViewGroup viewGroup = (ViewGroup) mPointerImageView.getParent();
466+
if(viewGroup != null) viewGroup.removeView(mPointerImageView);
467+
}
456468
}

app_pojavlauncher/src/main/java/net/kdt/pojavlaunch/customcontrols/gamepad/GamepadDataProvider.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ public interface GamepadDataProvider {
77
GamepadMap getGameMap();
88
boolean isGrabbing();
99
void attachGrabListener(GrabListener grabListener);
10+
void detachGrabListener(GrabListener grabListener);
1011
}

0 commit comments

Comments
 (0)