Android: Normalize pointer touches based on rendered aspect ratio

This allows the defaults to be actual defaults across devices with different
screen sizes
This commit is contained in:
zackhow
2019-01-19 22:49:04 -05:00
parent ec557eb3a2
commit e8739156e4
8 changed files with 117 additions and 20 deletions

View File

@ -492,4 +492,19 @@ public final class NativeLibrary
sEmulationActivity.clear();
}
public static void updateTouchPointer()
{
final EmulationActivity emulationActivity = sEmulationActivity.get();
if (emulationActivity == null)
{
Log.warning("[NativeLibrary] EmulationActivity is null.");
}
else
{
emulationActivity.runOnUiThread(emulationActivity::initInputPointer);
}
}
public static native float GetGameAspectRatio();
}

View File

@ -757,7 +757,7 @@ public final class EmulationActivity extends AppCompatActivity
builder.setPositiveButton(getString(R.string.ok), (dialogInterface, i) ->
{
editor.commit();
mEmulationFragment.refreshInputOverlay();
mEmulationFragment.initInputPointer();
});
AlertDialog alertDialog = builder.create();
@ -1064,4 +1064,9 @@ public final class EmulationActivity extends AppCompatActivity
{
return mSettings;
}
public void initInputPointer()
{
mEmulationFragment.initInputPointer();
}
}

View File

@ -201,6 +201,11 @@ public final class EmulationFragment extends Fragment implements SurfaceHolder.C
mInputOverlay.refreshControls();
}
public void initInputPointer()
{
mInputOverlay.initTouchPointer();
}
public void refreshInputOverlay()
{
mInputOverlay.refreshControls();

View File

@ -92,6 +92,7 @@ public final class InputOverlay extends SurfaceView implements OnTouchListener
mPreferences = PreferenceManager.getDefaultSharedPreferences(getContext());
if (!mPreferences.getBoolean("OverlayInitV2", false))
defaultOverlay();
// Load the controls.
refreshControls();
@ -105,6 +106,27 @@ public final class InputOverlay extends SurfaceView implements OnTouchListener
requestFocus();
}
public void initTouchPointer()
{
// Refresh before starting the pointer
refreshControls();
if (!EmulationActivity.isGameCubeGame())
{
int doubleTapButton = mPreferences.getInt("doubleTapButton",
InputOverlayPointer.DOUBLE_TAP_OPTIONS.get(InputOverlayPointer.DOUBLE_TAP_A));
if (mPreferences.getInt("wiiController", OVERLAY_WIIMOTE_NUNCHUCK) !=
InputOverlay.OVERLAY_WIIMOTE_CLASSIC &&
doubleTapButton == InputOverlayPointer.DOUBLE_TAP_CLASSIC_A)
{
doubleTapButton = InputOverlayPointer.DOUBLE_TAP_A;
}
overlayPointer = new InputOverlayPointer(this.getContext(), doubleTapButton);
}
}
@Override
public void draw(Canvas canvas)
{
@ -201,15 +223,22 @@ public final class InputOverlay extends SurfaceView implements OnTouchListener
// Release the buttons first, then press
for (int i = 0; i < dpadPressed.length; i++)
{
if (!dpadPressed[i])
{
NativeLibrary.onGamePadEvent(NativeLibrary.TouchScreenDevice, dpad.getId(i),
ButtonState.RELEASED);
}
}
// Press buttons
for (int i = 0; i < dpadPressed.length; i++)
{
if (dpadPressed[i])
{
NativeLibrary.onGamePadEvent(NativeLibrary.TouchScreenDevice, dpad.getId(i),
ButtonState.PRESSED);
}
}
setDpadState(dpad, dpadPressed[0], dpadPressed[1], dpadPressed[2], dpadPressed[3]);
}
break;
@ -671,19 +700,6 @@ public final class InputOverlay extends SurfaceView implements OnTouchListener
}
}
if (!EmulationActivity.isGameCubeGame())
{
int doubleTapButton = mPreferences.getInt("doubleTapButton",
InputOverlayPointer.DOUBLE_TAP_OPTIONS.get(InputOverlayPointer.DOUBLE_TAP_A));
if (mPreferences.getInt("wiiController", OVERLAY_WIIMOTE_NUNCHUCK) !=
InputOverlay.OVERLAY_WIIMOTE_CLASSIC &&
doubleTapButton == InputOverlayPointer.DOUBLE_TAP_CLASSIC_A)
doubleTapButton = InputOverlayPointer.DOUBLE_TAP_A;
overlayPointer = new InputOverlayPointer(this.getContext(), doubleTapButton);
}
invalidate();
}

View File

@ -19,8 +19,11 @@ public class InputOverlayPointer
public static final int DOUBLE_TAP_CLASSIC_A = 3;
private final float[] axes = {0f, 0f};
private float maxHeight;
private float maxWidth;
private float aspectAdjusted;
private boolean xAdjusted;
private boolean doubleTap = false;
private int doubleTapButton;
private int trackId = -1;
@ -41,8 +44,33 @@ public class InputOverlayPointer
DisplayMetrics outMetrics = new DisplayMetrics();
display.getMetrics(outMetrics);
doubleTapButton = button;
maxWidth = outMetrics.widthPixels / 2;
maxHeight = outMetrics.heightPixels / 2;
Integer y = outMetrics.heightPixels;
Integer x = outMetrics.widthPixels;
// Adjusting for device's black bars.
Float deviceAR = (float) x / y;
Float gameAR = NativeLibrary.GetGameAspectRatio();
aspectAdjusted = gameAR / deviceAR;
if (gameAR < deviceAR) // Black bars on left/right
{
xAdjusted = true;
Integer gameX = Math.round((float) y * gameAR);
Integer buffer = (x - gameX);
maxWidth = (float) (x - buffer) / 2;
maxHeight = (float) y / 2;
}
else // Bars on top/bottom
{
xAdjusted = false;
Integer gameY = Math.round((float) x * gameAR);
Integer buffer = (y - gameY);
maxWidth = (float) x / 2;
maxHeight = (float) (y - buffer) / 2;
}
}
public boolean onTouch(MotionEvent event)
@ -68,9 +96,16 @@ public class InputOverlayPointer
int x = (int) event.getX(event.findPointerIndex(trackId));
int y = (int) event.getY(event.findPointerIndex(trackId));
axes[0] = (y - maxHeight) / maxHeight;
axes[1] = (x - maxWidth) / maxWidth;
if (xAdjusted)
{
axes[0] = (y - maxHeight) / maxHeight;
axes[1] = ((x * aspectAdjusted) - maxWidth) / maxWidth;
}
else
{
axes[0] = ((y * aspectAdjusted) - maxHeight) / maxHeight;
axes[1] = (x - maxWidth) / maxWidth;
}
return false;
}