mirror of
https://github.com/AngelAuraMC/Amethyst-Android.git
synced 2025-09-17 16:47:14 -04:00
Refactor Touchpad.java
This commit is contained in:
parent
9cdb43c162
commit
ee279209f6
@ -27,19 +27,19 @@ import org.lwjgl.glfw.CallbackBridge;
|
|||||||
*/
|
*/
|
||||||
public class Touchpad extends FrameLayout {
|
public class Touchpad extends FrameLayout {
|
||||||
/* Whether the Touchpad should be displayed */
|
/* Whether the Touchpad should be displayed */
|
||||||
private boolean displayState;
|
private boolean mDisplayState;
|
||||||
/* Mouse pointer icon used by the touchpad */
|
/* Mouse pointer icon used by the touchpad */
|
||||||
private final ImageView mousePointer = new ImageView(getContext());
|
private final ImageView mMousePointerImageView = new ImageView(getContext());
|
||||||
/* Detect a classic android Tap */
|
/* Detect a classic android Tap */
|
||||||
private final GestureDetector singleTapDetector = new GestureDetector(getContext(), new SingleTapConfirm());
|
private final GestureDetector mSingleTapDetector = new GestureDetector(getContext(), new SingleTapConfirm());
|
||||||
/* Resolution scaler option, allow downsizing a window */
|
/* Resolution scaler option, allow downsizing a window */
|
||||||
private final float scaleFactor = DEFAULT_PREF.getInt("resolutionRatio",100)/100f;
|
private final float mScaleFactor = DEFAULT_PREF.getInt("resolutionRatio",100)/100f;
|
||||||
/* Current pointer ID to move the mouse */
|
/* Current pointer ID to move the mouse */
|
||||||
private int currentPointerID = -1000;
|
private int mCurrentPointerID = -1000;
|
||||||
/* Previous MotionEvent position, not scale */
|
/* Previous MotionEvent position, not scale */
|
||||||
private float prevX, prevY;
|
private float mPrevX, mPrevY;
|
||||||
/* Last first pointer positions non-scaled, used to scroll distance */
|
/* Last first pointer positions non-scaled, used to scroll distance */
|
||||||
private float scrollLastInitialX, scrollLastInitialY;
|
private float mScrollLastInitialX, mScrollLastInitialY;
|
||||||
|
|
||||||
public Touchpad(@NonNull Context context) {
|
public Touchpad(@NonNull Context context) {
|
||||||
this(context, null);
|
this(context, null);
|
||||||
@ -50,56 +50,6 @@ public class Touchpad extends FrameLayout {
|
|||||||
init();
|
init();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void init(){
|
|
||||||
// Setup mouse pointer
|
|
||||||
mousePointer.setImageDrawable(ResourcesCompat.getDrawable(getResources(), R.drawable.mouse_pointer, getContext().getTheme()));
|
|
||||||
mousePointer.post(() -> {
|
|
||||||
ViewGroup.LayoutParams params = mousePointer.getLayoutParams();
|
|
||||||
params.width = (int) (36 / 100f * LauncherPreferences.PREF_MOUSESCALE);
|
|
||||||
params.height = (int) (54 / 100f * LauncherPreferences.PREF_MOUSESCALE);
|
|
||||||
});
|
|
||||||
addView(mousePointer);
|
|
||||||
setFocusable(false);
|
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
|
||||||
setDefaultFocusHighlightEnabled(false);
|
|
||||||
}
|
|
||||||
|
|
||||||
// When the game is grabbing, we should not display the mouse
|
|
||||||
disable();
|
|
||||||
displayState = false;
|
|
||||||
Thread virtualMouseGrabThread = new Thread(() -> {
|
|
||||||
while (true) {
|
|
||||||
if (!CallbackBridge.isGrabbing() && displayState && getVisibility() != VISIBLE) {
|
|
||||||
post(this::enable);
|
|
||||||
}else{
|
|
||||||
if ((CallbackBridge.isGrabbing() && getVisibility() != View.GONE) || !displayState && getVisibility() == VISIBLE) {
|
|
||||||
post(this::disable);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}, "VirtualMouseGrabThread");
|
|
||||||
virtualMouseGrabThread.setPriority(Thread.MIN_PRIORITY);
|
|
||||||
virtualMouseGrabThread.start();
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Enable the touchpad */
|
|
||||||
public void enable(){
|
|
||||||
setVisibility(VISIBLE);
|
|
||||||
placeMouseAt(currentDisplayMetrics.widthPixels / 2, currentDisplayMetrics.heightPixels / 2);
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Disable the touchpad and hides the mouse */
|
|
||||||
public void disable(){
|
|
||||||
setVisibility(GONE);
|
|
||||||
}
|
|
||||||
|
|
||||||
/** @return The new state, enabled or disabled */
|
|
||||||
public boolean switchState(){
|
|
||||||
displayState = !displayState;
|
|
||||||
return displayState;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onTouchEvent(MotionEvent event) {
|
public boolean onTouchEvent(MotionEvent event) {
|
||||||
// MotionEvent reports input details from the touch screen
|
// MotionEvent reports input details from the touch screen
|
||||||
@ -114,77 +64,126 @@ public class Touchpad extends FrameLayout {
|
|||||||
|
|
||||||
float x = event.getX();
|
float x = event.getX();
|
||||||
float y = event.getY();
|
float y = event.getY();
|
||||||
float mouseX = mousePointer.getX();
|
float mouseX = mMousePointerImageView.getX();
|
||||||
float mouseY = mousePointer.getY();
|
float mouseY = mMousePointerImageView.getY();
|
||||||
|
|
||||||
if (singleTapDetector.onTouchEvent(event)) {
|
if (mSingleTapDetector.onTouchEvent(event)) {
|
||||||
CallbackBridge.mouseX = (mouseX * scaleFactor);
|
CallbackBridge.mouseX = (mouseX * mScaleFactor);
|
||||||
CallbackBridge.mouseY = (mouseY * scaleFactor);
|
CallbackBridge.mouseY = (mouseY * mScaleFactor);
|
||||||
CallbackBridge.sendCursorPos(CallbackBridge.mouseX, CallbackBridge.mouseY);
|
CallbackBridge.sendCursorPos(CallbackBridge.mouseX, CallbackBridge.mouseY);
|
||||||
CallbackBridge.sendMouseKeycode(LWJGLGLFWKeycode.GLFW_MOUSE_BUTTON_LEFT);
|
CallbackBridge.sendMouseKeycode(LwjglGlfwKeycode.GLFW_MOUSE_BUTTON_LEFT);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (action) {
|
switch (action) {
|
||||||
case MotionEvent.ACTION_POINTER_DOWN: // 5
|
case MotionEvent.ACTION_POINTER_DOWN: // 5
|
||||||
scrollLastInitialX = event.getX();
|
mScrollLastInitialX = event.getX();
|
||||||
scrollLastInitialY = event.getY();
|
mScrollLastInitialY = event.getY();
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case MotionEvent.ACTION_DOWN:
|
case MotionEvent.ACTION_DOWN:
|
||||||
prevX = x;
|
mPrevX = x;
|
||||||
prevY = y;
|
mPrevY = y;
|
||||||
currentPointerID = event.getPointerId(0);
|
mCurrentPointerID = event.getPointerId(0);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case MotionEvent.ACTION_MOVE: // 2
|
case MotionEvent.ACTION_MOVE: // 2
|
||||||
//Scrolling feature
|
//Scrolling feature
|
||||||
if (!LauncherPreferences.PREF_DISABLE_GESTURES && !CallbackBridge.isGrabbing() && event.getPointerCount() >= 2) {
|
if (!LauncherPreferences.PREF_DISABLE_GESTURES && !CallbackBridge.isGrabbing() && event.getPointerCount() >= 2) {
|
||||||
int hScroll = ((int) (event.getX() - scrollLastInitialX)) / FINGER_SCROLL_THRESHOLD;
|
int hScroll = ((int) (event.getX() - mScrollLastInitialX)) / FINGER_SCROLL_THRESHOLD;
|
||||||
int vScroll = ((int) (event.getY() - scrollLastInitialY)) / FINGER_SCROLL_THRESHOLD;
|
int vScroll = ((int) (event.getY() - mScrollLastInitialY)) / FINGER_SCROLL_THRESHOLD;
|
||||||
|
|
||||||
if(vScroll != 0 || hScroll != 0){
|
if(vScroll != 0 || hScroll != 0){
|
||||||
CallbackBridge.sendScroll(hScroll, vScroll);
|
CallbackBridge.sendScroll(hScroll, vScroll);
|
||||||
scrollLastInitialX = event.getX();
|
mScrollLastInitialX = event.getX();
|
||||||
scrollLastInitialY = event.getY();
|
mScrollLastInitialY = event.getY();
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Mouse movement
|
// Mouse movement
|
||||||
if(currentPointerID == event.getPointerId(0)) {
|
if(mCurrentPointerID == event.getPointerId(0)) {
|
||||||
mouseX = Math.max(0, Math.min(currentDisplayMetrics.widthPixels, mouseX + (x - prevX) * LauncherPreferences.PREF_MOUSESPEED));
|
mouseX = Math.max(0, Math.min(currentDisplayMetrics.widthPixels, mouseX + (x - mPrevX) * LauncherPreferences.PREF_MOUSESPEED));
|
||||||
mouseY = Math.max(0, Math.min(currentDisplayMetrics.heightPixels, mouseY + (y - prevY) * LauncherPreferences.PREF_MOUSESPEED));
|
mouseY = Math.max(0, Math.min(currentDisplayMetrics.heightPixels, mouseY + (y - mPrevY) * LauncherPreferences.PREF_MOUSESPEED));
|
||||||
|
|
||||||
placeMouseAt(mouseX, mouseY);
|
placeMouseAt(mouseX, mouseY);
|
||||||
CallbackBridge.sendCursorPos(CallbackBridge.mouseX, CallbackBridge.mouseY);
|
CallbackBridge.sendCursorPos(CallbackBridge.mouseX, CallbackBridge.mouseY);
|
||||||
}else currentPointerID = event.getPointerId(0);
|
}else mCurrentPointerID = event.getPointerId(0);
|
||||||
|
|
||||||
prevX = x;
|
mPrevX = x;
|
||||||
prevY = y;
|
mPrevY = y;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case MotionEvent.ACTION_UP:
|
case MotionEvent.ACTION_UP:
|
||||||
prevX = x;
|
mPrevX = x;
|
||||||
prevY = y;
|
mPrevY = y;
|
||||||
currentPointerID = -1000;
|
mCurrentPointerID = -1000;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//debugText.setText(CallbackBridge.DEBUG_STRING.toString());
|
//debugText.setText(CallbackBridge.DEBUG_STRING.toString());
|
||||||
CallbackBridge.DEBUG_STRING.setLength(0);
|
CallbackBridge.DEBUG_STRING.setLength(0);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Enable the touchpad */
|
||||||
|
public void enable(){
|
||||||
|
setVisibility(VISIBLE);
|
||||||
|
placeMouseAt(currentDisplayMetrics.widthPixels / 2, currentDisplayMetrics.heightPixels / 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Disable the touchpad and hides the mouse */
|
||||||
|
public void disable(){
|
||||||
|
setVisibility(GONE);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @return The new state, enabled or disabled */
|
||||||
|
public boolean switchState(){
|
||||||
|
mDisplayState = !mDisplayState;
|
||||||
|
return mDisplayState;
|
||||||
|
}
|
||||||
|
|
||||||
public void placeMouseAt(float x, float y) {
|
public void placeMouseAt(float x, float y) {
|
||||||
mousePointer.setX(x);
|
mMousePointerImageView.setX(x);
|
||||||
mousePointer.setY(y);
|
mMousePointerImageView.setY(y);
|
||||||
CallbackBridge.mouseX = (x * scaleFactor);
|
CallbackBridge.mouseX = (x * mScaleFactor);
|
||||||
CallbackBridge.mouseY = (y * scaleFactor);
|
CallbackBridge.mouseY = (y * mScaleFactor);
|
||||||
CallbackBridge.sendCursorPos(CallbackBridge.mouseX, CallbackBridge.mouseY);
|
CallbackBridge.sendCursorPos(CallbackBridge.mouseX, CallbackBridge.mouseY);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void init(){
|
||||||
|
// Setup mouse pointer
|
||||||
|
mMousePointerImageView.setImageDrawable(ResourcesCompat.getDrawable(getResources(), R.drawable.mouse_pointer, getContext().getTheme()));
|
||||||
|
mMousePointerImageView.post(() -> {
|
||||||
|
ViewGroup.LayoutParams params = mMousePointerImageView.getLayoutParams();
|
||||||
|
params.width = (int) (36 / 100f * LauncherPreferences.PREF_MOUSESCALE);
|
||||||
|
params.height = (int) (54 / 100f * LauncherPreferences.PREF_MOUSESCALE);
|
||||||
|
});
|
||||||
|
addView(mMousePointerImageView);
|
||||||
|
setFocusable(false);
|
||||||
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||||
|
setDefaultFocusHighlightEnabled(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
// When the game is grabbing, we should not display the mouse
|
||||||
|
disable();
|
||||||
|
mDisplayState = false;
|
||||||
|
Thread virtualMouseGrabThread = new Thread(() -> {
|
||||||
|
while (true) {
|
||||||
|
if (!CallbackBridge.isGrabbing() && mDisplayState && getVisibility() != VISIBLE) {
|
||||||
|
post(this::enable);
|
||||||
|
}else{
|
||||||
|
if ((CallbackBridge.isGrabbing() && getVisibility() != View.GONE) || !mDisplayState && getVisibility() == VISIBLE) {
|
||||||
|
post(this::disable);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}, "VirtualMouseGrabThread");
|
||||||
|
virtualMouseGrabThread.setPriority(Thread.MIN_PRIORITY);
|
||||||
|
virtualMouseGrabThread.start();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user