예제 #1
0
        // Enumerate the drawings in the DrawingGroup.
        private void EnumDrawingGroup(DrawingGroup drawingGroup)
        {
            // Enumerate the drawings in the DrawingCollection.
            foreach (Drawing drawing in drawingGroup.Children)
            {
                string objectId = SvgObject.GetId(drawing);
                if (!string.IsNullOrWhiteSpace(objectId))
                {
                    _idMap[objectId] = drawing;
                }
                string objectName = (string)drawing.GetValue(FrameworkElement.NameProperty);
                if (!string.IsNullOrWhiteSpace(objectName))
                {
                    _idMap[objectName] = drawing;
                }
                string uniqueId = SvgObject.GetUniqueId(drawing);
                if (!string.IsNullOrWhiteSpace(uniqueId))
                {
                    _guidMap[uniqueId] = drawing;
                }

                DrawingGroup groupDrawing = null;

                // If the drawing is a DrawingGroup, call the function recursively.
                if (TryCast.Cast(drawing, out groupDrawing))
                {
                    SvgObjectType objectType = SvgObject.GetType(groupDrawing);

                    if (objectType != SvgObjectType.Text)
                    {
                        EnumDrawingGroup(groupDrawing);
                    }
                }
            }
        }
예제 #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);
        }