Example #1
0
 public void Init(ILayoutEngine obj)
 {
     obj.Relayout(null);
 }
        public void Refresh()
        {
            if (Presentation == null)
            {
                return;
            }

            var layoutModule         = Presentation.GetModule <IGraphLayoutModule>();
            var transformationModule = myPresentation.GetModule <ITransformationModule>();

            // current assumption: it is enough to check the nodes as we would not render edges independent from nodes

            var visibleNodes = transformationModule.Graph.Nodes
                               .Where(node => Presentation.Picking.Pick(node))
                               .ToList();

            if (visibleNodes.Count == 0 && myDrawingElements.Count == 0)
            {
                // no visible nodes and this also already been reflected in the rendering result (canvas is empty)
                return;
            }

            var reLayout = visibleNodes
                           .Any(node => layoutModule.GetLayout(node) == null) ||
                           !myTransformationsJournal.IsEmpty;

            if (reLayout)
            {
                Contract.Invariant(LayoutEngine != null, "LayoutEngine not set");

                Debug.WriteLine("Relayouting");
                LayoutEngine.Relayout(Presentation);

                // reLayout requires reRender
                myDrawingElements.Clear();
            }

            this.SnapsToDevicePixels = true;

            if (myDrawingElements.Count == 0)
            {
                Debug.WriteLine("Re-Rendering");

                RemoveVisualChild(myDrawing);

                myDrawing.Children.Clear();

                myDrawing = new DrawingVisual();

                // first draw edges so that in case of overlap with nodes nodes are on top
                foreach (var edge in transformationModule.Graph.Edges)
                {
                    if (!Presentation.Picking.Pick(edge))
                    {
                        continue;
                    }

                    var visual = new EdgeVisual(edge, Presentation, myOldScaling);

                    myDrawingElements.Add(edge.Id, visual);

                    var layoutState = layoutModule.GetLayout(edge);
                    visual.Draw(layoutState);

                    myDrawing.Children.Add(visual.Visual);
                }

                foreach (var node in transformationModule.Graph.Nodes)
                {
                    if (!Presentation.Picking.Pick(node))
                    {
                        continue;
                    }

                    var visual = new NodeVisual(node, Presentation);

                    myDrawingElements.Add(node.Id, visual);

                    var layoutState = layoutModule.GetLayout(node);
                    visual.Draw(layoutState);

                    myDrawing.Children.Add(visual.Visual);
                }

                foreach (var cluster in transformationModule.Graph.Clusters)
                {
                    if (!cluster.Nodes.Any(n => Presentation.Picking.Pick(n)))
                    {
                        continue;
                    }

                    var visual = new ClusterVisual(cluster, Presentation);

                    myDrawingElements.Add(cluster.Id, visual);

                    visual.Draw(myDrawingElements);

                    myDrawing.Children.Insert(0, visual.Visual);
                }

                AddVisualChild(myDrawing);

                var selectionModule = Presentation.GetPropertySetFor <Selection>();
                foreach (var e in selectionModule.Items)
                {
                    AbstractElementVisual drawing;
                    if (myDrawingElements.TryGetValue(e.OwnerId, out drawing))
                    {
                        drawing.Select(e.IsSelected);
                    }
                }

                // clear journals - to avoid considering out-dated infos on next refresh
                myTransformationsJournal.Clear();
                myNodeMaskJournal.Clear();
                mySelectionJournal.Clear();

                InvalidateMeasure();

                RenderingFinished?.Invoke(this, EventArgs.Empty);
            }
            else
            {
                if (!myNodeMaskJournal.IsEmpty)
                {
                    foreach (var node in transformationModule.Graph.Nodes)
                    {
                        AbstractElementVisual visual;
                        if (myDrawingElements.TryGetValue(node.Id, out visual))
                        {
                            SetVisibility((NodeVisual)visual,
                                          Presentation.Picking.Pick(node),
                                          v => v.Draw(layoutModule.GetLayout(v.Owner)));
                        }
                    }

                    foreach (var edge in transformationModule.Graph.Edges)
                    {
                        AbstractElementVisual visual;
                        if (myDrawingElements.TryGetValue(edge.Id, out visual))
                        {
                            SetVisibility((EdgeVisual)visual,
                                          Presentation.Picking.Pick(edge),
                                          v => v.Draw(layoutModule.GetLayout(v.Owner)));
                        }
                    }

                    // if node visibility changed we should check whether clusters have to be redrawn
                    foreach (var cluster in transformationModule.Graph.Clusters)
                    {
                        if (cluster.Nodes.Any(n => Presentation.Picking.Pick(n)))
                        {
                            if (!myDrawingElements.ContainsKey(cluster.Id))
                            {
                                var visual = new ClusterVisual(cluster, Presentation);

                                myDrawingElements.Add(cluster.Id, visual);

                                visual.Draw(myDrawingElements);

                                myDrawing.Children.Insert(0, visual.Visual);
                            }
                        }
                        else
                        {
                            AbstractElementVisual visual;
                            if (myDrawingElements.TryGetValue(cluster.Id, out visual))
                            {
                                myDrawingElements.Remove(cluster.Id);

                                myDrawing.Children.Remove(visual.Visual);
                            }
                        }
                    }

                    myNodeMaskJournal.Clear();
                    InvalidateVisual();
                }

                if (!mySelectionJournal.IsEmpty)
                {
                    var selectionModule = Presentation.GetPropertySetFor <Selection>();
                    foreach (var e in mySelectionJournal.Entries)
                    {
                        AbstractElementVisual visual;
                        if (myDrawingElements.TryGetValue(e.OwnerId, out visual))
                        {
                            // for each change apply new status
                            visual.Select(selectionModule.TryGet(e.OwnerId)?.IsSelected ?? false);
                        }
                    }

                    mySelectionJournal.Clear();
                    InvalidateVisual();
                }

                if (Math.Abs(myCurrentScaling - myOldScaling) / myOldScaling > 0.15d)
                {
                    myOldScaling = myCurrentScaling;

                    foreach (var edge in myDrawingElements.Values.OfType <EdgeVisual>())
                    {
                        edge.ApplyZoomFactor(myCurrentScaling);
                    }

                    InvalidateVisual();
                }
            }
        }