mirror of
https://gitee.com/akwkevin/aistudio.-wpf.-diagram
synced 2026-04-08 18:26:35 +08:00
部分连接线拖拽完成
This commit is contained in:
@@ -32,14 +32,14 @@ namespace AIStudio.Wpf.DiagramDesigner
|
||||
return element as DesignerCanvas;
|
||||
}
|
||||
|
||||
public FullyCreatedConnectorInfo Info
|
||||
public ConnectorInfoBase Info
|
||||
{
|
||||
get
|
||||
{
|
||||
if (Content is FullyCreatedConnectorInfo connectorInfo)
|
||||
if (Content is ConnectorInfoBase connectorInfo)
|
||||
return connectorInfo;
|
||||
|
||||
return this.DataContext as FullyCreatedConnectorInfo;
|
||||
return this.DataContext as ConnectorInfoBase;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -37,7 +37,7 @@ namespace AIStudio.Wpf.DiagramDesigner
|
||||
return DiagramServicesProvider.Instance.Provider;
|
||||
}
|
||||
}
|
||||
private ConnectionViewModel partialConnection;
|
||||
private ConnectionViewModel partialConnection;
|
||||
|
||||
private Point? rubberbandSelectionStartPoint = null;
|
||||
|
||||
@@ -54,11 +54,14 @@ namespace AIStudio.Wpf.DiagramDesigner
|
||||
{
|
||||
sourceConnector = value;
|
||||
|
||||
FullyCreatedConnectorInfo sourceDataItem = sourceConnector.Info;
|
||||
ConnectorInfoBase sourceDataItem = sourceConnector.Info;
|
||||
|
||||
//Rect rectangleBounds = sourceConnector.TransformToVisual(this).TransformBounds(new Rect(sourceConnector.RenderSize));
|
||||
//Point point = new Point(rectangleBounds.Left + (rectangleBounds.Width / 2),
|
||||
// rectangleBounds.Bottom + (rectangleBounds.Height / 2));
|
||||
|
||||
Point point = sourceDataItem.MiddlePosition;
|
||||
|
||||
Rect rectangleBounds = sourceConnector.TransformToVisual(this).TransformBounds(new Rect(sourceConnector.RenderSize));
|
||||
Point point = new Point(rectangleBounds.Left + (rectangleBounds.Width / 2),
|
||||
rectangleBounds.Bottom + (rectangleBounds.Height / 2));
|
||||
partialConnection = new ConnectionViewModel(_viewModel, sourceDataItem, new PartCreatedConnectorInfo(point.X, point.Y), DrawMode, RouterMode);
|
||||
|
||||
_viewModel.Add(partialConnection);
|
||||
@@ -127,7 +130,7 @@ namespace AIStudio.Wpf.DiagramDesigner
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#region GridCellSize
|
||||
|
||||
public static readonly DependencyProperty GridCellSizeProperty =
|
||||
@@ -350,13 +353,24 @@ namespace AIStudio.Wpf.DiagramDesigner
|
||||
{
|
||||
// in case that this click is the start for a
|
||||
// drag operation we cache the start point
|
||||
rubberbandSelectionStartPoint = e.GetPosition(this);
|
||||
Point currentPoint = e.GetPosition(this);
|
||||
rubberbandSelectionStartPoint = currentPoint;
|
||||
|
||||
if (!(Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl)))
|
||||
{
|
||||
_viewModel.ClearSelectedItems();
|
||||
|
||||
}
|
||||
|
||||
if (_service.DrawModeViewModel.LineDrawModeSelected)//画线模式,可以不命中实体
|
||||
{
|
||||
if (SourceConnector == null)
|
||||
{
|
||||
//新建一个Part连接点
|
||||
SourceConnector = new Connector() { Content = new PartCreatedConnectorInfo(currentPoint.X, currentPoint.Y) };
|
||||
}
|
||||
}
|
||||
|
||||
e.Handled = true;
|
||||
}
|
||||
}
|
||||
@@ -365,8 +379,8 @@ namespace AIStudio.Wpf.DiagramDesigner
|
||||
|
||||
protected override void OnMouseMove(MouseEventArgs e)
|
||||
{
|
||||
var focusedElement = Keyboard.FocusedElement;
|
||||
Debug.WriteLine("focusedElement:" + focusedElement?.ToString());
|
||||
//var focusedElement = Keyboard.FocusedElement;
|
||||
//Debug.WriteLine("focusedElement:" + focusedElement?.ToString());
|
||||
|
||||
base.OnMouseMove(e);
|
||||
|
||||
@@ -440,32 +454,36 @@ namespace AIStudio.Wpf.DiagramDesigner
|
||||
|
||||
if (sourceConnector != null)
|
||||
{
|
||||
FullyCreatedConnectorInfo sourceDataItem = sourceConnector.Info;
|
||||
ConnectorInfoBase sourceDataItem = sourceConnector.Info;
|
||||
if (sinkConnector != null)
|
||||
{
|
||||
ConnectorInfoBase sinkDataItem = sinkConnector.Info;
|
||||
|
||||
FullyCreatedConnectorInfo sinkDataItem = sinkConnector.Info;
|
||||
|
||||
int indexOfLastTempConnection = sinkDataItem.DataItem.Root.Items.Count - 1;
|
||||
sinkDataItem.DataItem.Root.Remove(
|
||||
sinkDataItem.DataItem.Root.Items[indexOfLastTempConnection]);
|
||||
sinkDataItem.DataItem.Root.AddItemCommand.Execute(new ConnectionViewModel(_viewModel, sourceDataItem, sinkDataItem, DrawMode, RouterMode));
|
||||
_viewModel.Remove(partialConnection);
|
||||
_viewModel.AddItemCommand.Execute(new ConnectionViewModel(_viewModel, sourceDataItem, sinkDataItem, DrawMode, RouterMode));
|
||||
}
|
||||
else if (partialConnection.IsFullConnection)//自动连接模式
|
||||
{
|
||||
partialConnection.RaiseFullConnection();
|
||||
}
|
||||
else if (_service.DrawModeViewModel.LineDrawModeSelected)
|
||||
{
|
||||
Point currentPoint = e.GetPosition(this);
|
||||
ConnectorInfoBase sinkDataItem = new PartCreatedConnectorInfo(currentPoint.X, currentPoint.Y);
|
||||
|
||||
_viewModel.Remove(partialConnection);
|
||||
_viewModel.AddItemCommand.Execute(new ConnectionViewModel(_viewModel, sourceDataItem, sinkDataItem, DrawMode, RouterMode));
|
||||
}
|
||||
else
|
||||
{
|
||||
//Need to remove last item as we did not finish drawing the path
|
||||
int indexOfLastTempConnection = sourceDataItem.DataItem.Root.Items.Count - 1;
|
||||
sourceDataItem.DataItem.Root.Remove(
|
||||
sourceDataItem.DataItem.Root.Items[indexOfLastTempConnection]);
|
||||
_viewModel.Remove(partialConnection);
|
||||
}
|
||||
}
|
||||
|
||||
sourceConnector = null;
|
||||
sinkConnector = null;
|
||||
partialConnection = null;
|
||||
|
||||
if (_service.DrawModeViewModel.GetDrawMode() != DrawMode.DirectLine)
|
||||
{
|
||||
@@ -534,7 +552,7 @@ namespace AIStudio.Wpf.DiagramDesigner
|
||||
{
|
||||
if (hitObject is Connector connector)
|
||||
{
|
||||
return connector;
|
||||
return connector;
|
||||
}
|
||||
hitObject = VisualTreeHelper.GetParent(hitObject);
|
||||
}
|
||||
@@ -622,7 +640,7 @@ namespace AIStudio.Wpf.DiagramDesigner
|
||||
foreach (var port in _viewModel.Items.OfType<DesignerItemViewModelBase>().ToList().SelectMany(n => n.Connectors))
|
||||
{
|
||||
if (partialConnection.OnGoingPosition.DistanceTo(port.Position) < SnappingRadius &&
|
||||
partialConnection.SourceConnectorInfo.CanAttachTo(port))
|
||||
partialConnection.SourceConnectorInfoFully?.CanAttachTo(port) == true)
|
||||
return port;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Windows;
|
||||
@@ -44,10 +45,14 @@ namespace AIStudio.Wpf.DiagramDesigner.Controls
|
||||
designerItems.AddRange(designerItem.Root.SelectionService.GetGroupMembers(groupable).OfType<SelectableDesignerItemViewModelBase>());
|
||||
}
|
||||
|
||||
//拖动连线,把连接者也一起移动
|
||||
if (designerItem is ConnectionViewModel connector)
|
||||
{
|
||||
designerItems.Add(connector.SourceConnectorInfo.DataItem);
|
||||
if (connector.IsFullConnection)
|
||||
if (connector.SourceConnectorInfoFully != null)
|
||||
{
|
||||
designerItems.Add(connector.SourceConnectorInfoFully.DataItem);
|
||||
}
|
||||
if (connector.SinkConnectorInfoFully != null)
|
||||
{
|
||||
designerItems.Add(connector.SinkConnectorInfoFully.DataItem);
|
||||
}
|
||||
@@ -66,6 +71,13 @@ namespace AIStudio.Wpf.DiagramDesigner.Controls
|
||||
item.SetOldValue(item.TopLeft, nameof(item.TopLeft));
|
||||
}
|
||||
|
||||
//部分连接点可以移动
|
||||
foreach (ConnectionViewModel item in designerItems.OfType<ConnectionViewModel>())
|
||||
{
|
||||
item.SourceConnectorInfoPart?.SetOldValue(item.SourceConnectorInfoPart.Position, nameof(item.SourceConnectorInfoPart.Position));
|
||||
item.SinkConnectorInfoPart?.SetOldValue(item.SinkConnectorInfoPart.Position, nameof(item.SinkConnectorInfoPart.Position));
|
||||
}
|
||||
|
||||
e.Handled = true;
|
||||
}
|
||||
else
|
||||
@@ -76,10 +88,10 @@ namespace AIStudio.Wpf.DiagramDesigner.Controls
|
||||
|
||||
private void DragThumb_DragCompleted(object sender, DragCompletedEventArgs e)
|
||||
{
|
||||
if (drag == false)
|
||||
if (drag == false)
|
||||
{
|
||||
Interlocked.Decrement(ref DiagramViewModel.DoCommandManager.BeginDo);
|
||||
return;
|
||||
return;
|
||||
}
|
||||
|
||||
if (designerItems != null)
|
||||
@@ -92,6 +104,16 @@ namespace AIStudio.Wpf.DiagramDesigner.Controls
|
||||
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));
|
||||
|
||||
//部分连接点可以移动
|
||||
Dictionary<ConnectionViewModel, Tuple<Tuple<PointBase?, PointBase?>, Tuple<PointBase?, PointBase?>>> conncetorinfos =
|
||||
designerItems.OfType<ConnectionViewModel>().ToDictionary(p => p,
|
||||
p => new Tuple<Tuple<PointBase?, PointBase?>, Tuple<PointBase?, PointBase?>>(
|
||||
new Tuple<PointBase?, PointBase?>(p.SourceConnectorInfoPart?.GetOldValue<PointBase>(nameof(p.SourceConnectorInfoPart.Position)),
|
||||
p.SinkConnectorInfoPart?.GetOldValue<PointBase>(nameof(p.SinkConnectorInfoPart.Position))),
|
||||
new Tuple<PointBase?, PointBase?>(p.SourceConnectorInfoPart?.Position,
|
||||
p.SinkConnectorInfoPart?.Position)));
|
||||
|
||||
Interlocked.Decrement(ref DiagramViewModel.DoCommandManager.BeginDo);
|
||||
DiagramViewModel.DoCommandManager.DoNewCommand(this.ToString(),
|
||||
() => {
|
||||
@@ -99,12 +121,20 @@ namespace AIStudio.Wpf.DiagramDesigner.Controls
|
||||
{
|
||||
info.Key.TopLeft = info.Value.Item2;
|
||||
}
|
||||
foreach (var info in conncetorinfos)
|
||||
{
|
||||
info.Key.SetPartPostion(info.Value.Item2.Item1, info.Value.Item2.Item2);
|
||||
}
|
||||
},
|
||||
() => {
|
||||
foreach (var info in infos)
|
||||
{
|
||||
info.Key.TopLeft = info.Value.Item1;
|
||||
}
|
||||
foreach (var info in conncetorinfos)
|
||||
{
|
||||
info.Key.SetPartPostion(info.Value.Item1.Item1, info.Value.Item1.Item2);
|
||||
}
|
||||
});
|
||||
e.Handled = true;
|
||||
}
|
||||
@@ -121,6 +151,22 @@ namespace AIStudio.Wpf.DiagramDesigner.Controls
|
||||
item.Top += e.VerticalChange;
|
||||
}
|
||||
|
||||
//部分连接点可以移动
|
||||
foreach (ConnectionViewModel item in designerItems.OfType<ConnectionViewModel>())
|
||||
{
|
||||
PointBase? sourcePoint = null;
|
||||
PointBase? sinkPoint = null;
|
||||
if (item.SourceConnectorInfoPart != null)
|
||||
{
|
||||
sourcePoint = new PointBase(item.SourceConnectorInfoPart.Position.X + e.HorizontalChange, item.SourceConnectorInfoPart.Position.Y + e.VerticalChange);
|
||||
}
|
||||
if (item.SinkConnectorInfoPart != null)
|
||||
{
|
||||
sinkPoint = new PointBase(item.SinkConnectorInfoPart.Position.X + e.HorizontalChange, item.SinkConnectorInfoPart.Position.Y + e.VerticalChange);
|
||||
}
|
||||
item.SetPartPostion(sourcePoint, sinkPoint);
|
||||
}
|
||||
|
||||
e.Handled = true;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user