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

121 lines
4.1 KiB
C#
Raw Normal View History

2021-07-23 09:42:22 +08:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls.Primitives;
using System.Windows.Media;
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-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-12 15:26:58 +08:00
GetDesignerCanvas(this)?.Focus();
2023-02-12 11:02:20 +08:00
drag = false;
2023-01-24 09:02:40 +08:00
SelectableDesignerItemViewModelBase designerItem = this.DataContext as SelectableDesignerItemViewModelBase;
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-01-24 17:53:04 +08:00
if (designerItem is ConnectionViewModel connector)
2021-07-23 09:42:22 +08:00
{
designerItems.Add(connector.SourceConnectorInfo.DataItem);
2023-01-08 09:22:37 +08:00
if (connector.IsFullConnection)
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
}
if (designerItem is PointDesignerItemViewModel)
{
2023-01-24 09:02:40 +08:00
designerItems = new List<SelectableDesignerItemViewModelBase> { designerItem };
2021-07-23 09:42:22 +08:00
}
2023-03-07 22:59:27 +08:00
designerItems = designerItems.Distinct().ToList();
2021-07-23 09:42:22 +08:00
foreach (DesignerItemViewModelBase item in designerItems.OfType<DesignerItemViewModelBase>())
{
item.BeginDo = true;
item.SetOldValue(item.Left, nameof(item.Left));
item.SetOldValue(item.Top, nameof(item.Top));
}
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-02-12 11:02:20 +08:00
if (drag == false) return;
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();
item.BeginDo = false;
item.RaiseTopLeft();
2023-02-12 11:02:20 +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)
{
double minLeft = double.MaxValue;
2023-02-12 11:02:20 +08:00
double minTop = double.MaxValue;
2021-07-23 09:42:22 +08:00
foreach (DesignerItemViewModelBase item in designerItems.OfType<DesignerItemViewModelBase>())
{
double left = item.Left;
double top = item.Top;
minLeft = double.IsNaN(left) ? 0 : Math.Min(left, minLeft);
minTop = double.IsNaN(top) ? 0 : Math.Min(top, minTop);
double deltaHorizontal = Math.Max(-minLeft, e.HorizontalChange);
double deltaVertical = Math.Max(-minTop, e.VerticalChange);
item.Left += deltaHorizontal;
item.Top += deltaVertical;
}
e.Handled = true;
}
}
private DesignerCanvas GetDesignerCanvas(DependencyObject element)
{
while (element != null && !(element is DesignerCanvas))
element = VisualTreeHelper.GetParent(element);
return element as DesignerCanvas;
}
}
}