Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GrammaticalOptimization/SharpVectorRuntime/SvgAnimationLayer.cs @ 12762

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

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

File size: 23.1 KB
Line 
1using System;
2using System.Text;
3using System.Globalization;
4using System.Collections.Generic;
5
6using System.Windows;
7using System.Windows.Input;
8using System.Windows.Shapes;
9using System.Windows.Media;
10using System.Windows.Media.Animation;
11using System.Windows.Controls;
12using System.Windows.Controls.Primitives;
13
14namespace SharpVectors.Runtime
15{
16    /// <summary>
17    /// This creates a host for visuals derived from the <see cref="Canvas"/> class.
18    /// </summary>
19    /// <remarks>
20    /// This class provides layout, event handling, and container support for the
21    /// child visual objects.
22    /// </remarks>
23    public sealed class SvgAnimationLayer : DependencyObject
24    {
25        #region Private Fields
26
27        private Rect _bounds;
28
29        private Color _colorText;
30        private Color _colorLink;
31        private Color _colorSelected;
32        private Color _colorHover;
33
34        private ToolTip _tooltip;
35        private TextBlock _tooltipText;
36
37        private Transform _displayTransform;
38
39        private DrawingGroup _wholeDrawing;
40        private DrawingGroup _linksDrawing;
41
42        // Create a collection of child visual objects.
43        private List<Drawing> _linkObjects;
44        private List<Drawing> _drawObjects;
45
46        private Drawing _hitVisual;
47        private Drawing _selectedVisual;
48
49        private SvgAnimator _animator;
50
51        private SvgDrawingCanvas _drawingCanvas;
52
53        private Dictionary<string, Brush> _visualBrushes;
54
55        #endregion
56
57        #region Constructors and Destructor
58
59        public SvgAnimationLayer(SvgDrawingCanvas drawingCanvas)
60        {
61            _animator       = new SvgAnimator();
62            _drawingCanvas  = drawingCanvas;
63
64            _displayTransform = Transform.Identity;
65
66            _colorText      = Colors.Black;
67            _colorLink      = Colors.Blue;
68            _colorSelected  = Colors.Red;
69            _colorHover     = (Color)ColorConverter.ConvertFromString("#ffa500");
70
71            _visualBrushes  = new Dictionary<string, Brush>(StringComparer.OrdinalIgnoreCase);
72
73            _drawObjects       = new List<Drawing>();
74            _linkObjects       = new List<Drawing>();
75
76            // Create a tooltip and set its position.
77            _tooltip                    = new ToolTip();
78            _tooltip.Placement          = PlacementMode.MousePoint;
79            _tooltip.PlacementRectangle = new Rect(50, 0, 0, 0);
80            _tooltip.HorizontalOffset   = 20;
81            _tooltip.VerticalOffset     = 20;
82
83            _tooltipText        = new TextBlock();
84            _tooltipText.Text   = String.Empty;
85            _tooltipText.Margin = new Thickness(6, 0, 0, 0);
86
87            //Create BulletDecorator and set it as the tooltip content.
88            Ellipse bullet = new Ellipse();
89            bullet.Height  = 10;
90            bullet.Width   = 10;
91            bullet.Fill    = Brushes.LightCyan;
92
93            BulletDecorator decorator = new BulletDecorator();
94            decorator.Bullet = bullet;
95            decorator.Margin = new Thickness(0, 0, 10, 0);
96            decorator.Child  = _tooltipText;
97
98            _tooltip.Content    = decorator;
99            _tooltip.IsOpen     = false;
100            _tooltip.Visibility = Visibility.Hidden;
101
102            //Finally, set tooltip on this canvas
103            _drawingCanvas.ToolTip    = _tooltip;
104            //_drawingCanvas.Background = Brushes.Transparent;
105        }
106
107        #endregion
108
109        #region Public Properties
110
111        public Transform DisplayTransform
112        {
113            get
114            {
115                return _displayTransform;
116            }
117            set
118            {
119                if (value == null)
120                {
121                    _displayTransform = new MatrixTransform(Matrix.Identity);
122                }
123                else
124                {
125                    _displayTransform = value;
126                }
127            }
128        }
129
130        #endregion
131
132        #region Public Methods
133
134        public void LoadDiagrams(DrawingGroup linkGroups, DrawingGroup wholeGroup)
135        {
136            if (linkGroups == null)
137            {
138                return;
139            }
140            DrawingCollection drawings = linkGroups.Children;
141            if (drawings == null || drawings.Count == 0)
142            {
143                return;
144            }
145            else if (drawings.Count == 1)
146            {
147                DrawingGroup layerGroup = drawings[0] as DrawingGroup;
148                if (layerGroup != null)
149                {
150                    string elementId = SvgObject.GetId(layerGroup);
151                    if (!String.IsNullOrEmpty(elementId) &&
152                        String.Equals(elementId, "IndicateLayer", StringComparison.OrdinalIgnoreCase))
153                    {
154                        this.LoadLayerDiagrams(layerGroup);
155
156                        _wholeDrawing = wholeGroup;
157                        _linksDrawing = linkGroups;
158
159                        return;
160                    }
161                }
162            }
163
164            this.UnloadDiagrams();             
165
166            for (int i = 0; i < drawings.Count; i++)
167            {
168                DrawingGroup childGroup = drawings[i] as DrawingGroup;
169                if (childGroup != null)
170                {
171                    string groupName = SvgLink.GetKey(childGroup);
172                    //string groupName = childGroup.GetValue(FrameworkElement.NameProperty) as string;
173                    if (String.IsNullOrEmpty(groupName))
174                    {
175                        if (childGroup.Children != null && childGroup.Children.Count == 1)
176                        {
177                            this.AddDrawing(childGroup);
178                        }
179                        else
180                        {
181                            throw new InvalidOperationException("Error: The link group is in error.");
182                        }
183                    }
184                    else
185                    {
186                        if (childGroup.Children != null && childGroup.Children.Count == 1)
187                        {
188                            this.AddDrawing(childGroup, groupName);
189                        }
190                        else
191                        {
192                            throw new InvalidOperationException(
193                                String.Format("Error: The link group is in error - {0}", groupName));
194                        }
195                    }
196                }
197            }
198
199            _wholeDrawing = wholeGroup;
200            _linksDrawing = linkGroups;
201
202            if (_drawingCanvas != null)
203            {
204                _displayTransform = _drawingCanvas.DisplayTransform;
205            }
206        }
207
208        public void UnloadDiagrams()
209        {
210            _displayTransform = Transform.Identity;
211
212            _bounds = new Rect(0, 0, 1, 1);
213
214            //_brushIndex = 0;
215
216            if (_animator != null)
217            {
218                _animator.Stop();
219            }
220
221            this.ClearVisuals();
222
223            if (_tooltip != null)
224            {
225                _tooltip.IsOpen = false;
226                _tooltip.Visibility = Visibility.Hidden;
227            }
228
229            //this.RenderTransform = new TranslateTransform(0, 0);
230
231            _wholeDrawing = null;
232            _linksDrawing = null;
233        }
234
235        #endregion
236
237        #region Public Mouse Methods
238
239        public bool HandleMouseMove(MouseEventArgs e)
240        {
241            // Retrieve the coordinates of the mouse button event.
242            Point pt = e.GetPosition(_drawingCanvas);
243
244            Drawing hitVisual = HitTest(pt);
245
246            if (_selectedVisual != null && hitVisual == _selectedVisual)
247            {
248                _drawingCanvas.Cursor = Cursors.Hand;
249
250                return true;
251            }
252
253            string itemName = null;
254
255            if (hitVisual == null)
256            {
257                if (_hitVisual != null)
258                {
259                    itemName = _hitVisual.GetValue(FrameworkElement.NameProperty) as string;
260                    if (itemName == null)
261                    {
262                        _hitVisual = null;
263                        return false;
264                    }
265                    if (_visualBrushes.ContainsKey(itemName) && (_hitVisual != _selectedVisual))
266                    {
267                        SolidColorBrush brush = _visualBrushes[itemName] as SolidColorBrush;
268                        if (brush != null)
269                        {
270                            brush.Color = _colorLink;
271                        }
272                    }
273                    _hitVisual = null;
274                }
275
276                if (_tooltip != null)
277                {
278                    _tooltip.IsOpen     = false;
279                    _tooltip.Visibility = Visibility.Hidden;
280                }
281 
282                return false;
283            }
284            else
285            {
286                _drawingCanvas.Cursor = Cursors.Hand;
287
288                if (hitVisual == _hitVisual)
289                {
290                    return false;
291                }
292
293                if (_hitVisual != null)
294                {
295                    itemName = _hitVisual.GetValue(FrameworkElement.NameProperty) as string;
296                    if (itemName == null)
297                    {
298                        _hitVisual = null;
299                        return false;
300                    }
301                    if (_visualBrushes.ContainsKey(itemName) && (_hitVisual != _selectedVisual))
302                    {
303                        SolidColorBrush brush = _visualBrushes[itemName] as SolidColorBrush;
304                        if (brush != null)
305                        {
306                            brush.Color = _colorLink;
307                        }
308                    }
309                    _hitVisual = null;
310                }
311
312                itemName = hitVisual.GetValue(FrameworkElement.NameProperty) as string;
313                if (itemName == null)
314                {
315                    return false;
316                }
317                if (_visualBrushes.ContainsKey(itemName))
318                {
319                    SolidColorBrush brush = _visualBrushes[itemName] as SolidColorBrush;
320                    if (brush != null)
321                    {
322                        brush.Color = _colorHover;
323                    }
324                }
325                _hitVisual = hitVisual;
326
327                string tooltipText          = itemName;
328                SvgLinkAction linkAction = SvgLink.GetAction(hitVisual);
329                if (linkAction == SvgLinkAction.LinkTooltip &&
330                    _tooltip != null && !String.IsNullOrEmpty(tooltipText))
331                {
332                    Rect rectBounds = hitVisual.Bounds;
333
334                    _tooltip.PlacementRectangle = rectBounds;
335
336                    _tooltipText.Text = tooltipText;
337
338                    if (_tooltip.Visibility == Visibility.Hidden)
339                    {
340                        _tooltip.Visibility = Visibility.Visible;
341                    }
342
343                    _tooltip.IsOpen = true;
344                }
345                else
346                {
347                    if (_tooltip != null)
348                    {
349                        _tooltip.IsOpen     = false;
350                        _tooltip.Visibility = Visibility.Hidden;
351                    }
352                }
353            }
354
355            return true;
356        }
357
358        public bool HandleMouseDown(MouseButtonEventArgs e)
359        {
360            Point pt = e.GetPosition(_drawingCanvas);
361
362            Drawing visual = HitTest(pt);
363            if (visual == null)
364            {
365                if (_tooltip != null)
366                {
367                    _tooltip.IsOpen     = false;
368                    _tooltip.Visibility = Visibility.Hidden;
369                }
370 
371                return false;
372            }
373
374            if (_selectedVisual != null && visual == _selectedVisual)
375            {
376                _drawingCanvas.Cursor = Cursors.Hand;
377
378                return true;
379            }
380
381            string itemName = visual.GetValue(FrameworkElement.NameProperty) as string;
382            if (itemName == null)
383            {
384                if (_tooltip != null)
385                {
386                    _tooltip.IsOpen = false;
387                    _tooltip.Visibility = Visibility.Hidden;
388                }
389
390                return false;
391            }
392            SolidColorBrush brush = null;
393            if (_visualBrushes.ContainsKey(itemName))
394            {
395                brush = _visualBrushes[itemName] as SolidColorBrush;
396
397                if (brush != null)
398                {
399                    brush.Color = _colorSelected;
400                }
401            }
402            if (brush == null)
403            {
404                if (_tooltip != null)
405                {
406                    _tooltip.IsOpen     = false;
407                    _tooltip.Visibility = Visibility.Hidden;
408                }
409
410                return false;
411            }
412            if (_selectedVisual != null)
413            {
414                itemName = _selectedVisual.GetValue(FrameworkElement.NameProperty) as string;
415                if (itemName == null)
416                {
417                    return false;
418                }
419                if (_visualBrushes.ContainsKey(itemName))
420                {
421                    brush = _visualBrushes[itemName] as SolidColorBrush;
422
423                    if (brush != null)
424                    {
425                        brush.Color = _colorLink;
426                    }
427                }
428                else
429                {
430                    return false;
431                }
432            }
433
434            _selectedVisual = visual;
435
436            if (e.ChangedButton == MouseButton.Left)
437            {
438                string brushName = brush.GetValue(FrameworkElement.NameProperty) as string;
439                if (!String.IsNullOrEmpty(brushName))
440                {
441                    SvgLinkAction linkAction = SvgLink.GetAction(visual);
442                    if (linkAction == SvgLinkAction.LinkHtml ||
443                        linkAction == SvgLinkAction.LinkPage)
444                    {
445                        _animator.Start(brushName, brush);
446                    }
447                }
448            }
449            else if (e.ChangedButton == MouseButton.Right)
450            {
451                _animator.Stop();
452            }
453
454            return true;
455        }
456
457        public bool HandleMouseLeave(MouseEventArgs e)
458        {
459            if (_tooltip != null)
460            {
461                _tooltip.IsOpen     = false;
462                _tooltip.Visibility = Visibility.Hidden;
463            }
464
465            if (_hitVisual == null)
466            {
467                return false;
468            }
469
470            string itemName = _hitVisual.GetValue(FrameworkElement.NameProperty) as string;
471            if (itemName == null)
472            {
473                _hitVisual = null;
474                return false;
475            }
476            if (_visualBrushes.ContainsKey(itemName) && (_hitVisual != _selectedVisual))
477            {
478                SolidColorBrush brush = _visualBrushes[itemName] as SolidColorBrush;
479                if (brush != null)
480                {
481                    brush.Color = _colorLink;
482                }
483            }
484            _hitVisual = null;
485
486            return true;
487        }
488
489        #endregion
490
491        #region Private Methods
492
493        private void ClearVisuals()
494        {
495            if (_drawObjects != null && _drawObjects.Count != 0)
496            {
497                _drawObjects.Clear();
498            }
499
500            if (_linkObjects != null && _linkObjects.Count != 0)
501            {
502                _linkObjects.Clear();
503            }
504        }
505
506        private Drawing HitTest(Point pt)
507        {
508            Point ptDisplay = _displayTransform.Transform(pt);
509
510            for (int i = 0; i < _linkObjects.Count; i++)
511            {
512                Drawing drawing = _linkObjects[i];
513                if (drawing.Bounds.Contains(ptDisplay))
514                {
515                    return drawing;
516                }
517            }
518
519            return null;
520        }
521
522        private void AddDrawing(DrawingGroup group)
523        {               
524        }
525
526        private void AddDrawing(DrawingGroup group, string name)
527        {               
528        }
529
530        private void AddTextDrawing(DrawingGroup group, string name)
531        {
532            bool isHyperlink = false;
533            if (name == "x11201_1_")
534            {
535                isHyperlink = true;
536            }
537
538            SolidColorBrush textBrush = null;
539            if (name.StartsWith("XMLID_", StringComparison.OrdinalIgnoreCase))
540            {
541                textBrush = new SolidColorBrush(_colorText);
542            }
543            else
544            {
545                isHyperlink = true;
546                textBrush = new SolidColorBrush(_colorLink);
547            }
548            string brushName = name + "_Brush";
549            textBrush.SetValue(FrameworkElement.NameProperty, brushName);
550
551            DrawingCollection drawings = group.Children;
552            int itemCount = drawings != null ? drawings.Count : 0;
553            for (int i = 0; i < itemCount; i++)
554            {
555                DrawingGroup drawing = drawings[i] as DrawingGroup;
556                if (drawing != null)
557                {
558                    for (int j = 0; j < drawing.Children.Count; j++)
559                    {
560                        GlyphRunDrawing glyphDrawing = drawing.Children[j] as GlyphRunDrawing;
561                        if (glyphDrawing != null)
562                        {
563                            glyphDrawing.ForegroundBrush = textBrush;
564                        }
565                    }
566                }
567            }
568
569            if (isHyperlink)
570            {
571                _visualBrushes[name] = textBrush;
572
573                _linkObjects.Add(group);
574            }
575        }
576
577        private void AddGeometryDrawing(GeometryDrawing drawing)
578        {               
579        }
580
581        private void AddGeometryDrawing(GeometryDrawing drawing, string name)
582        {               
583        }
584
585        private static bool IsValidBounds(Rect rectBounds)
586        {
587            if (rectBounds.IsEmpty || Double.IsNaN(rectBounds.Width) || Double.IsNaN(rectBounds.Height)
588                || Double.IsInfinity(rectBounds.Width) || Double.IsInfinity(rectBounds.Height))
589            {
590                return false;
591            }
592
593            return true;
594        }
595
596        private static bool TryCast<TBase, TDerived>(TBase baseObj, out TDerived derivedObj)
597            where TDerived : class, TBase
598        {
599            return (derivedObj = baseObj as TDerived) != null;
600        }
601
602        private void LoadLayerDiagrams(DrawingGroup layerGroup)
603        {
604            this.UnloadDiagrams();
605
606            DrawingCollection drawings = layerGroup.Children;
607
608            DrawingGroup drawGroup = null;
609            GeometryDrawing drawGeometry = null;
610            for (int i = 0; i < drawings.Count; i++)
611            {
612                Drawing drawing = drawings[i];
613                if (TryCast(drawing, out drawGroup))
614                {
615                    string groupName = SvgLink.GetKey(drawGroup);
616                    if (String.IsNullOrEmpty(groupName))
617                    {
618                        groupName = SvgObject.GetId(drawGroup);
619                    }
620                    //string groupName = childGroup.GetValue(FrameworkElement.NameProperty) as string;
621                    if (String.IsNullOrEmpty(groupName))
622                    {
623                        LoadLayerGroup(drawGroup);
624                    }
625                    else
626                    {
627                        SvgObjectType elementType = SvgObject.GetType(drawGroup);
628
629                        if (elementType == SvgObjectType.Text)
630                        {
631                            this.AddTextDrawing(drawGroup, groupName);
632                        }
633                        else
634                        {   
635                            if (drawGroup.Children != null && drawGroup.Children.Count == 1)
636                            {
637                                this.AddDrawing(drawGroup, groupName);
638                            }
639                            else
640                            {
641                                //throw new InvalidOperationException(
642                                //    String.Format("Error: The link group is in error - {0}", groupName));
643                            }
644                        }
645                    }
646                }
647                else if (TryCast(drawing, out drawGeometry))
648                {
649                    this.AddGeometryDrawing(drawGeometry);
650                }
651            }
652
653            if (_drawingCanvas != null)
654            {
655                _displayTransform = _drawingCanvas.DisplayTransform;
656            }
657        }
658
659        private void LoadLayerGroup(DrawingGroup group)
660        {
661            DrawingCollection drawings = group.Children;
662
663            DrawingGroup drawGroup = null;
664            GeometryDrawing drawGeometry = null;
665            for (int i = 0; i < drawings.Count; i++)
666            {
667                Drawing drawing = drawings[i];
668                if (TryCast(drawing, out drawGroup))
669                {
670                    string groupName = SvgLink.GetKey(drawGroup);
671                    if (String.IsNullOrEmpty(groupName))
672                    {
673                        groupName = SvgObject.GetId(drawGroup);
674                    }
675                    //string groupName = childGroup.GetValue(FrameworkElement.NameProperty) as string;
676                    if (String.IsNullOrEmpty(groupName))
677                    {
678                        LoadLayerGroup(drawGroup);
679                    }
680                    else
681                    {
682                        SvgObjectType elementType = SvgObject.GetType(drawGroup);
683
684                        if (elementType == SvgObjectType.Text)
685                        {
686                            this.AddTextDrawing(drawGroup, groupName);
687                        }
688                        else
689                        {   
690                            if (drawGroup.Children != null && drawGroup.Children.Count == 1)
691                            {
692                                this.AddDrawing(drawGroup, groupName);
693                            }
694                            else
695                            {
696                                //throw new InvalidOperationException(
697                                //    String.Format("Error: The link group is in error - {0}", groupName));
698                            }
699                        }
700                    }
701                }
702                else if (TryCast(drawing, out drawGeometry))
703                {
704                    this.AddGeometryDrawing(drawGeometry);
705                }
706            }
707        }
708
709
710        #endregion
711    }
712}
Note: See TracBrowser for help on using the repository browser.