|
| 1 | +using System.Windows.Input; |
| 2 | +using System.Windows; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Windows.Controls.Primitives; |
| 5 | +using System.Diagnostics; |
| 6 | + |
| 7 | +namespace Nodify |
| 8 | +{ |
| 9 | + public partial class NodifyEditor |
| 10 | + { |
| 11 | + public static readonly DependencyProperty ItemsDragStartedCommandProperty = DependencyProperty.Register(nameof(ItemsDragStartedCommand), typeof(ICommand), typeof(NodifyEditor)); |
| 12 | + public static readonly DependencyProperty ItemsDragCompletedCommandProperty = DependencyProperty.Register(nameof(ItemsDragCompletedCommand), typeof(ICommand), typeof(NodifyEditor)); |
| 13 | + |
| 14 | + protected static readonly DependencyPropertyKey IsDraggingPropertyKey = DependencyProperty.RegisterReadOnly(nameof(IsDragging), typeof(bool), typeof(NodifyEditor), new FrameworkPropertyMetadata(BoxValue.False, OnIsDraggingChanged)); |
| 15 | + public static readonly DependencyProperty IsDraggingProperty = IsDraggingPropertyKey.DependencyProperty; |
| 16 | + |
| 17 | + private static void OnIsDraggingChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) |
| 18 | + { |
| 19 | + var editor = (NodifyEditor)d; |
| 20 | + |
| 21 | + if ((bool)e.NewValue == true) |
| 22 | + { |
| 23 | + editor.OnItemsDragStarted(); |
| 24 | + } |
| 25 | + else |
| 26 | + { |
| 27 | + editor.OnItemsDragCompleted(); |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + private void OnItemsDragCompleted() |
| 32 | + { |
| 33 | + if (ItemsDragCompletedCommand?.CanExecute(DataContext) ?? false) |
| 34 | + ItemsDragCompletedCommand.Execute(DataContext); |
| 35 | + } |
| 36 | + |
| 37 | + private void OnItemsDragStarted() |
| 38 | + { |
| 39 | + if (ItemsDragStartedCommand?.CanExecute(DataContext) ?? false) |
| 40 | + ItemsDragStartedCommand.Execute(DataContext); |
| 41 | + } |
| 42 | + |
| 43 | + /// <summary> |
| 44 | + /// Invoked when a drag operation starts for the <see cref="SelectedItems"/>, or when <see cref="IsPushingItems"/> is set to true. |
| 45 | + /// </summary> |
| 46 | + public ICommand? ItemsDragStartedCommand |
| 47 | + { |
| 48 | + get => (ICommand?)GetValue(ItemsDragStartedCommandProperty); |
| 49 | + set => SetValue(ItemsDragStartedCommandProperty, value); |
| 50 | + } |
| 51 | + |
| 52 | + /// <summary> |
| 53 | + /// Invoked when a drag operation is completed for the <see cref="SelectedItems"/>, or when <see cref="IsPushingItems"/> is set to false. |
| 54 | + /// </summary> |
| 55 | + public ICommand? ItemsDragCompletedCommand |
| 56 | + { |
| 57 | + get => (ICommand?)GetValue(ItemsDragCompletedCommandProperty); |
| 58 | + set => SetValue(ItemsDragCompletedCommandProperty, value); |
| 59 | + } |
| 60 | + |
| 61 | + /// <summary> |
| 62 | + /// Gets a value that indicates whether a dragging operation is in progress. |
| 63 | + /// </summary> |
| 64 | + public bool IsDragging |
| 65 | + { |
| 66 | + get => (bool)GetValue(IsDraggingProperty); |
| 67 | + private set => SetValue(IsDraggingPropertyKey, value); |
| 68 | + } |
| 69 | + |
| 70 | + /// <summary> |
| 71 | + /// Gets or sets if the current position of containers that are being dragged should not be committed until the end of the dragging operation. |
| 72 | + /// </summary> |
| 73 | + public static bool EnableDraggingContainersOptimizations { get; set; } = true; |
| 74 | + |
| 75 | + private IDraggingStrategy? _draggingStrategy; |
| 76 | + |
| 77 | + private void RegisterDragEvents() |
| 78 | + { |
| 79 | + AddHandler(ItemContainer.DragStartedEvent, new DragStartedEventHandler(OnItemsDragStarted)); |
| 80 | + AddHandler(ItemContainer.DragCompletedEvent, new DragCompletedEventHandler(OnItemsDragCompleted)); |
| 81 | + AddHandler(ItemContainer.DragDeltaEvent, new DragDeltaEventHandler(OnItemsDragDelta)); |
| 82 | + } |
| 83 | + |
| 84 | + /// <summary> |
| 85 | + /// Initiates the dragging operation using the currently selected <see cref="ItemContainer" />s. |
| 86 | + /// </summary> |
| 87 | + /// <remarks>This method has no effect if a dragging operation is already in progress.</remarks> |
| 88 | + public void BeginDragging() |
| 89 | + => BeginDragging(SelectedContainers); |
| 90 | + |
| 91 | + /// <summary> |
| 92 | + /// Initiates the dragging operation for the specified <see cref="ItemContainer" />s. |
| 93 | + /// </summary> |
| 94 | + /// <param name="containers">The collection of item containers to be dragged.</param> |
| 95 | + /// <remarks>This method has no effect if a dragging operation is already in progress.</remarks> |
| 96 | + public void BeginDragging(IEnumerable<ItemContainer> containers) |
| 97 | + { |
| 98 | + if(IsDragging) |
| 99 | + { |
| 100 | + return; |
| 101 | + } |
| 102 | + |
| 103 | + IsDragging = true; |
| 104 | + _draggingStrategy = CreateDraggingStrategy(containers); |
| 105 | + } |
| 106 | + |
| 107 | + /// <summary> |
| 108 | + /// Updates the position of the items being dragged by a specified offset. |
| 109 | + /// </summary> |
| 110 | + /// <param name="amount">The vector by which to adjust the position of the dragged items.</param> |
| 111 | + /// This method adjusts the items positions incrementally. It should only be called while a dragging operation is in progress (see <see cref="BeginDragging"/>). |
| 112 | + /// </remarks> |
| 113 | + public void UpdateDragging(Vector amount) |
| 114 | + { |
| 115 | + Debug.Assert(IsDragging); |
| 116 | + _draggingStrategy!.Update(amount); |
| 117 | + } |
| 118 | + |
| 119 | + /// <summary> |
| 120 | + /// Completes the dragging operation, finalizing the position of the dragged items. |
| 121 | + /// </summary> |
| 122 | + /// <remarks>This method has no effect if there's no dragging operation in progress.</remarks> |
| 123 | + public void EndDragging() |
| 124 | + { |
| 125 | + if (!IsDragging) |
| 126 | + { |
| 127 | + return; |
| 128 | + } |
| 129 | + |
| 130 | + IsBulkUpdatingItems = true; |
| 131 | + _draggingStrategy!.End(); |
| 132 | + IsBulkUpdatingItems = false; |
| 133 | + |
| 134 | + // Draw the containers at the new position. |
| 135 | + ItemsHost.InvalidateArrange(); |
| 136 | + |
| 137 | + _draggingStrategy = null; |
| 138 | + IsDragging = false; |
| 139 | + } |
| 140 | + |
| 141 | + /// <summary> |
| 142 | + /// Cancels the ongoing dragging operation, reverting any changes made to the positions of the dragged items. |
| 143 | + /// </summary> |
| 144 | + /// <remarks>This method has no effect if there's no dragging operation in progress.</remarks> |
| 145 | + public void CancelDragging() |
| 146 | + { |
| 147 | + if (!ItemContainer.AllowDraggingCancellation || !IsDragging) |
| 148 | + { |
| 149 | + return; |
| 150 | + } |
| 151 | + |
| 152 | + _draggingStrategy!.Abort(); |
| 153 | + IsDragging = false; |
| 154 | + } |
| 155 | + |
| 156 | + private IDraggingStrategy CreateDraggingStrategy(IEnumerable<ItemContainer> containers) |
| 157 | + { |
| 158 | + if (EnableDraggingContainersOptimizations) |
| 159 | + { |
| 160 | + return new DraggingOptimized(containers, GridCellSize); |
| 161 | + } |
| 162 | + |
| 163 | + return new DraggingSimple(containers, GridCellSize); |
| 164 | + } |
| 165 | + |
| 166 | + private void OnItemsDragStarted(object sender, DragStartedEventArgs e) |
| 167 | + { |
| 168 | + BeginDragging(); |
| 169 | + e.Handled = true; |
| 170 | + } |
| 171 | + |
| 172 | + private void OnItemsDragDelta(object sender, DragDeltaEventArgs e) |
| 173 | + { |
| 174 | + UpdateDragging(new Vector(e.HorizontalChange, e.VerticalChange)); |
| 175 | + } |
| 176 | + |
| 177 | + private void OnItemsDragCompleted(object sender, DragCompletedEventArgs e) |
| 178 | + { |
| 179 | + if (e.Canceled) |
| 180 | + { |
| 181 | + CancelDragging(); |
| 182 | + } |
| 183 | + else |
| 184 | + { |
| 185 | + EndDragging(); |
| 186 | + } |
| 187 | + } |
| 188 | + } |
| 189 | +} |
0 commit comments