diff --git a/AIStudio.Wpf.DiagramDesigner/Controls/DesignerCanvas.cs b/AIStudio.Wpf.DiagramDesigner/Controls/DesignerCanvas.cs index b2c4ddc..fcb4748 100644 --- a/AIStudio.Wpf.DiagramDesigner/Controls/DesignerCanvas.cs +++ b/AIStudio.Wpf.DiagramDesigner/Controls/DesignerCanvas.cs @@ -508,7 +508,7 @@ namespace AIStudio.Wpf.DiagramDesigner SourceItemsContainer.DragObject.Left = currentPoint.X - SourceItemsContainer.DragOffset.X; SourceItemsContainer.DragObject.Top = currentPoint.Y - SourceItemsContainer.DragOffset.Y; - _viewModel.PreviewNearBlock(new System.Collections.Generic.List { SourceItemsContainer.DragObject }); + (_viewModel as IBlockDiagramViewModel).PreviewNearBlock(new System.Collections.Generic.List { SourceItemsContainer.DragObject }); } } else @@ -575,7 +575,7 @@ namespace AIStudio.Wpf.DiagramDesigner } else if (SourceItemsContainer != null) { - _viewModel.FinishNearBlock(new System.Collections.Generic.List { SourceItemsContainer.DragObject }); + ( _viewModel as IBlockDiagramViewModel).FinishNearBlock(new System.Collections.Generic.List { SourceItemsContainer.DragObject }); ExitCursor(); } @@ -681,7 +681,7 @@ namespace AIStudio.Wpf.DiagramDesigner itemBase.Left = Math.Max(0, position.X - itemBase.GetItemWidth() / 2); itemBase.Top = Math.Max(0, position.Y - itemBase.GetItemHeight() / 2); - _viewModel.PreviewNearBlock(new System.Collections.Generic.List { itemBase }); + (_viewModel as IBlockDiagramViewModel)?.PreviewNearBlock(new System.Collections.Generic.List { itemBase }); } base.OnDragOver(e); @@ -746,7 +746,7 @@ namespace AIStudio.Wpf.DiagramDesigner if (itemBase is BlockDesignerItemViewModel block) { - _viewModel.FinishNearBlock(new System.Collections.Generic.List { block }); + (_viewModel as IBlockDiagramViewModel).FinishNearBlock(new System.Collections.Generic.List { block }); } } } diff --git a/AIStudio.Wpf.DiagramDesigner/Controls/DragThumb.cs b/AIStudio.Wpf.DiagramDesigner/Controls/DragThumb.cs index e734321..e896120 100644 --- a/AIStudio.Wpf.DiagramDesigner/Controls/DragThumb.cs +++ b/AIStudio.Wpf.DiagramDesigner/Controls/DragThumb.cs @@ -97,7 +97,7 @@ namespace AIStudio.Wpf.DiagramDesigner.Controls var blocks = designerItems.OfType().ToList(); if (blocks.Any()) { - DiagramViewModel.FinishNearBlock(blocks); + (DiagramViewModel as IBlockDiagramViewModel)?.FinishNearBlock(blocks); } Dictionary> infos = @@ -167,7 +167,7 @@ namespace AIStudio.Wpf.DiagramDesigner.Controls } var blocks = designerItems.OfType().ToList(); - DiagramViewModel.PreviewNearBlock(blocks); + (DiagramViewModel as IBlockDiagramViewModel)?.PreviewNearBlock(blocks); e.Handled = true; } diff --git a/AIStudio.Wpf.DiagramDesigner/ViewModels/BaseViewModel/DiagramViewModelHelper.cs b/AIStudio.Wpf.DiagramDesigner/ViewModels/BaseViewModel/DiagramViewModelHelper.cs index 6a3523d..9caf1b7 100644 --- a/AIStudio.Wpf.DiagramDesigner/ViewModels/BaseViewModel/DiagramViewModelHelper.cs +++ b/AIStudio.Wpf.DiagramDesigner/ViewModels/BaseViewModel/DiagramViewModelHelper.cs @@ -116,280 +116,6 @@ namespace AIStudio.Wpf.DiagramDesigner return null; } - /// - /// type=0最近且没有依附;=1 - /// - /// - /// - /// - public static Tuple FindNearPortToAttachTo(this IDiagramViewModel diagramViewModel, BlockDesignerItemViewModel blockDesignerItemViewModel, bool isExist) - { - if (blockDesignerItemViewModel == null) - return new Tuple(null, null); - - List items; - if (isExist == false) - { - items = diagramViewModel.Items.OfType(). - Where(p => p != blockDesignerItemViewModel && p != blockDesignerItemViewModel.Parent && p != blockDesignerItemViewModel.Next) - .ToList(); - - } - else - { - items = diagramViewModel.Items.OfType(). - Where(p => p != blockDesignerItemViewModel) - .ToList(); - } - - FullyCreatedConnectorInfo parent = null; - FullyCreatedConnectorInfo next = null; - - foreach (var port in items.SelectMany(n => n.Connectors)) - { - foreach (var port2 in blockDesignerItemViewModel.Connectors) - { - //parent - if (parent == null) - { - if ((port.Orientation == ConnectorOrientation.Right && port2.Orientation == ConnectorOrientation.Left) - || (port.Orientation == ConnectorOrientation.Bottom && port2.Orientation == ConnectorOrientation.Top)) - { - - if (port.Position.DistanceTo(port2.Position) < diagramViewModel.DiagramOption.SnappingOption.SnappingRadius) - { - port.DataItem.ShowConnectors = true; - if (port2.CanAttachTo(port) == true) - { - port.BeAttachTo = true; - parent = port; - continue; - } - else - { - port.DisableAttachTo = true; - } - - } - } - } - - //next - if (next == null) - { - if ((port.Orientation == ConnectorOrientation.Left && port2.Orientation == ConnectorOrientation.Right) - || (port.Orientation == ConnectorOrientation.Top && port2.Orientation == ConnectorOrientation.Bottom)) - { - - if (port.Position.DistanceTo(port2.Position) < diagramViewModel.DiagramOption.SnappingOption.SnappingRadius) - { - port.DataItem.ShowConnectors = true; - if (port2.CanAttachTo(port) == true) - { - port.BeAttachTo = true; - next = port; - continue; - } - else - { - port.DisableAttachTo = true; - } - - } - } - } - } - } - - return new Tuple(parent, next); - } - - public static BlockItemsContainerInfo FindNearContainerToAttachTo(this IDiagramViewModel diagramViewModel, BlockDesignerItemViewModel blockDesignerItemViewModel) - { - if (blockDesignerItemViewModel == null) - return null; - - List items; - - items = diagramViewModel.Items.OfType(). - Where(p => p != blockDesignerItemViewModel) - .ToList(); - - foreach (var port in items.SelectMany(n => n.Containers)) - { - if (port.GetBounds().IntersectsWith(blockDesignerItemViewModel.GetBounds())) //如果两个位置相交 - { - var innerport = port.GetAllContainers(port.Children, false).Where(p => p.GetBounds().IntersectsWith(blockDesignerItemViewModel.GetBounds())).OrderByDescending(p => p.ContainerLevel).FirstOrDefault(); - if (innerport != null) - { - innerport.DataItem.ShowConnectors = true; - if (innerport.CanAttachTo(blockDesignerItemViewModel) == true) - { - innerport.BeAttachTo = true; - } - else - { - innerport.DisableAttachTo = true; - } - return innerport; - } - else - { - port.DataItem.ShowConnectors = true; - if (port.CanAttachTo(blockDesignerItemViewModel) == true) - { - port.BeAttachTo = true; - } - else - { - port.DisableAttachTo = true; - } - return port; - } - } - } - - return null; - } - - /// - /// type=0最近且没有依附;=1 - /// - /// - /// - /// - public static Tuple FindNearPortToAttachTo(this IDiagramViewModel diagramViewModel, BlockDesignerItemTempLink blockDesignerItemTempLink, bool isExist) - { - if (blockDesignerItemTempLink == null || blockDesignerItemTempLink.Items == null || blockDesignerItemTempLink.Items.Count == 0) - return new Tuple(null, null); - - List items; - if (isExist == false) - { - items = diagramViewModel.Items.OfType().Where(p => !blockDesignerItemTempLink.Items.Contains(p)).ToList(); - } - else - { - items = diagramViewModel.Items.OfType().Where(p => !blockDesignerItemTempLink.Items.Contains(p)).ToList(); - } - - FullyCreatedConnectorInfo parent = null; - FullyCreatedConnectorInfo next = null; - - foreach (var port in items.SelectMany(n => n.Connectors).OfType()) - { - //已经被连接的不允许在顶部吸附了 - if ((port.Orientation == ConnectorOrientation.Top || port.Orientation == ConnectorOrientation.Left) && port.DataItem.Prev != null) - { - continue; - } - - foreach (var port2 in blockDesignerItemTempLink.Connectors) - { - //parent - if (parent == null) - { - if ((port.Orientation == ConnectorOrientation.Right && port2.Orientation == ConnectorOrientation.Left) - || (port.Orientation == ConnectorOrientation.Bottom && port2.Orientation == ConnectorOrientation.Top)) - { - - if (port.Position.DistanceTo(port2.Position) < diagramViewModel.DiagramOption.SnappingOption.SnappingRadius) - { - port.DataItem.ShowConnectors = true; - if (port2.CanAttachTo(port) == true) - { - port.BeAttachTo = true; - parent = port; - continue; - } - else - { - port.DisableAttachTo = true; - } - - } - } - } - - //next - if (next == null) - { - if ((port.Orientation == ConnectorOrientation.Left && port2.Orientation == ConnectorOrientation.Right) - || (port.Orientation == ConnectorOrientation.Top && port2.Orientation == ConnectorOrientation.Bottom)) - { - - if (port.Position.DistanceTo(port2.Position) < diagramViewModel.DiagramOption.SnappingOption.SnappingRadius) - { - port.DataItem.ShowConnectors = true; - if (port2.CanAttachTo(port) == true) - { - port.BeAttachTo = true; - next = port; - continue; - } - else - { - port.DisableAttachTo = true; - } - - } - } - } - } - } - - return new Tuple(parent, next); - } - - public static BlockItemsContainerInfo FindNearContainerToAttachTo(this IDiagramViewModel diagramViewModel, BlockDesignerItemTempLink blockDesignerItemTempLink) - { - if (blockDesignerItemTempLink == null || blockDesignerItemTempLink.Items == null || blockDesignerItemTempLink.Items.Count == 0) - return null; - - List items; - - items = diagramViewModel.Items.OfType().Where(p => !blockDesignerItemTempLink.Items.Contains(p)).ToList(); - - foreach (var port in items.SelectMany(n => n.Containers)) - { - if (port.GetBounds().IntersectsWith(blockDesignerItemTempLink.GetBounds())) //如果两个位置相交 - { - var innerport = port.GetAllContainers(port.Children, false).Where(p => p.GetBounds().IntersectsWith(blockDesignerItemTempLink.GetBounds())).OrderByDescending(p => p.ContainerLevel).FirstOrDefault(); - if (innerport != null) - { - innerport.DataItem.ShowConnectors = true; - if (innerport.CanAttachTo(blockDesignerItemTempLink.Items.FirstOrDefault()) == true) - { - innerport.BeAttachTo = true; - return innerport; - } - //else - //{ - // innerport.DisableAttachTo = true; - // return null; - //} - - } - else - { - port.DataItem.ShowConnectors = true; - if (port.CanAttachTo(blockDesignerItemTempLink.Items.FirstOrDefault()) == true) - { - port.BeAttachTo = true; - return port; - } - //else - //{ - // port.DisableAttachTo = true; - // return null; - //} - } - } - } - - return null; - } - public static void ClearNearPort(this IDiagramViewModel diagramViewModel) { diagramViewModel.Items.OfType().ToList().SelectMany(n => n.Connectors).Where(p => p.BeAttachTo == true || p.DisableAttachTo == true).ToList() @@ -398,94 +124,8 @@ namespace AIStudio.Wpf.DiagramDesigner p.BeAttachTo = false; }); } - - public static void ClearNearContain(this IDiagramViewModel diagramViewModel) - { - diagramViewModel.Items.OfType().ToList().SelectMany(n => n.GetAllContainers()).Where(p => p.BeAttachTo == true || p.DisableAttachTo == true).ToList() - .ForEach(p => { - p.DisableAttachTo = false; - p.BeAttachTo = false; - }); - } #endregion - #region Block拖拽预览-拖拽完成 - public static void PreviewNearBlock(this IDiagramViewModel diagramViewModel, List blocks) - { - if (blocks.Any()) - { - diagramViewModel.ClearNearPort(); - diagramViewModel.ClearNearContain(); - var links = BlockDesignerItemTempLink.Build(blocks); - foreach (BlockDesignerItemTempLink item in links) - { - var container = diagramViewModel.FindNearContainerToAttachTo(item); - if (container != null) - { - continue; - } - diagramViewModel.FindNearPortToAttachTo(item, false); - } - } - } - - - - public static void FinishNearBlock(this IDiagramViewModel diagramViewModel, List blocks) - { - if (blocks.Any()) - { - diagramViewModel.ClearNearPort(); - diagramViewModel.ClearNearContain(); - var links = BlockDesignerItemTempLink.Build(blocks); - - foreach (BlockDesignerItemTempLink item in links) - { - var container = diagramViewModel.FindNearContainerToAttachTo(item); - if (container != null) - { - container.DataItem.AddChild(item.Items.FirstOrDefault(), container);//待完善 - container.BeAttachTo = false; - container.DisableAttachTo = false; - continue; - } - - var portTuple = diagramViewModel.FindNearPortToAttachTo(item, true); - var portParent = portTuple.Item1; - var portNext = portTuple.Item2; - - if (portParent != null) - { - (portParent.DataItem as BlockDesignerItemViewModel).AddNext(item.Items.FirstOrDefault()); - portParent.BeAttachTo = false; - portParent.DisableAttachTo = false; - } - else - { - if (item.Items.FirstOrDefault().Parent != null) - { - (item.Items.FirstOrDefault().Parent as BlockDesignerItemViewModel).RemoveNext(); - } - } - - if (portNext != null) - { - item.Items.LastOrDefault().AddNext(portNext.DataItem as BlockDesignerItemViewModel); - portNext.BeAttachTo = false; - portNext.DisableAttachTo = false; - } - else - { - if (item.Items.LastOrDefault().Next != null) - { - item.Items.LastOrDefault().RemoveNext(); - } - } - } - } - } - - - #endregion + } } diff --git a/AIStudio.Wpf.DiagramDesigner/ViewModels/BlockViewModel/BlockDesignerItemViewModelHelper.cs b/AIStudio.Wpf.DiagramDesigner/ViewModels/BlockViewModel/BlockDesignerItemViewModelHelper.cs new file mode 100644 index 0000000..3c4d48b --- /dev/null +++ b/AIStudio.Wpf.DiagramDesigner/ViewModels/BlockViewModel/BlockDesignerItemViewModelHelper.cs @@ -0,0 +1,380 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AIStudio.Wpf.DiagramDesigner +{ + public static class BlockDesignerItemViewModelHelper + { + /// + /// type=0最近且没有依附;=1 + /// + /// + /// + /// + public static Tuple FindNearPortToAttachTo(this IDiagramViewModel diagramViewModel, BlockDesignerItemViewModel blockDesignerItemViewModel, bool isExist) + { + if (blockDesignerItemViewModel == null) + return new Tuple(null, null); + + List items; + if (isExist == false) + { + items = diagramViewModel.Items.OfType(). + Where(p => p != blockDesignerItemViewModel && p != blockDesignerItemViewModel.Parent && p != blockDesignerItemViewModel.Next) + .ToList(); + + } + else + { + items = diagramViewModel.Items.OfType(). + Where(p => p != blockDesignerItemViewModel) + .ToList(); + } + + FullyCreatedConnectorInfo parent = null; + FullyCreatedConnectorInfo next = null; + + foreach (var port in items.SelectMany(n => n.Connectors)) + { + foreach (var port2 in blockDesignerItemViewModel.Connectors) + { + //parent + if (parent == null) + { + if ((port.Orientation == ConnectorOrientation.Right && port2.Orientation == ConnectorOrientation.Left) + || (port.Orientation == ConnectorOrientation.Bottom && port2.Orientation == ConnectorOrientation.Top)) + { + + if (port.Position.DistanceTo(port2.Position) < diagramViewModel.DiagramOption.SnappingOption.SnappingRadius) + { + port.DataItem.ShowConnectors = true; + if (port2.CanAttachTo(port) == true) + { + port.BeAttachTo = true; + parent = port; + continue; + } + else + { + port.DisableAttachTo = true; + } + + } + } + } + + //next + if (next == null) + { + if ((port.Orientation == ConnectorOrientation.Left && port2.Orientation == ConnectorOrientation.Right) + || (port.Orientation == ConnectorOrientation.Top && port2.Orientation == ConnectorOrientation.Bottom)) + { + + if (port.Position.DistanceTo(port2.Position) < diagramViewModel.DiagramOption.SnappingOption.SnappingRadius) + { + port.DataItem.ShowConnectors = true; + if (port2.CanAttachTo(port) == true) + { + port.BeAttachTo = true; + next = port; + continue; + } + else + { + port.DisableAttachTo = true; + } + + } + } + } + } + } + + return new Tuple(parent, next); + } + + public static BlockItemsContainerInfo FindNearContainerToAttachTo(this IDiagramViewModel diagramViewModel, BlockDesignerItemViewModel blockDesignerItemViewModel) + { + if (blockDesignerItemViewModel == null) + return null; + + List items; + + items = diagramViewModel.Items.OfType(). + Where(p => p != blockDesignerItemViewModel) + .ToList(); + + foreach (var port in items.SelectMany(n => n.Containers)) + { + if (port.GetBounds().IntersectsWith(blockDesignerItemViewModel.GetBounds())) //如果两个位置相交 + { + var innerport = port.GetAllContainers(port.Children, false).Where(p => p.GetBounds().IntersectsWith(blockDesignerItemViewModel.GetBounds())).OrderByDescending(p => p.ContainerLevel).FirstOrDefault(); + if (innerport != null) + { + innerport.DataItem.ShowConnectors = true; + if (innerport.CanAttachTo(blockDesignerItemViewModel) == true) + { + innerport.BeAttachTo = true; + } + else + { + innerport.DisableAttachTo = true; + } + return innerport; + } + else + { + port.DataItem.ShowConnectors = true; + if (port.CanAttachTo(blockDesignerItemViewModel) == true) + { + port.BeAttachTo = true; + } + else + { + port.DisableAttachTo = true; + } + return port; + } + } + } + + return null; + } + + /// + /// type=0最近且没有依附;=1 + /// + /// + /// + /// + public static Tuple FindNearPortToAttachTo(this IDiagramViewModel diagramViewModel, BlockDesignerItemTempLink blockDesignerItemTempLink, bool isExist) + { + if (blockDesignerItemTempLink == null || blockDesignerItemTempLink.Items == null || blockDesignerItemTempLink.Items.Count == 0) + return new Tuple(null, null); + + List items; + if (isExist == false) + { + items = diagramViewModel.Items.OfType().Where(p => !blockDesignerItemTempLink.Items.Contains(p)).ToList(); + } + else + { + items = diagramViewModel.Items.OfType().Where(p => !blockDesignerItemTempLink.Items.Contains(p)).ToList(); + } + + FullyCreatedConnectorInfo parent = null; + FullyCreatedConnectorInfo next = null; + + foreach (var port in items.SelectMany(n => n.Connectors).OfType()) + { + //已经被连接的不允许在顶部吸附了 + if ((port.Orientation == ConnectorOrientation.Top || port.Orientation == ConnectorOrientation.Left) && port.DataItem.Prev != null) + { + continue; + } + + foreach (var port2 in blockDesignerItemTempLink.Connectors) + { + //parent + if (parent == null) + { + if ((port.Orientation == ConnectorOrientation.Right && port2.Orientation == ConnectorOrientation.Left) + || (port.Orientation == ConnectorOrientation.Bottom && port2.Orientation == ConnectorOrientation.Top)) + { + + if (port.Position.DistanceTo(port2.Position) < diagramViewModel.DiagramOption.SnappingOption.SnappingRadius) + { + port.DataItem.ShowConnectors = true; + if (port2.CanAttachTo(port) == true) + { + port.BeAttachTo = true; + parent = port; + continue; + } + else + { + port.DisableAttachTo = true; + } + + } + } + } + + //next + if (next == null) + { + if ((port.Orientation == ConnectorOrientation.Left && port2.Orientation == ConnectorOrientation.Right) + || (port.Orientation == ConnectorOrientation.Top && port2.Orientation == ConnectorOrientation.Bottom)) + { + + if (port.Position.DistanceTo(port2.Position) < diagramViewModel.DiagramOption.SnappingOption.SnappingRadius) + { + port.DataItem.ShowConnectors = true; + if (port2.CanAttachTo(port) == true) + { + port.BeAttachTo = true; + next = port; + continue; + } + else + { + port.DisableAttachTo = true; + } + + } + } + } + } + } + + return new Tuple(parent, next); + } + + + public static void ClearNearContain(this IDiagramViewModel diagramViewModel) + { + diagramViewModel.Items.OfType().ToList().SelectMany(n => n.GetAllContainers()).Where(p => p.BeAttachTo == true || p.DisableAttachTo == true).ToList() + .ForEach(p => { + p.DisableAttachTo = false; + p.BeAttachTo = false; + }); + } + + public static BlockItemsContainerInfo FindNearContainerToAttachTo(this IDiagramViewModel diagramViewModel, BlockDesignerItemTempLink blockDesignerItemTempLink) + { + if (blockDesignerItemTempLink == null || blockDesignerItemTempLink.Items == null || blockDesignerItemTempLink.Items.Count == 0) + return null; + + List items; + + items = diagramViewModel.Items.OfType().Where(p => !blockDesignerItemTempLink.Items.Contains(p)).ToList(); + + foreach (var port in items.SelectMany(n => n.Containers)) + { + if (port.GetBounds().IntersectsWith(blockDesignerItemTempLink.GetBounds())) //如果两个位置相交 + { + var innerport = port.GetAllContainers(port.Children, false).Where(p => p.GetBounds().IntersectsWith(blockDesignerItemTempLink.GetBounds())).OrderByDescending(p => p.ContainerLevel).FirstOrDefault(); + if (innerport != null) + { + innerport.DataItem.ShowConnectors = true; + if (innerport.CanAttachTo(blockDesignerItemTempLink.Items.FirstOrDefault()) == true) + { + innerport.BeAttachTo = true; + return innerport; + } + //else + //{ + // innerport.DisableAttachTo = true; + // return null; + //} + + } + else + { + port.DataItem.ShowConnectors = true; + if (port.CanAttachTo(blockDesignerItemTempLink.Items.FirstOrDefault()) == true) + { + port.BeAttachTo = true; + return port; + } + //else + //{ + // port.DisableAttachTo = true; + // return null; + //} + } + } + } + + return null; + } + + #region Block拖拽预览-拖拽完成 + public static void PreviewNearBlock(this IBlockDiagramViewModel diagramViewModel, List blocks) + { + if (diagramViewModel == null) + return; + + if (blocks.Any()) + { + diagramViewModel.ClearNearPort(); + diagramViewModel.ClearNearContain(); + var links = BlockDesignerItemTempLink.Build(blocks); + foreach (BlockDesignerItemTempLink item in links) + { + var container = diagramViewModel.FindNearContainerToAttachTo(item); + if (container != null) + { + continue; + } + diagramViewModel.FindNearPortToAttachTo(item, false); + } + } + } + + + + public static void FinishNearBlock(this IBlockDiagramViewModel diagramViewModel, List blocks) + { + if (diagramViewModel == null) + return; + + if (blocks.Any()) + { + diagramViewModel.ClearNearPort(); + diagramViewModel.ClearNearContain(); + var links = BlockDesignerItemTempLink.Build(blocks); + + foreach (BlockDesignerItemTempLink item in links) + { + var container = diagramViewModel.FindNearContainerToAttachTo(item); + if (container != null) + { + diagramViewModel.AddChildCommand.Execute(new BlockContainerPara() { Item = container.DataItem, Child = item.Items.FirstOrDefault(), Container = container });//待完善 + container.BeAttachTo = false; + container.DisableAttachTo = false; + continue; + } + + var portTuple = diagramViewModel.FindNearPortToAttachTo(item, true); + var portParent = portTuple.Item1; + var portNext = portTuple.Item2; + + if (portParent != null) + { + diagramViewModel.AddNextCommand.Execute(new BlockNextPara() { Item = portParent.DataItem as BlockDesignerItemViewModel, Next = item.Items.FirstOrDefault() }); + portParent.BeAttachTo = false; + portParent.DisableAttachTo = false; + } + else + { + if (item.Items.FirstOrDefault().Parent != null) + { + diagramViewModel.RemoveNextCommand.Execute(new BlockNextPara() { Item = item.Items.FirstOrDefault().Parent as BlockDesignerItemViewModel, Next = (item.Items.FirstOrDefault().Parent as BlockDesignerItemViewModel)?.Next }); + } + } + + if (portNext != null) + { + diagramViewModel.AddNextCommand.Execute(new BlockNextPara() { Item = item.Items.LastOrDefault(), Next = portNext.DataItem as BlockDesignerItemViewModel }); + portNext.BeAttachTo = false; + portNext.DisableAttachTo = false; + } + else + { + if (item.Items.LastOrDefault().Next != null) + { + diagramViewModel.RemoveNextCommand.Execute(new BlockNextPara() { Item = item.Items.LastOrDefault(), Next = item.Items.LastOrDefault()?.Next }); + } + } + } + } + } + + + #endregion + } +} diff --git a/AIStudio.Wpf.DiagramDesigner/ViewModels/BlockViewModel/BlockDiagramViewModel.cs b/AIStudio.Wpf.DiagramDesigner/ViewModels/BlockViewModel/BlockDiagramViewModel.cs index c762243..72c2133 100644 --- a/AIStudio.Wpf.DiagramDesigner/ViewModels/BlockViewModel/BlockDiagramViewModel.cs +++ b/AIStudio.Wpf.DiagramDesigner/ViewModels/BlockViewModel/BlockDiagramViewModel.cs @@ -3,11 +3,12 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using System.Windows.Controls; using System.Windows.Input; namespace AIStudio.Wpf.DiagramDesigner { - public class BlockDiagramViewModel : DiagramViewModel + public class BlockDiagramViewModel : DiagramViewModel, IBlockDiagramViewModel { public BlockDiagramViewModel() { @@ -54,25 +55,86 @@ namespace AIStudio.Wpf.DiagramDesigner #region Block使用 private void ExecutedAddNextCommand(object parameter) { - if (parameter is Tuple blockTuple) + if (parameter is BlockNextPara blockItemPara) { + DoCommandManager.DoNewCommand(this.ToString(), + () => { + blockItemPara.Item.AddNext(blockItemPara.Next); + }, + () => { + blockItemPara.Item.RemoveNext(); + }); } - } private void ExecutedRemoveNextCommand(object parameter) { - + if (parameter is BlockNextPara blockItemPara) + { + DoCommandManager.DoNewCommand(this.ToString(), + () => { + blockItemPara.Item.RemoveNext(); + }, + () => { + blockItemPara.Item.AddNext(blockItemPara.Next); + }); + } } private void ExecutedAddChildCommand(object parameter) { - + if (parameter is BlockContainerPara blockContainerPara) + { + DoCommandManager.DoNewCommand(this.ToString(), + () => { + blockContainerPara.Item.AddChild(blockContainerPara.Child, blockContainerPara.Container); + }, + () => { + blockContainerPara.Item.RemoveChild(blockContainerPara.Child, blockContainerPara.Container); + }); + } } private void ExecutedRemoveChildCommand(object parameter) { - + if (parameter is BlockContainerPara blockContainerPara) + { + DoCommandManager.DoNewCommand(this.ToString(), + () => { + blockContainerPara.Item.RemoveChild(blockContainerPara.Child, blockContainerPara.Container); + }, + () => { + blockContainerPara.Item.AddChild(blockContainerPara.Child, blockContainerPara.Container); + }); + } } #endregion } + + public class BlockNextPara + { + public BlockDesignerItemViewModel Item + { + get; set; + } + public BlockDesignerItemViewModel Next + { + get;set; + } + } + + public class BlockContainerPara + { + public BlockDesignerItemViewModel Item + { + get; set; + } + public BlockDesignerItemViewModel Child + { + get; set; + } + public BlockItemsContainerInfo Container + { + get; set; + } + } } diff --git a/AIStudio.Wpf.DiagramDesigner/ViewModels/BlockViewModel/IBlockDiagramViewModel.cs b/AIStudio.Wpf.DiagramDesigner/ViewModels/BlockViewModel/IBlockDiagramViewModel.cs new file mode 100644 index 0000000..3001f23 --- /dev/null +++ b/AIStudio.Wpf.DiagramDesigner/ViewModels/BlockViewModel/IBlockDiagramViewModel.cs @@ -0,0 +1,32 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Input; + +namespace AIStudio.Wpf.DiagramDesigner +{ + public interface IBlockDiagramViewModel : IDiagramViewModel + { + ICommand AddNextCommand + { + get; + } + + ICommand RemoveNextCommand + { + get; + } + + ICommand AddChildCommand + { + get; + } + + ICommand RemoveChildCommand + { + get; + } + } +} diff --git a/AIStudio.Wpf.DiagramDesigner/ViewModels/INPCBase.cs b/AIStudio.Wpf.DiagramDesigner/ViewModels/INPCBase.cs deleted file mode 100644 index 4ef2ae5..0000000 --- a/AIStudio.Wpf.DiagramDesigner/ViewModels/INPCBase.cs +++ /dev/null @@ -1,61 +0,0 @@ -using System; -using System.ComponentModel; -using System.Linq; -using System.Reactive.Linq; - -namespace AIStudio.Wpf.DiagramDesigner -{ - //[Serializable] - //public abstract class BindableBase : INotifyPropertyChanged - //{ - // #region INotifyPropertyChanged Implementation - // /// - // /// Occurs when any properties are changed on this object. - // /// - // public event PropertyChangedEventHandler PropertyChanged - // { - // add { this.propertyChanged += value; } - // remove { this.propertyChanged -= value; } - // } - - // protected event PropertyChangedEventHandler propertyChanged; - - - // /// - // /// A helper method that raises the PropertyChanged event for a property. - // /// - // /// The names of the properties that changed. - // public virtual void NotifyChanged(params string[] propertyNames) - // { - // foreach (string name in propertyNames) - // { - // OnPropertyChanged(new PropertyChangedEventArgs(name)); - // } - // } - - // /// - // /// Raises the PropertyChanged event. - // /// - // /// Event arguments. - // protected virtual void OnPropertyChanged(PropertyChangedEventArgs e) - // { - // if (this.propertyChanged != null) - // { - // this.propertyChanged(this, e); - // } - // } - // #endregion - - // public IObservable WhenPropertyChanged - // { - // get - // { - // return Observable - // .FromEventPattern( - // h => this.propertyChanged += h, - // h => this.propertyChanged -= h) - // .Select(x => x.EventArgs.PropertyName); - // } - // } - //} -}