This repository has been archived by the owner on Jul 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 217
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Honeycomb button clipping * Forward event if not inside
- Loading branch information
1 parent
91e3eaa
commit 237441f
Showing
5 changed files
with
424 additions
and
27 deletions.
There are no files selected for viewing
131 changes: 131 additions & 0 deletions
131
app/src/common/shared/org/mozilla/vrbrowser/ui/views/ClippedEventDelegate.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
package org.mozilla.vrbrowser.ui.views; | ||
|
||
import android.graphics.Path; | ||
import android.graphics.RectF; | ||
import android.graphics.Region; | ||
import android.view.MotionEvent; | ||
import android.view.View; | ||
import android.view.View.OnClickListener; | ||
|
||
import androidx.annotation.DrawableRes; | ||
import androidx.annotation.NonNull; | ||
|
||
import org.mozilla.vrbrowser.utils.ViewUtils; | ||
|
||
import java.util.Deque; | ||
|
||
public class ClippedEventDelegate implements View.OnHoverListener, View.OnTouchListener { | ||
|
||
private View mView; | ||
private Region mRegion; | ||
private boolean mHovered; | ||
private boolean mTouched; | ||
private OnClickListener mClickListener; | ||
|
||
public ClippedEventDelegate(@DrawableRes int res, @NonNull View view) { | ||
mView = view; | ||
mHovered = false; | ||
mTouched = false; | ||
|
||
view.getViewTreeObserver().addOnGlobalLayoutListener( | ||
() -> { | ||
Path path = createPathFromResource(res); | ||
RectF bounds = new RectF(); | ||
path.computeBounds(bounds, true); | ||
|
||
bounds = new RectF(); | ||
path.computeBounds(bounds, true); | ||
mRegion = new Region(); | ||
mRegion.setPath(path, new Region((int) bounds.left, (int) bounds.top, (int) bounds.right, (int) bounds.bottom)); | ||
}); | ||
} | ||
|
||
public void setOnClickListener(OnClickListener listener) { | ||
mClickListener = listener; | ||
} | ||
|
||
private Path createPathFromResource(@DrawableRes int res) { | ||
VectorShape shape = new VectorShape(mView.getContext(), res); | ||
shape.onResize(mView.getWidth(), mView.getHeight()); | ||
Deque<VectorShape.Layer> layers = shape.getLayers(); | ||
VectorShape.Layer layer = layers.getFirst(); | ||
|
||
// TODO Handle state changes and update the Region based on the new current state shape | ||
|
||
return layer.transformedPath; | ||
} | ||
|
||
@Override | ||
public boolean onHover(View v, MotionEvent event) { | ||
if(!mRegion.contains((int)event.getX(),(int) event.getY())) { | ||
if (mHovered) { | ||
mHovered = false; | ||
event.setAction(MotionEvent.ACTION_HOVER_EXIT); | ||
return v.onHoverEvent(event); | ||
} | ||
|
||
return true; | ||
|
||
} else { | ||
if (!mHovered) { | ||
mHovered = true; | ||
event.setAction(MotionEvent.ACTION_HOVER_ENTER); | ||
} | ||
|
||
return v.onHoverEvent(event); | ||
} | ||
} | ||
|
||
@Override | ||
public boolean onTouch(View v, MotionEvent event) { | ||
v.getParent().requestDisallowInterceptTouchEvent(true); | ||
|
||
if(!mRegion.contains((int)event.getX(),(int) event.getY())) { | ||
switch (event.getAction()) { | ||
case MotionEvent.ACTION_UP: | ||
if (mTouched) { | ||
v.requestFocus(); | ||
v.requestFocusFromTouch(); | ||
if (mClickListener != null) { | ||
mClickListener.onClick(v); | ||
} | ||
} | ||
v.setPressed(false); | ||
mTouched = false; | ||
} | ||
|
||
return true; | ||
|
||
} else { | ||
switch (event.getAction()) { | ||
case MotionEvent.ACTION_DOWN: | ||
v.setPressed(true); | ||
mTouched = true; | ||
return true; | ||
|
||
case MotionEvent.ACTION_UP: | ||
if (mTouched && ViewUtils.isInsideView(v, (int)event.getRawX(), (int)event.getRawY())) { | ||
v.requestFocus(); | ||
v.requestFocusFromTouch(); | ||
if (mClickListener != null) { | ||
mClickListener.onClick(v); | ||
} | ||
} | ||
v.setPressed(false); | ||
mTouched = false; | ||
return true; | ||
|
||
case MotionEvent.ACTION_MOVE: | ||
return true; | ||
|
||
case MotionEvent.ACTION_CANCEL: | ||
v.setPressed(false); | ||
mTouched = false; | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
144 changes: 144 additions & 0 deletions
144
app/src/common/shared/org/mozilla/vrbrowser/ui/views/VectorClippedEventDelegate.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
package org.mozilla.vrbrowser.ui.views; | ||
|
||
import android.graphics.Path; | ||
import android.graphics.RectF; | ||
import android.graphics.Region; | ||
import android.view.MotionEvent; | ||
import android.view.View; | ||
import android.view.View.OnClickListener; | ||
import android.view.ViewTreeObserver; | ||
|
||
import androidx.annotation.DrawableRes; | ||
import androidx.annotation.NonNull; | ||
|
||
import org.mozilla.vrbrowser.utils.ViewUtils; | ||
|
||
import java.util.Deque; | ||
|
||
public class VectorClippedEventDelegate implements View.OnHoverListener, View.OnTouchListener { | ||
|
||
private View mView; | ||
private @DrawableRes int mRes; | ||
private Region mRegion; | ||
private boolean mHovered; | ||
private boolean mTouched; | ||
private OnClickListener mClickListener; | ||
|
||
VectorClippedEventDelegate(@DrawableRes int res, @NonNull View view) { | ||
mView = view; | ||
mRes = res; | ||
mHovered = false; | ||
mTouched = false; | ||
|
||
view.getViewTreeObserver().addOnGlobalLayoutListener(mLayoutListener); | ||
} | ||
|
||
private ViewTreeObserver.OnGlobalLayoutListener mLayoutListener = new ViewTreeObserver.OnGlobalLayoutListener() { | ||
@Override | ||
public void onGlobalLayout() { | ||
Path path = createPathFromResource(mRes); | ||
RectF bounds = new RectF(); | ||
path.computeBounds(bounds, true); | ||
|
||
bounds = new RectF(); | ||
path.computeBounds(bounds, true); | ||
mRegion = new Region(); | ||
mRegion.setPath(path, new Region((int) bounds.left, (int) bounds.top, (int) bounds.right, (int) bounds.bottom)); | ||
|
||
mView.getViewTreeObserver().removeOnGlobalLayoutListener(mLayoutListener); | ||
} | ||
}; | ||
|
||
public void setOnClickListener(OnClickListener listener) { | ||
mClickListener = listener; | ||
} | ||
|
||
private Path createPathFromResource(@DrawableRes int res) { | ||
VectorShape shape = new VectorShape(mView.getContext(), res); | ||
shape.onResize(mView.getWidth(), mView.getHeight()); | ||
Deque<VectorShape.Layer> layers = shape.getLayers(); | ||
VectorShape.Layer layer = layers.getFirst(); | ||
|
||
// TODO Handle state changes and update the Region based on the new current state shape | ||
|
||
return layer.transformedPath; | ||
} | ||
|
||
@Override | ||
public boolean onHover(View v, MotionEvent event) { | ||
if(!isInside(event)) { | ||
if (mHovered) { | ||
mHovered = false; | ||
event.setAction(MotionEvent.ACTION_HOVER_EXIT); | ||
return v.onHoverEvent(event); | ||
} | ||
|
||
return false; | ||
|
||
} else { | ||
if (!mHovered) { | ||
mHovered = true; | ||
event.setAction(MotionEvent.ACTION_HOVER_ENTER); | ||
} | ||
|
||
return v.onHoverEvent(event); | ||
} | ||
} | ||
|
||
@Override | ||
public boolean onTouch(View v, MotionEvent event) { | ||
v.getParent().requestDisallowInterceptTouchEvent(true); | ||
|
||
if(!isInside(event)) { | ||
switch (event.getAction()) { | ||
case MotionEvent.ACTION_UP: | ||
if (mTouched) { | ||
v.requestFocus(); | ||
v.requestFocusFromTouch(); | ||
if (mClickListener != null) { | ||
mClickListener.onClick(v); | ||
} | ||
} | ||
v.setPressed(false); | ||
mTouched = false; | ||
} | ||
|
||
return true; | ||
|
||
} else { | ||
switch (event.getAction()) { | ||
case MotionEvent.ACTION_DOWN: | ||
v.setPressed(true); | ||
mTouched = true; | ||
return true; | ||
|
||
case MotionEvent.ACTION_UP: | ||
if (mTouched && ViewUtils.isInsideView(v, (int)event.getRawX(), (int)event.getRawY())) { | ||
v.requestFocus(); | ||
v.requestFocusFromTouch(); | ||
if (mClickListener != null) { | ||
mClickListener.onClick(v); | ||
} | ||
} | ||
v.setPressed(false); | ||
mTouched = false; | ||
return true; | ||
|
||
case MotionEvent.ACTION_MOVE: | ||
return true; | ||
|
||
case MotionEvent.ACTION_CANCEL: | ||
v.setPressed(false); | ||
mTouched = false; | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
} | ||
|
||
public boolean isInside(MotionEvent event) { | ||
return mRegion.contains((int)event.getX(),(int) event.getY()); | ||
} | ||
|
||
} |
Oops, something went wrong.