コード例 #1
0
        /// <summary>
        /// Initializes the new instance of <see cref="SnappingViewModel"/> class.
        /// </summary>
        public SnappingViewModel(DemoControl demo)
        {
            View = demo;

            //Intialize the nodes and connectors collection
            this.Nodes      = new NodeCollection();
            this.Connectors = new ConnectorCollection();

            //Enable the ports visibility
            this.PortVisibility = PortVisibility.Collapse;


            //Initialize the selector view model and disbale the quick coammnds.
            this.SelectedItems = new SelectorViewModel();
            (this.SelectedItems as SelectorViewModel).SelectorConstraints &= ~(SelectorConstraints.QuickCommands);
            //Initialize the command to show gridlines and enable the snapping on gridlines
            ShowGridlinesCommand       = new DelegateCommand(OnShowGridlinesCommandExecute);
            SnappingToGridlinesCommand = new DelegateCommand(OnSnappingCommandExecute);
            ItemAddedCommand           = new DelegateCommand(OnItemAddedCommandExecute);

            this.SnapSettings = new SnapSettings()
            {
                SnapToObject    = SnapToObject.All,
                SnapConstraints = SnapConstraints.All,
            };

            #region Nodes

            node1 = CreateNodes(350, 250, 100, 100, "Shape 1", "Rectangle");
            node2 = CreateNodes(650, 250, 100, 100, "Shape 2", "Rectangle");
            node3 = CreateNodes(500, 400, 100, 100, "Shape 3", "Rectangle");
            node4 = CreateNodeforLabel("Rectangle", "");
            (this.Nodes as NodeCollection).Add(node1);
            (this.Nodes as NodeCollection).Add(node2);
            (this.Nodes as NodeCollection).Add(node3);
            (this.Nodes as NodeCollection).Add(node4);
            CreateNodePorts(node1);
            CreateNodePorts(node2);
            connector1 = CreateConnectors(node1, node3);
            DockPortViewModel dockPort = new DockPortViewModel()
            {
                SourcePoint = new Point(0, 0),
                TargetPoint = new Point(0, 1),
            };
            dockPort.Constraints &= ~PortConstraints.InheritHitPadding;
            dockPort.HitPadding   = 20;
            (node2.Ports as PortCollection).Add(dockPort);
            (this.Connectors as ObservableCollection <ConnectorViewModel>).Add(connector1);
            this.PortVisibility = PortVisibility.Visible;
            (this.SelectedItems as SelectorViewModel).SelectorConstraints |= (SelectorConstraints.QuickCommands);
            #endregion
        }
コード例 #2
0
        /// <summary>
        /// To change the snap to object value and updates the lable node content with repective to snapto object value to guide the interaction.
        /// </summary>
        /// <param name="snaptoObjectvalues">SnapToObject value.</param>
        private void OnSnapToObjectChanged(SnapToObject snaptoObjectvalues)
        {
            if (this.SnapSettings != null)
            {
                this.SnapSettings.SnapToObject = snaptoObjectvalues;
            }

            this.Nodes      = null;
            this.Connectors = null;

            this.Nodes = new NodeCollection();
            (this.Nodes as ObservableCollection <NodeViewModel>).Add(node4);
            this.Connectors = new ConnectorCollection();
            (this.Connectors as ObservableCollection <ConnectorViewModel>).Remove(connector1);
            this.PortVisibility = PortVisibility.Collapse;
            ChangeInstructions("");

            (this.SelectedItems as SelectorViewModel).SelectorConstraints &= ~(SelectorConstraints.QuickCommands);

            if (this.SnapSettings != null)
            {
                if (this.SnapSettings.SnapToObject == SnapToObject.All)
                {
                    node1 = CreateNodes(350, 250, 100, 100, "Shape 1", "Rectangle");
                    node2 = CreateNodes(650, 250, 100, 100, "Shape 2", "Rectangle");
                    node3 = CreateNodes(500, 400, 100, 100, "Shape 3", "Rectangle");
                    (this.Nodes as NodeCollection).Add(node1);
                    (this.Nodes as NodeCollection).Add(node2);
                    (this.Nodes as NodeCollection).Add(node3);
                    CreateNodePorts(node1);
                    CreateNodePorts(node2);
                    connector1 = CreateConnectors(node1, node3);
                    DockPortViewModel dockPort = new DockPortViewModel()
                    {
                        SourcePoint = new Point(0, 0),
                        TargetPoint = new Point(0, 1),
                    };
                    dockPort.Constraints &= ~PortConstraints.InheritHitPadding;
                    dockPort.HitPadding   = 20;
                    (node2.Ports as PortCollection).Add(dockPort);
                    (this.Connectors as ObservableCollection <ConnectorViewModel>).Add(connector1);
                    this.PortVisibility = PortVisibility.Visible;
                    (this.SelectedItems as SelectorViewModel).SelectorConstraints |= (SelectorConstraints.QuickCommands);
                    ChangeInstructions("");
                }
                else if (this.SnapSettings.SnapToObject == SnapToObject.Bottom)
                {
                    node1 = CreateNodes(350, 400, 100, 100, "Shape 1", "Rectangle");
                    node2 = CreateNodes(650, 250, 100, 100, "Shape 2", "Rectangle");
                    (this.Nodes as NodeCollection).Add(node1);
                    (this.Nodes as NodeCollection).Add(node2);
                    node2.IsSelected = true;
                    ChangeInstructions("Drag the Shape 2 vertically down");
                }
                else if (this.SnapSettings.SnapToObject == SnapToObject.BottomBottom)
                {
                    node1 = CreateNodes(350, 350, 100, 100, "Shape 1", "Rectangle");
                    node2 = CreateNodes(650, 250, 100, 100, "Shape 2", "Rectangle");
                    (this.Nodes as NodeCollection).Add(node1);
                    (this.Nodes as NodeCollection).Add(node2);
                    node2.IsSelected = true;
                    ChangeInstructions("Drag the Shape 2 vertically down");
                }
                else if (this.SnapSettings.SnapToObject == SnapToObject.BottomTop)
                {
                    node1 = CreateNodes(350, 400, 100, 100, "Shape 1", "Rectangle");
                    node2 = CreateNodes(650, 250, 100, 100, "Shape 2", "Rectangle");
                    (this.Nodes as NodeCollection).Add(node1);
                    (this.Nodes as NodeCollection).Add(node2);
                    node2.IsSelected = true;
                    ChangeInstructions("Drag the Shape 2 vertically down");
                }
                else if (this.SnapSettings.SnapToObject == SnapToObject.Height)
                {
                    node1 = CreateNodes(350, 350, 100, 100, "Shape 1", "Rectangle");
                    node2 = CreateNodes(650, 320, 100, 40, "Shape 2", "Rectangle");
                    (this.Nodes as NodeCollection).Add(node1);
                    (this.Nodes as NodeCollection).Add(node2);
                    node2.IsSelected = true;
                    ChangeInstructions("Increase the Height of the Shape 2");
                }
                else if (this.SnapSettings.SnapToObject == SnapToObject.HorizontalCenter)
                {
                    node1 = CreateNodes(350, 250, 100, 100, "Shape 1", "Rectangle");
                    node2 = CreateNodes(650, 250, 100, 100, "Shape 2", "Rectangle");
                    (this.Nodes as NodeCollection).Add(node1);
                    (this.Nodes as NodeCollection).Add(node2);
                    node2.IsSelected = true;
                    ChangeInstructions("Drag the Shape2 horizontally");
                }
                else if (this.SnapSettings.SnapToObject == SnapToObject.HorizontalSpacing)
                {
                    node1 = CreateNodes(350, 250, 100, 100, "Shape 1", "Rectangle");
                    node2 = CreateNodes(650, 250, 100, 100, "Shape 2", "Rectangle");
                    node3 = CreateNodes(500, 400, 100, 100, "Shape 3", "Rectangle");
                    (this.Nodes as NodeCollection).Add(node1);
                    (this.Nodes as NodeCollection).Add(node2);
                    (this.Nodes as NodeCollection).Add(node3);
                    node3.IsSelected = true;
                    ChangeInstructions("Drag the shape 3 in-between shapes 1 & 2");
                }
                else if (this.SnapSettings.SnapToObject == SnapToObject.Left || this.SnapSettings.SnapToObject == SnapToObject.LeftLeft ||
                         this.SnapSettings.SnapToObject == SnapToObject.LeftRight)
                {
                    node1 = CreateNodes(350, 250, 100, 100, "Shape 1", "Rectangle");
                    node2 = CreateNodes(250, 450, 100, 100, "Shape 2", "Rectangle");
                    (this.Nodes as NodeCollection).Add(node1);
                    (this.Nodes as NodeCollection).Add(node2);
                    node2.IsSelected = true;
                    ChangeInstructions("Drag the Shape 2 horizontally right");
                }
                else if (this.SnapSettings.SnapToObject == SnapToObject.Port)
                {
                    this.PortVisibility = PortVisibility.Visible;
                    node1 = CreateNodes(350, 250, 100, 100, "Shape 1", "Rectangle");
                    CreateNodePorts(node1);
                    node2 = CreateNodes(650, 250, 100, 100, "Shape 2", "Rectangle");
                    CreateNodePorts(node2);
                    (this.Nodes as NodeCollection).Add(node1);
                    (this.Nodes as NodeCollection).Add(node2);
                    node2.IsSelected = true;
                    ChangeInstructions("Drag the Shape 2 horizontally");
                }
                else if (this.SnapSettings.SnapToObject == SnapToObject.Right || this.SnapSettings.SnapToObject == SnapToObject.RightLeft ||
                         this.SnapSettings.SnapToObject == SnapToObject.RightRight)
                {
                    node1 = CreateNodes(350, 250, 100, 100, "Shape 1", "Rectangle");
                    node2 = CreateNodes(200, 450, 100, 100, "Shape 2", "Rectangle");
                    (this.Nodes as NodeCollection).Add(node1);
                    (this.Nodes as NodeCollection).Add(node2);
                    node2.IsSelected = true;
                    ChangeInstructions("Drag the Shape 2 horizontally right");
                }
                else if (this.SnapSettings.SnapToObject == SnapToObject.Segment)
                {
                    node1 = CreateNodes(350, 250, 100, 100, "Shape 1", "Rectangle");
                    node2 = CreateNodes(550, 250, 100, 100, "Shape 2", "Rectangle");
                    (this.Nodes as NodeCollection).Add(node1);
                    this.PortVisibility = PortVisibility.Visible;
                    DockPortViewModel dockPort = new DockPortViewModel()
                    {
                        SourcePoint = new Point(0, 0),
                        TargetPoint = new Point(0, 1),
                    };
                    dockPort.Constraints &= ~PortConstraints.InheritHitPadding;
                    dockPort.HitPadding   = 20;
                    (node2.Ports as PortCollection).Add(dockPort);
                    node1.IsSelected = true;
                    (this.Nodes as NodeCollection).Add(node2);
                    ChangeInstructions("Draw a connector from Shape 1 to Dock port of shape 2");
                    (this.SelectedItems as SelectorViewModel).SelectorConstraints |= (SelectorConstraints.QuickCommands);
                }
                else if (this.SnapSettings.SnapToObject == SnapToObject.Size)
                {
                    node1 = CreateNodes(350, 250, 100, 100, "Shape 1", "Rectangle");
                    (this.Nodes as NodeCollection).Add(node1);
                    node2 = CreateNodes(650, 250, 100, 100, "Shape 2", "Rectangle");
                    (this.Nodes as NodeCollection).Add(node2);
                    node3 = CreateNodes(500, 400, 60, 60, "Shape 3", "Rectangle");
                    (this.Nodes as NodeCollection).Add(node3);
                    node3.IsSelected = true;
                    ChangeInstructions("Resize the Shape 3");
                }
                else if (this.SnapSettings.SnapToObject == SnapToObject.Top || this.SnapSettings.SnapToObject == SnapToObject.TopBottom ||
                         this.SnapSettings.SnapToObject == SnapToObject.TopTop)
                {
                    node1 = CreateNodes(350, 250, 100, 100, "Shape 1", "Rectangle");
                    (this.Nodes as NodeCollection).Add(node1);
                    node2 = CreateNodes(650, 450, 100, 100, "Shape 2", "Rectangle");
                    (this.Nodes as NodeCollection).Add(node2);
                    node2.IsSelected = true;
                    ChangeInstructions("Drag the Shape 2 vertically up");
                }
                else if (this.SnapSettings.SnapToObject == SnapToObject.VerticalCenter)
                {
                    node1 = CreateNodes(350, 250, 100, 100, "Shape 1", "Rectangle");
                    (this.Nodes as NodeCollection).Add(node1);
                    node2 = CreateNodes(350, 500, 100, 100, "Shape 2", "Rectangle");
                    (this.Nodes as NodeCollection).Add(node2);
                    node2.IsSelected = true;
                    ChangeInstructions("Drag the Shape 2 vertically");
                }
                else if (this.SnapSettings.SnapToObject == SnapToObject.VerticalSpacing)
                {
                    node1 = CreateNodes(350, 190, 100, 100, "Shape 1", "Rectangle");
                    (this.Nodes as NodeCollection).Add(node1);
                    node2 = CreateNodes(350, 510, 100, 100, "Shape 3", "Rectangle");
                    (this.Nodes as NodeCollection).Add(node2);
                    node3 = CreateNodes(550, 350, 100, 100, "Shape 2", "Rectangle");
                    (this.Nodes as NodeCollection).Add(node3);
                    node3.IsSelected = true;
                    ChangeInstructions("Drag the Shape 2 in-between Shape 1 & 3");
                }
                else if (this.SnapSettings.SnapToObject == SnapToObject.Width)
                {
                    node1 = CreateNodes(350, 250, 100, 100, "Shape 1", "Rectangle");
                    (this.Nodes as NodeCollection).Add(node1);
                    node2 = CreateNodes(650, 250, 100, 100, "Shape 2", "Rectangle");
                    (this.Nodes as NodeCollection).Add(node2);
                    node3 = CreateNodes(500, 400, 60, 100, "Shape 3", "Rectangle");
                    (this.Nodes as NodeCollection).Add(node3);
                    node3.IsSelected = true;
                    ChangeInstructions("Increase the width of Shape 3");
                }
            }
        }