Refactor input event handling in the Game activity

This commit is contained in:
Cameron Gutman
2022-10-03 21:25:43 -05:00
parent 539daf5789
commit 23bc4daf9f
2 changed files with 30 additions and 8 deletions
+21 -5
View File
@@ -89,8 +89,7 @@ import java.util.Locale;
public class Game extends Activity implements SurfaceHolder.Callback, public class Game extends Activity implements SurfaceHolder.Callback,
OnGenericMotionListener, OnTouchListener, NvConnectionListener, EvdevListener, OnGenericMotionListener, OnTouchListener, NvConnectionListener, EvdevListener,
OnSystemUiVisibilityChangeListener, GameGestures, StreamView.InputCallbacks, OnSystemUiVisibilityChangeListener, GameGestures, StreamView.InputCallbacks,
PerfOverlayListener, UsbDriverService.UsbDriverStateListener PerfOverlayListener, UsbDriverService.UsbDriverStateListener, View.OnKeyListener {
{
private int lastButtonState = 0; private int lastButtonState = 0;
// Only 2 touches are supported // Only 2 touches are supported
@@ -235,6 +234,7 @@ public class Game extends Activity implements SurfaceHolder.Callback,
// Listen for non-touch events on the game surface // Listen for non-touch events on the game surface
streamView = findViewById(R.id.surfaceView); streamView = findViewById(R.id.surfaceView);
streamView.setOnGenericMotionListener(this); streamView.setOnGenericMotionListener(this);
streamView.setOnKeyListener(this);
streamView.setInputCallbacks(this); streamView.setInputCallbacks(this);
// Listen for touch events on the background touch view to enable trackpad mode // Listen for touch events on the background touch view to enable trackpad mode
@@ -256,6 +256,13 @@ public class Game extends Activity implements SurfaceHolder.Callback,
InputDevice.SOURCE_CLASS_POSITION | // Touchpads InputDevice.SOURCE_CLASS_POSITION | // Touchpads
InputDevice.SOURCE_CLASS_TRACKBALL // Mice (pointer capture) InputDevice.SOURCE_CLASS_TRACKBALL // Mice (pointer capture)
); );
backgroundTouchView.requestUnbufferedDispatch(
InputDevice.SOURCE_CLASS_BUTTON | // Keyboards
InputDevice.SOURCE_CLASS_JOYSTICK | // Gamepads
InputDevice.SOURCE_CLASS_POINTER | // Touchscreens and mice (w/o pointer capture)
InputDevice.SOURCE_CLASS_POSITION | // Touchpads
InputDevice.SOURCE_CLASS_TRACKBALL // Mice (pointer capture)
);
// Since the OS isn't going to batch for us, we have to batch mouse events to // Since the OS isn't going to batch for us, we have to batch mouse events to
// avoid triggering a bug in GeForce Experience that can lead to massive latency. // avoid triggering a bug in GeForce Experience that can lead to massive latency.
@@ -269,9 +276,6 @@ public class Game extends Activity implements SurfaceHolder.Callback,
inputCaptureProvider = InputCaptureManager.getInputCaptureProvider(this, this); inputCaptureProvider = InputCaptureManager.getInputCaptureProvider(this, this);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// The view must be focusable for pointer capture to work.
streamView.setFocusable(true);
streamView.setDefaultFocusHighlightEnabled(false);
streamView.setOnCapturedPointerListener(new View.OnCapturedPointerListener() { streamView.setOnCapturedPointerListener(new View.OnCapturedPointerListener() {
@Override @Override
public boolean onCapturedPointer(View view, MotionEvent motionEvent) { public boolean onCapturedPointer(View view, MotionEvent motionEvent) {
@@ -2189,4 +2193,16 @@ public class Game extends Activity implements SurfaceHolder.Callback,
suppressPipRefCount--; suppressPipRefCount--;
updatePipAutoEnter(); updatePipAutoEnter();
} }
@Override
public boolean onKey(View view, int keyCode, KeyEvent keyEvent) {
switch (keyEvent.getAction()) {
case KeyEvent.ACTION_DOWN:
return handleKeyDown(keyEvent);
case KeyEvent.ACTION_UP:
return handleKeyUp(keyEvent);
default:
return false;
}
}
} }
+7 -1
View File
@@ -14,7 +14,13 @@
android:id="@+id/surfaceView" android:id="@+id/surfaceView"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:layout_gravity="center" /> android:layout_gravity="center"
android:focusable="true"
android:focusableInTouchMode="true"
android:focusedByDefault="true"
android:defaultFocusHighlightEnabled="false">
<requestFocus />
</com.limelight.ui.StreamView>
<TextView <TextView
android:id="@+id/performanceOverlay" android:id="@+id/performanceOverlay"