using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Text; using System.Threading.Tasks; 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) { } 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)); IsReadOnlyText = true; } public BlockDesignerItemViewModel Next { get; set; } public ItemsContainerInfo ParentContain { get; set; } public void AddNext(BlockDesignerItemViewModel next) { if (this.Next == next) { AlignNext(next); return; } RemoveNext(); next.Left = this.Left; next.Top = this.Top + this.GetItemHeight(); next.ParentId = this.Id; next.Parent = this; this.Next = next; if (next.Next != null) { next.AlignNext(next.Next); } } public void AlignNext(BlockDesignerItemViewModel next) { if (next != null && this.Next == next) { next.Left = this.Left; next.Top = this.Top + this.GetItemHeight(); if (next.Next != null) { next.AlignNext(next.Next); } return; } } public void RemoveNext() { var next = this.Next; if (next != null) { next.ParentId = new Guid(); next.Parent = null; this.Next = null; } } public BlockDesignerItemViewModel GetLastNext() { var next = this.Next; if (next != null) { while (next.Next != null) { next = next.Next; } } return next; } 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 AddChild(BlockDesignerItemViewModel child) { child.RemoveFromSelection(); this.AddToSelection(true, false); System.Windows.Application.Current?.Dispatcher.BeginInvoke(new Action(async () => { await Task.Delay(10); AlignNext(this.Next); })); } public virtual void RemoveChild(BlockDesignerItemViewModel child) { this.RemoveFromSelection(); child.AddToSelection(true, false); System.Windows.Application.Current?.Dispatcher.BeginInvoke(new Action(async () => { await Task.Delay(10); AlignNext(this.Next); })); } public ObservableCollection Contains { get; set; } = new ObservableCollection(); public ItemsContainerInfo FirstContain { get { return Contains?.FirstOrDefault(); } } } }