From eca02270ee3816b34776ba5ee34fe4c59b4027c3 Mon Sep 17 00:00:00 2001 From: reuk Date: Mon, 25 Oct 2021 18:54:20 +0100 Subject: [PATCH] Component: Add Point overloads for some common functions --- .../components/juce_Component.cpp | 24 ++++++------ .../components/juce_Component.h | 39 ++++++++++++++++--- .../mouse/juce_MouseInputSource.cpp | 4 +- 3 files changed, 48 insertions(+), 19 deletions(-) diff --git a/modules/juce_gui_basics/components/juce_Component.cpp b/modules/juce_gui_basics/components/juce_Component.cpp index adf76bd077..390792e337 100644 --- a/modules/juce_gui_basics/components/juce_Component.cpp +++ b/modules/juce_gui_basics/components/juce_Component.cpp @@ -1400,15 +1400,15 @@ void Component::getInterceptsMouseClicks (bool& allowsClicksOnThisComponent, bool Component::contains (Point point) { - return containsInternal (point.toFloat()); + return contains (point.toFloat()); } -bool Component::containsInternal (Point point) +bool Component::contains (Point point) { if (ComponentHelpers::hitTest (*this, point)) { if (parentComponent != nullptr) - return parentComponent->containsInternal (ComponentHelpers::convertToParentSpace (*this, point)); + return parentComponent->contains (ComponentHelpers::convertToParentSpace (*this, point)); if (flags.hasHeavyweightPeerFlag) if (auto* peer = getPeer()) @@ -1420,26 +1420,26 @@ bool Component::containsInternal (Point point) bool Component::reallyContains (Point point, bool returnTrueIfWithinAChild) { - return reallyContainsInternal (point.toFloat(), returnTrueIfWithinAChild); + return reallyContains (point.toFloat(), returnTrueIfWithinAChild); } -bool Component::reallyContainsInternal (Point point, bool returnTrueIfWithinAChild) +bool Component::reallyContains (Point point, bool returnTrueIfWithinAChild) { - if (! containsInternal (point)) + if (! contains (point)) return false; auto* top = getTopLevelComponent(); - auto* compAtPosition = top->getComponentAtInternal (top->getLocalPoint (this, point)); + auto* compAtPosition = top->getComponentAt (top->getLocalPoint (this, point)); return (compAtPosition == this) || (returnTrueIfWithinAChild && isParentOf (compAtPosition)); } Component* Component::getComponentAt (Point position) { - return getComponentAtInternal (position.toFloat()); + return getComponentAt (position.toFloat()); } -Component* Component::getComponentAtInternal (Point position) +Component* Component::getComponentAt (Point position) { if (flags.visibleFlag && ComponentHelpers::hitTest (*this, position)) { @@ -1447,7 +1447,7 @@ Component* Component::getComponentAtInternal (Point position) { auto* child = childComponentList.getUnchecked (i); - child = child->getComponentAtInternal (ComponentHelpers::convertFromParentSpace (*child, position)); + child = child->getComponentAt (ComponentHelpers::convertFromParentSpace (*child, position)); if (child != nullptr) return child; @@ -1461,7 +1461,7 @@ Component* Component::getComponentAtInternal (Point position) Component* Component::getComponentAt (int x, int y) { - return getComponentAt ({ x, y }); + return getComponentAt (Point { x, y }); } //============================================================================== @@ -3074,7 +3074,7 @@ bool Component::isMouseOver (bool includeChildren) const if (c != nullptr && (c == this || (includeChildren && isParentOf (c)))) if (ms.isDragging() || ! (ms.isTouch() || ms.isPen())) - if (c->reallyContainsInternal (c->getLocalPoint (nullptr, ms.getScreenPosition()), false)) + if (c->reallyContains (c->getLocalPoint (nullptr, ms.getScreenPosition()), false)) return true; } diff --git a/modules/juce_gui_basics/components/juce_Component.h b/modules/juce_gui_basics/components/juce_Component.h index 6837fd9d3d..2d03ee97f6 100644 --- a/modules/juce_gui_basics/components/juce_Component.h +++ b/modules/juce_gui_basics/components/juce_Component.h @@ -936,6 +936,19 @@ public: */ bool contains (Point localPoint); + /** Returns true if a given point lies within this component or one of its children. + + Never override this method! Use hitTest to create custom hit regions. + + @param localPoint the coordinate to test, relative to this component's top-left. + @returns true if the point is within the component's hit-test area, but only if + that part of the component isn't clipped by its parent component. Note + that this won't take into account any overlapping sibling components + which might be in the way - for that, see reallyContains() + @see hitTest, reallyContains, getComponentAt + */ + bool contains (Point localPoint); + /** Returns true if a given point lies in this component, taking any overlapping siblings into account. @@ -946,6 +959,16 @@ public: */ bool reallyContains (Point localPoint, bool returnTrueIfWithinAChild); + /** Returns true if a given point lies in this component, taking any overlapping + siblings into account. + + @param localPoint the coordinate to test, relative to this component's top-left. + @param returnTrueIfWithinAChild if the point actually lies within a child of this component, + this determines whether that is counted as a hit. + @see contains, getComponentAt + */ + bool reallyContains (Point localPoint, bool returnTrueIfWithinAChild); + /** Returns the component at a certain point within this one. @param x the x coordinate to test, relative to this component's left edge. @@ -969,6 +992,17 @@ public: */ Component* getComponentAt (Point position); + /** Returns the component at a certain point within this one. + + @param position the coordinate to test, relative to this component's top-left. + @returns the component that is at this position - which may be 0, this component, + or one of its children. Note that overlapping siblings that might actually + be in the way are not taken into account by this method - to account for these, + instead call getComponentAt on the top-level parent of this component. + @see hitTest, contains, reallyContains + */ + Component* getComponentAt (Point position); + //============================================================================== /** Marks the whole component as needing to be redrawn. @@ -2474,7 +2508,6 @@ private: //============================================================================== friend class ComponentPeer; - friend class MouseInputSource; friend class MouseInputSourceInternal; #ifndef DOXYGEN @@ -2574,10 +2607,6 @@ private: void sendEnablementChangeMessage(); void sendVisibilityChangeMessage(); - bool containsInternal (Point); - bool reallyContainsInternal (Point, bool); - Component* getComponentAtInternal (Point); - struct ComponentHelpers; friend struct ComponentHelpers; diff --git a/modules/juce_gui_basics/mouse/juce_MouseInputSource.cpp b/modules/juce_gui_basics/mouse/juce_MouseInputSource.cpp index 2e6ab4c148..449e6530e5 100644 --- a/modules/juce_gui_basics/mouse/juce_MouseInputSource.cpp +++ b/modules/juce_gui_basics/mouse/juce_MouseInputSource.cpp @@ -78,8 +78,8 @@ public: auto& comp = peer->getComponent(); // (the contains() call is needed to test for overlapping desktop windows) - if (comp.containsInternal (relativePos)) - return comp.getComponentAtInternal (relativePos); + if (comp.contains (relativePos)) + return comp.getComponentAt (relativePos); } return nullptr;