using Serein.Library; using Serein.Library.Utils; using Serein.Workbench.Models; using Serein.Workbench.Services; using System; using System.Collections; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace Serein.Workbench.Customs { /// /// 拖拽创建节点类型 /// public static class MouseNodeType { /// /// 创建来自DLL的节点 /// public static string CreateDllNodeInCanvas { get; } = nameof(CreateDllNodeInCanvas); /// /// 创建基础节点 /// public static string CreateBaseNodeInCanvas { get; } = nameof(CreateBaseNodeInCanvas); } /// /// FlowMethodInfoListBox.xaml 的交互逻辑 /// public partial class FlowMethodInfoListBox : UserControl, System.ComponentModel.INotifyPropertyChanged { private object viewMethodInfo; public object ViewMethodInfo { get => viewMethodInfo; set { if (viewMethodInfo != value) { viewMethodInfo = value; PropertyChanged?.Invoke(this, new System.ComponentModel.PropertyChangedEventArgs(nameof(ViewMethodInfo))); } } } public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged; public FlowMethodInfoListBox() { InitializeComponent(); } public static readonly DependencyProperty ItemsSourceProperty = DependencyProperty.Register(nameof(ItemsSource), typeof(IEnumerable), typeof(FlowMethodInfoListBox), new PropertyMetadata(null)); public IEnumerable ItemsSource { get => (IEnumerable)GetValue(ItemsSourceProperty); set => SetValue(ItemsSourceProperty, value); } public static readonly DependencyProperty BackgroundProperty = DependencyProperty.Register(nameof(Background), typeof(Brush), typeof(FlowMethodInfoListBox), new PropertyMetadata(Brushes.Transparent)); public Brush Background { get => (Brush)GetValue(BackgroundProperty); set => SetValue(BackgroundProperty, value); } /// /// 存储拖拽开始时的鼠标位置 /// private Point _dragStartPoint; private void Grid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { // 记录鼠标按下时的位置 _dragStartPoint = e.GetPosition(null); } private void Grid_MouseMove(object sender, MouseEventArgs e) { // 获取当前鼠标位置 Point mousePos = e.GetPosition(null); // 计算鼠标移动的距离 Vector diff = _dragStartPoint - mousePos; // 判断是否符合拖拽的最小距离要求 if (e.LeftButton == MouseButtonState.Pressed && (Math.Abs(diff.X) > SystemParameters.MinimumHorizontalDragDistance || Math.Abs(diff.Y) > SystemParameters.MinimumVerticalDragDistance)) { // 获取触发事件的 TextBlock if (sender is Grid grid && grid.DataContext is MethodDetailsInfo mdInfo) { if (!EnumHelper.TryConvertEnum(mdInfo.NodeType, out var nodeType)) { return; } MoveNodeModel moveNodeModel = new MoveNodeModel() { NodeControlType = nodeType switch { NodeType.Action => NodeControlType.Action, NodeType.Flipflop => NodeControlType.Flipflop, NodeType.UI => NodeControlType.UI, _ => NodeControlType.None, }, MethodDetailsInfo = mdInfo }; //MoveNodeData moveNodeData = new MoveNodeData //{ // MethodDetailsInfo = mdInfo, //}; if (moveNodeModel.NodeControlType == NodeControlType.None) { return; } // 创建一个 DataObject 用于拖拽操作,并设置拖拽效果 DataObject dragData = new DataObject(MouseNodeType.CreateDllNodeInCanvas, moveNodeModel); DragDrop.DoDragDrop(grid, dragData, DragDropEffects.Move); } } } private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e) { if(sender is ListBox listBox) { if (listBox.SelectedIndex != -1) { var item = listBox.SelectedItem; if (item is MethodDetailsInfo mdInfo) { App.GetService().CurrentMethodDetailsInfo = mdInfo; } } // Serein.Workbench.Models.FlowLibraryInfo } } } }