Files
aistudio-wpf-diagram/AIStudio.Wpf.DiagramDesigner/ViewModels/ConnectorViewModel.cs

332 lines
10 KiB
C#
Raw Normal View History

2021-07-23 09:42:22 +08:00
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Windows;
using System.Windows.Media;
2022-10-28 22:45:39 +08:00
using AIStudio.Wpf.DiagramDesigner.Helpers;
2021-07-23 09:42:22 +08:00
2022-10-28 22:45:39 +08:00
namespace AIStudio.Wpf.DiagramDesigner
2021-07-23 09:42:22 +08:00
{
public class ConnectorViewModel : SelectableDesignerItemViewModelBase
{
public ConnectorViewModel(IDiagramViewModel parent, FullyCreatedConnectorInfo sourceConnectorInfo, FullyCreatedConnectorInfo sinkConnectorInfo,
SelectableDesignerItemBase designer, DrawMode vectorLineDrawMode) : base(parent, designer)
2021-07-23 09:42:22 +08:00
{
VectorLineDrawMode = vectorLineDrawMode;
2021-07-23 09:42:22 +08:00
Init(sourceConnectorInfo, sinkConnectorInfo);
2022-11-30 22:28:22 +08:00
2021-07-23 09:42:22 +08:00
}
public ConnectorViewModel(FullyCreatedConnectorInfo sourceConnectorInfo, ConnectorInfoBase sinkConnectorInfo, DrawMode vectorLineDrawMode)
2021-07-23 09:42:22 +08:00
{
VectorLineDrawMode = vectorLineDrawMode;
2021-07-23 09:42:22 +08:00
Init(sourceConnectorInfo, sinkConnectorInfo);
}
2022-11-30 22:28:22 +08:00
public IPathFinder PathFinder
{
get; set;
}
2021-07-23 09:42:22 +08:00
public bool IsFullConnection
{
2022-11-30 22:28:22 +08:00
get
{
return _sinkConnectorInfo is FullyCreatedConnectorInfo;
}
2021-07-23 09:42:22 +08:00
}
private Point _sourceA;
public Point SourceA
{
get
{
return _sourceA;
}
set
{
if (SetProperty(ref _sourceA, value))
{
UpdateArea();
}
}
}
private Point _sourceB;
public Point SourceB
{
get
{
return _sourceB;
}
set
{
if (SetProperty(ref _sourceB, value))
{
UpdateArea();
}
}
}
private List<PointInfoBase> _connectionPoints;
public List<PointInfoBase> ConnectionPoints
2021-07-23 09:42:22 +08:00
{
get
{
return _connectionPoints;
}
private set
{
if (_connectionPoints != null)
{
_connectionPoints.ForEach(p => p.PropertyChanged -= new WeakINPCEventHandler(ConnectionPoint_PropertyChanged).Handler);
}
2021-07-23 09:42:22 +08:00
SetProperty(ref _connectionPoints, value);
if (_connectionPoints != null)
{
_connectionPoints.ForEach(p => p.PropertyChanged += new WeakINPCEventHandler(ConnectionPoint_PropertyChanged).Handler);
}
2021-07-23 09:42:22 +08:00
}
}
private Point _startPoint;
public Point StartPoint
{
get
{
return _startPoint;
}
private set
{
SetProperty(ref _startPoint, value);
}
}
private Point _endPoint;
public Point EndPoint
{
get
{
return _endPoint;
}
private set
{
SetProperty(ref _endPoint, value);
}
}
private Rect _area;
public Rect Area
{
get
{
return _area;
}
private set
{
if (SetProperty(ref _area, value))
{
2021-07-23 09:42:22 +08:00
UpdateConnectionPoints();
OutTextItemLocation(_area, value);
}
}
}
2022-11-30 22:28:22 +08:00
public DrawMode VectorLineDrawMode
{
get; set;
}
2021-07-23 09:42:22 +08:00
public ConnectorInfo ConnectorInfo(ConnectorOrientation orientation, double left, double top, double width, double height, Point position)
{
return new ConnectorInfo()
{
Orientation = orientation,
DesignerItemSize = new Size(width, height),
DesignerItemLeft = left,
DesignerItemTop = top,
Position = position
};
}
private FullyCreatedConnectorInfo _sourceConnectorInfo;
public FullyCreatedConnectorInfo SourceConnectorInfo
{
get
{
return _sourceConnectorInfo;
}
set
{
if (SetProperty(ref _sourceConnectorInfo, value))
{
SourceA = PointHelper.GetPointForConnector(_sourceConnectorInfo);
(_sourceConnectorInfo.DataItem as INotifyPropertyChanged).PropertyChanged += new WeakINPCEventHandler(ConnectorViewModel_PropertyChanged).Handler;
}
}
}
private ConnectorInfoBase _sinkConnectorInfo;
public ConnectorInfoBase SinkConnectorInfo
{
get
{
return _sinkConnectorInfo;
}
set
{
if (SetProperty(ref _sinkConnectorInfo, value))
{
if (_sinkConnectorInfo is FullyCreatedConnectorInfo)
{
SourceB = PointHelper.GetPointForConnector((FullyCreatedConnectorInfo)_sinkConnectorInfo);
(((FullyCreatedConnectorInfo)_sinkConnectorInfo).DataItem as INotifyPropertyChanged).PropertyChanged += new WeakINPCEventHandler(ConnectorViewModel_PropertyChanged).Handler;
}
else
{
SourceB = ((PartCreatedConnectionInfo)SinkConnectorInfo).CurrentLocation;
}
}
}
}
private void UpdateArea()
{
Area = new Rect(SourceA, SourceB);
}
private void UpdateConnectionPoints()
{
2022-11-30 22:28:22 +08:00
ConnectionPoints = PathFinder.UpdateConnectionPoints(Parent, SourceA, SourceB, SourceConnectorInfo, SinkConnectorInfo);
StartPoint = ConnectionPoints.First();
2021-07-23 09:42:22 +08:00
EndPoint = ConnectionPoints.Last();
2022-11-30 22:28:22 +08:00
}
2021-07-23 09:42:22 +08:00
private void ConnectorViewModel_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
switch (e.PropertyName)
{
case "ItemHeight":
case "ItemWidth":
case "Left":
case "Top":
SourceA = PointHelper.GetPointForConnector(this.SourceConnectorInfo);
if (this.SinkConnectorInfo is FullyCreatedConnectorInfo)
{
SourceB = PointHelper.GetPointForConnector((FullyCreatedConnectorInfo)this.SinkConnectorInfo);
}
break;
}
}
private void ConnectionPoint_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
switch (e.PropertyName)
{
case "Left":
case "Top":
RaisePropertyChanged(nameof(ConnectionPoints));
break;
}
}
2021-07-23 09:42:22 +08:00
private void Init(FullyCreatedConnectorInfo sourceConnectorInfo, ConnectorInfoBase sinkConnectorInfo)
{
this.Parent = sourceConnectorInfo.DataItem.Parent;
2022-11-30 22:28:22 +08:00
if (VectorLineDrawMode == DrawMode.ConnectingLine)
{
PathFinder = new StraightLinePathFinder();
}
else if (VectorLineDrawMode == DrawMode.BoundaryConnectingLine)
{
PathFinder = new BoundaryPathFinder();
}
else
{
PathFinder = new OrthogonalPathFinder();
}
2021-07-23 09:42:22 +08:00
this.SourceConnectorInfo = sourceConnectorInfo;
2022-11-30 22:28:22 +08:00
this.SinkConnectorInfo = sinkConnectorInfo;
2021-07-23 09:42:22 +08:00
DeleteConnectionCommand = new SimpleCommand(DeleteConnection);
if (sinkConnectorInfo is FullyCreatedConnectorInfo sink && sink.DataItem.ShowArrow == false)
{
this.ColorViewModel.RightArrowPathStyle = ArrowPathStyle.None;
}
2021-07-23 09:42:22 +08:00
}
2022-11-30 22:28:22 +08:00
public SimpleCommand DeleteConnectionCommand
{
get; set;
}
2021-07-23 09:42:22 +08:00
private void DeleteConnection(object args)
{
if (this.Parent is IDiagramViewModel)
{
var diagramVM = this.Parent as IDiagramViewModel;
diagramVM.RemoveItemCommand.Execute(this);
}
}
protected override void ExecuteEditCommand(object param)
{
if (this.OutTextItem != null) return;
AddText("");
}
public void AddText(string text)
{
if (this.Parent is IDiagramViewModel)
{
var diagramVM = this.Parent as IDiagramViewModel;
TextDesignerItemViewModel textitem = new TextDesignerItemViewModel();
textitem.ItemWidth = Double.NaN;
textitem.ItemHeight = double.NaN;
if (diagramVM.DiagramType == DiagramType.FlowChart)
{
var mid = (int)(ConnectionPoints.Count / 2);
2022-11-30 22:28:22 +08:00
var p = BoundaryPathFinder.SegmentMiddlePoint(ConnectionPoints[mid - 1], ConnectionPoints[mid]);
2021-07-23 09:42:22 +08:00
textitem.Left = this.Area.Left + p.X + 2;
textitem.Top = this.Area.Top + p.Y - 15;
}
else
{
textitem.Left = this.Area.Left + this.Area.Width / 2 - 16;
textitem.Top = this.Area.Top + this.Area.Height / 2 - 5;
}
textitem.Watermark = null;
textitem.ZIndex = diagramVM.Items.Count;
textitem.ParentId = this.Id;
textitem.ParentItem = this;
textitem.ColorViewModel.FillColor = new ColorObject() { Color = Colors.White };
textitem.Text = text;
diagramVM.DirectAddItemCommand.Execute(textitem);
this.OutTextItem = textitem;
}
}
public void OutTextItemLocation(Rect oldArea, Rect newArea)
{
if (this.OutTextItem is TextDesignerItemViewModel text)
{
var oldpoint = new Point(oldArea.Left + oldArea.Width / 2, oldArea.Top + oldArea.Height / 2);
var newpoint = new Point(newArea.Left + newArea.Width / 2, newArea.Top + newArea.Height / 2);
text.Left = text.Left + newpoint.X - oldpoint.X;
text.Top = text.Top + newpoint.Y - oldpoint.Y;
}
}
}
}