diff --git a/docs/SkRect_Reference.bmh b/docs/SkRect_Reference.bmh index 2a8275c74f..3b9f1199df 100644 --- a/docs/SkRect_Reference.bmh +++ b/docs/SkRect_Reference.bmh @@ -1494,6 +1494,39 @@ describes an area: fLeft is less than fRight, and fTop is less than fBottom. # ------------------------------------------------------------------------------ +#Method bool contains(SkScalar x, SkScalar y) const + +#In Intersection +Returns true if: fLeft <= x < fRight && fTop <= y < fBottom. +Returns false if SkRect is empty. + +#Param x test SkPoint x-coordinate ## +#Param y test SkPoint y-coordinate ## + +#Return true if (x, y) is inside SkRect ## + +#Example + SkRect rect = { 30, 50, 40, 60 }; + SkPoint tests[] = { { 30, 50 }, { 39, 49 }, { 29, 59 } }; + for (auto contained : tests) { + SkDebugf("rect: (%g, %g, %g, %g) %s (%g, %g)\n", + rect.left(), rect.top(), rect.right(), rect.bottom(), + rect.contains(contained.x(), contained.y()) ? "contains" : "does not contain", + contained.x(), contained.y()); + } +#StdOut +rect: (30, 50, 40, 60) contains (30, 50) +rect: (30, 50, 40, 60) does not contain (39, 49) +rect: (30, 50, 40, 60) does not contain (29, 59) +## +## + +#SeeAlso SkIRect::contains + +## + +# ------------------------------------------------------------------------------ + #Method bool contains(const SkRect& r) const #In Intersection diff --git a/include/core/SkRect.h b/include/core/SkRect.h index ff7715853a..7f1eff0690 100644 --- a/include/core/SkRect.h +++ b/include/core/SkRect.h @@ -1423,6 +1423,17 @@ public: fBottom = SkMaxScalar(fBottom, r.bottom()); } + /** Returns true if: fLeft <= x < fRight && fTop <= y < fBottom. + Returns false if SkRect is empty. + + @param x test SkPoint x-coordinate + @param y test SkPoint y-coordinate + @return true if (x, y) is inside SkRect + */ + bool contains(SkScalar x, SkScalar y) const { + return x >= fLeft && x < fRight && y >= fTop && y < fBottom; + } + /** Returns true if SkRect contains r. Returns false if SkRect is empty or r is empty. diff --git a/tools/viewer/SlideDir.cpp b/tools/viewer/SlideDir.cpp index ce036b4885..da8685ddf7 100644 --- a/tools/viewer/SlideDir.cpp +++ b/tools/viewer/SlideDir.cpp @@ -153,8 +153,7 @@ public: bool onMouse(SkScalar x, SkScalar y, sk_app::Window::InputState state, uint32_t modifiers) { SkASSERT(fTarget); - // TODO: no SkRect::contains(SkPoint) ?! - if (x < fRect.fLeft || x > fRect.fRight || y < fRect.fTop || y > fRect.fBottom) { + if (!fRect.contains(x, y)) { this->startUnfocus(); return true; }