From 70bde1495b272a2cc702f560c7d1e531ce7f5c0d Mon Sep 17 00:00:00 2001 From: Mikael Sand Date: Thu, 11 Oct 2018 00:59:38 +0300 Subject: [PATCH] [android] Fix stroke hit testing --- .../com/horcrux/svg/RenderableShadowNode.java | 23 +++++++++++++++---- .../main/java/com/horcrux/svg/SvgView.java | 8 ++++++- .../java/com/horcrux/svg/TSpanShadowNode.java | 12 +++++++--- .../java/com/horcrux/svg/VirtualNode.java | 3 +++ 4 files changed, 38 insertions(+), 8 deletions(-) diff --git a/android/src/main/java/com/horcrux/svg/RenderableShadowNode.java b/android/src/main/java/com/horcrux/svg/RenderableShadowNode.java index 2efdb2d3..7ac1f225 100644 --- a/android/src/main/java/com/horcrux/svg/RenderableShadowNode.java +++ b/android/src/main/java/com/horcrux/svg/RenderableShadowNode.java @@ -307,7 +307,8 @@ abstract public class RenderableShadowNode extends VirtualNode { opacity *= mOpacity; if (opacity > MIN_OPACITY_FOR_DRAW) { - if (mPath == null) { + boolean computePaths = mPath == null; + if (computePaths) { mPath = getPath(canvas, paint); mPath.setFillType(mFillRule); } @@ -322,9 +323,17 @@ abstract public class RenderableShadowNode extends VirtualNode { clip(canvas, paint); if (setupFillPaint(paint, opacity * mFillOpacity)) { + if (computePaths) { + mFillPath = new Path(); + paint.getFillPath(path, mFillPath); + } canvas.drawPath(path, paint); } if (setupStrokePaint(paint, opacity * mStrokeOpacity)) { + if (computePaths) { + mStrokePath = new Path(); + paint.getFillPath(path, mStrokePath); + } canvas.drawPath(path, paint); } } @@ -414,10 +423,16 @@ abstract public class RenderableShadowNode extends VirtualNode { int x = Math.round(dst[0]); int y = Math.round(dst[1]); - if (mRegion == null && mPath != null) { - mRegion = getRegion(mPath); + if (mRegion == null && mFillPath != null) { + mRegion = getRegion(mFillPath); } - if (mRegion == null || !mRegion.contains(x, y)) { + if (mStrokeRegion == null && mStrokePath != null) { + mStrokeRegion = getRegion(mStrokePath); + } + if ( + (mRegion == null || !mRegion.contains(x, y)) && + (mStrokeRegion == null || !mStrokeRegion.contains(x, y)) + ) { return -1; } diff --git a/android/src/main/java/com/horcrux/svg/SvgView.java b/android/src/main/java/com/horcrux/svg/SvgView.java index 94f76a15..0e1f9332 100644 --- a/android/src/main/java/com/horcrux/svg/SvgView.java +++ b/android/src/main/java/com/horcrux/svg/SvgView.java @@ -17,6 +17,7 @@ import android.view.View; import com.facebook.react.bridge.ReactContext; import com.facebook.react.uimanager.ReactCompoundView; +import com.facebook.react.uimanager.ReactCompoundViewGroup; import com.facebook.react.uimanager.ReactShadowNodeImpl; import com.facebook.react.views.view.ReactViewGroup; @@ -26,7 +27,12 @@ import javax.annotation.Nullable; * Custom {@link View} implementation that draws an RNSVGSvg React view and its children. */ @SuppressLint("ViewConstructor") -public class SvgView extends ReactViewGroup implements ReactCompoundView { +public class SvgView extends ReactViewGroup implements ReactCompoundView, ReactCompoundViewGroup { + + @Override + public boolean interceptsTouchEvent(float touchX, float touchY) { + return true; + } @SuppressWarnings("unused") public enum Events { diff --git a/android/src/main/java/com/horcrux/svg/TSpanShadowNode.java b/android/src/main/java/com/horcrux/svg/TSpanShadowNode.java index 3eafd7a8..33f133a8 100644 --- a/android/src/main/java/com/horcrux/svg/TSpanShadowNode.java +++ b/android/src/main/java/com/horcrux/svg/TSpanShadowNode.java @@ -973,10 +973,16 @@ class TSpanShadowNode extends TextShadowNode { int x = Math.round(dst[0]); int y = Math.round(dst[1]); - if (mRegion == null && mPath != null) { - mRegion = getRegion(mPath); + if (mRegion == null && mFillPath != null) { + mRegion = getRegion(mFillPath); } - if (mRegion == null || !mRegion.contains(x, y)) { + if (mStrokeRegion == null && mStrokePath != null) { + mStrokeRegion = getRegion(mStrokePath); + } + if ( + (mRegion == null || !mRegion.contains(x, y)) && + (mStrokeRegion == null || !mStrokeRegion.contains(x, y)) + ) { return -1; } diff --git a/android/src/main/java/com/horcrux/svg/VirtualNode.java b/android/src/main/java/com/horcrux/svg/VirtualNode.java index b585efca..f0f22cc9 100644 --- a/android/src/main/java/com/horcrux/svg/VirtualNode.java +++ b/android/src/main/java/com/horcrux/svg/VirtualNode.java @@ -77,8 +77,11 @@ abstract class VirtualNode extends LayoutShadowNode { private GlyphContext glyphContext; Path mPath; + Path mFillPath; + Path mStrokePath; RectF mBox; Region mRegion; + Region mStrokeRegion; Region mClipRegion; Path mClipRegionPath;