mirror of
https://gitee.com/akwkevin/aistudio.-wpf.-diagram
synced 2026-03-03 00:00:57 +08:00
block 可以拖拽到内部,还有少量问题待解决
This commit is contained in:
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user