Files
aistudio-wpf-diagram/AIStudio.Wpf.DiagramDesigner/Controls/DragThumb.cs

186 lines
7.7 KiB
C#
Raw Normal View History

2021-07-23 09:42:22 +08:00
using System;
using System.Collections.Generic;
2023-05-03 09:59:46 +08:00
using System.Diagnostics;
2021-07-23 09:42:22 +08:00
using System.Linq;
2023-04-08 21:48:43 +08:00
using System.Threading;
2021-07-23 09:42:22 +08:00
using System.Windows;
using System.Windows.Controls.Primitives;
using System.Windows.Media;
2023-03-26 23:23:34 +08:00
using AIStudio.Wpf.DiagramDesigner.Geometrys;
2021-07-23 09:42:22 +08:00
2022-10-28 22:45:39 +08:00
namespace AIStudio.Wpf.DiagramDesigner.Controls
2021-07-23 09:42:22 +08:00
{
public class DragThumb : Thumb
{
public DragThumb()
{
base.DragDelta += new DragDeltaEventHandler(DragThumb_DragDelta);
base.DragStarted += DragThumb_DragStarted;
base.DragCompleted += DragThumb_DragCompleted;
}
2023-03-26 23:23:34 +08:00
public IDiagramViewModel DiagramViewModel
{
get;set;
2023-03-26 23:23:34 +08:00
}
2023-01-24 09:02:40 +08:00
private List<SelectableDesignerItemViewModelBase> designerItems;
2021-07-23 09:42:22 +08:00
2023-02-12 11:02:20 +08:00
private bool drag;
2021-07-23 09:42:22 +08:00
private void DragThumb_DragStarted(object sender, DragStartedEventArgs e)
{
2023-03-28 23:16:56 +08:00
GetDesignerCanvas(this)?.Focus();
2023-03-12 15:26:58 +08:00
2023-02-12 11:02:20 +08:00
drag = false;
2023-01-24 09:02:40 +08:00
SelectableDesignerItemViewModelBase designerItem = this.DataContext as SelectableDesignerItemViewModelBase;
DiagramViewModel = designerItem?.Root;
2021-07-23 09:42:22 +08:00
if (designerItem != null && designerItem.IsSelected)
{
// we only move DesignerItems
2023-01-24 16:20:39 +08:00
designerItems = designerItem.Root.SelectedItems.ToList();
2023-03-07 22:59:27 +08:00
if (designerItem is IGroupable groupable)
{
designerItems.AddRange(designerItem.Root.SelectionService.GetGroupMembers(groupable).OfType<SelectableDesignerItemViewModelBase>());
}
2023-05-03 09:59:46 +08:00
//拖动连线,把连接者也一起移动
2023-01-24 17:53:04 +08:00
if (designerItem is ConnectionViewModel connector)
2021-07-23 09:42:22 +08:00
{
2023-05-03 09:59:46 +08:00
if (connector.SourceConnectorInfoFully != null)
{
designerItems.Add(connector.SourceConnectorInfoFully.DataItem);
}
if (connector.SinkConnectorInfoFully != null)
2022-12-04 23:07:20 +08:00
{
2023-01-08 09:22:37 +08:00
designerItems.Add(connector.SinkConnectorInfoFully.DataItem);
2022-12-04 23:07:20 +08:00
}
2021-07-23 09:42:22 +08:00
}
2023-03-07 22:59:27 +08:00
designerItems = designerItems.Distinct().ToList();
2023-04-08 21:48:43 +08:00
Interlocked.Increment(ref DiagramViewModel.DoCommandManager.BeginDo);
2021-07-23 09:42:22 +08:00
foreach (DesignerItemViewModelBase item in designerItems.OfType<DesignerItemViewModelBase>())
2023-03-28 23:16:56 +08:00
{
2023-03-26 23:23:34 +08:00
item.SetOldValue(item.TopLeft, nameof(item.TopLeft));
2021-07-23 09:42:22 +08:00
}
2023-05-03 09:59:46 +08:00
//部分连接点可以移动
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));
}
2021-07-23 09:42:22 +08:00
e.Handled = true;
}
else
{
designerItems = null;
}
}
2023-02-12 11:02:20 +08:00
2021-07-23 09:42:22 +08:00
private void DragThumb_DragCompleted(object sender, DragCompletedEventArgs e)
{
2023-05-03 09:59:46 +08:00
if (drag == false)
2023-04-08 21:48:43 +08:00
{
Interlocked.Decrement(ref DiagramViewModel.DoCommandManager.BeginDo);
2023-05-03 09:59:46 +08:00
return;
2023-03-28 23:16:56 +08:00
}
2023-02-12 11:02:20 +08:00
2021-07-23 09:42:22 +08:00
if (designerItems != null)
{
foreach (DesignerItemViewModelBase item in designerItems.OfType<DesignerItemViewModelBase>())
2023-02-12 11:02:20 +08:00
{
2021-07-23 09:42:22 +08:00
item.SetCellAlignment();
2023-02-12 11:02:20 +08:00
}
2021-07-23 09:42:22 +08:00
2023-06-26 22:50:08 +08:00
Interlocked.Decrement(ref DiagramViewModel.DoCommandManager.BeginDo);
var blocks = designerItems.OfType<BlockDesignerItemViewModel>().ToList();
if (blocks.Any())
{
2023-06-20 13:46:26 +08:00
(DiagramViewModel as IBlockDiagramViewModel)?.FinishNearBlock(blocks);
}
2023-06-26 22:50:08 +08:00
2023-03-28 23:16:56 +08:00
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));
2023-05-03 09:59:46 +08:00
//部分连接点可以移动
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)));
2023-03-26 23:23:34 +08:00
DiagramViewModel.DoCommandManager.DoNewCommand(this.ToString(),
() => {
foreach (var info in infos)
{
info.Key.TopLeft = info.Value.Item2;
}
2023-05-03 09:59:46 +08:00
foreach (var info in conncetorinfos)
{
info.Key.SetPartPostion(info.Value.Item2.Item1, info.Value.Item2.Item2);
}
2023-03-26 23:23:34 +08:00
},
() => {
foreach (var info in infos)
{
info.Key.TopLeft = info.Value.Item1;
}
2023-05-03 09:59:46 +08:00
foreach (var info in conncetorinfos)
{
info.Key.SetPartPostion(info.Value.Item1.Item1, info.Value.Item1.Item2);
}
2023-03-26 23:23:34 +08:00
});
2021-07-23 09:42:22 +08:00
e.Handled = true;
}
}
void DragThumb_DragDelta(object sender, DragDeltaEventArgs e)
{
2023-02-12 11:02:20 +08:00
drag = true;
2021-07-23 09:42:22 +08:00
if (designerItems != null)
{
foreach (DesignerItemViewModelBase item in designerItems.OfType<DesignerItemViewModelBase>())
{
2023-03-28 23:16:56 +08:00
item.Left += e.HorizontalChange;
item.Top += e.VerticalChange;
2021-07-23 09:42:22 +08:00
}
2023-05-03 09:59:46 +08:00
//部分连接点可以移动
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);
}
var blocks = designerItems.OfType<BlockDesignerItemViewModel>().ToList();
2023-06-20 13:46:26 +08:00
(DiagramViewModel as IBlockDiagramViewModel)?.PreviewNearBlock(blocks);
2021-07-23 09:42:22 +08:00
e.Handled = true;
}
}
private DesignerCanvas GetDesignerCanvas(DependencyObject element)
{
while (element != null && !(element is DesignerCanvas))
element = VisualTreeHelper.GetParent(element);
return element as DesignerCanvas;
}
}
}