From d4658bfd97e1770e7ab4de356b3fd8c0d1999493 Mon Sep 17 00:00:00 2001 From: crupest Date: Sat, 24 Nov 2018 00:41:38 +0800 Subject: Improve hit test for clip. --- src/ui/control.cpp | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) (limited to 'src/ui/control.cpp') diff --git a/src/ui/control.cpp b/src/ui/control.cpp index 9afa5497..91b5aea3 100644 --- a/src/ui/control.cpp +++ b/src/ui/control.cpp @@ -211,6 +211,30 @@ namespace cru::ui return false; } + Control* Control::HitTest(const Point& point) + { + const auto point_inside = IsPointInside(point); + + if (!point_inside && IsClipToPadding()) + return nullptr; // if clip then don't test children. + + const auto& children = GetChildren(); + + for (auto i = children.crbegin(); i != children.crend(); ++i) + { + const auto&& lefttop = (*i)->GetPositionRelative(); + const auto&& coerced_point = Point(point.x - lefttop.x, point.y - lefttop.y); + const auto child_hit_test_result = (*i)->HitTest(coerced_point); + if (child_hit_test_result != nullptr) + return child_hit_test_result; + } + + if (!point_inside) + return nullptr; + + return this; + } + void Control::SetClipToPadding(const bool clip) { if (clip_to_padding_ == clip) @@ -385,7 +409,6 @@ namespace cru::ui child->TraverseDescendants([window](Control* control) { control->OnAttachToWindow(window); }); - window->RefreshControlList(); InvalidateLayout(); } } @@ -397,7 +420,6 @@ namespace cru::ui child->TraverseDescendants([window](Control* control) { control->OnDetachToWindow(window); }); - window->RefreshControlList(); InvalidateLayout(); } } -- cgit v1.2.3