Navigation Menu

Skip to content

Commit

Permalink
Clean up captured pointer code to avoid logcat clutter on pre-8.0 sys…
Browse files Browse the repository at this point in the history
…tems (thanks Rachel!)
  • Loading branch information
slouken committed Jul 13, 2018
1 parent df0d3f1 commit fd8e8f9
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 60 deletions.
47 changes: 43 additions & 4 deletions android-project/app/src/main/java/org/libsdl/app/SDLActivity.java
Expand Up @@ -1382,10 +1382,6 @@ public SDLSurface(Context context) {
setOnGenericMotionListener(SDLActivity.getMotionListener());
}

if ((Build.VERSION.SDK_INT >= 26) && !SDLActivity.isDeXMode()) {
setOnCapturedPointerListener(new SDLCapturedPointerListener_API26());
}

// Some arbitrary defaults to avoid a potential division by zero
mWidth = 1.0f;
mHeight = 1.0f;
Expand Down Expand Up @@ -1740,6 +1736,49 @@ public void onSensorChanged(SensorEvent event) {
event.values[2] / SensorManager.GRAVITY_EARTH);
}
}

// Captured pointer events for API 26.
public boolean onCapturedPointerEvent(MotionEvent event)
{
int action = event.getActionMasked();

float x, y;
switch (action) {
case MotionEvent.ACTION_SCROLL:
x = event.getAxisValue(MotionEvent.AXIS_HSCROLL, 0);
y = event.getAxisValue(MotionEvent.AXIS_VSCROLL, 0);
SDLActivity.onNativeMouse(0, action, x, y, false);
return true;

case MotionEvent.ACTION_HOVER_MOVE:
case MotionEvent.ACTION_MOVE:
x = event.getX(0);
y = event.getY(0);
SDLActivity.onNativeMouse(0, action, x, y, true);
return true;

case MotionEvent.ACTION_BUTTON_PRESS:
case MotionEvent.ACTION_BUTTON_RELEASE:

// Change our action value to what SDL's code expects.
if (action == MotionEvent.ACTION_BUTTON_PRESS) {
action = MotionEvent.ACTION_DOWN;
}
else if (action == MotionEvent.ACTION_BUTTON_RELEASE) {
action = MotionEvent.ACTION_UP;
}

x = event.getX(0);
y = event.getY(0);
int button = event.getButtonState();

SDLActivity.onNativeMouse(button, action, x, y, true);
return true;
}

return false;
}

}

/* This is a fake invisible editor view that receives the input and defines the
Expand Down
Expand Up @@ -749,7 +749,7 @@ public boolean onGenericMotion(View v, MotionEvent event) {

@Override
public boolean supportsRelativeMouse() {
return true;
return !SDLActivity.isDeXMode();
}

@Override
Expand All @@ -759,20 +759,26 @@ public boolean inRelativeMode() {

@Override
public boolean setRelativeMouseEnabled(boolean enabled) {
if (enabled) {
SDLActivity.getContentView().requestPointerCapture();
if (!SDLActivity.isDeXMode()) {
if (enabled) {
SDLActivity.getContentView().requestPointerCapture();
}
else {
SDLActivity.getContentView().releasePointerCapture();
}
mRelativeModeEnabled = enabled;
return true;
}
else {
SDLActivity.getContentView().releasePointerCapture();
else
{
return false;
}
mRelativeModeEnabled = enabled;
return true;
}

@Override
public void reclaimRelativeMouseModeIfNeeded()
{
if (mRelativeModeEnabled) {
if (mRelativeModeEnabled && !SDLActivity.isDeXMode()) {
SDLActivity.getContentView().requestPointerCapture();
}
}
Expand All @@ -788,51 +794,4 @@ public float getEventY(MotionEvent event) {
// Relative mouse in capture mode will only have relative for X/Y
return event.getY(0);
}
}

class SDLCapturedPointerListener_API26 implements View.OnCapturedPointerListener
{
@Override
public boolean onCapturedPointer(View view, MotionEvent event)
{
int action = event.getActionMasked();

float x, y;
switch (action) {
case MotionEvent.ACTION_SCROLL:
x = event.getAxisValue(MotionEvent.AXIS_HSCROLL, 0);
y = event.getAxisValue(MotionEvent.AXIS_VSCROLL, 0);
SDLActivity.onNativeMouse(0, action, x, y, false);
return true;

case MotionEvent.ACTION_HOVER_MOVE:
case MotionEvent.ACTION_MOVE:
x = event.getX(0);
y = event.getY(0);
SDLActivity.onNativeMouse(0, action, x, y, true);
return true;

case MotionEvent.ACTION_BUTTON_PRESS:
case MotionEvent.ACTION_BUTTON_RELEASE:

// Change our action value to what SDL's code expects.
if (action == MotionEvent.ACTION_BUTTON_PRESS) {
action = MotionEvent.ACTION_DOWN;
}
else if (action == MotionEvent.ACTION_BUTTON_RELEASE) {
action = MotionEvent.ACTION_UP;
}

x = event.getX(0);
y = event.getY(0);
int button = event.getButtonState();

SDLActivity.onNativeMouse(button, action, x, y, true);
return true;

}

return false;
}
}

}

0 comments on commit fd8e8f9

Please sign in to comment.