block 可以拖拽到内部,还有少量问题待解决

This commit is contained in:
艾竹
2023-06-11 23:05:21 +08:00
parent 4674b8904b
commit 538898d238
73 changed files with 7132 additions and 242 deletions

View File

@@ -0,0 +1,33 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
namespace AIStudio.Wpf.DiagramDesigner
{
public class BlockConnector : ContentControl
{
public ConnectorOrientation Orientation { get; set; }
public ConnectorInfoBase Info
{
get
{
if (Content is ConnectorInfoBase connectorInfo)
return connectorInfo;
return this.DataContext as ConnectorInfoBase;
}
}
}
public class BlockBorder : Border
{
}
}

View File

@@ -1,22 +1,14 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Media;
using System.Xml;
using System.Linq;
using System.Windows.Shapes;
using System.Windows.Resources;
using System.Runtime.InteropServices;
using Newtonsoft.Json;
using AIStudio.Wpf.DiagramDesigner.Models;
using AIStudio.Wpf.DiagramDesigner.ViewModels;
using AIStudio.Wpf.DiagramDesigner.ViewModels.BaseViewModel;
using System.Diagnostics;
namespace AIStudio.Wpf.DiagramDesigner
{
@@ -37,7 +29,7 @@ namespace AIStudio.Wpf.DiagramDesigner
return DiagramServicesProvider.Instance.Provider;
}
}
private ConnectionViewModel partialConnection;
private ConnectionViewModel _partialConnection;
private Point? rubberbandSelectionStartPoint = null;
@@ -63,10 +55,10 @@ namespace AIStudio.Wpf.DiagramDesigner
Point point = sourceDataItem.MiddlePosition;
partialConnection = new ConnectionViewModel(_viewModel, sourceDataItem, new PartCreatedConnectorInfo(point.X, point.Y), LineDrawMode, RouterMode);
_partialConnection = new ConnectionViewModel(_viewModel, sourceDataItem, new PartCreatedConnectorInfo(point.X, point.Y), LineDrawMode, RouterMode);
_viewModel.Add(partialConnection);
partialConnection.ZIndex = -1;
_viewModel.Add(_partialConnection);
_partialConnection.ZIndex = -1;
}
}
}
@@ -91,6 +83,30 @@ namespace AIStudio.Wpf.DiagramDesigner
}
}
}
private ItemsContainer _sourceItemsContainer;
public ItemsContainer SourceItemsContainer
{
get
{
return _sourceItemsContainer;
}
set
{
if (_sourceItemsContainer != value)
{
_sourceItemsContainer = value;
if (_sourceItemsContainer != null)
{
ItemsContainerInfo sourceDataItem = _sourceItemsContainer.Info;
sourceDataItem.DataItem.RemoveChild(_sourceItemsContainer.DragObject);
EnterMove();
}
}
}
}
private DrawMode DrawMode
{
@@ -467,7 +483,7 @@ namespace AIStudio.Wpf.DiagramDesigner
{
if (e.LeftButton == MouseButtonState.Pressed)
{
partialConnection.SinkConnectorInfo = new PartCreatedConnectorInfo(currentPoint.X, currentPoint.Y);
_partialConnection.SinkConnectorInfo = new PartCreatedConnectorInfo(currentPoint.X, currentPoint.Y);
SinkConnector = HitTesting(currentPoint);
if (SinkConnector?.Info?.CanAttachTo(SourceConnector?.Info) == false)
@@ -477,14 +493,24 @@ namespace AIStudio.Wpf.DiagramDesigner
if (_viewModel.DiagramOption.SnappingOption.EnableSnapping)
{
var nearPort = _viewModel.FindNearPortToAttachTo(partialConnection);
var nearPort = _viewModel.FindNearPortToAttachTo(_partialConnection);
if (nearPort != null)
{
partialConnection.SinkConnectorInfo = nearPort;
_partialConnection.SinkConnectorInfo = nearPort;
}
}
}
}
else if (SourceItemsContainer != null)
{
if (e.LeftButton == MouseButtonState.Pressed)
{
SourceItemsContainer.DragObject.Left = currentPoint.X - SourceItemsContainer.DragOffset.X;
SourceItemsContainer.DragObject.Top = currentPoint.Y - SourceItemsContainer.DragOffset.Y;
_viewModel.PreviewNearBlock(new System.Collections.Generic.List<BlockDesignerItemViewModel> { SourceItemsContainer.DragObject });
}
}
else
{
// if mouse button is not pressed we have no drag operation, ...
@@ -526,10 +552,10 @@ namespace AIStudio.Wpf.DiagramDesigner
{
ConnectorInfoBase sinkDataItem = SinkConnector.Info;
_viewModel.Delete(partialConnection);
_viewModel.Delete(_partialConnection);
_viewModel.AddCommand.Execute(new ConnectionViewModel(_viewModel, sourceDataItem, sinkDataItem, LineDrawMode, RouterMode));
}
else if (partialConnection.IsFullConnection)//自动连接模式
else if (_partialConnection.IsFullConnection)//自动连接模式
{
_viewModel.ClearNearPort();
}
@@ -538,19 +564,26 @@ namespace AIStudio.Wpf.DiagramDesigner
Point currentPoint = e.GetPosition(this);
ConnectorInfoBase sinkDataItem = new PartCreatedConnectorInfo(currentPoint.X, currentPoint.Y);
_viewModel.Delete(partialConnection);
_viewModel.Delete(_partialConnection);
_viewModel.AddCommand.Execute(new ConnectionViewModel(_viewModel, sourceDataItem, sinkDataItem, LineDrawMode, RouterMode));
}
else
{
//Need to remove last item as we did not finish drawing the path
_viewModel.Delete(partialConnection);
_viewModel.Delete(_partialConnection);
}
}
else if (SourceItemsContainer != null)
{
_viewModel.FinishNearBlock(new System.Collections.Generic.List<BlockDesignerItemViewModel> { SourceItemsContainer.DragObject });
ExitCursor();
}
SourceConnector = null;
SinkConnector = null;
partialConnection = null;
_partialConnection = null;
SourceItemsContainer = null;
_service.DrawModeViewModel.ResetDrawMode();
}
@@ -624,6 +657,36 @@ namespace AIStudio.Wpf.DiagramDesigner
return null;
}
protected override void OnDragOver(DragEventArgs e)
{
DragObject dragObject = e.Data.GetData(typeof(DragObject)) as DragObject;
if (dragObject != null && (dragObject.ContentType == typeof(BlockDesignerItemViewModel) || dragObject.ContentType.IsSubclassOf(typeof(BlockDesignerItemViewModel))))
{
var position = e.GetPosition(this);
BlockDesignerItemViewModel itemBase = Activator.CreateInstance(dragObject.ContentType) as BlockDesignerItemViewModel;
itemBase.Text = dragObject.Text;
itemBase.Icon = dragObject.Icon;
itemBase.ColorViewModel = CopyHelper.Mapper(dragObject.ColorViewModel);
if (dragObject.DesiredSize != null)
{
itemBase.ItemWidth = dragObject.DesiredSize.Value.Width;
itemBase.ItemHeight = dragObject.DesiredSize.Value.Height;
}
if (dragObject.DesiredMinSize != null)
{
itemBase.MinItemWidth = dragObject.DesiredMinSize.Value.Width;
itemBase.MinItemHeight = dragObject.DesiredMinSize.Value.Height;
}
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<BlockDesignerItemViewModel> { itemBase });
}
base.OnDragOver(e);
}
protected override void OnDrop(DragEventArgs e)
{
base.OnDrop(e);
@@ -640,8 +703,8 @@ namespace AIStudio.Wpf.DiagramDesigner
var designerItems = serializableObject.ToObject();
var minleft = designerItems.OfType<DesignerItemViewModelBase>().Min(p => p.Left);
var mintop = designerItems.OfType<DesignerItemViewModelBase>().Min(p => p.Top);
var maxright = designerItems.OfType<DesignerItemViewModelBase>().Max(p => p.Left + p.ItemWidth);
var maxbottom = designerItems.OfType<DesignerItemViewModelBase>().Max(p => p.Top + p.ItemHeight);
var maxright = designerItems.OfType<DesignerItemViewModelBase>().Max(p => p.Left + p.GetItemWidth());
var maxbottom = designerItems.OfType<DesignerItemViewModelBase>().Max(p => p.Top + p.GetItemHeight());
var itemswidth = maxright - minleft;
var itemsheight = maxbottom - mintop;
@@ -662,6 +725,7 @@ namespace AIStudio.Wpf.DiagramDesigner
else
{
itemBase = Activator.CreateInstance(dragObject.ContentType) as DesignerItemViewModelBase;
itemBase.Text = dragObject.Text;
itemBase.Icon = dragObject.Icon;
itemBase.ColorViewModel = CopyHelper.Mapper(dragObject.ColorViewModel);
if (dragObject.DesiredSize != null)
@@ -669,28 +733,42 @@ namespace AIStudio.Wpf.DiagramDesigner
itemBase.ItemWidth = dragObject.DesiredSize.Value.Width;
itemBase.ItemHeight = dragObject.DesiredSize.Value.Height;
}
if (dragObject.DesiredMinSize != null)
{
itemBase.MinItemWidth = dragObject.DesiredMinSize.Value.Width;
itemBase.MinItemHeight = dragObject.DesiredMinSize.Value.Height;
}
}
itemBase.Left = Math.Max(0, position.X - itemBase.ItemWidth / 2);
itemBase.Top = Math.Max(0, position.Y - itemBase.ItemHeight / 2);
itemBase.Left = Math.Max(0, position.X - itemBase.GetItemWidth() / 2);
itemBase.Top = Math.Max(0, position.Y - itemBase.GetItemHeight() / 2);
_viewModel.AddCommand.Execute(itemBase);
if (itemBase is BlockDesignerItemViewModel block)
{
_viewModel.FinishNearBlock(new System.Collections.Generic.List<BlockDesignerItemViewModel> { block });
}
}
}
var dragFile = e.Data.GetData(DataFormats.FileDrop);
if (dragFile != null && dragFile is string[] files)
else
{
foreach (var file in files)
var dragFile = e.Data.GetData(DataFormats.FileDrop);
if (dragFile != null && dragFile is string[] files)
{
_viewModel.ClearSelectedItems();
Point position = e.GetPosition(this);
ImageItemViewModel itemBase = new ImageItemViewModel();
itemBase.Icon = file;
itemBase.Suffix = System.IO.Path.GetExtension(itemBase.Icon).ToLower();
itemBase.InitWidthAndHeight();
itemBase.AutoSize();
foreach (var file in files)
{
_viewModel.ClearSelectedItems();
Point position = e.GetPosition(this);
ImageItemViewModel itemBase = new ImageItemViewModel();
itemBase.Icon = file;
itemBase.Suffix = System.IO.Path.GetExtension(itemBase.Icon).ToLower();
itemBase.InitWidthAndHeight();
itemBase.AutoSize();
itemBase.Left = Math.Max(0, position.X - itemBase.ItemWidth / 2);
itemBase.Top = Math.Max(0, position.Y - itemBase.ItemHeight / 2);
_viewModel.AddCommand.Execute(itemBase);
itemBase.Left = Math.Max(0, position.X - itemBase.GetItemWidth() / 2);
itemBase.Top = Math.Max(0, position.Y - itemBase.GetItemHeight() / 2);
_viewModel.AddCommand.Execute(itemBase);
}
}
}
e.Handled = true;

View File

@@ -21,10 +21,7 @@ namespace AIStudio.Wpf.DiagramDesigner.Controls
public IDiagramViewModel DiagramViewModel
{
get
{
return (this.DataContext as SelectableDesignerItemViewModelBase)?.Root;
}
get;set;
}
private List<SelectableDesignerItemViewModelBase> designerItems;
@@ -35,6 +32,7 @@ namespace AIStudio.Wpf.DiagramDesigner.Controls
drag = false;
SelectableDesignerItemViewModelBase designerItem = this.DataContext as SelectableDesignerItemViewModelBase;
DiagramViewModel = designerItem?.Root;
if (designerItem != null && designerItem.IsSelected)
{
@@ -99,47 +97,9 @@ namespace AIStudio.Wpf.DiagramDesigner.Controls
var blocks = designerItems.OfType<BlockDesignerItemViewModel>().ToList();
if (blocks.Any())
{
foreach (BlockDesignerItemViewModel item in blocks)
{
var portTuple = DiagramViewModel.FindNearPortToAttachTo(item, true);
var portParent = portTuple.Item1;
var portNext = portTuple.Item2;
if (portParent != null)
{
(portParent.DataItem as BlockDesignerItemViewModel).AddNext(item);
portParent.BeAttachTo = false;
portParent.DisableAttachTo = false;
}
else
{
if (item.Parent != null)
{
(item.Parent as BlockDesignerItemViewModel).Next = null;
item.Parent = null;
item.ParentId = new Guid();
}
}
if (portNext != null)
{
item.AddNext(portNext.DataItem as BlockDesignerItemViewModel);
portNext.BeAttachTo = false;
portNext.DisableAttachTo = false;
}
else
{
if (item.Next != null)
{
item.Next.Parent = null;
item.Next.ParentId = new Guid();
item.Next = null;
}
}
}
//-DiagramViewModel.ClearNearPort();
DiagramViewModel.FinishNearBlock(blocks);
}
Dictionary<DesignerItemViewModelBase, Tuple<PointBase, PointBase>> infos =
designerItems.OfType<DesignerItemViewModelBase>().ToDictionary(p => p,
p => new Tuple<PointBase, PointBase>(p.GetOldValue<PointBase>(nameof(p.TopLeft)), p.TopLeft));
@@ -207,14 +167,8 @@ namespace AIStudio.Wpf.DiagramDesigner.Controls
}
var blocks = designerItems.OfType<BlockDesignerItemViewModel>().ToList();
if (blocks.Any())
{
DiagramViewModel.ClearNearPort();
foreach (BlockDesignerItemViewModel item in blocks)
{
DiagramViewModel.FindNearPortToAttachTo(item, false);
}
}
DiagramViewModel.PreviewNearBlock(blocks);
e.Handled = true;
}
}

View File

@@ -0,0 +1,200 @@
using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using AIStudio.Wpf.DiagramDesigner.Helpers;
namespace AIStudio.Wpf.DiagramDesigner
{
public class ItemsContainer : ContentControl
{
static ItemsContainer()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(ItemsContainer), new FrameworkPropertyMetadata(typeof(ItemsContainer)));
}
public static readonly DependencyProperty BorderProperty = DependencyProperty.Register(
nameof(Border), typeof(object), typeof(ItemsContainer), new PropertyMetadata(default(object)));
public object Border
{
get => GetValue(BorderProperty);
set => SetValue(BorderProperty, value);
}
public static readonly DependencyProperty BorderTemplateProperty = DependencyProperty.Register(
nameof(BorderTemplate), typeof(DataTemplate), typeof(ItemsContainer), new PropertyMetadata(default(DataTemplate)));
[Bindable(true), Category("Content")]
public DataTemplate BorderTemplate
{
get => (DataTemplate)GetValue(BorderTemplateProperty);
set => SetValue(BorderTemplateProperty, value);
}
public static readonly DependencyProperty BorderTemplateSelectorProperty = DependencyProperty.Register(
nameof(BorderTemplateSelector), typeof(DataTemplateSelector), typeof(ItemsContainer), new PropertyMetadata(default(DataTemplateSelector)));
[Bindable(true), Category("Content")]
public DataTemplateSelector BorderTemplateSelector
{
get => (DataTemplateSelector)GetValue(BorderTemplateSelectorProperty);
set => SetValue(BorderTemplateSelectorProperty, value);
}
public static readonly DependencyProperty BorderStringFormatProperty = DependencyProperty.Register(
nameof(BorderStringFormat), typeof(string), typeof(ItemsContainer), new PropertyMetadata(default(string)));
[Bindable(true), Category("Content")]
public string BorderStringFormat
{
get => (string)GetValue(BorderStringFormatProperty);
set => SetValue(BorderStringFormatProperty, value);
}
public static readonly DependencyProperty ParentPanelProperty =
DependencyProperty.Register(nameof(ParentPanel), typeof(FrameworkElement), typeof(ItemsContainer),
new FrameworkPropertyMetadata(null));
public FrameworkElement ParentPanel
{
get
{
return (FrameworkElement)GetValue(ParentPanelProperty);
}
set
{
SetValue(ParentPanelProperty, value);
}
}
public static readonly DependencyProperty GetOffSetFuncProperty =
DependencyProperty.Register(nameof(GetOffSetFunc),
typeof(Func<Point>),
typeof(ItemsContainer),
new FrameworkPropertyMetadata(null));
public Func<Point> GetOffSetFunc
{
get
{
return (Func<Point>)this.GetValue(GetOffSetFuncProperty);
}
set
{
this.SetValue(GetOffSetFuncProperty, value);
}
}
public Func<Point> GetOffSet
{
get
{
return new Func<Point>(() => this.TransformToAncestor(ParentPanel).Transform(new Point(0, 0)));
}
}
public ItemsContainerInfo Info
{
get
{
if (Border is ItemsContainerInfo itemsContainerInfo)
return itemsContainerInfo;
return this.DataContext as ItemsContainerInfo;
}
}
public BlockDesignerItemViewModel DragObject
{
get; private set;
}
public Point DragOffset
{
get; private set;
}
public ItemsContainer()
{
}
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
GetOffSetFunc = GetOffSet;
}
Point? firstPoint;
protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
{
base.OnMouseLeftButtonDown(e);
firstPoint = e.GetPosition(this);
}
protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);
if (firstPoint != null)
{
var currentPoint = e.GetPosition(this);
if (Math.Sqrt(Math.Pow(firstPoint.Value.X - currentPoint.X, 2) + Math.Pow(firstPoint.Value.Y - currentPoint.Y, 2)) > 5)
{
if (Info?.Children?.Count > 0)
{
var borders = VisualHelper.FindVisualChildren<BlockBorder>(this);
foreach (var border in borders)
{
var point = border.TransformToAncestor(this).Transform(new Point(0, 0));
var rect = new Rect(point.X, point.Y, border.ActualWidth, border.ActualHeight);
if (rect.Contains(currentPoint))
{
DragObject = border.DataContext as BlockDesignerItemViewModel;
DragOffset = new Point(currentPoint.X - point.X, currentPoint.Y - point.Y);
break;
}
}
if (DragObject != null)
{
DesignerCanvas canvas = GetDesignerCanvas(this);
if (canvas != null)
{
canvas.SourceItemsContainer = this;
}
}
}
firstPoint = null;
}
}
}
protected override void OnMouseUp(MouseButtonEventArgs e)
{
base.OnMouseUp(e);
firstPoint = null;
DragObject = null;
}
public ConnectorOrientation Orientation
{
get; set;
}
// iterate through visual tree to get parent DesignerCanvas
private DesignerCanvas GetDesignerCanvas(DependencyObject element)
{
while (element != null && !(element is DesignerCanvas))
element = VisualTreeHelper.GetParent(element);
return element as DesignerCanvas;
}
}
}

View File

@@ -95,13 +95,13 @@ namespace AIStudio.Wpf.DiagramDesigner.Controls
{
case VerticalAlignment.Bottom:
dragDeltaVertical = Math.Min(-e.VerticalChange, minDeltaVertical);
scale = (item.ItemHeight - dragDeltaVertical) / item.ItemHeight;
scale = (item.GetItemHeight() - dragDeltaVertical) / item.GetItemHeight();
DragBottom(scale, item, DiagramViewModel.SelectionService);
break;
case VerticalAlignment.Top:
double top = item.Top;
dragDeltaVertical = Math.Min(Math.Max(-minTop, e.VerticalChange), minDeltaVertical);
scale = (item.ItemHeight - dragDeltaVertical) / item.ItemHeight;
scale = (item.GetItemHeight() - dragDeltaVertical) / item.GetItemHeight();
DragTop(scale, item, DiagramViewModel.SelectionService);
break;
default:
@@ -113,12 +113,12 @@ namespace AIStudio.Wpf.DiagramDesigner.Controls
case HorizontalAlignment.Left:
double left = item.Left;
dragDeltaHorizontal = Math.Min(Math.Max(-minLeft, e.HorizontalChange), minDeltaHorizontal);
scale = (item.ItemWidth - dragDeltaHorizontal) / item.ItemWidth;
scale = (item.GetItemWidth() - dragDeltaHorizontal) / item.GetItemWidth();
DragLeft(scale, item, DiagramViewModel.SelectionService);
break;
case HorizontalAlignment.Right:
dragDeltaHorizontal = Math.Min(-e.HorizontalChange, minDeltaHorizontal);
scale = (item.ItemWidth - dragDeltaHorizontal) / item.ItemWidth;
scale = (item.GetItemWidth() - dragDeltaHorizontal) / item.GetItemWidth();
DragRight(scale, item, DiagramViewModel.SelectionService);
break;
default:
@@ -137,26 +137,26 @@ namespace AIStudio.Wpf.DiagramDesigner.Controls
{
IEnumerable<DesignerItemViewModelBase> groupItems = selectionService.GetGroupMembers(item, false).Cast<DesignerItemViewModelBase>();
double groupLeft = item.Left + item.ItemWidth;
double groupLeft = item.Left + item.GetItemWidth();
foreach (DesignerItemViewModelBase groupItem in groupItems)
{
double groupItemLeft = groupItem.Left;
double delta = (groupLeft - groupItemLeft) * (scale - 1);
groupItem.Left = groupItemLeft - delta;
groupItem.ItemWidth = groupItem.ItemWidth * scale;
groupItem.ItemWidth = groupItem.GetItemWidth() * scale;
}
}
private void DragTop(double scale, DesignerItemViewModelBase item, SelectionService selectionService)
{
IEnumerable<DesignerItemViewModelBase> groupItems = selectionService.GetGroupMembers(item, false).Cast<DesignerItemViewModelBase>();
double groupBottom = item.Top + item.ItemHeight;
double groupBottom = item.Top + item.GetItemHeight();
foreach (DesignerItemViewModelBase groupItem in groupItems)
{
double groupItemTop = groupItem.Top;
double delta = (groupBottom - groupItemTop) * (scale - 1);
groupItem.Top = groupItemTop - delta;
groupItem.ItemHeight = groupItem.ItemHeight * scale;
groupItem.ItemHeight = groupItem.GetItemHeight() * scale;
}
}
@@ -171,7 +171,7 @@ namespace AIStudio.Wpf.DiagramDesigner.Controls
double delta = (groupItemLeft - groupLeft) * (scale - 1);
groupItem.Left = groupItemLeft + delta;
groupItem.ItemWidth = groupItem.ItemWidth * scale;
groupItem.ItemWidth = groupItem.GetItemWidth() * scale;
}
}
@@ -185,7 +185,7 @@ namespace AIStudio.Wpf.DiagramDesigner.Controls
double delta = (groupItemTop - groupTop) * (scale - 1);
groupItem.Top = groupItemTop + delta;
groupItem.ItemHeight = groupItem.ItemHeight * scale;
groupItem.ItemHeight = groupItem.GetItemHeight() * scale;
}
}

View File

@@ -53,8 +53,8 @@ namespace AIStudio.Wpf.DiagramDesigner.Controls
if (this.designerCanvas != null)
{
this.centerPoint =
new Point(this.designerItem.Left + this.designerItem.ItemWidth * 0.5,
this.designerItem.Top + this.designerItem.ItemHeight * 0.5);
new Point(this.designerItem.Left + this.designerItem.GetItemHeight() * 0.5,
this.designerItem.Top + this.designerItem.GetItemHeight() * 0.5);
Point startPoint = Mouse.GetPosition(this.designerCanvas);
this.startVector = Point.Subtract(startPoint, this.centerPoint);