Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GrammaticalOptimization/SharpVectorRuntime/SvgDrawingCanvas.cs @ 13617

Last change on this file since 13617 was 12762, checked in by aballeit, 9 years ago

#2283 GUI updates, Tree-chart, MCTS Version 2 (prune leaves)

File size: 36.6 KB
Line 
1using System;
2using System.IO;
3using System.IO.Compression;
4using System.Xml;
5using System.Linq;
6using System.Text;
7using System.Diagnostics;
8using System.Collections.Generic;
9
10using IoPath = System.IO.Path;
11
12using System.Windows;
13using System.Windows.Input;
14using System.Windows.Media;
15using System.Windows.Markup;
16//using System.Windows.Media.Effects;
17using System.Windows.Media.Animation;
18using System.Windows.Shapes;
19using System.Windows.Controls;
20using System.Windows.Controls.Primitives;
21
22using SharpVectors.Runtime.Utils;
23
24namespace SharpVectors.Runtime
25{
26    /// <summary>
27    /// This is the main drawing canvas for the wiring diagrams.
28    /// </summary>
29    public class SvgDrawingCanvas : Canvas
30    {
31        #region Private Fields
32
33        private bool _drawForInteractivity;
34
35        private Rect _bounds;
36
37        private Transform _displayTransform;
38
39        private double _offsetX;
40        private double _offsetY;
41
42        private ToolTip   _tooltip;
43        private TextBlock _tooltipText;
44
45        private DrawingGroup _wholeDrawing;
46        private DrawingGroup _linksDrawing;
47        private DrawingGroup _mainDrawing;
48
49        private Drawing _hitVisual;
50
51        private DrawingVisual _hostVisual;
52
53        // Create a collection of child visual objects.
54        private List<Drawing> _drawObjects;
55
56        // Create a collection of child visual link objects.
57        private List<Drawing> _linkObjects;
58
59        private SvgAnimationLayer _animationCanvas;
60
61        #endregion
62
63        #region Constructors and Destructor
64
65        static SvgDrawingCanvas()
66        {
67            DefaultStyleKeyProperty.OverrideMetadata(typeof(SvgDrawingCanvas),
68                new FrameworkPropertyMetadata(typeof(SvgDrawingCanvas)));
69        }
70
71        public SvgDrawingCanvas()
72        {
73            _drawForInteractivity = true;
74
75            _drawObjects = new List<Drawing>();
76            _linkObjects = new List<Drawing>();
77
78            _displayTransform = Transform.Identity;
79
80            // Create a tooltip and set its position.
81            _tooltip                    = new ToolTip();
82            _tooltip.Placement          = PlacementMode.MousePoint;
83            _tooltip.PlacementRectangle = new Rect(50, 0, 0, 0);
84            _tooltip.HorizontalOffset   = 20;
85            _tooltip.VerticalOffset     = 20;
86
87            _tooltipText        = new TextBlock();
88            _tooltipText.Text   = String.Empty;
89            _tooltipText.Margin = new Thickness(6, 0, 0, 0);
90
91            //Create BulletDecorator and set it as the tooltip content.
92            Ellipse bullet = new Ellipse();
93            bullet.Height  = 10;
94            bullet.Width   = 10;
95            bullet.Fill    = Brushes.LightCyan;
96
97            BulletDecorator decorator = new BulletDecorator();
98            decorator.Bullet = bullet;
99            decorator.Margin = new Thickness(0, 0, 10, 0);
100            decorator.Child  = _tooltipText;
101
102            _tooltip.Content    = decorator;
103            _tooltip.IsOpen     = false;
104            _tooltip.Visibility = Visibility.Hidden;
105
106            //Finally, set tooltip on this canvas
107            this.ToolTip    = _tooltip;
108            this.Background = Brushes.Transparent;
109
110            _animationCanvas = new SvgAnimationLayer(this);
111        }
112
113        #endregion
114
115        #region Public Properties
116
117        public Rect Bounds
118        {
119            get
120            {
121                return _bounds;
122            }
123        }
124
125        public SvgAnimationLayer AnimationCanvas
126        {
127            get
128            {
129                return _animationCanvas;
130            }
131        }
132
133        public Transform DisplayTransform
134        {
135            get
136            {
137                return _displayTransform;
138            }
139        }
140
141        #endregion
142
143        #region Protected Properties
144
145        // Provide a required override for the VisualChildrenCount property.
146        protected override int VisualChildrenCount
147        {
148            get
149            {
150                if (_hostVisual != null)
151                {
152                    return 1;
153                }
154
155                return 0;
156            }
157        }
158
159        #endregion
160
161        #region Public Methods
162
163        public void LoadDiagrams(string fileName)
164        {
165            if (String.IsNullOrEmpty(fileName) || !File.Exists(fileName))
166            {
167                return;
168            }
169
170            this.UnloadDiagrams();
171
172            string fileExt = IoPath.GetExtension(fileName);
173
174            Cursor curCursor = this.Cursor;
175            this.Cursor = Cursors.Wait;
176            try
177            {
178                object xamlObject = null;
179
180                if (String.Equals(fileExt, ".xaml", StringComparison.OrdinalIgnoreCase))
181                {
182                    using (XmlReader xmlReader = XmlReader.Create(new StreamReader(fileName)))
183                    {
184                        xamlObject = XamlReader.Load(xmlReader);
185                    }
186                }
187                else if (String.Equals(fileExt, ".zaml", StringComparison.OrdinalIgnoreCase))
188                {
189                    using (FileStream fileStream = File.OpenRead(fileName))
190                    {
191                        using (GZipStream zipStream =
192                            new GZipStream(fileStream, CompressionMode.Decompress))
193                        {
194                            xamlObject = XamlReader.Load(zipStream);
195                        }
196                    }
197                }
198
199                if (xamlObject is SvgImage)
200                {
201                    SvgImage imageDrawing = (SvgImage)xamlObject;
202                    RenderDiagrams(imageDrawing);
203                }
204                else if (xamlObject is DrawingGroup)
205                {
206                    DrawingGroup groupDrawing = (DrawingGroup)xamlObject;
207                    RenderDiagrams(groupDrawing);
208                }
209            }
210            finally
211            {
212                this.Cursor = curCursor;
213            }
214        }
215
216        public void LoadDiagrams(DrawingGroup whole,
217            DrawingGroup links, DrawingGroup main)
218        {
219            if (whole == null)
220            {
221                return;
222            }
223
224            this.UnloadDiagrams();
225
226            this.Draw(whole, main);
227
228            _wholeDrawing = whole;
229            _linksDrawing = links;
230            _mainDrawing  = main;
231
232            this.InvalidateMeasure();
233        }
234
235        public void UnloadDiagrams()
236        {
237            _offsetX = 0;
238            _offsetY = 0;
239            _bounds  = new Rect(0, 0, 1, 1);
240
241            _wholeDrawing = null;
242
243            _displayTransform = Transform.Identity;
244
245            this.ClearVisuals();
246            this.ClearDrawings();
247
248            this.InvalidateMeasure();
249            this.InvalidateVisual();
250        }
251
252        #region RenderDiagrams Methods
253
254        public void RenderDiagrams(SvgImage image)
255        {
256            DrawingImage drawingImage = image.Source as DrawingImage;
257            if (drawingImage == null)
258            {
259                return;
260            }
261
262            DrawingGroup renderedGroup = drawingImage.Drawing as DrawingGroup;
263
264            if (renderedGroup != null)
265            {
266                this.RenderDiagrams(renderedGroup);
267            }
268        }
269
270        public void RenderDiagrams(DrawingGroup renderedGroup)
271        {
272            DrawingCollection drawings = renderedGroup.Children;
273            int linkIndex = -1;
274            int drawIndex = -1;
275            for (int i = 0; i < drawings.Count; i++)
276            {
277                Drawing drawing = drawings[i];
278                //string drawingName = drawing.GetValue(FrameworkElement.NameProperty) as string;
279                string drawingName = SvgLink.GetKey(drawing);
280                if (!String.IsNullOrEmpty(drawingName) &&
281                    String.Equals(drawingName, SvgObject.DrawLayer))
282                {
283                    drawIndex = i;
284                }
285                else if (!String.IsNullOrEmpty(drawingName) &&
286                    String.Equals(drawingName, SvgObject.LinksLayer))
287                {
288                    linkIndex = i;
289                }
290            }
291
292            DrawingGroup mainGroups = null;
293            if (drawIndex >= 0)
294            {
295                mainGroups = drawings[drawIndex] as DrawingGroup;
296            }
297            DrawingGroup linkGroups = null;
298            if (linkIndex >= 0)
299            {
300                linkGroups = drawings[linkIndex] as DrawingGroup;
301            }
302
303            this.LoadDiagrams(renderedGroup, linkGroups, mainGroups);
304
305            if (linkGroups != null)
306            {
307                _animationCanvas.LoadDiagrams(linkGroups, renderedGroup);
308            }
309
310            this.InvalidateMeasure();
311            this.InvalidateVisual();
312        }
313
314        #endregion
315
316        #endregion
317
318        #region Protected Methods
319
320        // Provide a required override for the GetVisualChild method.
321        protected override Visual GetVisualChild(int index)
322        {
323            if (_hostVisual == null)
324            {
325                return null;
326            }
327
328            return _hostVisual;
329        }
330
331        protected override Size MeasureOverride(Size constraint)
332        {
333            //_offsetX = 0;
334            //_offsetY = 0;
335
336            if (_wholeDrawing != null)
337            {
338                Rect rectBounds = _wholeDrawing.Bounds;
339
340                if (!rectBounds.IsEmpty)
341                {
342                    // Return the new size
343                    double diaWidth  = rectBounds.Width;
344                    double diaHeight = rectBounds.Height;
345                    if (rectBounds.X > 0)
346                    {
347                        //diaWidth += rectBounds.X;
348                        //_offsetX = rectBounds.X;
349                    }
350                    if (rectBounds.Y > 0)
351                    {
352                        //diaHeight += rectBounds.Y;
353                        //_offsetY = rectBounds.Y;
354                    }
355
356                    _bounds = rectBounds;
357
358                    //double sizeX = this.Width;
359                    //double sizeY = this.Height;
360                    //if ((!Double.IsNaN(sizeX) && !Double.IsInfinity(sizeX)) &&
361                    //    (!Double.IsNaN(sizeY) && !Double.IsInfinity(sizeY)))
362                    //{
363                    //    diaWidth = sizeX;
364                    //    diaHeight = sizeY;
365
366                    //    _bounds.Width  = sizeX;
367                    //    _bounds.Height = sizeY;
368                    //}
369
370                    return new Size(diaWidth, diaHeight);
371
372                    //double inflateX = 6;
373                    //double inflateY = 6;
374                    //if (rectBounds.X > 0)
375                    //{
376                    //    inflateX = rectBounds.X/2d;
377                    //}
378                    //double diaHeight = rectBounds.Height;
379                    //if (rectBounds.Y > 0)
380                    //{
381                    //    inflateY = rectBounds.Y / 2d;
382                    //}
383
384                    //rectBounds.Inflate(inflateX, inflateY);
385
386                    //_bounds = rectBounds;
387
388                    //return rectBounds.Size;
389                }
390            }
391
392            return new Size(640, 480);
393            //return base.MeasureOverride(constraint);
394        }
395
396        protected override void OnMouseDown(MouseButtonEventArgs e)
397        {
398            base.OnMouseDown(e);
399
400            if (_animationCanvas != null && _animationCanvas.HandleMouseDown(e))
401            {
402                return;
403            }
404
405            Point pt = e.GetPosition(this);
406
407            Drawing visual = HitTest(pt);
408            if (visual == null)
409            {
410                if (_tooltip != null)
411                {
412                    _tooltip.IsOpen = false;
413                    _tooltip.Visibility = Visibility.Hidden;
414                }
415
416                this.Cursor = Cursors.Arrow;
417                return;
418            }
419
420            string itemName = visual.GetValue(FrameworkElement.NameProperty) as string;
421            if (itemName == null)
422            {
423                if (_tooltip != null)
424                {
425                    _tooltip.IsOpen = false;
426                    _tooltip.Visibility = Visibility.Hidden;
427                }
428
429                return;
430            }
431            //Brush brush = null;
432            //if (_visualBrushes.ContainsKey(itemName))
433            //{
434            //    brush = _visualBrushes[itemName];
435            //}
436            //if (brush == null)
437            //{
438            //    if (_tooltip != null)
439            //    {
440            //        _tooltip.IsOpen = false;
441            //        _tooltip.Visibility = Visibility.Hidden;
442            //    }
443
444            //    return;
445            //}
446
447            //if (e.ChangedButton == MouseButton.Left)
448            //{
449            //    string brushName = brush.GetValue(FrameworkElement.NameProperty) as string;
450            //    if (!String.IsNullOrEmpty(brushName))
451            //    {
452            //        SvgLinkAction linkAction = SvgLink.GetLinkAction(visual);
453            //        if (linkAction == SvgLinkAction.LinkHtml ||
454            //            linkAction == SvgLinkAction.LinkPage)
455            //        {
456            //            _animator.Start(brushName, brush);
457            //        }
458            //    }
459            //}
460            //else if (e.ChangedButton == MouseButton.Right)
461            //{
462            //    _animator.Stop();
463            //}
464        }
465
466        protected override void OnMouseMove(MouseEventArgs e)
467        {
468            base.OnMouseMove(e);
469
470            if (_animationCanvas != null && _animationCanvas.HandleMouseMove(e))
471            {
472                return;
473            }
474
475            // Retrieve the coordinates of the mouse button event.
476            Point pt = e.GetPosition(this);
477
478            Drawing hitVisual = HitTest(pt);
479
480            //string itemName = null;
481
482            if (hitVisual == null)
483            {
484                this.Cursor = Cursors.Arrow;
485
486                if (_hitVisual != null)
487                {
488                    //itemName = _hitVisual.GetValue(FrameworkElement.NameProperty) as string;
489                    //if (itemName == null)
490                    //{
491                    //    _hitVisual = null;
492                    //    return;
493                    //}
494                    //if (_visualBrushes.ContainsKey(itemName))
495                    //{
496                    //    Brush brush = _visualBrushes[itemName];
497                    //    brush.Opacity = 0;
498                    //}
499                    _hitVisual = null;
500                }
501
502                if (_tooltip != null)
503                {
504                    _tooltip.IsOpen = false;
505                    _tooltip.Visibility = Visibility.Hidden;
506                }
507
508                return;
509            }
510            else
511            {
512                this.Cursor = Cursors.Hand;
513
514                if (hitVisual == _hitVisual)
515                {
516                    return;
517                }
518
519                if (_hitVisual != null)
520                {
521                    //itemName = _hitVisual.GetValue(FrameworkElement.NameProperty) as string;
522                    //if (itemName == null)
523                    //{
524                    //    _hitVisual = null;
525                    //    return;
526                    //}
527                    //if (_visualBrushes.ContainsKey(itemName))
528                    //{
529                    //    Brush brush = _visualBrushes[itemName];
530                    //    brush.Opacity = 0;
531                    //}
532                    _hitVisual = null;
533                }
534
535                //itemName = hitVisual.GetValue(FrameworkElement.NameProperty) as string;
536                //if (itemName == null)
537                //{
538                //    return;
539                //}
540                //if (_visualBrushes.ContainsKey(itemName))
541                //{
542                //    Brush brush = _visualBrushes[itemName];
543                //    brush.Opacity = 0.5;
544                //}
545                _hitVisual = hitVisual;
546
547                string tooltipText = SvgObject.GetTitle(_hitVisual);
548                Rect rectBounds = _hitVisual.Bounds;
549                //Drawing drawing = _hitVisual.GetValue(FrameworkElement.TagProperty) as Drawing;
550                //if (drawing != null)
551                //{
552                //    rectBounds  = drawing.Bounds;
553                //    tooltipText = SvgObject.GetTitle(drawing);
554                //}
555
556                if (_tooltip != null && !String.IsNullOrEmpty(tooltipText))
557                {
558                    _tooltip.PlacementRectangle = rectBounds;
559
560                    _tooltipText.Text = tooltipText;
561
562                    if (_tooltip.Visibility == Visibility.Hidden)
563                    {
564                        _tooltip.Visibility = Visibility.Visible;
565                    }
566
567                    _tooltip.IsOpen = true;
568                }
569                else
570                {
571                    if (_tooltip != null)
572                    {
573                        _tooltip.IsOpen = false;
574                        _tooltip.Visibility = Visibility.Hidden;
575                    }
576                }
577            }
578        }
579
580        protected override void OnMouseUp(MouseButtonEventArgs e)
581        {
582            base.OnMouseUp(e);
583        }
584
585        protected override void OnMouseLeave(MouseEventArgs e)
586        {
587            base.OnMouseLeave(e);
588
589            if (_animationCanvas != null && _animationCanvas.HandleMouseLeave(e))
590            {
591                return;
592            }
593
594            if (_tooltip != null)
595            {
596                _tooltip.IsOpen = false;
597                _tooltip.Visibility = Visibility.Hidden;
598            }
599
600            if (_hitVisual == null)
601            {
602                return;
603            }
604
605            string itemName = _hitVisual.GetValue(FrameworkElement.NameProperty) as string;
606            if (itemName == null)
607            {
608                _hitVisual = null;
609                return;
610            }
611            //if (_visualBrushes.ContainsKey(itemName))
612            //{
613            //    Brush brush = _visualBrushes[itemName];
614            //    brush.Opacity = 0;
615            //}
616            _hitVisual = null;
617
618            this.Cursor = Cursors.Arrow;
619        }
620
621        #endregion
622
623        #region Private Methods
624
625        #region Visuals Methods
626
627        private void AddVisual(DrawingVisual visual)
628        {
629            if (visual == null)
630            {
631                return;
632            }
633
634            _hostVisual = visual;
635
636            base.AddVisualChild(visual);
637            base.AddLogicalChild(visual);
638        }
639
640        private void RemoveVisual(DrawingVisual visual)
641        {
642            if (visual == null)
643            {
644                return;
645            }
646
647            base.RemoveVisualChild(visual);
648            base.RemoveLogicalChild(visual);
649        }
650
651        private void ClearVisuals()
652        {
653            if (_hostVisual != null)
654            {
655                base.RemoveVisualChild(_hostVisual);
656                base.RemoveLogicalChild(_hostVisual);
657
658                _hostVisual = null;
659            }
660        }
661
662        private void AddDrawing(Drawing visual)
663        {
664            if (visual == null)
665            {
666                return;
667            }
668
669            _drawObjects.Add(visual);
670        }
671
672        private void RemoveDrawing(Drawing visual)
673        {
674            if (visual == null)
675            {
676                return;
677            }
678
679            _drawObjects.Remove(visual);
680
681            if (_linkObjects != null && _linkObjects.Count != 0)
682            {
683                _linkObjects.Remove(visual);
684            }
685        }
686
687        private void ClearDrawings()
688        {
689            if (_drawObjects != null && _drawObjects.Count != 0)
690            {
691                _drawObjects.Clear();
692            }
693
694            if (_linkObjects != null && _linkObjects.Count != 0)
695            {
696                _linkObjects.Clear();
697            }
698        }
699
700        #endregion
701
702        #region Draw Methods
703
704        private void Draw(DrawingGroup group, DrawingGroup main)
705        {
706            DrawingVisual drawingVisual = new DrawingVisual();
707
708            DrawingContext drawingContext = drawingVisual.RenderOpen();
709
710            _offsetX = 0;
711            _offsetY = 0;
712            _displayTransform = Transform.Identity;
713
714            TranslateTransform offsetTransform = null;
715            Rect rectBounds = group.Bounds;
716            if (!rectBounds.IsEmpty)
717            {
718                // Return the new size
719                //double diaWidth = rectBounds.Width;
720                if (rectBounds.X > 0)
721                {
722                    //diaWidth += rectBounds.X;
723                    _offsetX = rectBounds.X;
724                }
725                //double diaHeight = rectBounds.Height;
726                if (rectBounds.Y > 0)
727                {
728                    //diaHeight += rectBounds.Y;
729                    _offsetY = rectBounds.Y;
730                }
731
732                _bounds = rectBounds;
733
734                if (_offsetX > 0 || _offsetY > 0)
735                {
736                    offsetTransform   = new TranslateTransform(-_offsetX, -_offsetY); 
737                    _displayTransform = new TranslateTransform(_offsetX, _offsetY); // the inverse...
738                }
739            }
740
741            //Canvas.SetTop(this, -_offsetX);
742            //Canvas.SetLeft(this, -_offsetY);
743
744            //if (offsetTransform != null)
745            //{
746            //    drawingContext.PushTransform(offsetTransform);
747            //}
748
749            drawingContext.DrawDrawing(group);
750
751            //if (offsetTransform != null)
752            //{
753            //    drawingContext.Pop();
754            //}
755
756            drawingVisual.Opacity = group.Opacity;
757
758            //Transform transform = group.Transform;
759            //if (transform == null)
760            //{
761            //    transform = offsetTransform;
762            //}
763            if (offsetTransform != null)
764            {
765                drawingVisual.Transform = offsetTransform;
766            }
767            Geometry clipGeometry = group.ClipGeometry;
768            if (clipGeometry != null)
769            {
770                drawingVisual.Clip = clipGeometry;
771            }
772
773            drawingContext.Close();
774
775            this.AddVisual(drawingVisual);
776
777            if (_drawForInteractivity)
778            {
779                if (main == null)
780                {
781                    main = group;
782                }
783
784                this.EnumerateDrawings(main);
785            }
786        }
787
788        //private void Draw(DrawingGroup group)
789        //{
790        //    DrawingVisual drawingVisual = new DrawingVisual();
791
792        //    DrawingContext drawingContext = drawingVisual.RenderOpen();
793
794        //    if (_offsetTransform != null)
795        //    {
796        //        drawingContext.PushTransform(_offsetTransform);
797        //    }
798
799        //    drawingContext.DrawDrawing(group);
800
801        //    if (_offsetTransform != null)
802        //    {
803        //        drawingContext.Pop();
804        //    }
805
806        //    drawingContext.DrawDrawing(group);
807        //    drawingVisual.Opacity = group.Opacity;
808
809        //    Transform transform = group.Transform;
810        //    if (transform != null)
811        //    {
812        //        drawingVisual.Transform = transform;
813        //    }
814        //    Geometry clipGeometry = group.ClipGeometry;
815        //    if (clipGeometry != null)
816        //    {
817        //        drawingVisual.Clip = clipGeometry;
818        //    }
819
820        //    drawingContext.Close();
821
822        //    this.AddVisual(drawingVisual);
823
824        //    if (_drawForInteractivity)
825        //    {
826        //        this.EnumerateDrawings(group);
827        //    }
828        //}
829
830        private void EnumerateDrawings(DrawingGroup group)
831        {
832            if (group == null || group == _linksDrawing)
833            {
834                return;
835            }
836
837            DrawingCollection drawings = group.Children;
838            for (int i = 0; i < drawings.Count; i++)
839            {
840                Drawing drawing = drawings[i];
841                DrawingGroup childGroup = drawing as DrawingGroup;
842                if (childGroup != null)
843                {
844                    SvgObjectType objectType = SvgObject.GetType(childGroup);
845                    if (objectType == SvgObjectType.Link)
846                    {
847                        InsertLinkDrawing(childGroup);
848                    }
849                    else if (objectType == SvgObjectType.Text)
850                    {
851                        InsertTextDrawing(childGroup);
852                    }
853                    else
854                    {
855                        EnumerateDrawings(childGroup);
856                    }
857                }
858                else
859                {
860                    InsertDrawing(drawing);
861                }
862            }
863        }
864
865        private void InsertTextDrawing(DrawingGroup group)
866        {
867            this.AddDrawing(group);
868        }
869
870        private void InsertLinkDrawing(DrawingGroup group)
871        {
872            this.AddDrawing(group);
873           
874            if (_linkObjects != null)
875            {
876                _linkObjects.Add(group);
877            }
878        }
879
880        private void InsertDrawing(Drawing drawing)
881        {
882            this.AddDrawing(drawing);
883        }
884
885        #endregion
886
887        #region HitTest Methods
888
889        //private Brush _testHitBrush;
890        //private Brush _testHitBrushPen;
891        //private Pen _testHitPen;
892        //private GeometryDrawing _testHit;
893        //private DrawingGroup _testHitGroup;
894        private Drawing HitTest(Point pt)
895        {
896            if (_linkObjects == null)
897            {
898                return null;
899            }   
900           
901            Point ptDisplay = _displayTransform.Transform(pt);
902
903            DrawingGroup groupDrawing       = null;
904            GlyphRunDrawing glyRunDrawing   = null;
905            GeometryDrawing geometryDrawing = null;
906
907            Drawing foundDrawing = null;
908
909            //for (int i = 0; i < _linkObjects.Count; i++)
910            for (int i = _linkObjects.Count - 1; i >= 0; i--)
911            {
912                Drawing drawing = _linkObjects[i];
913                if (TryCast.Cast(drawing, out geometryDrawing))
914                {
915                    if (HitTestDrawing(geometryDrawing, ptDisplay))
916                    {
917                        foundDrawing = drawing;
918                        break;
919                    }
920                }
921                else if (TryCast.Cast(drawing, out groupDrawing))
922                {
923                    if (HitTestDrawing(groupDrawing, ptDisplay))
924                    {
925                        foundDrawing = drawing;
926                        break;
927                    }
928                    else if (SvgObject.GetType(groupDrawing) == SvgObjectType.Text &&
929                        groupDrawing.Bounds.Contains(ptDisplay))
930                    {
931                        foundDrawing = drawing;
932                        break;
933                    }
934                }
935                else if (TryCast.Cast(drawing, out glyRunDrawing))
936                {
937                    if (HitTestDrawing(glyRunDrawing, ptDisplay))
938                    {
939                        foundDrawing = drawing;
940                        break;
941                    }
942                }
943            }
944
945            //if (_testHit != null)
946            //{
947            //    if (_testHitBrush != null)
948            //    {
949            //        _testHit.Brush = _testHitBrush;
950            //    }
951            //    else if (_testHitPen != null && _testHitBrushPen != null)
952            //    {
953            //        _testHit.Pen.Brush = _testHitBrushPen;
954            //    }
955
956            //    _testHit = null;
957            //    _testHitPen = null;
958            //    _testHitBrush = null;
959            //}
960            //if (_testHitGroup != null)
961            //{
962            //    _testHitGroup.BitmapEffect = null;
963            //    _testHitGroup = null;
964            //}
965
966            //_testHit = foundDrawing as GeometryDrawing;
967            //if (_testHit != null)
968            //{
969            //    _testHitBrush = _testHit.Brush;
970            //    _testHitPen = _testHit.Pen;
971
972            //    // Create and animate a Brush to set the button's Background.
973            //    SolidColorBrush animationBrush = new SolidColorBrush();
974            //    animationBrush.Color = Colors.Blue;
975
976            //    ColorAnimation colorAnimation = new ColorAnimation();
977            //    colorAnimation.From           = Colors.Blue;
978            //    colorAnimation.To             = Colors.Red;
979            //    colorAnimation.Duration       = new Duration(TimeSpan.FromMilliseconds(1000));
980            //    colorAnimation.AutoReverse    = true;
981            //    colorAnimation.RepeatBehavior = RepeatBehavior.Forever;
982
983            //    if (_testHitBrush != null)
984            //    {
985            //        _testHit.Brush = animationBrush;
986            //    }
987            //    else if (_testHitPen != null)
988            //    {
989            //        _testHitBrushPen  = _testHitPen.Brush;
990            //        _testHitPen.Brush = animationBrush;
991            //    }
992
993            //    // Apply the animation to the brush's Color property.
994            //    //animationBrush.BeginAnimation(SolidColorBrush.ColorProperty, colorAnimation);
995            //}
996            //else
997            //{
998            //    _testHitGroup = foundDrawing as DrawingGroup;
999            //    if (_testHitGroup != null)
1000            //    {
1001            //        //// Create a blur effect.
1002            //        //BlurBitmapEffect blurEffect = new BlurBitmapEffect();
1003            //        //blurEffect.Radius = 3.0;
1004
1005            //        //// Apply it to the drawing group.
1006            //        //_testHitGroup.BitmapEffect = blurEffect;
1007
1008            //        // Initialize a new OuterGlowBitmapEffect that will be applied
1009            //        // to the TextBox.
1010            //        OuterGlowBitmapEffect glowEffect = new OuterGlowBitmapEffect();
1011
1012            //        // Set the size of the glow to 30 pixels.
1013            //        glowEffect.GlowSize = 3;
1014
1015            //        // Set the color of the glow to blue.
1016            //        Color glowColor = new Color();
1017            //        glowColor.ScA = 1;
1018            //        glowColor.ScB = 0;
1019            //        glowColor.ScG = 0;
1020            //        glowColor.ScR = 1;
1021            //        glowEffect.GlowColor = glowColor;
1022
1023            //        // Set the noise of the effect to the maximum possible (range 0-1).
1024            //        glowEffect.Noise = 0;
1025
1026            //        // Set the Opacity of the effect to 75%. Note that the same effect
1027            //        // could be done by setting the ScA property of the Color to 0.75.
1028            //        glowEffect.Opacity = 0.5;
1029
1030            //        // Apply the bitmap effect to the TextBox.
1031            //        _testHitGroup.BitmapEffect = glowEffect;
1032            //    }
1033            //}   
1034
1035            return foundDrawing;
1036        }
1037
1038        private bool HitTestDrawing(GlyphRunDrawing drawing, Point pt)
1039        {
1040            if (drawing != null && drawing.Bounds.Contains(pt))
1041            {
1042                return true;
1043            }
1044
1045            return false;
1046        }
1047
1048        private bool HitTestDrawing(GeometryDrawing drawing, Point pt)
1049        {
1050            Pen pen = drawing.Pen;
1051            Brush brush = drawing.Brush;
1052            if (pen != null && brush == null)
1053            {
1054                if (drawing.Geometry.StrokeContains(pen, pt))
1055                {
1056                    return true;
1057                }
1058                else
1059                {
1060                    Geometry geometry = drawing.Geometry;
1061
1062                    EllipseGeometry ellipse = null;
1063                    RectangleGeometry rectangle = null;
1064                    PathGeometry path = null;
1065                    if (TryCast.Cast(geometry, out ellipse))
1066                    {
1067                        if (ellipse.FillContains(pt))
1068                        {
1069                            return true;
1070                        }
1071                    }
1072                    else if (TryCast.Cast(geometry, out rectangle))
1073                    {
1074                        if (rectangle.FillContains(pt))
1075                        {
1076                            return true;
1077                        }
1078                    }
1079                    else if (TryCast.Cast(geometry, out path))
1080                    {
1081                        PathFigureCollection pathFigures = path.Figures;
1082                        int itemCount = pathFigures.Count;
1083                        if (itemCount == 1)
1084                        {
1085                            if (pathFigures[0].IsClosed && path.FillContains(pt))
1086                            {
1087                                return true;
1088                            }
1089                        }
1090                        else
1091                        {
1092                            for (int f = 0; f < itemCount; f++)
1093                            {
1094                                PathFigure pathFigure = pathFigures[f];
1095                                if (pathFigure.IsClosed)
1096                                {
1097                                    PathFigureCollection testFigures = new PathFigureCollection();
1098                                    testFigures.Add(pathFigure);
1099
1100                                    PathGeometry testPath = new PathGeometry();
1101                                    testPath.Figures = testFigures;
1102
1103                                    if (testPath.FillContains(pt))
1104                                    {
1105                                        return true;
1106                                    }
1107                                }
1108                            }
1109                        }
1110                    }
1111                }
1112            }
1113            else if (brush != null && drawing.Geometry.FillContains(pt))
1114            {
1115                return true;
1116            }
1117
1118            return false;
1119        }
1120
1121        private bool HitTestDrawing(DrawingGroup group, Point pt)
1122        {
1123            if (group.Bounds.Contains(pt))
1124            {
1125                DrawingGroup groupDrawing       = null;
1126                GlyphRunDrawing glyRunDrawing   = null;
1127                GeometryDrawing geometryDrawing = null;
1128                DrawingCollection drawings = group.Children;
1129
1130                for (int i = 0; i < drawings.Count; i++)
1131                {
1132                    Drawing drawing = drawings[i];
1133                    if (TryCast.Cast(drawing, out geometryDrawing))
1134                    {
1135                        if (HitTestDrawing(geometryDrawing, pt))
1136                        {
1137                            return true;
1138                        }               
1139                    }
1140                    else if (TryCast.Cast(drawing, out groupDrawing))
1141                    {
1142                        if (HitTestDrawing(groupDrawing, pt))
1143                        {
1144                            return true;
1145                        }
1146                        SvgObjectType objectType = SvgObject.GetType(groupDrawing);
1147                        if (objectType == SvgObjectType.Text && groupDrawing.Bounds.Contains(pt))
1148                        {
1149                            return true;
1150                        }
1151                    }
1152                    else if (TryCast.Cast(drawing, out glyRunDrawing))
1153                    {
1154                        if (HitTestDrawing(glyRunDrawing, pt))
1155                        {
1156                            return true;
1157                        }
1158                    }
1159                }
1160            }
1161
1162            return false;
1163        }
1164
1165        #endregion
1166
1167        private static bool IsValidBounds(Rect rectBounds)
1168        {
1169            if (rectBounds.IsEmpty || Double.IsNaN(rectBounds.Width) || Double.IsNaN(rectBounds.Height)
1170                || Double.IsInfinity(rectBounds.Width) || Double.IsInfinity(rectBounds.Height))
1171            {
1172                return false;
1173            }
1174
1175            return true;
1176        }
1177
1178        #endregion
1179    }
1180}
Note: See TracBrowser for help on using the repository browser.