using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Documents; using AIStudio.Wpf.DiagramDesigner.Geometrys; using AIStudio.Wpf.DiagramDesigner.Models; using static System.Net.Mime.MediaTypeNames; namespace AIStudio.Wpf.DiagramDesigner { public class BlockDesignerItemViewModel : DesignerItemViewModelBase { public BlockDesignerItemViewModel() { } public BlockDesignerItemViewModel(IDiagramViewModel root) : base(root) { } public BlockDesignerItemViewModel(IDiagramViewModel root, SelectableItemBase designer) : base(root, designer) { } public BlockDesignerItemViewModel(IDiagramViewModel root, SerializableItem serializableItem, string serializableType) : base(root, serializableItem, serializableType) { } public override SelectableItemBase GetSerializableObject() { return new BlockDesignerItem(this); } protected override void InitNew() { ItemWidth = double.NaN; ItemHeight = double.NaN; AddConnector(new BlockConnectorInfo(this.Root, this, ConnectorOrientation.Top)); AddConnector(new BlockConnectorInfo(this.Root, this, ConnectorOrientation.Bottom)); } protected override void Init(IDiagramViewModel root, bool initNew) { base.Init(root, initNew); IsReadOnlyText = true; } protected override void LoadDesignerItemViewModel(SelectableItemBase designerbase) { base.LoadDesignerItemViewModel(designerbase); if (designerbase is BlockDesignerItem designer) { if (designer.Connectors != null) { ClearConnectors(); foreach (var connector in designer.Connectors) { BlockConnectorInfo fullyCreatedConnectorInfo = new BlockConnectorInfo(this.Root, this, connector); AddConnector(fullyCreatedConnectorInfo); } } if (designer.Containers != null) { ClearContainers(); foreach (var container in designer.Containers) { BlockItemsContainerInfo blockitemsContainerInfo = new BlockItemsContainerInfo(this.Root, this, container); AddContainer(blockitemsContainerInfo); } } } } public void AddNext(BlockDesignerItemViewModel next, BlockDesignerItemViewModel first = null) { if (LinkNode.Next?.Value == next) { AlignNext(LinkNode.Next); return; } var linkedList = LinkNode.List; var nextlinkedList = next.LinkNode.List; if (nextlinkedList == linkedList)//后面的拖到前面去了 { if (first != null) { first.RemovePrevious(); } else { next.LinkNode.Previous?.Value.RemoveNext(); } nextlinkedList = next.LinkNode.List; } linkedList.InsertRange(LinkNode, nextlinkedList); nextlinkedList.Clear(); AlignNext(LinkNode.Next); } public void AlignNext(LinkedListNode nextnode) { if (nextnode != null && LinkNode.Next == nextnode) { nextnode.Value.Left = this.Left; nextnode.Value.Top = this.Top + this.GetItemHeight(); if (nextnode.Next != null) { nextnode.Value.AlignNext(nextnode.Next); } } } public List RemoveNext(bool allbreak = false) { List removes = new List(); var next = LinkNode?.Next; if (next != null) { var linkedList = LinkNode.List; var list = linkedList.ToList(); int index = list.IndexOf(next.Value); //BlockDesignerItemViewModel[] array = new BlockDesignerItemViewModel[linkedList.Count]; //linkedList.CopyTo(array, index); var nextlinkedList = new LinkedList(); for (int i = list.Count - 1; i >= index; i--) { var nextnode = linkedList.Find(list[i]); linkedList.Remove(nextnode); if (allbreak)//全部变成散落的 { list[i].ParentId = new Guid(); } else//后面的连接成一个链表 { list[i].LinkNode = nextlinkedList.AddFirst(list[i]); } removes.Add(list[i]); } if (nextlinkedList.First != null) { nextlinkedList.First.Value.ParentId = new Guid(); } } return removes; } public List RemoveSelf(bool allbreak = false) { List removes = new List(); var linkedList = LinkNode.List; var list = linkedList.ToList(); int index = list.IndexOf(this); var nextlinkedList = new LinkedList(); for (int i = list.Count - 1; i >= index; i--) { var nextnode = linkedList.Find(list[i]); linkedList.Remove(nextnode); if (allbreak)//全部变成散落的 { list[i].ParentId = new Guid(); } else//后面的连接成一个链表 { list[i].LinkNode = nextlinkedList.AddFirst(list[i]); } removes.Add(list[i]); } if (nextlinkedList.First != null) { nextlinkedList.First.Value.ParentId = new Guid(); } return removes; } public BlockDesignerItemViewModel RemovePrevious(bool allbreak = false) { var previous = this.LinkNode.Previous; if (previous != null) { var linkedList = LinkNode.List; var previouslinkedList = new LinkedList(); var list = linkedList.ToList(); int index = list.IndexOf(previous.Value); for (int i = 0; i <= index; i++) { var previousnode = linkedList.Find(list[i]); linkedList.Remove(previousnode); if (allbreak)//全部变成散落的 { list[i].ParentId = new Guid(); } else//前面的连接成一个链表 { list[i].LinkNode = previouslinkedList.AddLast(list[i]); } } this.ParentId = new Guid(); return previous.Value; } else { return null; } } public override void AddToSelection(bool selected, bool clearother) { if (clearother) { foreach (SelectableDesignerItemViewModelBase item in Root.SelectedItems.ToList()) { if (item != this) { item.RemoveFromSelection(); } } } IsSelected = selected; } public virtual void InsertChild(BlockDesignerItemViewModel child, BlockItemsContainerInfo container, int index) { if (container == null) { container = FirstContainer; } if (container.OnlyOneChild) { var oldchildren = container.Children.FirstOrDefault(); if (oldchildren != null) { this.RemoveChild(oldchildren, container); } Root.Items.Remove(child); if (child.LinkNode?.Previous != null) { child.LinkNode.Previous.Value.RemoveNext(); } if (child.LinkNode?.Next != null) { child.LinkNode.Next.Value.RemovePrevious(); } container.InsertChild(child, index); } else { var items = new List(); var linkedList = child.LinkNode.List; if (child.LinkNode?.Previous != null) { items = child.LinkNode.Previous.Value.RemoveNext(true); } else { items = child.LinkNode.Value.RemoveSelf(true); } items.ForEach(p => { Root.Items.Remove(p); container.InsertChild(p, index); }); } child.RemoveFromSelection(); this.GetRootContainItem.AddToSelection(true, true); System.Windows.Application.Current?.Dispatcher.BeginInvoke(new Action(async () => { await Task.Delay(10); AlignNext(LinkNode?.Next); })); } public virtual void RemoveChild(BlockDesignerItemViewModel child, BlockItemsContainerInfo container) { if (container == null) { container = FirstContainer; } Root.Items.Add(child); container.RemoveChild(child); this.RemoveFromSelection(); child.AddToSelection(true, true); System.Windows.Application.Current?.Dispatcher.BeginInvoke(new Action(async () => { await Task.Delay(10); AlignNext(LinkNode?.Next); })); } private bool _isExecuting; public bool IsExecuting { get { return _isExecuting; } set { SetProperty(ref _isExecuting, value); } } public string Flag { get; set; } public int LinkNodeLevel { get { if (LinkNode?.Previous == null) { return 0; } else { return LinkNode.Previous.Value.LinkNodeLevel + 1; } } } private LinkedListNode _linkNode; public LinkedListNode LinkNode { get { if (_linkNode == null || _linkNode.List == null) { var linklist = new LinkedList(); ; _linkNode = linklist.AddFirst(this); } return _linkNode; } set { _linkNode = value; } } public BlockItemsContainerInfo ParentContainer { get; set; } public ObservableCollection Containers { get; set; } = new ObservableCollection(); public BlockItemsContainerInfo FirstContainer { get { return Containers?.FirstOrDefault(); } } public BlockItemsContainerInfo SecondContainer { get { return Containers?.Skip(1)?.FirstOrDefault(); } } public BlockItemsContainerInfo ThirdContainer { get { return Containers?.Skip(2)?.FirstOrDefault(); } } public void AddContainer(BlockItemsContainerInfo container) { Containers.Add(container); RaisePropertyChanged(nameof(FirstContainer)); RaisePropertyChanged(nameof(SecondContainer)); RaisePropertyChanged(nameof(ThirdContainer)); } public void RemoveContainer(BlockItemsContainerInfo container) { Containers.Remove(container); RaisePropertyChanged(nameof(FirstContainer)); RaisePropertyChanged(nameof(SecondContainer)); RaisePropertyChanged(nameof(ThirdContainer)); } public void ClearContainers() { Containers.Clear(); RaisePropertyChanged(nameof(FirstContainer)); RaisePropertyChanged(nameof(SecondContainer)); RaisePropertyChanged(nameof(ThirdContainer)); } public List GetAllContainers() { return Containers.SelectMany(p => p.GetAllContainers(p.Children, true)).ToList(); } public BlockDesignerItemViewModel GetRootContainItem { get { if (ParentContainer == null) { return this; } else { return ParentContainer.DataItem.GetRootContainItem; } } } #region 执行 protected override async void ExecuteEditCommand(object param) { await this.LinkNode.List.First.Value.Execute(); } public override void PreviewExecuteEdit() { var items = this.LinkNode.List.ToList(); items.ForEach(p => p.IsExecuting = true); } public override void ExitPreviewExecuteEdit() { var items = this.LinkNode.List.ToList(); items.ForEach(p => p.IsExecuting = false); } public async Task Execute() { await StopExecution(); await BeforeExecution(); if (await Executing()) { await AfterExecution(); } else { IsExecuting = false; } } public virtual Task BeforeExecution() { IsExecuting = true; return Task.CompletedTask; } public virtual Task Executing() { return Task.FromResult(true); } public virtual Task StopExecution() { return Task.CompletedTask; } public virtual async Task AfterExecution() { if (IsExecuting) { IsExecuting = false; if (LinkNode.Next != null) await LinkNode.Next.Value.Execute(); } } public virtual object GetResult() { if (FirstContainer != null) { return FirstContainer.GetResult(); } else if (SecondContainer != null) { return SecondContainer.GetResult(); } else if (ThirdContainer != null) { return ThirdContainer.GetResult(); } return null; } #endregion } #region 扩展 public class BlockContainDesignerItemViewModel : BlockDesignerItemViewModel { public BlockContainDesignerItemViewModel() { } public BlockContainDesignerItemViewModel(IDiagramViewModel root) : base(root) { } public BlockContainDesignerItemViewModel(IDiagramViewModel root, SelectableItemBase designer) : base(root, designer) { } public BlockContainDesignerItemViewModel(IDiagramViewModel root, SerializableItem serializableItem, string serializableType) : base(root, serializableItem, serializableType) { } protected override void InitNew() { base.InitNew(); Containers.Add(new BlockItemsContainerInfo(this.Root, this, true, null)); } } public class BlockContainListDesignerItemViewModel : BlockDesignerItemViewModel { public BlockContainListDesignerItemViewModel() { } public BlockContainListDesignerItemViewModel(IDiagramViewModel root) : base(root) { } public BlockContainListDesignerItemViewModel(IDiagramViewModel root, SelectableItemBase designer) : base(root, designer) { } public BlockContainListDesignerItemViewModel(IDiagramViewModel root, SerializableItem serializableItem, string serializableType) : base(root, serializableItem, serializableType) { } protected override void InitNew() { base.InitNew(); Containers.Add(new BlockItemsContainerInfo(this.Root, this, false, null)); } } #endregion #region 帮助 public class BlockDesignerItemTempLink { public LinkedList LinkList { get; set; } = new LinkedList(); public RectangleBase GetBounds() { return LinkList.FirstOrDefault().GetBounds(); } public List Connectors { get { List connectors = new List(); if (LinkList.FirstOrDefault().TopConnector != null) { connectors.Add(LinkList.FirstOrDefault().TopConnector); } if (LinkList.FirstOrDefault().LeftConnector != null) { connectors.Add(LinkList.FirstOrDefault().LeftConnector); } if (LinkList.LastOrDefault().BottomConnector != null) { connectors.Add(LinkList.LastOrDefault().BottomConnector); } if (LinkList.LastOrDefault().RightConnector != null) { connectors.Add(LinkList.LastOrDefault().RightConnector); } return connectors; } } public static List Build(List blocks) { List links = new List(); foreach (var blockgroup in blocks.GroupBy(p => p.LinkNode.List)) { var link = new BlockDesignerItemTempLink(); foreach (var block in blockgroup.OrderBy(p => p.LinkNodeLevel)) { link.LinkList.AddLast(block); } links.Add(link); } return links; } } #endregion }