GetBoundingBoxOfNodes() public static method

public static GetBoundingBoxOfNodes ( List nodes ) : Rect
nodes List
return System.Windows.Rect
Example #1
0
        public void ZoomToFitNodes()
        {
            var bBox = new Rect();

            //if (item.Name.Contains("Align"))
            //bBox = Node.GetBoundingBoxOfNodes(HostCanvas.SelectedNodes.ToList());
            // fit pan
            bBox = Node.GetBoundingBoxOfNodes(NodeCollection.ToList());

            var deltaX = bBox.Left + bBox.Width / 2 - ActualWidth / 2;
            var deltaY = bBox.Top + bBox.Height / 2 - ActualHeight / 2;

            foreach (var node in NodeCollection)
            {
                node.Left -= deltaX;
                node.Top  -= deltaY;
            }

            this.Refresh();

            // fit scale
            bBox = Node.GetBoundingBoxOfNodes(NodeCollection.ToList());

            var ratioX = bBox.Width / ActualWidth;
            var ratioY = bBox.Height / ActualHeight;
            var ratio  = Math.Max(ratioX, ratioY);

            //ratio =Math.Ceiling(ratio*10)/10-1;
            ratio -= 1;

            if (ratio < 0)
            {
                // HostCanvas.DoZoomIn(new Point(HostCanvas.ActualWidth/2, HostCanvas.ActualHeight/2),
                //     Math.Abs(ratio));
            }

            this.Refresh();

            // fit pan
            bBox = Node.GetBoundingBoxOfNodes(NodeCollection.ToList());

            deltaX = bBox.Left + bBox.Width / 2 - ActualWidth / 2;
            deltaY = bBox.Top + bBox.Height / 2 - ActualHeight / 2;

            foreach (var node in NodeCollection)
            {
                node.Left -= deltaX;
                node.Top  -= deltaY;
            }
            this.Refresh();
        }
Example #2
0
        /*
         * public void DoDynamicAnimation(object sender, MouseButtonEventArgs args)
         * {
         *  for (var i = 0; i < 36; ++i)
         *  {
         *      // var e = new Button { Width = 50, Height = 16, Content="Test" };
         *
         *      //var e = new Ellipse { Width = 16, Height = 16, Fill = SystemColors.HighlightBrush };
         *      //var e = new Ellipse { Width = 6, Height = 6, Fill=Brushes.HotPink };
         *
         *      var e = new SliderNode(this) {Left = Mouse.GetPosition(this).X, Top = Mouse.GetPosition(this).Y};
         *
         *      // var e = new TextBlock { Text = "Test" };
         *      // var e = new Slider { Width = 100 };
         *
         *      // var e = new ProgressBar { Width = 100 , Height =10, Value=i };
         *
         *      // var e = =new DataGrid{Width=100, Height=100};
         *      // var e = new TextBox { Text = "Hallo" };
         *      // var e = new Label { Content = "Halllo" };
         *      // var e = new RadioButton { Content="dfsdf" };
         *
         *      //Canvas.SetLeft(e, Mouse.GetPosition(this).X);
         *      //Canvas.SetTop(e, Mouse.GetPosition(this).Y);
         *
         *      var tg = new TransformGroup();
         *      var translation = new TranslateTransform(30, 0);
         *      var translationName = "myTranslation" + translation.GetHashCode();
         *      RegisterName(translationName, translation);
         *      tg.Children.Add(translation);
         *      tg.Children.Add(new RotateTransform(i*10));
         *      e.RenderTransform = tg;
         *
         *      NodeCollection.Add(e);
         *      Children.Add(e);
         *
         *      var anim = new DoubleAnimation(3, 250, new Duration(new TimeSpan(0, 0, 0, 2, 0)))
         *      {
         *          EasingFunction = new PowerEase {EasingMode = EasingMode.EaseOut}
         *      };
         *
         *      var s = new Storyboard();
         *      Storyboard.SetTargetName(s, translationName);
         *      Storyboard.SetTargetProperty(s, new PropertyPath(TranslateTransform.YProperty));
         *      var storyboardName = "s" + s.GetHashCode();
         *      Resources.Add(storyboardName, s);
         *
         *      s.Children.Add(anim);
         *
         *      s.Completed +=
         *          (sndr, evtArgs) =>
         *          {
         *              //panel.Children.Remove(e);
         *              Resources.Remove(storyboardName);
         *              UnregisterName(translationName);
         *          };
         *      s.Begin();
         *  }
         * }
         */


        public void VplControl_KeyUp(object sender, KeyEventArgs e)
        {
            switch (e.Key)
            {
            case Key.Delete:

                foreach (var node in SelectedNodes)
                {
                    node.Delete();
                }

                SelectedNodes.Clear();
                break;

            case Key.C:
            {
                if (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl))
                {
                    tempCollection = new TrulyObservableCollection <Node>();


                    foreach (var node in SelectedNodes)
                    {
                        tempCollection.Add(node);
                    }
                }
            }
            break;

            case Key.V:
            {
                if (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl))
                {
                    if (tempCollection == null)
                    {
                        return;
                    }
                    if (tempCollection.Count == 0)
                    {
                        return;
                    }

                    var bBox = Node.GetBoundingBoxOfNodes(tempCollection.ToList());

                    var copyPoint  = new Point(bBox.Left + bBox.Size.Width / 2, bBox.Top + bBox.Size.Height / 2);
                    var pastePoint = Mouse.GetPosition(this);

                    var delta = Point.Subtract(pastePoint, copyPoint);

                    SelectedNodes.Clear();

                    var alreadyClonedConnectors = new List <Connector>();
                    var copyConnections         = new List <CopyConnection>();

                    // copy nodes from clipboard to canvas
                    foreach (var node in tempCollection)
                    {
                        var newNode = node.Clone();

                        newNode.Left += delta.X;
                        newNode.Top  += delta.Y;

                        newNode.Left = Convert.ToInt32(newNode.Left);
                        newNode.Top  = Convert.ToInt32(newNode.Top);

                        NodeCollection.Add(newNode);

                        copyConnections.Add(new CopyConnection {
                                NewNode = newNode, OldNode = node
                            });
                    }

                    foreach (var cc in copyConnections)
                    {
                        var counter = 0;

                        foreach (var conn in cc.OldNode.InputPorts)
                        {
                            foreach (var connector in conn.ConnectedConnectors)
                            {
                                if (!alreadyClonedConnectors.Contains(connector))
                                {
                                    Connector newConnector = null;

                                    // start and end node are contained in selection
                                    if (tempCollection.Contains(connector.StartPort.ParentNode))
                                    {
                                        var cc2 =
                                            copyConnections.FirstOrDefault(
                                                i => Equals(i.OldNode, connector.StartPort.ParentNode));

                                        if (cc2 != null)
                                        {
                                            newConnector = new Connector(this, cc2.NewNode.OutputPorts[0],
                                                                         cc.NewNode.InputPorts[counter]);
                                        }
                                    }
                                    // only end node is contained in selection
                                    else
                                    {
                                        newConnector = new Connector(this, connector.StartPort,
                                                                     cc.NewNode.InputPorts[counter]);
                                    }

                                    if (newConnector != null)
                                    {
                                        alreadyClonedConnectors.Add(connector);
                                        ConnectorCollection.Add(newConnector);
                                    }
                                }
                            }
                            counter++;
                        }
                    }
                }
            }
            break;

            case Key.G:
            {
                if (Keyboard.Modifiers == ModifierKeys.Control)
                {
                    GroupNodes();
                }
            }
            break;

            case Key.S:
            {
                if (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl))
                {
                    SaveFile();
                }
            }
            break;

            case Key.O:
            {
                if (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl))
                {
                    OpenFile();
                }
            }
            break;

            case Key.A:
            {
                if (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl))
                {
                    SelectedNodes.Clear();

                    foreach (var node in NodeCollection)
                    {
                        node.IsSelected = true;
                        SelectedNodes.Add(node);
                    }
                }
            }
            break;
            }
        }