Esempio n. 1
0
        private bool HitTestDrawing(DrawingGroup group, Point pt, out Drawing hitDrawing, bool isText = false)
        {
            hitDrawing = null;
            bool isHit = false;

            if (!group.Bounds.Contains(pt))
            {
                return(isHit);
            }
            var transform = group.Transform;

            if (transform != null && !transform.Value.IsIdentity)
            {
                pt = transform.Inverse.Transform(pt);
            }

            var groupHitPath = _hitPath;

            DrawingGroup    groupDrawing    = null;
            GlyphRunDrawing glyRunDrawing   = null;
            GeometryDrawing geometryDrawing = null;

            DrawingCollection drawings = group.Children;

            for (int i = drawings.Count - 1; i >= 0; i--)
            {
                Drawing drawing = drawings[i];

//                _hitPath = _hitPath.AddChild(SvgObject.GetUniqueId(drawing));

                if (TryCast.Cast(drawing, out geometryDrawing))
                {
                    if (HitTestDrawing(geometryDrawing, pt))
                    {
//                        _hitPath = _hitPath.AddChild(SvgObject.GetUniqueId(geometryDrawing));

                        hitDrawing = drawing;
                        int orderNumber = SvgObject.GetOrder(drawing);
                        if (orderNumber >= 0)
                        {
                            _hitList[orderNumber]  = drawing;
                            _hitPaths[orderNumber] = _hitPath.AddChild(SvgObject.GetUniqueId(geometryDrawing));
                            isHit = false;
                        }
                        else
                        {
                            orderNumber = SvgObject.GetOrder(group);
                            if (orderNumber >= 0)
                            {
                                _hitList[orderNumber]  = group;
                                _hitPaths[orderNumber] = _hitPath.AddChild(SvgObject.GetUniqueId(group));
                            }
                            if (!string.IsNullOrWhiteSpace(SvgObject.GetUniqueId(group)))
                            {
                                _hitGroup = group;
                            }
                            return(true);
                        }
                    }
                }
                else if (TryCast.Cast(drawing, out groupDrawing))
                {
                    _hitPath = groupHitPath.AddChild(SvgObject.GetUniqueId(groupDrawing));

                    SvgObjectType objectType = SvgObject.GetType(groupDrawing);
//                    if (objectType == SvgObjectType.Text && groupDrawing.Bounds.Contains(pt))
                    if (objectType == SvgObjectType.Text && this.HitTestText(groupDrawing, pt, out hitDrawing))
//                    if (objectType == SvgObjectType.Text)
                    {
                        hitDrawing = drawing;
                        int orderNumber = SvgObject.GetOrder(drawing);
                        if (orderNumber >= 0)
                        {
                            _hitList[orderNumber]  = drawing;
                            _hitPaths[orderNumber] = _hitPath;
                            isHit = false;
                        }
                        else
                        {
                            return(true);
                        }
                    }
                    else
                    {
                        //                        _hitPath = _hitPath.AddChild(SvgObject.GetUniqueId(hitDrawing));

                        var currentPath = _hitPath;
                        try
                        {
                            if (HitTestDrawing(groupDrawing, pt, out hitDrawing))
                            {
//                                int orderNumber = SvgObject.GetOrder(drawing);
                                int orderNumber = SvgObject.GetOrder(hitDrawing);
                                if (orderNumber >= 0)
                                {
                                    _hitList[orderNumber]  = drawing;
                                    _hitPaths[orderNumber] = _hitPath.AddChild(SvgObject.GetUniqueId(hitDrawing));
                                    isHit = false;
                                }
                                else
                                {
                                    orderNumber = SvgObject.GetOrder(groupDrawing);
                                    if (orderNumber >= 0)
                                    {
                                        _hitList[orderNumber]  = groupDrawing;
                                        _hitPaths[orderNumber] = _hitPath.AddChild(SvgObject.GetUniqueId(groupDrawing));
                                    }
                                    return(true);
                                }
                            }
                        }
                        finally
                        {
                            _hitPath = currentPath;
                        }
                    }
                }
                else if (TryCast.Cast(drawing, out glyRunDrawing))
                {
                    if (HitTestDrawing(glyRunDrawing, pt))
                    {
//                        _hitPath = _hitPath.AddChild(SvgObject.GetUniqueId(glyRunDrawing));

                        hitDrawing = glyRunDrawing;
                        int orderNumber = SvgObject.GetOrder(drawing);
                        if (orderNumber >= 0)
                        {
                            _hitList[orderNumber]  = drawing;
                            _hitPaths[orderNumber] = _hitPath.AddChild(SvgObject.GetUniqueId(glyRunDrawing));
                            isHit = false;
                        }
                        else
                        {
                            return(true);
                        }
                    }
                }
            }
            string uniqueId = SvgObject.GetUniqueId(group);

            if (!string.IsNullOrWhiteSpace(uniqueId))
            {
                _hitGroup = group;
            }

            return(isHit);
        }
Esempio n. 2
0
        private Drawing PerformHitTest(Point pt)
        {
            if (_svgDrawing == null)
            {
                return(null);
            }

            _hitGroup = null;
            if (_hitList == null)
            {
                _hitList  = new SortedList <int, Drawing>();
                _hitPaths = new SortedList <int, WpfHitPath>();
            }
            else if (_hitList.Count != 0)
            {
                _hitList.Clear();
                _hitPaths.Clear();
            }

            var hitRootPath = new WpfHitPath();

            _hitPath = hitRootPath;

            Point ptDisplay = _displayTransform.Transform(pt);

            DrawingGroup    groupDrawing    = null;
            GlyphRunDrawing glyRunDrawing   = null;
            GeometryDrawing geometryDrawing = null;

            Drawing foundDrawing = null;

            //var isFound = false;
            //var drawingLayer = this.GetDrawingLayer(_svgDrawing, ref isFound);
            //System.Diagnostics.Trace.WriteLine("GetId: " + SvgObject.GetId(drawingLayer));
            //System.Diagnostics.Trace.WriteLine("GetName: " + SvgObject.GetName(drawingLayer));
            //System.Diagnostics.Trace.WriteLine("GetUniqueId: " + SvgObject.GetUniqueId(drawingLayer));

            //DrawingCollection drawings = drawingLayer.Children;
            DrawingCollection drawings = _svgDrawing.Children;

            for (int i = drawings.Count - 1; i >= 0; i--)
            {
                Drawing drawing = drawings[i];
                System.Diagnostics.Trace.WriteLine("GetId: " + SvgObject.GetId(drawing));
                System.Diagnostics.Trace.WriteLine("GetName: " + SvgObject.GetName(drawing));
                System.Diagnostics.Trace.WriteLine("GetUniqueId: " + SvgObject.GetUniqueId(drawing));

                if (TryCast.Cast(drawing, out geometryDrawing))
                {
                    if (HitTestDrawing(geometryDrawing, ptDisplay))
                    {
//                        _hitPath = _hitPath.AddChild(SvgObject.GetUniqueId(geometryDrawing));

                        int orderNumber = SvgObject.GetOrder(drawing);
                        if (orderNumber >= 0)
                        {
                            _hitList[orderNumber]  = drawing;
                            _hitPaths[orderNumber] = _hitPath.AddChild(SvgObject.GetUniqueId(geometryDrawing));
                        }
                        else
                        {
                            string uniqueId = SvgObject.GetUniqueId(geometryDrawing);
                            if (!string.IsNullOrWhiteSpace(uniqueId))
                            {
                                foundDrawing = drawing;
                                break;
                            }
                        }
                    }
                }
                else if (TryCast.Cast(drawing, out groupDrawing))
                {
                    //var ptSaved = ptDisplay;
                    //var transform = groupDrawing.Transform;
                    //if (transform != null && !transform.Value.IsIdentity)
                    //{
                    //    ptDisplay = transform.Inverse.Transform(ptDisplay);
                    //}

                    _hitPath = hitRootPath.AddChild(SvgObject.GetUniqueId(groupDrawing));

                    int orderNumber = SvgObject.GetOrder(groupDrawing);
                    //if (SvgObject.GetType(groupDrawing) == SvgObjectType.Text &&
                    //    groupDrawing.Bounds.Contains(ptDisplay))
                    if (SvgObject.GetType(groupDrawing) == SvgObjectType.Text &&
                        this.HitTestText(groupDrawing, ptDisplay, out foundDrawing))
                    {
                        if (orderNumber >= 0)
                        {
                            _hitList[orderNumber]  = drawing;
                            _hitPaths[orderNumber] = _hitPath;
                        }
                        else
                        {
                            foundDrawing = drawing;
                            break;
                        }
                    }
                    else
                    {
//                        _hitPath = _hitPath.AddChild(SvgObject.GetUniqueId(groupDrawing));

                        var currentPath = _hitPath;
                        try
                        {
                            if (HitTestDrawing(groupDrawing, ptDisplay, out foundDrawing))
                            {
                                if (orderNumber >= 0)
                                {
                                    _hitList[orderNumber]  = drawing;
                                    _hitPaths[orderNumber] = _hitPath.AddChild(SvgObject.GetUniqueId(foundDrawing));
                                }
                                else
                                {
                                    string uniqueId = SvgObject.GetUniqueId(foundDrawing);
                                    if (!string.IsNullOrWhiteSpace(uniqueId))
                                    {
                                        //foundDrawing = drawing;
                                        break;
                                    }
                                }
                            }
                        }
                        finally
                        {
                            _hitPath = currentPath;
                        }
                    }
                }
                else if (TryCast.Cast(drawing, out glyRunDrawing))
                {
                    if (HitTestDrawing(glyRunDrawing, ptDisplay))
                    {
//                        _hitPath = _hitPath.AddChild(SvgObject.GetUniqueId(glyRunDrawing));

                        int orderNumber = SvgObject.GetOrder(drawing);
                        if (orderNumber >= 0)
                        {
                            _hitList[orderNumber]  = drawing;
                            _hitPaths[orderNumber] = _hitPath.AddChild(SvgObject.GetUniqueId(glyRunDrawing));
                        }
                        else
                        {
                            foundDrawing = drawing;
                            break;
                        }
                    }
                }
            }

            if (_hitList.Count != 0)
            {
                var key = _hitList.LastOrDefault().Key;
                if (_hitPaths.ContainsKey(key))
                {
                    System.Diagnostics.Trace.WriteLine("Key Found: " + _hitPaths[key].Path);
                }
                return(_hitList.LastOrDefault().Value);
            }

            if (foundDrawing == null)
            {
                return(_hitGroup);
            }

            return(foundDrawing);
        }