Skip to content

Commit

Permalink
Working on FULLSCREEN behind. Reintroduced selectors. Cleaned up quic…
Browse files Browse the repository at this point in the history
…k returning
  • Loading branch information
jfeinstein10 committed Jan 3, 2013
1 parent 9b7d10a commit 6ca0c5e
Show file tree
Hide file tree
Showing 5 changed files with 209 additions and 137 deletions.
8 changes: 6 additions & 2 deletions library/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,14 @@
<enum name="margin" value="0" />
<enum name="fullscreen" value="1" />
</attr>
<attr name="touchModeBehind">
<enum name="margin" value="0" />
<enum name="fullscreen" value="1" />
</attr>
<attr name="shadowDrawable" format="reference" />
<attr name="shadowWidth" format="dimension" />
<attr name="behindFadeEnabled" format="boolean" />
<attr name="behindFadeDegree" format="float" />
<attr name="fadeEnabled" format="boolean" />
<attr name="fadeDegree" format="float" />
<attr name="selectorEnabled" format="boolean" />
<attr name="selectorDrawable" format="reference" />
</declare-styleable>
Expand Down
154 changes: 57 additions & 97 deletions library/src/com/slidingmenu/lib/CustomViewAbove.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
public class CustomViewAbove extends ViewGroup {

private static final String TAG = "CustomViewAbove";
private static final boolean DEBUG = false;
private static final boolean DEBUG = true;

private static final boolean USE_CACHE = false;

Expand Down Expand Up @@ -86,7 +86,7 @@ public float getInterpolation(float t) {
protected int mMaximumVelocity;
private int mFlingDistance;

private CustomViewBehind mCustomViewBehind;
private CustomViewBehind mViewBehind;
// private int mMode;
private boolean mEnabled = true;

Expand Down Expand Up @@ -154,10 +154,6 @@ public CustomViewAbove(Context context) {
}

public CustomViewAbove(Context context, AttributeSet attrs) {
this(context, attrs, true);
}

public CustomViewAbove(Context context, AttributeSet attrs, boolean isAbove) {
super(context, attrs);
initCustomViewAbove();
}
Expand All @@ -174,14 +170,14 @@ void initCustomViewAbove() {
mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
setInternalPageChangeListener(new SimpleOnPageChangeListener() {
public void onPageSelected(int position) {
if (mCustomViewBehind != null) {
if (mViewBehind != null) {
switch (position) {
case 0:
case 2:
mCustomViewBehind.setChildrenEnabled(true);
mViewBehind.setChildrenEnabled(true);
break;
case 1:
mCustomViewBehind.setChildrenEnabled(false);
mViewBehind.setChildrenEnabled(false);
break;
}
}
Expand Down Expand Up @@ -227,7 +223,7 @@ void setCurrentItemInternal(int item, boolean smoothScroll, boolean always, int
return;
}

item = mCustomViewBehind.getMenuPage(item);
item = mViewBehind.getMenuPage(item);

final boolean dispatchSelected = mCurItem != item;
mCurItem = item;
Expand Down Expand Up @@ -312,19 +308,19 @@ public int getDestScrollX(int page) {
switch (page) {
case 0:
case 2:
return mCustomViewBehind.getMenuLeft(mContent, page);
return mViewBehind.getMenuLeft(mContent, page);
case 1:
return mContent.getLeft();
}
return 0;
}

private int getLeftBound() {
return mCustomViewBehind.getAbsLeftBound(mContent);
return mViewBehind.getAbsLeftBound(mContent);
}

private int getRightBound() {
return mCustomViewBehind.getAbsRightBound(mContent);
return mViewBehind.getAbsRightBound(mContent);
}

public int getContentLeft() {
Expand All @@ -345,10 +341,10 @@ private boolean isInIgnoredView(MotionEvent ev) {
}

public int getBehindWidth() {
if (mCustomViewBehind == null) {
if (mViewBehind == null) {
return 0;
} else {
return mCustomViewBehind.getBehindWidth();
return mViewBehind.getBehindWidth();
}
}

Expand Down Expand Up @@ -446,7 +442,7 @@ public View getContent() {
}

public void setCustomViewBehind(CustomViewBehind cvb) {
mCustomViewBehind = cvb;
mViewBehind = cvb;
}

@Override
Expand Down Expand Up @@ -477,7 +473,7 @@ protected void onSizeChanged(int w, int h, int oldw, int oldh) {
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
final int width = r - l;
final int height = b - t;
final int height = b - t;
mContent.layout(0, 0, width, height);
}

Expand Down Expand Up @@ -580,15 +576,15 @@ public int getTouchMode() {
private boolean thisTouchAllowed(MotionEvent ev) {
int x = (int) (ev.getX() + mScrollX);
if (isMenuOpen()) {
return mCustomViewBehind.menuOpenTouchAllowed(mContent, mCurItem, x);
return mViewBehind.menuOpenTouchAllowed(mContent, mCurItem, x);
} else {
switch (mTouchMode) {
case SlidingMenu.TOUCHMODE_FULLSCREEN:
return !isInIgnoredView(ev);
case SlidingMenu.TOUCHMODE_NONE:
return false;
case SlidingMenu.TOUCHMODE_MARGIN:
return mCustomViewBehind.marginTouchAllowed(mContent, x);
return mViewBehind.marginTouchAllowed(mContent, x);
}
}
return false;
Expand All @@ -597,9 +593,9 @@ private boolean thisTouchAllowed(MotionEvent ev) {
private boolean thisSlideAllowed(float dx) {
boolean allowed = false;
if (isMenuOpen()) {
allowed = mCustomViewBehind.menuOpenSlideAllowed(dx);
allowed = mViewBehind.menuOpenSlideAllowed(dx);
} else {
allowed = mCustomViewBehind.menuClosedSlideAllowed(dx);
allowed = mViewBehind.menuClosedSlideAllowed(dx);
}
if (DEBUG)
Log.v(TAG, "this slide allowed " + allowed + " dx: " + dx);
Expand All @@ -613,6 +609,8 @@ private int getPointerIndex(MotionEvent ev, int id) {
return activePointerIndex;
}

private boolean mQuickReturn = false;

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {

Expand All @@ -635,19 +633,16 @@ public boolean onInterceptTouchEvent(MotionEvent ev) {
final int activePointerId = mActivePointerId;
if (activePointerId == INVALID_POINTER)
break;

final int pointerIndex = this.getPointerIndex(ev, activePointerId);
if (mActivePointerId == INVALID_POINTER)
break;
final float x = MotionEventCompat.getX(ev, pointerIndex);
final float dx = x - mLastMotionX;
final float xDiff = Math.abs(dx);
final float y = MotionEventCompat.getY(ev, pointerIndex);
final float yDiff = Math.abs(y - mLastMotionY);
if (DEBUG) Log.v(TAG, "onInterceptTouch moved to:(" + x + ", " + y + "), diff:(" + xDiff + ", " + yDiff + "), mLastMotionX:" + mLastMotionX);
// if (DEBUG) Log.v(TAG, "onInterceptTouch moved to:(" + x + ", " + y + "), diff:(" + xDiff + ", " + yDiff + "), mLastMotionX:" + mLastMotionX);
if (xDiff > mTouchSlop && xDiff > yDiff && thisSlideAllowed(dx)) {
if (DEBUG) Log.v(TAG, "Starting drag! from onInterceptTouch");
mIsBeingDragged = true;
startDrag();
mLastMotionX = x;
setScrollingCacheEnabled(true);
} else if (yDiff > mTouchSlop) {
Expand All @@ -663,8 +658,9 @@ public boolean onInterceptTouchEvent(MotionEvent ev) {
if (thisTouchAllowed(ev)) {
mIsBeingDragged = false;
mIsUnableToDrag = false;
if (isMenuOpen())
return true;
if (isMenuOpen() && mViewBehind.menuTouchInQuickReturn(mContent, mCurItem, ev.getX() + mScrollX)) {
mQuickReturn = true;
}
} else {
mIsUnableToDrag = true;
}
Expand All @@ -680,8 +676,7 @@ public boolean onInterceptTouchEvent(MotionEvent ev) {
}
mVelocityTracker.addMovement(ev);
}

return mIsBeingDragged;
return mIsBeingDragged || mQuickReturn;
}


Expand All @@ -691,7 +686,10 @@ public boolean onTouchEvent(MotionEvent ev) {
if (!mEnabled)
return false;

if (!mIsBeingDragged && !thisTouchAllowed(ev))
// if (!mIsBeingDragged && !thisTouchAllowed(ev))
// return false;

if (!mIsBeingDragged && !mQuickReturn)
return false;

final int action = ev.getAction();
Expand All @@ -714,22 +712,24 @@ public boolean onTouchEvent(MotionEvent ev) {
mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
break;
case MotionEvent.ACTION_MOVE:
if (!mIsBeingDragged) {
final int pointerIndex = getPointerIndex(ev, mActivePointerId);
if (mActivePointerId == INVALID_POINTER) {
if (!mIsBeingDragged) {
if (mActivePointerId == INVALID_POINTER)
break;
}
final int pointerIndex = getPointerIndex(ev, mActivePointerId);
final float x = MotionEventCompat.getX(ev, pointerIndex);
final float dx = x - mLastMotionX;
final float xDiff = Math.abs(dx);
final float y = MotionEventCompat.getY(ev, pointerIndex);
final float yDiff = Math.abs(y - mLastMotionY);
if (DEBUG) Log.v(TAG, "onTouch moved to:(" + x + ", " + y + "), diff:(" + xDiff + ", " + yDiff + "), mLastMotionX:" + mLastMotionX);
if (xDiff > mTouchSlop && xDiff > yDiff && thisSlideAllowed(dx)) {
// if (DEBUG) Log.v(TAG, "onTouch moved to:(" + x + ", " + y + "), diff:(" + xDiff + ", " + yDiff + ")\nmIsBeingDragged:" + mIsBeingDragged + ", mLastMotionX:" + mLastMotionX);
if ((xDiff > mTouchSlop || mQuickReturn) && xDiff > yDiff && thisSlideAllowed(dx)) {
if (DEBUG) Log.v(TAG, "Starting drag! from onTouch");
mIsBeingDragged = true;
startDrag();
mLastMotionX = x;
setScrollingCacheEnabled(true);
} else {
if (DEBUG) Log.v(TAG, "onTouch returning false");
return false;
}
}
if (mIsBeingDragged) {
Expand Down Expand Up @@ -763,8 +763,8 @@ public boolean onTouchEvent(MotionEvent ev) {
int initialVelocity = (int) VelocityTrackerCompat.getXVelocity(
velocityTracker, mActivePointerId);
final int scrollX = getScrollX();
// final int widthWithMargin = getWidth();
// final float pageOffset = (float) (scrollX % widthWithMargin) / widthWithMargin;
// final int widthWithMargin = getWidth();
// final float pageOffset = (float) (scrollX % widthWithMargin) / widthWithMargin;
// TODO test this. should get better flinging behavior
final float pageOffset = (float) (scrollX - getDestScrollX(mCurItem)) / getBehindWidth();
final int activePointerIndex = getPointerIndex(ev, mActivePointerId);
Expand All @@ -778,9 +778,10 @@ public boolean onTouchEvent(MotionEvent ev) {
}
mActivePointerId = INVALID_POINTER;
endDrag();
} else if (isMenuOpen()) {
} else if (mQuickReturn && mViewBehind.menuTouchInQuickReturn(mContent, mCurItem, ev.getX() + mScrollX)) {
// close the menu
setCurrentItem(1);
endDrag();
}
break;
case MotionEvent.ACTION_CANCEL:
Expand Down Expand Up @@ -813,9 +814,9 @@ public void scrollTo(int x, int y) {
super.scrollTo(x, y);
mScrollX = x;
if (mEnabled)
mCustomViewBehind.scrollBehindTo(mContent, x, y);
// if (mSelectorDrawable != null)
// invalidate();
mViewBehind.scrollBehindTo(mContent, x, y);
// if (mSelectorDrawable != null)
// invalidate();
}

private int determineTargetPage(float pageOffset, int velocity, int deltaX) {
Expand All @@ -833,68 +834,21 @@ private int determineTargetPage(float pageOffset, int velocity, int deltaX) {
}

protected float getPercentOpen() {
return Math.abs(mScrollX) / getBehindWidth();
if (DEBUG) Log.v(TAG, ""+ Math.abs(mScrollX-mContent.getLeft()) / getBehindWidth());
return Math.abs(mScrollX-mContent.getLeft()) / getBehindWidth();
}

@Override
protected void dispatchDraw(Canvas canvas) {
super.dispatchDraw(canvas);
// Draw the margin drawable if needed.
mCustomViewBehind.drawShadow(mContent, canvas);

mCustomViewBehind.drawFade(mContent, canvas, getPercentOpen());
// if (mSelectorEnabled)
// onDrawMenuSelector(canvas, getPercentOpen());
mViewBehind.drawShadow(mContent, canvas);
mViewBehind.drawFade(mContent, canvas, getPercentOpen());
mViewBehind.drawSelector(mContent, canvas, getPercentOpen());
}

// variables for drawing
private float mScrollX = 0.0f;
// for the indicator
private boolean mSelectorEnabled = true;
private Bitmap mSelectorDrawable;
private View mSelectedView;

private void onDrawMenuSelector(Canvas canvas, float openPercent) {
if (mSelectorDrawable != null && mSelectedView != null) {
String tag = (String) mSelectedView.getTag(R.id.selected_view);
if (tag.equals(TAG+"SelectedView")) {
int right = getContentLeft();
int left = (int) (right - mSelectorDrawable.getWidth() * openPercent);

canvas.save();
canvas.clipRect(left, 0, right, getHeight());
canvas.drawBitmap(mSelectorDrawable, left, getSelectedTop(), null);
canvas.restore();
}
}
}

public void setSelectorEnabled(boolean b) {
mSelectorEnabled = b;
}

public void setSelectedView(View v) {
if (mSelectedView != null) {
mSelectedView.setTag(R.id.selected_view, null);
mSelectedView = null;
}
if (v.getParent() != null) {
mSelectedView = v;
mSelectedView.setTag(R.id.selected_view, TAG+"SelectedView");
invalidate();
}
}

private int getSelectedTop() {
int y = mSelectedView.getTop();
y += (mSelectedView.getHeight() - mSelectorDrawable.getHeight()) / 2;
return y;
}

public void setSelectorBitmap(Bitmap b) {
mSelectorDrawable = b;
refreshDrawableState();
}

private void onSecondaryPointerUp(MotionEvent ev) {
if (DEBUG) Log.v(TAG, "onSecondaryPointerUp called");
Expand All @@ -912,7 +866,13 @@ private void onSecondaryPointerUp(MotionEvent ev) {
}
}

private void startDrag() {
mIsBeingDragged = true;
mQuickReturn = false;
}

private void endDrag() {
mQuickReturn = false;
mIsBeingDragged = false;
mIsUnableToDrag = false;
mActivePointerId = INVALID_POINTER;
Expand Down Expand Up @@ -1021,7 +981,7 @@ public boolean arrowScroll(int direction) {
direction);
if (nextFocused != null && nextFocused != currentFocused) {
if (direction == View.FOCUS_LEFT) {
handled = nextFocused.requestFocus();
handled = nextFocused.requestFocus();
} else if (direction == View.FOCUS_RIGHT) {
// If there is nothing to the right, or this is causing us to
// jump to the left, then what we really want to do is page right.
Expand Down
Loading

0 comments on commit 6ca0c5e

Please sign in to comment.