Skip to content

Commit

Permalink
Fix[input]: fix the remaining bugs
Browse files Browse the repository at this point in the history
- Fixed scroll when the user scrolls too slowly
- Inlined mouseX and mouseY assignments
  • Loading branch information
artdeell committed Apr 9, 2024
1 parent 2141106 commit a85af24
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,11 @@ public boolean onTouchEvent(MotionEvent event) {
else mDropGesture.submit();
// Determine the hotbar slot
float x = event.getX();
if(x < 0 || x > mWidth) return true;
if(x < 0 || x > mWidth) {
// If out of bounds, cancel the hotbar gesture to avoid dropping items on last hotbar slots
mDropGesture.cancel();
return true;
}
int hotbarIndex = (int)MathUtils.map(x, 0, mWidth, 0, HOTBAR_KEYS.length);
// Check if the slot changed and we need to make a key press
if(hotbarIndex == mLastIndex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class InGUIEventProcessor implements TouchEventProcessor {
private AbstractTouchpad mTouchpad;
private boolean mIsMouseDown = false;
private final float mScaleFactor;
private float mScrollOvershootH, mScrollOvershootV;
public static final float FINGER_SCROLL_THRESHOLD = Tools.dpToPx(6);
public InGUIEventProcessor(float scaleFactor) {
mSingleTapDetector = new GestureDetector(null, new SingleTapConfirm());
Expand Down Expand Up @@ -43,14 +44,10 @@ public boolean processTouchEvent(MotionEvent motionEvent) {
sendTouchCoordinates(mainPointerX, mainPointerY);
if(!mIsMouseDown) enableMouse();
}
} else {
float[] motionVector = mTracker.getMotionVector();
int hScroll = (int)(motionVector[0] / FINGER_SCROLL_THRESHOLD);
int vScroll = (int)(motionVector[1] / FINGER_SCROLL_THRESHOLD);
if(hScroll != 0 | vScroll != 0) CallbackBridge.sendScroll(hScroll, vScroll);
}
} else performScroll();
break;
case MotionEvent.ACTION_UP:
resetScrollOvershoot();
mTracker.cancelTracking();
if(mIsMouseDown) disableMouse();
}
Expand All @@ -66,10 +63,22 @@ public void setAbstractTouchpad(AbstractTouchpad touchpad) {
mTouchpad = touchpad;
}

private void performScroll() {
float[] motionVector = mTracker.getMotionVector();
float hScroll = (motionVector[0] / FINGER_SCROLL_THRESHOLD) + mScrollOvershootH;
float vScroll = (motionVector[1] / FINGER_SCROLL_THRESHOLD) + mScrollOvershootV;
int hScrollRound = (int) hScroll, vScrollRound = (int) vScroll;
if(hScrollRound != 0 || vScrollRound != 0) CallbackBridge.sendScroll(hScroll, vScroll);
mScrollOvershootH = hScroll - hScrollRound;
mScrollOvershootV = vScroll - vScrollRound;
}

private void resetScrollOvershoot() {
mScrollOvershootH = mScrollOvershootV = 0f;
}

private void sendTouchCoordinates(float x, float y) {
CallbackBridge.mouseX = x * mScaleFactor;
CallbackBridge.mouseY = y * mScaleFactor;
CallbackBridge.sendCursorPos(CallbackBridge.mouseX, CallbackBridge.mouseY);
CallbackBridge.sendCursorPos( x * mScaleFactor, y * mScaleFactor);
}

private void enableMouse() {
Expand All @@ -89,6 +98,7 @@ private void clickMouse() {

@Override
public void cancelPendingActions() {
resetScrollOvershoot();
disableMouse();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,7 @@ public void placeMouseAt(float x, float y) {
}

private void sendMousePosition() {
CallbackBridge.mouseX = (mMouseX * mScaleFactor);
CallbackBridge.mouseY = (mMouseY * mScaleFactor);
CallbackBridge.sendCursorPos(CallbackBridge.mouseX, CallbackBridge.mouseY);
CallbackBridge.sendCursorPos((mMouseX * mScaleFactor), (mMouseY * mScaleFactor));
}

private void updateMousePosition() {
Expand Down

0 comments on commit a85af24

Please sign in to comment.