Prevent scrolling in opposite direction of main finger direction when dealing with multiple scroll panes

This commit is contained in:
drdev
2014-05-17 16:24:30 +00:00
parent 08039be686
commit d19aa2c88a
8 changed files with 42 additions and 13 deletions

View File

@@ -424,10 +424,10 @@ public class Forge implements ApplicationListener {
}
@Override
public boolean pan(float x, float y, float deltaX, float deltaY) {
public boolean pan(float x, float y, float deltaX, float deltaY, boolean moreVertical) {
try {
for (FDisplayObject listener : potentialListeners) {
if (listener.pan(listener.screenToLocalX(x), listener.screenToLocalY(y), deltaX, deltaY)) {
if (listener.pan(listener.screenToLocalX(x), listener.screenToLocalY(y), deltaX, deltaY, moreVertical)) {
return true;
}
}
@@ -491,10 +491,10 @@ public class Forge implements ApplicationListener {
boolean handled;
if (KeyInputAdapter.isShiftKeyDown()) {
handled = pan(mouseMovedX, mouseMovedY, -Utils.AVG_FINGER_WIDTH * amount, 0);
handled = pan(mouseMovedX, mouseMovedY, -Utils.AVG_FINGER_WIDTH * amount, 0, false);
}
else {
handled = pan(mouseMovedX, mouseMovedY, 0, -Utils.AVG_FINGER_HEIGHT * amount);
handled = pan(mouseMovedX, mouseMovedY, 0, -Utils.AVG_FINGER_HEIGHT * amount, true);
}
if (panStop(mouseMovedX, mouseMovedY)) {
handled = true;

View File

@@ -226,11 +226,17 @@ public class MatchScreen extends FScreen {
@Override
public boolean zoom(float x, float y, float amount) {
y += getScrollTop();
float yRatioBefore = (y + getScrollTop()) / getScrollHeight();
extraHeight += amount;
if (extraHeight < 0) {
extraHeight = 0;
}
revalidate();
float yRatioAfter = (y + getScrollTop()) / getScrollHeight();
return true;
}
}

View File

@@ -99,7 +99,7 @@ public abstract class FDialog extends FOverlay {
}
@Override
public boolean pan(float x, float y, float deltaX, float deltaY) {
public boolean pan(float x, float y, float deltaX, float deltaY, boolean moreVertical) {
for (FDisplayObject child : FDialog.this.getChildren()) {
child.setLeft(child.getLeft() + deltaX);
child.setTop(child.getTop() + deltaY);

View File

@@ -121,7 +121,7 @@ public abstract class FDisplayObject {
return false;
}
public boolean pan(float x, float y, float deltaX, float deltaY) {
public boolean pan(float x, float y, float deltaX, float deltaY, boolean moreVertical) {
return false;
}

View File

@@ -17,7 +17,7 @@ public abstract class FGestureAdapter extends InputAdapter {
public abstract boolean release(float x, float y);
public abstract boolean tap(float x, float y, int count);
public abstract boolean fling(float velocityX, float velocityY);
public abstract boolean pan(float x, float y, float deltaX, float deltaY);
public abstract boolean pan(float x, float y, float deltaX, float deltaY, boolean moreVertical);
public abstract boolean panStop(float x, float y);
public abstract boolean zoom(float x, float y, float amount);
@@ -166,7 +166,8 @@ public abstract class FGestureAdapter extends InputAdapter {
// if we have left the tap square, we are panning
if (!inTapSquare) {
panning = true;
return pan(x, y, tracker.deltaX, tracker.deltaY);
boolean moreVertical = Math.abs(tracker.startY - y) > Math.abs(tracker.startX - x);
return pan(x, y, tracker.deltaX, tracker.deltaY, moreVertical);
}
return false;
@@ -279,6 +280,7 @@ public abstract class FGestureAdapter extends InputAdapter {
private static class VelocityTracker {
int sampleSize = 10;
float startX, startY;
float lastX, lastY;
float deltaX, deltaY;
long lastTime;
@@ -288,6 +290,8 @@ public abstract class FGestureAdapter extends InputAdapter {
long[] meanTime = new long[sampleSize];
public void start(float x, float y, long timeStamp) {
startX = x;
startY = y;
lastX = x;
lastY = y;
deltaX = 0;

View File

@@ -86,7 +86,7 @@ public abstract class FOverlay extends FContainer {
}
@Override
public boolean pan(float x, float y, float deltaX, float deltaY) {
public boolean pan(float x, float y, float deltaX, float deltaY, boolean moreVertical) {
return true;
}

View File

@@ -215,6 +215,15 @@ public abstract class FScrollPane extends FContainer {
@Override
public boolean fling(float velocityX, float velocityY) {
if (Math.abs(velocityY) > Math.abs(velocityX)) {
if (getMaxScrollTop() == 0) {
return false; //if fling is more vertical and can't scroll vertically, don't scroll at all
}
}
else if (getMaxScrollLeft() == 0) {
return false; //if fling is more horizontal and can't scroll horizontally, don't scroll at all
}
velocityX = -velocityX; //reverse velocities to account for scroll moving in opposite direction
velocityY = -velocityY;
@@ -226,7 +235,7 @@ public abstract class FScrollPane extends FContainer {
activeFlingAnimation.physicsObj.getVelocity().set(velocityX, velocityY);
activeFlingAnimation.physicsObj.setDecel(FLING_DECEL, FLING_DECEL);
}
return false; //don't prevent outer scroll panes from working
return true;
}
@Override
@@ -251,9 +260,19 @@ public abstract class FScrollPane extends FContainer {
}
@Override
public boolean pan(float x, float y, float deltaX, float deltaY) {
public boolean pan(float x, float y, float deltaX, float deltaY, boolean moreVertical) {
if (getMaxScrollTop() == 0 && (moreVertical || y < 0 || y >= getHeight() || Math.abs(deltaY) > Math.abs(deltaX))) {
//if can't scroll vertically, don't scroll at all if pan is more vertical
//or current position is above or below this scroll pane
return false;
}
if (getMaxScrollLeft() == 0 && (!moreVertical || x < 0 || x >= getWidth() || Math.abs(deltaX) > Math.abs(deltaY))) {
//if can't scroll horizontally, don't scroll at all if pan is more horizontal
//or current position is left or right of this scroll pane
return false;
}
setScrollPositions(scrollLeft - deltaX, scrollTop - deltaY);
return false; //don't prevent outer scroll panes from working
return true;
}
@Override

View File

@@ -95,7 +95,7 @@ public class FToggleSwitch extends FDisplayObject {
//support dragging finger left or right to toggle on/off
@Override
public final boolean pan(float x, float y, float deltaX, float deltaY) {
public final boolean pan(float x, float y, float deltaX, float deltaY, boolean moreVertical) {
if (contains(getLeft() + x, getTop() + y)) {
if (x < getHeight()) {
setToggled(false, true);