Skip to content

Commit

Permalink
DO NOT USE. Feat[gamepad_remapper]: start work on gamepad remapper
Browse files Browse the repository at this point in the history
  • Loading branch information
artdeell committed Aug 19, 2024
1 parent ae48e8e commit 80c5ea8
Show file tree
Hide file tree
Showing 14 changed files with 980 additions and 151 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import androidx.annotation.RequiresApi;

import net.kdt.pojavlaunch.customcontrols.ControlLayout;
import net.kdt.pojavlaunch.customcontrols.gamepad.DefaultDataProvider;
import net.kdt.pojavlaunch.customcontrols.gamepad.Gamepad;
import net.kdt.pojavlaunch.customcontrols.mouse.AbstractTouchpad;
import net.kdt.pojavlaunch.customcontrols.mouse.AndroidPointerCapture;
Expand Down Expand Up @@ -212,7 +213,7 @@ public boolean dispatchGenericMotionEvent(MotionEvent event) {

if(Gamepad.isGamepadEvent(event)){
if(mGamepad == null){
mGamepad = new Gamepad(this, event.getDevice());
mGamepad = new Gamepad(this, event.getDevice(), DefaultDataProvider.INSTANCE);
}

mInputManager.handleMotionEventInput(getContext(), event, mGamepad);
Expand Down Expand Up @@ -286,7 +287,7 @@ public boolean processKeyEvent(KeyEvent event) {

if(Gamepad.isGamepadEvent(event)){
if(mGamepad == null){
mGamepad = new Gamepad(this, event.getDevice());
mGamepad = new Gamepad(this, event.getDevice(), DefaultDataProvider.INSTANCE);
}

mInputManager.handleKeyEventInput(getContext(), event, mGamepad);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package net.kdt.pojavlaunch.customcontrols.gamepad;

import net.kdt.pojavlaunch.GrabListener;

import org.lwjgl.glfw.CallbackBridge;

public class DefaultDataProvider implements GamepadDataProvider {
public static final DefaultDataProvider INSTANCE = new DefaultDataProvider();

private DefaultDataProvider() {
reloadGamepadMaps();
}

@Override
public GamepadMap getGameMap() {
return GamepadMapStore.getGameMap();
}


@Override
public GamepadMap getMenuMap() {
return GamepadMapStore.getMenuMap();
}

@Override
public boolean isGrabbing() {
return CallbackBridge.isGrabbing();
}

@Override
public void attachGrabListener(GrabListener grabListener) {
CallbackBridge.addGrabListener(grabListener);
}

@Override
public void reloadGamepadMaps() {
GamepadMapStore.load();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.core.content.res.ResourcesCompat;
import androidx.core.math.MathUtils;
import androidx.fragment.app.Fragment;

import net.kdt.pojavlaunch.GrabListener;
import net.kdt.pojavlaunch.LwjglGlfwKeycode;
import net.kdt.pojavlaunch.R;
import net.kdt.pojavlaunch.Tools;
import net.kdt.pojavlaunch.prefs.LauncherPreferences;
import net.kdt.pojavlaunch.utils.MCOptionUtils;

Expand Down Expand Up @@ -75,12 +75,11 @@ public class Gamepad implements GrabListener, GamepadHandler {
private double mMouseAngle;
private double mMouseSensitivity = 19;

private final GamepadMap mGameMap = GamepadMap.getDefaultGameMap();
private final GamepadMap mMenuMap = GamepadMap.getDefaultMenuMap();
private GamepadMap mCurrentMap = mGameMap;
private GamepadMap mGameMap;
private GamepadMap mMenuMap;
private GamepadMap mCurrentMap;

// The negation is to force trigger the onGrabState
private boolean isGrabbing = !CallbackBridge.isGrabbing();
private boolean isGrabbing;


/* Choreographer with time to compute delta on ticking */
Expand All @@ -91,7 +90,9 @@ public class Gamepad implements GrabListener, GamepadHandler {
@SuppressWarnings("FieldCanBeLocal") //the field is used in a WeakReference
private final MCOptionUtils.MCOptionListener mGuiScaleListener = () -> notifyGUISizeChange(getMcScale());

public Gamepad(View contextView, InputDevice inputDevice){
private final GamepadDataProvider mMapProvider;

public Gamepad(View contextView, InputDevice inputDevice, GamepadDataProvider mapProvider){
Settings.setDeadzoneScale(PREF_DEADZONE_SCALE);

mScreenChoreographer = Choreographer.getInstance();
Expand Down Expand Up @@ -120,16 +121,33 @@ public void doFrame(long frameTimeNanos) {
int size = (int) ((22 * getMcScale()) / mScaleFactor);
mPointerImageView.setLayoutParams(new FrameLayout.LayoutParams(size, size));

mMapProvider = mapProvider;

CallbackBridge.sendCursorPos(CallbackBridge.windowWidth/2f, CallbackBridge.windowHeight/2f);
((ViewGroup)contextView.getParent()).addView(mPointerImageView);
ViewParent parent = contextView.getParent();
if(parent instanceof FrameLayout) {
// ((FrameLayout)parent).addView(mPointerImageView);
}

placePointerView(CallbackBridge.physicalWidth/2, CallbackBridge.physicalHeight/2);

CallbackBridge.addGrabListener(this);
reloadGamepadMaps();
mMapProvider.attachGrabListener(this);
}



public void reloadGamepadMaps() {
if(mGameMap != null) mGameMap.resetPressedState();
if(mMenuMap != null) mMenuMap.resetPressedState();
mMapProvider.reloadGamepadMaps();
mGameMap = mMapProvider.getGameMap();
mMenuMap = mMapProvider.getMenuMap();
mCurrentMap = mGameMap;
// Force state refresh
boolean currentGrab = CallbackBridge.isGrabbing();
isGrabbing = !currentGrab;
onGrabState(currentGrab);
}

public void updateJoysticks(){
updateDirectionalJoystick();
Expand All @@ -144,29 +162,32 @@ public void notifyGUISizeChange(int newSize){
}


public static void sendInput(int[] keycodes, boolean isDown){
for(int keycode : keycodes){
public static void sendInput(short[] keycodes, boolean isDown){
for(short keycode : keycodes){
switch (keycode){
case GamepadMap.MOUSE_SCROLL_DOWN:
if(isDown) CallbackBridge.sendScroll(0, -1);
break;
case GamepadMap.MOUSE_SCROLL_UP:
if(isDown) CallbackBridge.sendScroll(0, 1);
break;

case LwjglGlfwKeycode.GLFW_MOUSE_BUTTON_RIGHT:
case GamepadMap.MOUSE_LEFT:
sendMouseButton(LwjglGlfwKeycode.GLFW_MOUSE_BUTTON_LEFT, isDown);
break;
case GamepadMap.MOUSE_MIDDLE:
sendMouseButton(LwjglGlfwKeycode.GLFW_MOUSE_BUTTON_MIDDLE, isDown);
break;
case GamepadMap.MOUSE_RIGHT:
sendMouseButton(LwjglGlfwKeycode.GLFW_MOUSE_BUTTON_RIGHT, isDown);
break;
case LwjglGlfwKeycode.GLFW_MOUSE_BUTTON_LEFT:
sendMouseButton(LwjglGlfwKeycode.GLFW_MOUSE_BUTTON_LEFT, isDown);
case GamepadMap.UNSPECIFIED:
break;


default:
sendKeyPress(keycode, CallbackBridge.getCurrentMods(), isDown);
CallbackBridge.setModifiers(keycode, isDown);
break;
}
CallbackBridge.setModifiers(keycode, isDown);
}

}
Expand Down Expand Up @@ -261,32 +282,32 @@ private GamepadMap getCurrentMap(){
private static void sendDirectionalKeycode(int direction, boolean isDown, GamepadMap map){
switch (direction){
case DIRECTION_NORTH:
sendInput(map.DIRECTION_FORWARD, isDown);
map.DIRECTION_FORWARD.update(isDown);
break;
case DIRECTION_NORTH_EAST:
sendInput(map.DIRECTION_FORWARD, isDown);
sendInput(map.DIRECTION_RIGHT, isDown);
map.DIRECTION_FORWARD.update(isDown);
map.DIRECTION_RIGHT.update(isDown);
break;
case DIRECTION_EAST:
sendInput(map.DIRECTION_RIGHT, isDown);
map.DIRECTION_RIGHT.update(isDown);
break;
case DIRECTION_SOUTH_EAST:
sendInput(map.DIRECTION_RIGHT, isDown);
sendInput(map.DIRECTION_BACKWARD, isDown);
map.DIRECTION_RIGHT.update(isDown);
map.DIRECTION_BACKWARD.update(isDown);
break;
case DIRECTION_SOUTH:
sendInput(map.DIRECTION_BACKWARD, isDown);
map.DIRECTION_BACKWARD.update(isDown);
break;
case DIRECTION_SOUTH_WEST:
sendInput(map.DIRECTION_BACKWARD, isDown);
sendInput(map.DIRECTION_LEFT, isDown);
map.DIRECTION_BACKWARD.update(isDown);
map.DIRECTION_LEFT.update(isDown);
break;
case DIRECTION_WEST:
sendInput(map.DIRECTION_LEFT, isDown);
map.DIRECTION_LEFT.update(isDown);
break;
case DIRECTION_NORTH_WEST:
sendInput(map.DIRECTION_FORWARD, isDown);
sendInput(map.DIRECTION_LEFT, isDown);
map.DIRECTION_FORWARD.update(isDown);
map.DIRECTION_LEFT.update(isDown);
break;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,46 +1,30 @@
package net.kdt.pojavlaunch.customcontrols.gamepad;

import android.view.KeyEvent;

/**
* Simple button able to store its state and some properties
* This class corresponds to a button that does exist on the gamepad
*/
public class GamepadButton {

public int[] keycodes;
public class GamepadButton extends GamepadEmulatedButton {
public boolean isToggleable = false;
private boolean mIsDown = false;
private boolean mIsToggled = false;

public void update(KeyEvent event){
boolean isKeyDown = (event.getAction() == KeyEvent.ACTION_DOWN);
update(isKeyDown);
}

public void update(boolean isKeyDown){
if(isKeyDown != mIsDown){
mIsDown = isKeyDown;
if(isToggleable){
if(isKeyDown){
mIsToggled = !mIsToggled;
Gamepad.sendInput(keycodes, mIsToggled);
}
return;
}
Gamepad.sendInput(keycodes, mIsDown);
@Override
protected void onDownStateChanged(boolean isDown) {
if(isToggleable && isDown){
mIsToggled = !mIsToggled;
Gamepad.sendInput(keycodes, mIsToggled);
return;
}
super.onDownStateChanged(isDown);
}

public void resetButtonState(){
if(mIsDown || mIsToggled){
@Override
public void resetButtonState() {
if(!mIsDown && mIsToggled) {
Gamepad.sendInput(keycodes, false);
mIsToggled = false;
} else {
super.resetButtonState();
}
mIsDown = false;
mIsToggled = false;
}

public boolean isDown(){
return isToggleable ? mIsToggled : mIsDown;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package net.kdt.pojavlaunch.customcontrols.gamepad;

import net.kdt.pojavlaunch.GrabListener;

public interface GamepadDataProvider {
GamepadMap getMenuMap();
GamepadMap getGameMap();
boolean isGrabbing();
void attachGrabListener(GrabListener grabListener);
void reloadGamepadMaps();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package net.kdt.pojavlaunch.customcontrols.gamepad;

import android.view.KeyEvent;

/**
* This class corresponds to a button that does not physically exist on the gamepad, but is
* emulated from other inputs on it (like WASD directional keys)
*/
public class GamepadEmulatedButton {
public short[] keycodes;
protected boolean mIsDown = false;

public void update(KeyEvent event) {
boolean isKeyDown = (event.getAction() == KeyEvent.ACTION_DOWN);
update(isKeyDown);
}

public void update(boolean isKeyDown){
if(isKeyDown != mIsDown){
mIsDown = isKeyDown;
onDownStateChanged(mIsDown);
}
}

public void resetButtonState() {
if(mIsDown) Gamepad.sendInput(keycodes, false);
mIsDown = false;
}

protected void onDownStateChanged(boolean isDown) {
Gamepad.sendInput(keycodes, mIsDown);
}
}
Loading

0 comments on commit 80c5ea8

Please sign in to comment.