添加block快,不需要连接线,直接吸附。

This commit is contained in:
艾竹
2023-05-21 22:06:59 +08:00
parent 2fd8321363
commit fde899cfa9
30 changed files with 680 additions and 338 deletions

View File

@@ -204,7 +204,7 @@ namespace AIStudio.Wpf.DiagramDesigner
}
}
private bool _isEditName;
[Browsable(false)]
@@ -373,24 +373,6 @@ namespace AIStudio.Wpf.DiagramDesigner
}
}
private ICommand _addItemCommand;
public ICommand AddItemCommand
{
get
{
return this._addItemCommand ?? (this._addItemCommand = new SimpleCommand(ExecuteEnable, ExecuteAddItemCommand));
}
}
private ICommand _removeItemCommand;
public ICommand RemoveItemCommand
{
get
{
return this._removeItemCommand ?? (this._removeItemCommand = new SimpleCommand(ExecuteEnable, ExecuteRemoveItemCommand));
}
}
private ICommand _clearSelectedItemsCommand;
public ICommand ClearSelectedItemsCommand
{
@@ -544,6 +526,24 @@ namespace AIStudio.Wpf.DiagramDesigner
}
}
private ICommand _addCommand;
public ICommand AddCommand
{
get
{
return this._addCommand ?? (this._addCommand = new SimpleCommand(ExecuteEnable, ExecuteAddCommand));
}
}
private ICommand _deleteCommand;
public virtual ICommand DeleteCommand
{
get
{
return this._deleteCommand ?? (this._deleteCommand = new SimpleCommand(ExecuteEnable, ExecuteDeleteCommand));
}
}
private ICommand _copyCommand;
public ICommand CopyCommand
{
@@ -571,15 +571,6 @@ namespace AIStudio.Wpf.DiagramDesigner
}
}
private ICommand _deleteCommand;
public virtual ICommand DeleteCommand
{
get
{
return this._deleteCommand ?? (this._deleteCommand = new SimpleCommand(ExecuteEnable, ExecuteDeleteCommand));
}
}
private ICommand _leftMoveCommand;
public ICommand LeftMoveCommand
{
@@ -1004,143 +995,13 @@ namespace AIStudio.Wpf.DiagramDesigner
var items = this.Items.ToList();
DoCommandManager.DoNewCommand(this.ToString(),
() => {
this.Items.Clear();
Delete(items);
},
() => {
foreach (var item in items)
{
Add(item);
}
Add(items);
});
}
private void ExecuteAddItemCommand(object parameter)
{
List<SelectableDesignerItemViewModelBase> newitems = new List<SelectableDesignerItemViewModelBase>();
if (parameter is SelectableDesignerItemViewModelBase ite)
{
newitems.Add(ite);
}
else if (parameter is IEnumerable<SelectableDesignerItemViewModelBase> items)
{
newitems.AddRange(items);
}
DoCommandManager.DoNewCommand(this.ToString(),
() => {
ClearSelectedItems();
Add(newitems, true);
},
() => {
foreach (var item in newitems)
{
Items.Remove(item);
}
});
}
public bool AddVerify(SelectableDesignerItemViewModelBase item)
{
if (item.Verify() == false)
return false;
return true;
}
//使用程序添加对象比如Demo初始化
public void Add(object parameter, bool? isSelected = false)
{
if (parameter is SelectableDesignerItemViewModelBase ite)
{
if (AddVerify(ite) != true) return;
Add(ite, isSelected);
}
else if (parameter is IEnumerable<SelectableDesignerItemViewModelBase> items)
{
if (items.Select(p => AddVerify(p)).Any() != true) return;
foreach (var item in items)
{
Add(item, isSelected);
}
}
}
private void Add(SelectableDesignerItemViewModelBase item, bool? isSelected = true)
{
item.Root = this;
item.ZIndex = Items.Any() ? Items.Max(p => p.ZIndex) + 1 : 0;
if (item.Id == Guid.Empty)
{
item.Id = Guid.NewGuid();
}
var logical = item as LogicalGateItemViewModelBase;
if (logical != null)
{
logical.OrderNumber = Items.OfType<LogicalGateItemViewModelBase>().Count() + 1;
}
var designerItemViewModelBase = item as DesignerItemViewModelBase;
if (designerItemViewModelBase != null)
{
designerItemViewModelBase.SetCellAlignment();
}
Items.Add(item);
if (isSelected != null)
{
item.IsSelected = isSelected.Value;
}
}
public void Remove(object parameter)
{
if (parameter is SelectableDesignerItemViewModelBase ite)
{
ite.IsSelected = false;
ite.Dispose();
Items.Remove(ite);
}
else if (parameter is IEnumerable<SelectableDesignerItemViewModelBase> items)
{
foreach (var item in items)
{
item.IsSelected = false;
item.Dispose();
Items.Remove(item);
}
}
}
private void ExecuteRemoveItemCommand(object parameter)
{
List<SelectableDesignerItemViewModelBase> olditems = new List<SelectableDesignerItemViewModelBase>();
if (parameter is SelectableDesignerItemViewModelBase node)
{
olditems.Add(node);
}
else if (parameter is IEnumerable<SelectableDesignerItemViewModelBase> para)
{
olditems.AddRange(para);
}
else
{
olditems.AddRange(SelectedItems);
}
if (olditems.Any())
{
DoCommandManager.DoNewCommand(this.ToString(),
() => {
Remove(olditems);
},
() => {
Add(olditems);
});
}
}
private void ExecuteClearSelectedItemsCommand(object parameter)
{
List<SelectableDesignerItemViewModelBase> selectedItems = new List<SelectableDesignerItemViewModelBase>();
@@ -1187,7 +1048,7 @@ namespace AIStudio.Wpf.DiagramDesigner
public virtual void ExecuteNextCommand(object parameter)
{
}
private void ExecuteSelectAllCommand(object parameter)
@@ -1284,7 +1145,84 @@ namespace AIStudio.Wpf.DiagramDesigner
}
#endregion
#region ,
#region , ,
private void ExecuteAddCommand(object parameter)
{
List<SelectableDesignerItemViewModelBase> newitems = new List<SelectableDesignerItemViewModelBase>();
if (parameter is SelectableDesignerItemViewModelBase ite)
{
newitems.Add(ite);
}
else if (parameter is IEnumerable<SelectableDesignerItemViewModelBase> items)
{
newitems.AddRange(items);
}
DoCommandManager.DoNewCommand(this.ToString(),
() => {
ClearSelectedItems();
Add(newitems, true);
},
() => {
Delete(newitems);
});
}
public bool AddVerify(SelectableDesignerItemViewModelBase item)
{
if (item.Verify() == false)
return false;
return true;
}
//使用程序添加对象比如Demo初始化
public void Add(object parameter, bool? isSelected = false)
{
if (parameter is SelectableDesignerItemViewModelBase ite)
{
if (AddVerify(ite) != true) return;
Add(ite, isSelected);
}
else if (parameter is IEnumerable<SelectableDesignerItemViewModelBase> items)
{
if (items.Select(p => AddVerify(p)).Any() != true) return;
foreach (var item in items)
{
Add(item, isSelected);
}
}
}
private void Add(SelectableDesignerItemViewModelBase item, bool? isSelected = true)
{
item.Root = this;
item.ZIndex = Items.Any() ? Items.Max(p => p.ZIndex) + 1 : 0;
if (item.Id == Guid.Empty)
{
item.Id = Guid.NewGuid();
}
var logical = item as LogicalGateItemViewModelBase;
if (logical != null)
{
logical.OrderNumber = Items.OfType<LogicalGateItemViewModelBase>().Count() + 1;
}
var designerItemViewModelBase = item as DesignerItemViewModelBase;
if (designerItemViewModelBase != null)
{
designerItemViewModelBase.SetCellAlignment();
}
Items.Add(item);
if (isSelected != null)
{
item.IsSelected = isSelected.Value;
}
}
private void ExecuteCopyCommand(object parameter)
{
Copy(parameter);
@@ -1436,10 +1374,7 @@ namespace AIStudio.Wpf.DiagramDesigner
FixOtherInfo(items);
},
() => {
foreach (var item in items)
{
Items.Remove(item);
}
Delete(items);
});
}
}
@@ -1484,16 +1419,10 @@ namespace AIStudio.Wpf.DiagramDesigner
{
DoCommandManager.DoNewCommand(this.ToString(),
() => {
foreach (var item in items)
{
Items.Remove(item);
}
Delete(items);
},
() => {
foreach (var item in items)
{
Items.Add(item);
}
Add(items);
});
}
}
@@ -1508,21 +1437,20 @@ namespace AIStudio.Wpf.DiagramDesigner
{
DoCommandManager.DoNewCommand(this.ToString(),
() => {
foreach (var item in items)
{
Items.Remove(item);
}
Delete(items);
},
() => {
foreach (var item in items)
{
Items.Add(item);
}
Add(items);
});
}
}
protected virtual List<SelectableDesignerItemViewModelBase> Delete(object parameter, bool delete = true)
public void Delete(object parameter)
{
Delete(parameter, true, true);
}
protected virtual List<SelectableDesignerItemViewModelBase> Delete(object parameter, bool delete = true, bool direct = false)
{
List<SelectableDesignerItemViewModelBase> itemsToRemove = new List<SelectableDesignerItemViewModelBase>();
if (parameter is SelectableDesignerItemViewModelBase node)
@@ -1538,22 +1466,25 @@ namespace AIStudio.Wpf.DiagramDesigner
itemsToRemove.AddRange(SelectedItems);
}
List<SelectableDesignerItemViewModelBase> connectionsToAlsoRemove = new List<SelectableDesignerItemViewModelBase>();
foreach (var connector in Items.OfType<ConnectionViewModel>())
if (direct == false)
{
if (ItemsToDeleteHasConnector(itemsToRemove, connector.SourceConnectorInfo))
{
connectionsToAlsoRemove.Add(connector);
}
List<SelectableDesignerItemViewModelBase> connectionsToAlsoRemove = new List<SelectableDesignerItemViewModelBase>();
if (ItemsToDeleteHasConnector(itemsToRemove, connector.SinkConnectorInfo))
foreach (var connector in Items.OfType<ConnectionViewModel>())
{
connectionsToAlsoRemove.Add(connector);
}
if (ItemsToDeleteHasConnector(itemsToRemove, connector.SourceConnectorInfo))
{
connectionsToAlsoRemove.Add(connector);
}
if (ItemsToDeleteHasConnector(itemsToRemove, connector.SinkConnectorInfo))
{
connectionsToAlsoRemove.Add(connector);
}
}
itemsToRemove.AddRange(connectionsToAlsoRemove);
}
itemsToRemove.AddRange(connectionsToAlsoRemove);
if (itemsToRemove.Any())
{
@@ -1561,6 +1492,7 @@ namespace AIStudio.Wpf.DiagramDesigner
{
foreach (var item in itemsToRemove)
{
item.Dispose();
Items.Remove(item);
}
}
@@ -2501,7 +2433,7 @@ namespace AIStudio.Wpf.DiagramDesigner
groupItem.AddToSelection(true, true);
},
() => {
Remove(groupItem);
Delete(groupItem);
foreach (DesignerItemViewModelBase item in items)
{
item.IsSelected = true;
@@ -2533,10 +2465,7 @@ namespace AIStudio.Wpf.DiagramDesigner
DoCommandManager.DoNewCommand(this.ToString(),
() => {
foreach (DesignerItemViewModelBase groupRoot in groups)
{
Remove(groupRoot);
}
Delete(groups);
foreach (var item in items)
{
item.Key.ParentId = Guid.Empty;
@@ -2544,10 +2473,7 @@ namespace AIStudio.Wpf.DiagramDesigner
UpdateZIndex();
},
() => {
foreach (DesignerItemViewModelBase groupRoot in groups)
{
Add(groupRoot);
}
Add(groups);
foreach (var item in items)
{
item.Key.ParentId = item.Value.Item1;
@@ -3105,13 +3031,101 @@ namespace AIStudio.Wpf.DiagramDesigner
port.DisableAttachTo = true;
return null;
}
}
}
return null;
}
/// <summary>
/// type=0最近且没有依附=1
/// </summary>
/// <param name="blockDesignerItemViewModel"></param>
/// <param name="type"></param>
/// <returns></returns>
public Tuple<FullyCreatedConnectorInfo, FullyCreatedConnectorInfo> FindNearPortToAttachTo(BlockDesignerItemViewModel blockDesignerItemViewModel, bool isExist)
{
if (blockDesignerItemViewModel == null)
return new Tuple<FullyCreatedConnectorInfo, FullyCreatedConnectorInfo>(null, null);
List<BlockDesignerItemViewModel> items;
if (isExist == false)
{
items = Items.OfType<BlockDesignerItemViewModel>().
Where(p => p != blockDesignerItemViewModel && p != blockDesignerItemViewModel.Parent && p != blockDesignerItemViewModel.Next)
.ToList();
}
else
{
items = Items.OfType<BlockDesignerItemViewModel>().
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) < 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) < 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<FullyCreatedConnectorInfo, FullyCreatedConnectorInfo>(parent, next);
}
public void ClearNearPort()
{
Items.OfType<DesignerItemViewModelBase>().ToList().SelectMany(n => n.Connectors).Where(p => p.BeAttachTo == true || p.DisableAttachTo == true).ToList()