public static FrameworkElement HitTestWithDataContext <T>(Visual reference, Point2 point) where T : class { FrameworkElement result = null; HitTestFilterCallback filterCallback = (DependencyObject candidate) => { var fe = candidate as FrameworkElement; if (fe == null) { return(HitTestFilterBehavior.ContinueSkipSelf); } var dc = fe.DataContext as T; if (dc == null) { return(HitTestFilterBehavior.ContinueSkipSelf); } return(HitTestFilterBehavior.Continue); }; HitTestResultCallback resultCallback = (HitTestResult hitTestResult) => { result = hitTestResult.VisualHit as FrameworkElement; return(HitTestResultBehavior.Stop); }; VisualTreeHelper.HitTest(reference, filterCallback, resultCallback, new PointHitTestParameters(new Point(point.X, point.Y))); return(result); }
public IEnumerable <IElement> HitTest(IPoint point, double radius) { var canvas = this; var selectedElements = new List <DependencyObject>(); var elippse = new EllipseGeometry() { RadiusX = radius, RadiusY = radius, Center = new Point(point.X, point.Y) }; var hitTestParams = new GeometryHitTestParameters(elippse); var resultCallback = new HitTestResultCallback(result => HitTestResultBehavior.Continue); var filterCallback = new HitTestFilterCallback( element => { if (VisualTreeHelper.GetParent(element) == canvas) { selectedElements.Add(element); } return(HitTestFilterBehavior.Continue); }); VisualTreeHelper.HitTest(canvas, filterCallback, resultCallback, hitTestParams); return(selectedElements.Cast <IElement>()); }
public static bool CanHit(this FrameworkElement element, Visual parentScopeElement) { var point = element.TransformToAncestor(parentScopeElement).Transform(new Point(element.ActualWidth / 2, element.ActualHeight / 2)); var hitTestOk = false; HitTestFilterCallback filterCallback = target => { var uiElement = target as UIElement; if (uiElement != null) { if (!uiElement.IsHitTestVisible || !uiElement.IsVisible) { return(HitTestFilterBehavior.ContinueSkipSelfAndChildren); } } if (Equals(target, element)) { hitTestOk = true; return(HitTestFilterBehavior.Stop); } return(HitTestFilterBehavior.Continue); }; HitTestResultCallback resultCallback = result => HitTestResultBehavior.Stop; VisualTreeHelper.HitTest(parentScopeElement, filterCallback, resultCallback, new PointHitTestParameters(point)); return(hitTestOk); }
static ICollection <DesignItem> GetChildDesignItemsInContainer( DesignItem container, Geometry geometry) { HashSet <DesignItem> resultItems = new HashSet <DesignItem>(); ViewService viewService = container.Services.View; HitTestFilterCallback filterCallback = delegate(DependencyObject potentialHitTestTarget) { FrameworkElement element = potentialHitTestTarget as FrameworkElement; if (element != null) { // ensure we are able to select elements with width/height=0 if (element.ActualWidth == 0 || element.ActualHeight == 0) { DependencyObject tmp = element; DesignItem model = null; while (tmp != null) { model = viewService.GetModel(tmp); if (model != null) { break; } tmp = VisualTreeHelper.GetParent(tmp); } if (model != container) { resultItems.Add(model); return(HitTestFilterBehavior.ContinueSkipChildren); } } } return(HitTestFilterBehavior.Continue); }; HitTestResultCallback resultCallback = delegate(HitTestResult result) { if (((GeometryHitTestResult)result).IntersectionDetail == IntersectionDetail.FullyInside) { // find the model for the visual contained in the selection area DependencyObject tmp = result.VisualHit; DesignItem model = null; while (tmp != null) { model = viewService.GetModel(tmp); if (model != null) { break; } tmp = VisualTreeHelper.GetParent(tmp); } if (model != container) { resultItems.Add(model); } } return(HitTestResultBehavior.Continue); }; VisualTreeHelper.HitTest(container.View, filterCallback, resultCallback, new GeometryHitTestParameters(geometry)); return(resultItems); }
private void GridA_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { if (e.LeftButton == MouseButtonState.Released) { hits.Clear(); mouseSelectionCounter = 0; GridA.Children.Remove(pth1); selectBox = new RectangleGeometry(); recTemp = new Rect(); pth1 = new Path(); //HitTestResult result = VisualTreeHelper.HitTest(GridA, e.GetPosition(this)); //Debug.WriteLine((result.VisualHit as Path).Name); GeometryHitTestParameters parameters = new GeometryHitTestParameters(recHitResult); HitTestResultCallback callback = new HitTestResultCallback((HitTestResult result) => { GeometryHitTestResult geometryResult = (GeometryHitTestResult)result; Path visual = result.VisualHit as Path; if (visual != null && geometryResult.IntersectionDetail == IntersectionDetail.FullyInside) { hits.Add(visual); } return(HitTestResultBehavior.Continue); }); VisualTreeHelper.HitTest(this, null, callback, parameters); foreach (Path x in hits) { Debug.WriteLine(x.Name); } } }
private Adorner GetHitAdorner(Visual visual, Point point, Type[] adornerFilterTypes) { PointHitTestResult hitTestResult = (PointHitTestResult)null; HitTestFilterCallback filterCallback = (HitTestFilterCallback)(testObject => { foreach (Type type in adornerFilterTypes) { if (type.IsInstanceOfType((object)testObject)) { return(HitTestFilterBehavior.Continue); } } return(HitTestFilterBehavior.ContinueSkipSelf); }); HitTestResultCallback resultCallback = (HitTestResultCallback)(hitItemsResult => { hitTestResult = (PointHitTestResult)hitItemsResult; return(HitTestResultBehavior.Stop); }); PointHitTestParameters hitTestParameters = new PointHitTestParameters(point); VisualTreeHelper.HitTest(visual, filterCallback, resultCallback, (HitTestParameters)hitTestParameters); Adorner adorner = (Adorner)null; if (hitTestResult != null) { adorner = hitTestResult.VisualHit as Adorner; } return(adorner); }
private static DependencyObject HitTest <T>(Point p, UIElement container) { PointHitTestParameters parameter = new PointHitTestParameters(p); DependencyObject hitTestResult = null; HitTestResultCallback resultCallback = (result) => { UIElement elemCandidateResult = result.VisualHit as UIElement; // result can be collapsed! Even though documentation indicates otherwise if (null != elemCandidateResult && elemCandidateResult.Visibility == Visibility.Visible) { hitTestResult = result.VisualHit; return(HitTestResultBehavior.Stop); } return(HitTestResultBehavior.Continue); }; HitTestFilterCallback filterCallBack = (potentialHitTestTarget) => { if (potentialHitTestTarget is T) { hitTestResult = potentialHitTestTarget; return(HitTestFilterBehavior.Stop); } return(HitTestFilterBehavior.Continue); }; VisualTreeHelper.HitTest(container, filterCallBack, resultCallback, parameter); return(hitTestResult); }
internal List <DrawingVisual> GetVisuals(Point point) { hits.Clear(); //Rect rect = new Rect(new Point(point.X - 2, point.Y - 2), new Point(point.X + 2, point.Y + 2)); //RectangleGeometry rectGeo = new RectangleGeometry(rect); //GeometryHitTestParameters parameters = new GeometryHitTestParameters(rectGeo); PointHitTestParameters parameters = new PointHitTestParameters(point); HitTestResultCallback callback = new HitTestResultCallback(this.HitTestCallback); VisualTreeHelper.HitTest(this, null, callback, parameters); try { if (hits.Count == 0) { return(null); } else { return(hits); } } catch { return(null); } }
public static HitTestResult HitTest(Visual reference, HitTestParameters hitTestParameters, bool ignoreDisabled, HitTestFilterCallback filterCallback) { HitTestResult hitTestResult = (HitTestResult)null; HitTestResultCallback resultCallback = (HitTestResultCallback)(hitItemsResult => { hitTestResult = hitItemsResult; return(HitTestResultBehavior.Stop); }); HitTestFilterCallback filterCallback1 = (HitTestFilterCallback)(hit => { UIElement uiElement = hit as UIElement; if (uiElement != null && (!uiElement.IsVisible || !uiElement.IsHitTestVisible || ignoreDisabled && !uiElement.IsEnabled)) { return(HitTestFilterBehavior.ContinueSkipSelfAndChildren); } HitTestFilterBehavior testFilterBehavior = HitTestFilterBehavior.Continue; if (filterCallback != null) { testFilterBehavior = filterCallback(hit); } return(testFilterBehavior); }); VisualTreeHelper.HitTest(reference, filterCallback1, resultCallback, hitTestParameters); return(hitTestResult); }
public List<DrawingVisual> GetVisuals(Geometry region) { hits.Clear(); GeometryHitTestParameters parameters = new GeometryHitTestParameters(region); HitTestResultCallback callback = new HitTestResultCallback(this.HitTestCallback); VisualTreeHelper.HitTest(this, null, callback, parameters); return hits; }
private IVertexElement GetVertexElement(Point p) { _targetUiElement = null; HitTestResultCallback callback = HitTestClickCallback; VisualTreeHelper.HitTest(this, null, callback, new PointHitTestParameters(p)); return(_targetUiElement as IVertexElement); }
public HitTestResultCallbackWrapper(HitTestResultCallback resultCallback, HitTestResultBehavior behaviorOnResult) { this._resultCallback = resultCallback; this._resultInsertionIndex = 0; this._results = (List <HitTestResult>)null; this._onHitFoundBehavior = behaviorOnResult; this._wrappedResultCallback = new HitTestResultCallback(this.OnResult); }
public List <DrawingVisual> GetVisuals(Geometry region) { hits.Clear(); GeometryHitTestParameters parameters = new GeometryHitTestParameters(region); HitTestResultCallback callback = new HitTestResultCallback(this.HitTestCallback); VisualTreeHelper.HitTest(this, null, callback, parameters); return(hits); }
/// <summary> /// Initiate a hit test using delegates. /// </summary> public static void HitTest( Visual3D reference, HitTestFilterCallback filterCallback, HitTestResultCallback resultCallback, HitTestParameters3D hitTestParameters) { CheckVisualReferenceArgument(reference); reference.HitTest(filterCallback, resultCallback, hitTestParameters); }
public List <DrawingVisual> GetVisuals(System.Windows.Media.Geometry region) { hits.Clear(); GeometryHitTestParameters parameters = new GeometryHitTestParameters(region); HitTestResultCallback callback = HitTestCallback; VisualTreeHelper.HitTest(this, null, callback, parameters); return(hits); }
private void UpdateWithWpfHitObjectUnderMouseOnLocation(Point mousePosition, HitTestResultCallback hitTestResultCallback) { this._objectUnderMouseDetectionLocation = mousePosition; // Expand the hit test area by creating a geometry centered on the hit test point. var expandedHitTestArea = new RectangleGeometry(this.MouseHitToleranceRectangle(mousePosition, this.MouseHitTolerance)); // Set up a callback to receive the hit test result enumeration. VisualTreeHelper.HitTest(this.GraphCanvas, null, hitTestResultCallback, new GeometryHitTestParameters(expandedHitTestArea)); }
public List <DrawingVisual> GetVisuals(Geometry region) //region对象用于命中测试 { hits.Clear(); //情况命中测试结果的集合 GeometryHitTestParameters parameters = new GeometryHitTestParameters(region); HitTestResultCallback callback = new HitTestResultCallback(this.HitTestCallback); //通过调用VisualTreeHelper.HitTest()方法启动命中测试过程 VisualTreeHelper.HitTest(this, null, callback, parameters); //当该过程结束时,该方法返回包含所有找到的可视化对象的集合 return(hits); }
private void NodeGraphView_OnMouseMove(object sender, MouseEventArgs e) { if (m_isDrawinSelectionRect) { Point mousePos = e.GetPosition(NodeCanvas); if (mousePos.X > m_selectionStartPoint.X) { Canvas.SetLeft(SelectionRect, m_selectionStartPoint.X); SelectionRect.Width = mousePos.X - m_selectionStartPoint.X; } else { Canvas.SetLeft(SelectionRect, mousePos.X); SelectionRect.Width = m_selectionStartPoint.X - mousePos.X; } if (mousePos.Y > m_selectionStartPoint.Y) { Canvas.SetTop(SelectionRect, m_selectionStartPoint.Y); SelectionRect.Height = mousePos.Y - m_selectionStartPoint.Y; } else { Canvas.SetTop(SelectionRect, mousePos.Y); SelectionRect.Height = m_selectionStartPoint.Y - mousePos.Y; } var rect = new RectangleGeometry(new Rect(m_selectionStartPoint, mousePos)); var hitTestParams = new GeometryHitTestParameters(rect); HashSet <CScriptNodeViewmodel> containedNodes = new HashSet <CScriptNodeViewmodel>(); var resultCallback = new HitTestResultCallback(element => { return(HitTestResultBehavior.Continue); }); var filterCallback = new HitTestFilterCallback(element => { if (element is FrameworkElement uiElement) { if (uiElement.DataContext is CScriptNodeViewmodel vm) { containedNodes.Add(vm); return(HitTestFilterBehavior.ContinueSkipSelfAndChildren); } } return(HitTestFilterBehavior.Continue); }); VisualTreeHelper.HitTest(NodeCanvas, filterCallback, resultCallback, hitTestParams); ((CNodeGraphViewModel)DataContext).SelectNodes(containedNodes); } }
void RunHitTest(Visual reference, Point point, HitTestFilterCallback filterCallback, HitTestResultCallback resultCallback) { if (!Keyboard.IsKeyDown(Key.LeftAlt)) { hitTestElements.Clear(); skippedHitTestElements.Clear(); } VisualTreeHelper.HitTest(reference, filterCallback, resultCallback, new PointHitTestParameters(point)); }
internal HitTestResultBehavior RaiseCallback(HitTestResultCallback resultCallback, HitTestFilterCallback filterCallback, HitTestResultBehavior lastResult, double distanceAdjustment) { results.Sort(RayHitTestResult.CompareByDistanceToRayOrigin); for (int i = 0, count = results.Count; i < count; i++) { RayHitTestResult result = results[i]; result.SetDistanceToRayOrigin(result.DistanceToRayOrigin + distanceAdjustment); Viewport2DVisual3D viewport2DVisual3D = result.VisualHit as Viewport2DVisual3D; if (viewport2DVisual3D != null) { Point intersectionPoint; Visual viewport2DVisual3DChild = viewport2DVisual3D.Visual; if (viewport2DVisual3DChild != null) { if (Viewport2DVisual3D.GetIntersectionInfo(result, out intersectionPoint)) { // convert the resulting point to visual coordinates Point visualPoint = Viewport2DVisual3D.TextureCoordsToVisualCoords(intersectionPoint, viewport2DVisual3DChild); GeneralTransform gt = viewport2DVisual3DChild.TransformToOuterSpace().Inverse; Point pointOnChild; if (gt != null && gt.TryTransform(visualPoint, out pointOnChild)) { HitTestResultBehavior behavior2D = viewport2DVisual3DChild.HitTestPoint(filterCallback, resultCallback, new PointHitTestParameters(pointOnChild)); if (behavior2D == HitTestResultBehavior.Stop) { return(HitTestResultBehavior.Stop); } } } } } HitTestResultBehavior behavior = resultCallback(results[i]); if (behavior == HitTestResultBehavior.Stop) { return(HitTestResultBehavior.Stop); } } return(lastResult); }
public IEnumerable <DrawingVisual> GetVisuals(Geometry region) { // Очистить результаты предыдущего поиска _hits.Clear(); // Подготовить параметры для операции проверки попадания (геометрию и обратный вызов) var parameters = new GeometryHitTestParameters(region); var callback = new HitTestResultCallback(HitTestCallback); // Поиск попаданий VisualTreeHelper.HitTest(this, null, callback, parameters); return(_hits); }
private static List <T> GetShapesHit <T>(Visual surface, Point p, Func <List <T>, Point, HitTestResultCallback> target) { var shapesHit = new List <T>(); var callback = new HitTestResultCallback(target(shapesHit, p)); // Specify that the hit test should search for every shape within 2 pixels of the mouse click. var rect = new Rect(new Point(p.X - 2, p.Y - 2), new Size(5, 5)); var parameters = new GeometryHitTestParameters(new RectangleGeometry(rect)); // Perform the hit test VisualTreeHelper.HitTest(surface, null, callback, parameters); return(shapesHit); }
private IEnumerable <HitTestResult> GetHits(HitTestFilterCallback htrcb, Point position) { var results = new List <HitTestResult>(); HitTestResultCallback cb = delegate(HitTestResult result) { results.Add(result); return(HitTestResultBehavior.Continue); }; VisualTreeHelper.HitTest(mainViewport, htrcb, cb, new PointHitTestParameters(position)); return(results); }
public List <DrawingVisual> GetVisuals(Geometry region) { // Очистить результаты предыдущей проверки _hits.Clear(); // Подготовить параметры для операции проверки попадания (геометрию и обратный вызов). var parameters = new GeometryHitTestParameters(region); HitTestResultCallback callback = HitTestCallback; VisualTreeHelper.HitTest(this, null, callback, parameters); return(_hits); }
/// <summary> /// Initiates a hit test on the specified Visual object, with called-defined HitTestFilterCallback and HitTestResultCallback methods. /// </summary> /// <param name="region">The region to hit test against.</param> /// <returns></returns> public List <DrawingVisual> GetVisuals(Geometry region) { // Remove matches from the previous search. hits.Clear(); // Prepare the parameters for the hit test operation. // The geometry and callback. GeometryHitTestParameters parameters = new GeometryHitTestParameters(region); HitTestResultCallback callback = new HitTestResultCallback(this.HitTestCallback); // Search for hits. VisualTreeHelper.HitTest(this, null, callback, parameters); return(hits); }
public static T AreaHitTest <T>(this FrameworkElement element, Point center, double radius) where T : DependencyObject { T hitTestResult = null; var hitTestParams = new GeometryHitTestParameters(new EllipseGeometry(center, radius, radius)); var resultCallback = new HitTestResultCallback(t => HitTestResultBehavior.Continue); var filterCallback = new HitTestFilterCallback(t => { hitTestResult = GetParent <T>(t as FrameworkElement); return(HitTestFilterBehavior.Continue); }); VisualTreeHelper.HitTest(element, filterCallback, resultCallback, hitTestParams); return(hitTestResult); }
/// <summary> /// Returns a collection of Elements of type targetType that are located in the area of the Geometry geo in the subtree of the UIElement reference /// </summary> /// <param name="geo">the geometry which will be tested</param> /// <param name="reference">specifies the subtree that will be tested</param> /// <param name="targetType">specifies the type of elements that will be returned (subclasses of this type will also be returned)</param> /// <returns>A collection of Elements that are found by hittesting with the given parameters</returns> public static IEnumerable <DependencyObject> GetElementsInGeometry(Geometry geo, Visual reference, Type targetType) { List <DependencyObject> results = new List <DependencyObject>(); HitTestFilterCallback filter = (o) => { Type matchType = o.GetType(); //if we find an element of correct type, we can ignore its subtree //if we find an element of incorrect type, we can ignore it, but must check its children if (matchType == targetType || matchType.IsSubclassOf(targetType)) { return(HitTestFilterBehavior.ContinueSkipChildren); } else { return(HitTestFilterBehavior.ContinueSkipSelf); } }; HitTestResultCallback result = (r) => { GeometryHitTestResult ghr = r as GeometryHitTestResult; if (ghr.IntersectionDetail == IntersectionDetail.Empty) { return(HitTestResultBehavior.Continue); } Type matchType = r.VisualHit.GetType(); //again we check the type of the element, just to be sure that only correct types go into our collection if (matchType == targetType || matchType.IsSubclassOf(targetType)) { results.Add(r.VisualHit); } return(HitTestResultBehavior.Continue); }; VisualTreeHelper.HitTest(reference, filter, result, new GeometryHitTestParameters(geo)); return(results); }
public static IList <T> AreaHitTest <T>(this FrameworkElement element, Point startPoint, double width, double height) where T : DependencyObject { var result = new List <T>(); var hitTestParams = new GeometryHitTestParameters(new RectangleGeometry(new Rect(startPoint, new Size(width, height)))); var resultCallback = new HitTestResultCallback(t => HitTestResultBehavior.Continue); var filterCallback = new HitTestFilterCallback(t => { result.Add(GetParent <T>(t as FrameworkElement)); return(HitTestFilterBehavior.Continue); }); VisualTreeHelper.HitTest(element, filterCallback, resultCallback, hitTestParams); return(result.Where(c => c != null).Distinct().ToList()); }
/// <summary> /// 自分が選択された事を知らせるイベントを発行する /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected virtual void YdHostMouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e) { HitTestResultCallback cback = (x) => { DrawingVisual dv = x.VisualHit as DrawingVisual; if (dv != null) { if (this.Selected != null) { this.Selected(this, new SelectedEventArgs()); } } return(HitTestResultBehavior.Stop); }; VisualTreeHelper.HitTest(this.GetVisualChild(0), null, cback, new GeometryHitTestParameters(new EllipseGeometry(e.GetPosition(this), 10, 10))); }
/// <summary> /// Returns a collection of Elements of type T that are located in the area of the Geometry geo in the subtree of the UIElement reference /// </summary> /// <typeparam name="T">specifies the type of elements that will be returned (subclasses of this type will also be returned)</typeparam> /// <param name="geo">the geometry which will be tested</param> /// <param name="reference">specifies the subtree that will be tested</param> /// <returns>A collection of Elements that are found by hittesting with the given parameters</returns> public static IEnumerable <T> GetElementsInGeometry <T>(Geometry geo, Visual reference) where T : DependencyObject { var results = new List <T>(); if (geo == null) { return(results); } HitTestFilterCallback filter = o => { //if we find an element of correct type, we can ignore its subtree //if we find an element of incorrect type, we can ignore it, but must check its children if (o is T) { return(HitTestFilterBehavior.ContinueSkipChildren); } return(HitTestFilterBehavior.ContinueSkipSelf); }; HitTestResultCallback result = r => { //again we check the type of the element, just to be sure that only correct types go into our collection var ghr = r as GeometryHitTestResult; if (ghr.IntersectionDetail == IntersectionDetail.Empty) { return(HitTestResultBehavior.Continue); } if (r.VisualHit is T) { results.Add(r.VisualHit as T); } return(HitTestResultBehavior.Continue); }; VisualTreeHelper.HitTest(reference, filter, result, new GeometryHitTestParameters(geo)); return(results); }
bool IsHitTestOnJack(Point hitPoint) { _hitResultsList.Clear(); var filterCallback = new HitTestFilterCallback(x => { return(x.GetType() == typeof(Jack) ? HitTestFilterBehavior.ContinueSkipSelfAndChildren : HitTestFilterBehavior.Continue); }); var resultCallback = new HitTestResultCallback(x => { _hitResultsList.Add(x.VisualHit); return(HitTestResultBehavior.Continue); }); VisualTreeHelper.HitTest(_adornedElement, filterCallback, resultCallback, new PointHitTestParameters(hitPoint)); return(_hitResultsList.Count > 0); }
public void HitTest(HitTestFilterCallback filterCallback, HitTestResultCallback resultCallback, HitTestParameters hitTestParameters) { }
internal HitTestResultBehavior RaiseCallback(HitTestResultCallback resultCallback, HitTestFilterCallback filterCallback, HitTestResultBehavior lastResult) { return RaiseCallback(resultCallback, filterCallback, lastResult, 0.0 /* distance adjustment */); }
public static void HitTest(System.Windows.Media.Media3D.Visual3D reference, HitTestFilterCallback filterCallback, HitTestResultCallback resultCallback, System.Windows.Media.Media3D.HitTestParameters3D hitTestParameters) { Contract.Requires(reference != null); }
private static void endCreation(Connection newConnection) { DesignArea designArea = newConnection.DesignArea; // Remove the mouse move and mouse up listeners designArea.MouseMove -= newConnection.creationMouseMoveListener; designArea.MouseUp -= newConnection.creationMouseUpListener; // Check if the mouse is currently intersecting with a input variable HitTestFilterCallback filterCallback = new HitTestFilterCallback(element => element == newConnection ? HitTestFilterBehavior.ContinueSkipSelfAndChildren : HitTestFilterBehavior.Continue); HitTestResultCallback resultCallback = new HitTestResultCallback(element => finalizeCreation(element, newConnection)); VisualTreeHelper.HitTest(designArea.Canvas, filterCallback, resultCallback, new PointHitTestParameters(newConnection.EndPoint)); }
void UpdateWithWpfHitObjectUnderMouseOnLocation(WpfPoint pt, HitTestResultCallback hitTestResultCallback) { _objectUnderMouseDetectionLocation = pt; // Expand the hit test area by creating a geometry centered on the hit test point. var rect = new Rect(new WpfPoint(pt.X - MouseHitTolerance, pt.Y - MouseHitTolerance), new WpfPoint(pt.X + MouseHitTolerance, pt.Y + MouseHitTolerance)); var expandedHitTestArea = new RectangleGeometry(rect); // Set up a callback to receive the hit test result enumeration. VisualTreeHelper.HitTest(_graphCanvas, null, hitTestResultCallback, new GeometryHitTestParameters(expandedHitTestArea)); }
public static void HitTest(Visual reference, HitTestFilterCallback filterCallback, HitTestResultCallback resultCallback, HitTestParameters hitTestParameters) { Contract.Requires(reference != null); }
internal override HitTestResultBehavior HitTestPointInternal( HitTestFilterCallback filterCallback, HitTestResultCallback resultCallback, PointHitTestParameters hitTestParameters) { if (_children.Count != 0) { double distanceAdjustment; RayHitTestParameters rayParams = Camera.RayFromViewportPoint( hitTestParameters.HitPoint, Viewport.Size, BBoxSubgraph, out distanceAdjustment); HitTestResultBehavior result = Visual3D.HitTestChildren(filterCallback, rayParams, this); return rayParams.RaiseCallback(resultCallback, filterCallback, result, distanceAdjustment); } return HitTestResultBehavior.Continue; }
internal HitTestResultBehavior RaiseCallback(HitTestResultCallback resultCallback, HitTestFilterCallback filterCallback, HitTestResultBehavior lastResult, double distanceAdjustment) { results.Sort(RayHitTestResult.CompareByDistanceToRayOrigin); for(int i = 0, count = results.Count; i < count; i++) { RayHitTestResult result = results[i]; result.SetDistanceToRayOrigin(result.DistanceToRayOrigin + distanceAdjustment); Viewport2DVisual3D viewport2DVisual3D = result.VisualHit as Viewport2DVisual3D; if (viewport2DVisual3D != null) { Point intersectionPoint; Visual viewport2DVisual3DChild = viewport2DVisual3D.Visual; if (viewport2DVisual3DChild != null) { if (Viewport2DVisual3D.GetIntersectionInfo(result, out intersectionPoint)) { // convert the resulting point to visual coordinates Point visualPoint = Viewport2DVisual3D.TextureCoordsToVisualCoords(intersectionPoint, viewport2DVisual3DChild); GeneralTransform gt = viewport2DVisual3DChild.TransformToOuterSpace().Inverse; Point pointOnChild; if (gt != null && gt.TryTransform(visualPoint, out pointOnChild)) { HitTestResultBehavior behavior2D = viewport2DVisual3DChild.HitTestPoint(filterCallback, resultCallback, new PointHitTestParameters(pointOnChild)); if (behavior2D == HitTestResultBehavior.Stop) { return HitTestResultBehavior.Stop; } } } } } HitTestResultBehavior behavior = resultCallback(results[i]); if (behavior == HitTestResultBehavior.Stop) { return HitTestResultBehavior.Stop; } } return lastResult; }
public List<DependencyObject> HitTest(Visual visual, IPoint point, double radius) { var elements = new List<DependencyObject>(); var elippse = new EllipseGeometry() { RadiusX = radius, RadiusY = radius, Center = new Point(point.X, point.Y), }; var hitTestParams = new GeometryHitTestParameters(elippse); var resultCallback = new HitTestResultCallback(result => HitTestResultBehavior.Continue); var filterCallback = new HitTestFilterCallback( element => { elements.Add(element); return HitTestFilterBehavior.Continue; }); VisualTreeHelper.HitTest(visual, filterCallback, resultCallback, hitTestParams); return elements; }
public IEnumerable<IElement> HitTest(IRect rect) { var canvas = this; var r = new Rect(new Point(rect.X1, rect.Y1), new Point(rect.X2, rect.Y2)); var selectedElements = new List<DependencyObject>(); var rectangle = new RectangleGeometry(r, 0.0, 0.0); var hitTestParams = new GeometryHitTestParameters(rectangle); var resultCallback = new HitTestResultCallback(result => HitTestResultBehavior.Continue); var filterCallback = new HitTestFilterCallback( element => { if (VisualTreeHelper.GetParent(element) == canvas) selectedElements.Add(element); return HitTestFilterBehavior.Continue; }); VisualTreeHelper.HitTest(canvas, filterCallback, resultCallback, hitTestParams); return selectedElements.Cast<IElement>(); }
internal HitTestResultBehavior HitTestGeometry( HitTestFilterCallback filterCallback, HitTestResultCallback resultCallback, GeometryHitTestParameters geometryParams) { // we do not need parameter checks because they are done in HitTest() Geometry clip = VisualClip; if (clip != null) { // HitTest with a Geometry and a clip should hit test with // the intersection of the geometry and the clip, not the entire geometry IntersectionDetail intersectionDetail = clip.FillContainsWithDetail(geometryParams.InternalHitGeometry); Debug.Assert(intersectionDetail != IntersectionDetail.NotCalculated); if (intersectionDetail == IntersectionDetail.Empty) { // bail out if there is a clip and this region is not inside return HitTestResultBehavior.Continue; } } // // Check if the geometry intersects with our hittest bounds. // If not, the Visual is not hit-testable at all. if (_bboxSubgraph.IntersectsWith(geometryParams.Bounds)) { // // Determine if there is a special filter behavior defined for this // Visual. // HitTestFilterBehavior filter = HitTestFilterBehavior.Continue; if (filterCallback != null) { filter = filterCallback(this); if (filter == HitTestFilterBehavior.ContinueSkipSelfAndChildren) { return HitTestResultBehavior.Continue; } if (filter == HitTestFilterBehavior.Stop) { return HitTestResultBehavior.Stop; } } // // Hit-test against the children. // int childCount = VisualChildrenCount; if (filter != HitTestFilterBehavior.ContinueSkipChildren) { for (int i=childCount-1; i>=0; i--) { Visual child = GetVisualChild(i); if (child != null) { // Hit the scollClip bounds first, which are in the child's outer-space. Rect? scrollClip = ScrollableAreaClipField.GetValue(child); if (scrollClip.HasValue) { // Hit-testing with a Geometry and a clip should hit test with // the intersection of the geometry and the clip, not the entire geometry RectangleGeometry rectClip = new RectangleGeometry(scrollClip.Value); IntersectionDetail intersectionDetail = rectClip.FillContainsWithDetail(geometryParams.InternalHitGeometry); Debug.Assert(intersectionDetail != IntersectionDetail.NotCalculated); if (intersectionDetail == IntersectionDetail.Empty) { // Skip child if there is a scrollable clip and this region is not inside it. continue; } } // Transform the geometry below offset and transform. Matrix inv = Matrix.Identity; inv.Translate(-child._offset.X, -child._offset.Y); Transform childTransform = TransformField.GetValue(child); if (childTransform != null) { Matrix m = childTransform.Value; // If we can't invert the transform, the child is not hitable. This makes sense since // the node's rendered content is degnerated, i.e. does not really take up any space. // Skipping the child by continuing the loop. if (!m.HasInverse) { continue; } // Inverse the transform. m.Invert(); // Multiply the inverse and the offset together. // inv = inv * m; MatrixUtil.MultiplyMatrix(ref inv, ref m); } // Push the transform on the geometry params. geometryParams.PushMatrix(ref inv); // Hit-Test against the children. HitTestResultBehavior result = child.HitTestGeometry(filterCallback, resultCallback, geometryParams); // Pop the transform from the geometry params. geometryParams.PopMatrix(); // Process the result. if (result == HitTestResultBehavior.Stop) { return HitTestResultBehavior.Stop; } } } } // // Hit-test against the content of the Visual. // if (filter != HitTestFilterBehavior.ContinueSkipSelf) { GeometryHitTestResult hitResult = HitTestCore(geometryParams); if (hitResult != null) { Debug.Assert(resultCallback != null); return resultCallback(hitResult); } } } return HitTestResultBehavior.Continue; }
public static void HitTest (Visual visual, HitTestFilterCallback filter, HitTestResultCallback results, HitTestParameters parameters) { throw new NotImplementedException (); }
/// <summary> /// This method provides an internal extension point for Viewport3DVisual /// to grab the HitTestFilterCallback and ResultDelegate before it gets lost in the /// forward to HitTestCore. /// </summary> internal virtual HitTestResultBehavior HitTestPointInternal( HitTestFilterCallback filterCallback, HitTestResultCallback resultCallback, PointHitTestParameters hitTestParameters) { HitTestResult hitResult = HitTestCore(hitTestParameters); if (hitResult != null) { return resultCallback(hitResult); } return HitTestResultBehavior.Continue; }
/// <summary> /// Initiate a hit test using delegates. /// </summary> internal void HitTest( HitTestFilterCallback filterCallback, HitTestResultCallback resultCallback, HitTestParameters3D hitTestParameters) { if (resultCallback == null) { throw new ArgumentNullException("resultCallback"); } if (hitTestParameters == null) { throw new ArgumentNullException("hitTestParameters"); } VerifyAPIReadWrite(); RayHitTestParameters rayParams = hitTestParameters as RayHitTestParameters; if (rayParams != null) { // In case the user is reusing the same RayHitTestParameters rayParams.ClearResults(); HitTestResultBehavior result = RayHitTest(filterCallback, rayParams); rayParams.RaiseCallback(resultCallback, filterCallback, result); } else { // This should never happen, users can not extend the abstract HitTestParameters3D class. Invariant.Assert(false, String.Format(System.Globalization.CultureInfo.InvariantCulture, "'{0}' HitTestParameters3D are not supported on {1}.", hitTestParameters.GetType().Name, this.GetType().Name)); } }
/// <summary> /// Re-exposes the Visual base class's corresponding VisualTreeHelper implementation as public method. /// </summary> new public void HitTest(HitTestFilterCallback filterCallback, HitTestResultCallback resultCallback, HitTestParameters hitTestParameters) { base.HitTest(filterCallback, resultCallback, hitTestParameters); }
/// <summary> /// Initiate a hit test using delegates. /// </summary> internal void HitTest( HitTestFilterCallback filterCallback, HitTestResultCallback resultCallback, HitTestParameters hitTestParameters) { if (resultCallback == null) { throw new ArgumentNullException("resultCallback"); } if (hitTestParameters == null) { throw new ArgumentNullException("hitTestParameters"); } VerifyAPIReadWrite(); Precompute(); PointHitTestParameters pointParams = hitTestParameters as PointHitTestParameters; if (pointParams != null) { // Because we call dynamic code during the hit testing walk we need to back up // the original hit point in case the user's delegate throws an exception so that // we can restore it. Point backupHitPoint = pointParams.HitPoint; try { HitTestPoint(filterCallback, resultCallback, pointParams); } catch { // If an exception occured, restore the user's hit point and rethrow. pointParams.SetHitPoint(backupHitPoint); throw; } finally { Debug.Assert(Point.Equals(pointParams.HitPoint, backupHitPoint), "Failed to restore user's hit point back to the original coordinate system."); } } else { GeometryHitTestParameters geometryParams = hitTestParameters as GeometryHitTestParameters; if (geometryParams != null) { // Because we call dynamic code during the hit testing walk we need to ensure // that if the user's delegate throws an exception we restore the original // transform on the hit test geometry. #if DEBUG // Internally we replace the hit geometry with a copy which is guaranteed to have // a MatrixTransform so we do not need to worry about null dereferences here. Matrix originalMatrix = geometryParams.InternalHitGeometry.Transform.Value; #endif // DEBUG try { HitTestGeometry(filterCallback, resultCallback, geometryParams); } catch { geometryParams.EmergencyRestoreOriginalTransform(); throw; } #if DEBUG finally { Debug.Assert(Matrix.Equals(geometryParams.InternalHitGeometry.Transform.Value, originalMatrix), "Failed to restore user's hit geometry back to the original coordinate system."); } #endif // DEBUG } else { // This should never happen, users can not extend the abstract HitTestParameters class. Invariant.Assert(false, String.Format(System.Globalization.CultureInfo.InvariantCulture, "'{0}' HitTestParameters are not supported on {1}.", hitTestParameters.GetType().Name, this.GetType().Name)); } } }
void RunHitTest(Visual reference, Point point, HitTestFilterCallback filterCallback, HitTestResultCallback resultCallback) { VisualTreeHelper.HitTest(reference, filterCallback, resultCallback, new PointHitTestParameters(point)); }
internal HitTestResultBehavior HitTestPoint( HitTestFilterCallback filterCallback, HitTestResultCallback resultCallback, PointHitTestParameters pointParams) { // we do not need parameter checks because they are done in HitTest() Geometry clip = VisualClip; // Before we continue hit-testing we check against the hit-test bounds for the sub-graph. // If the point is not with-in the hit-test bounds, the sub-graph can be skipped. if (_bboxSubgraph.Contains(pointParams.HitPoint) && ((null == clip) || clip.FillContains(pointParams.HitPoint))) // Check that the hit-point is with-in the clip. { // // Determine if there is a special filter behavior defined for this // Visual. // HitTestFilterBehavior filter = HitTestFilterBehavior.Continue; if (filterCallback != null) { filter = filterCallback(this); if (filter == HitTestFilterBehavior.ContinueSkipSelfAndChildren) { return HitTestResultBehavior.Continue; } if (filter == HitTestFilterBehavior.Stop) { return HitTestResultBehavior.Stop; } } // if there is a bitmap effect transform the point // Backup the hit point so that we can restore it later on. Point originalHitPoint = pointParams.HitPoint; Point hitPoint = originalHitPoint; if (CheckFlagsAnd(VisualFlags.NodeHasEffect)) { Effect imageEffect = EffectField.GetValue(this); if (imageEffect != null) { GeneralTransform effectHitTestInverse = imageEffect.EffectMapping.Inverse; // only do work if the transform isn't the identity transform if (effectHitTestInverse != Transform.Identity) { bool ok = false; // Convert to unit space Point? unitHitPoint = Effect.WorldToUnit(originalHitPoint, _bboxSubgraph); if (unitHitPoint != null) { Point transformedPt = new Point(); // Do the transform if (effectHitTestInverse.TryTransform(unitHitPoint.Value, out transformedPt)) { // Convert back to world space Point? worldSpace = Effect.UnitToWorld(transformedPt, _bboxSubgraph); if (worldSpace != null) { hitPoint = worldSpace.Value; ok = true; } } } if (!ok) { return HitTestResultBehavior.Continue; } } } else { Debug.Assert(BitmapEffectStateField.GetValue(this) != null); // BitmapEffects are deprecated so they no longer affect hit testing. } } // // Hit test against the children. // if (filter != HitTestFilterBehavior.ContinueSkipChildren) { int childCount = VisualChildrenCount; for (int i=childCount-1; i>=0; i--) { Visual child = GetVisualChild(i); if (child != null) { // Hit the scollClip bounds first, which are in the child's outer-space. Rect? scrollClip = ScrollableAreaClipField.GetValue(child); if (scrollClip.HasValue && !scrollClip.Value.Contains(hitPoint)) { // Skip child if the point is not within the ScrollableClip. continue; } // // Transform the hit-test point below offset and transform. // Point newHitPoint = hitPoint; // Apply the offset. newHitPoint = newHitPoint - child._offset; // If we have a transform, apply the transform. Transform childTransform = TransformField.GetValue(child); if (childTransform != null) { Matrix inv = childTransform.Value; // If we can't invert the transform, the child is not hitable. This makes sense since // the node's rendered content is degenerate, i.e. does not really take up any space. // Skip the child by continuing in the loop. if (!inv.HasInverse) { continue; } inv.Invert(); newHitPoint = newHitPoint * inv; } // Set the new hittesting point into the hittest params. pointParams.SetHitPoint(newHitPoint); // Perform the hit-test against the child. HitTestResultBehavior result = child.HitTestPoint(filterCallback, resultCallback, pointParams); // Restore the hit-test point. pointParams.SetHitPoint(originalHitPoint); if (result == HitTestResultBehavior.Stop) { return HitTestResultBehavior.Stop; } } } } // // Hit test against the content of this Visual. // if (filter != HitTestFilterBehavior.ContinueSkipSelf) { // set the transformed hit point pointParams.SetHitPoint(hitPoint); HitTestResultBehavior result = HitTestPointInternal(filterCallback, resultCallback, pointParams); // restore the hit point back to its original pointParams.SetHitPoint(originalHitPoint); if (result == HitTestResultBehavior.Stop) { return HitTestResultBehavior.Stop; } } } return HitTestResultBehavior.Continue; }
public IEnumerable<IElement> HitTest(IPoint point, double radius) { var canvas = this; var selectedElements = new List<DependencyObject>(); var elippse = new EllipseGeometry() { RadiusX = radius, RadiusY = radius, Center = new Point(point.X, point.Y) }; var hitTestParams = new GeometryHitTestParameters(elippse); var resultCallback = new HitTestResultCallback(result => HitTestResultBehavior.Continue); var filterCallback = new HitTestFilterCallback( element => { if (VisualTreeHelper.GetParent(element) == canvas) selectedElements.Add(element); return HitTestFilterBehavior.Continue; }); VisualTreeHelper.HitTest(canvas, filterCallback, resultCallback, hitTestParams); return selectedElements.Cast<IElement>(); }