TestArrangementOfElements.axaml.cs 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. using Avalonia;
  2. using Avalonia.Controls;
  3. using Avalonia.Input;
  4. using Avalonia.Interactivity;
  5. using Avalonia.Markup.Xaml;
  6. using Avalonia.Media;
  7. using EntranseTesting.Models;
  8. using EntranseTesting.ViewModels;
  9. using System.Diagnostics;
  10. namespace EntranseTesting;
  11. public partial class TestArrangementOfElements : UserControl
  12. {
  13. private Point _ghostPosition = new(0, 0);
  14. private readonly Point _mouseOffset = new(-5, -5);
  15. private Point _startItemPosition = new(0, 0);
  16. private Point _endItemPosition = new(0, 0);
  17. public TestArrangementOfElements()
  18. {
  19. InitializeComponent();
  20. AddHandler(DragDrop.DragOverEvent, DragOver);
  21. AddHandler(DragDrop.DropEvent, Drop);
  22. }
  23. protected override void OnLoaded(RoutedEventArgs e)
  24. {
  25. GhostItem.IsVisible = false;
  26. base.OnLoaded(e);
  27. }
  28. private async void OnPointerPressed(object? sender, PointerPressedEventArgs e)
  29. {
  30. Debug.WriteLine("DoDrag start");
  31. if (sender is not Border border) return;
  32. if (border.DataContext is not ElementOfArrangement elem) return;
  33. var ghostPos = GhostItem.Bounds.Position;
  34. _ghostPosition = new Point(ghostPos.X + _mouseOffset.X, ghostPos.Y + _mouseOffset.Y);
  35. var mousePos = e.GetPosition(MainContainer);
  36. var offsetX = mousePos.X - ghostPos.X;
  37. var offsetY = mousePos.Y - ghostPos.Y + _mouseOffset.X;
  38. GhostItem.RenderTransform = new TranslateTransform(offsetX, offsetY);
  39. _startItemPosition = new Point(offsetX, offsetY);
  40. elem.Width = border.Bounds.Width;
  41. elem.Height = border.Bounds.Height;
  42. if (DataContext is not MainWindowViewModel vm) return;
  43. vm.TestMain.TestPages.TestAE.StartDrag(elem);
  44. GhostItem.IsVisible = true;
  45. var dragData = new DataObject();
  46. dragData.Set(TestArrangementOfElementsViewModel.CustomFormat, elem);
  47. var result = await DragDrop.DoDragDrop(e, dragData, DragDropEffects.Move);
  48. Debug.WriteLine($"DragAndDrop result: {result}");
  49. GhostItem.IsVisible = false;
  50. }
  51. private void DragOver(object? sender, DragEventArgs e)
  52. {
  53. var currentPosition = e.GetPosition(MainContainer);
  54. var offsetX = currentPosition.X - _ghostPosition.X;
  55. var offsetY = currentPosition.Y - _ghostPosition.Y;
  56. GhostItem.RenderTransform = new TranslateTransform(offsetX, offsetY);
  57. _endItemPosition = new Point(offsetX, offsetY);
  58. e.DragEffects = DragDropEffects.Move;
  59. if (DataContext is not MainWindowViewModel vm) return;
  60. var data = e.Data.Get(TestArrangementOfElementsViewModel.CustomFormat);
  61. if (data is not ElementOfArrangement elem) return;
  62. }
  63. private void Drop(object? sender, DragEventArgs e)
  64. {
  65. Debug.WriteLine("Drop");
  66. var data = e.Data.Get(TestArrangementOfElementsViewModel.CustomFormat);
  67. if (data is not ElementOfArrangement elem)
  68. {
  69. Debug.WriteLine("No task item");
  70. return;
  71. }
  72. if (DataContext is not MainWindowViewModel vm) return;
  73. vm.TestMain.TestPages.TestAE.Drop(elem, _startItemPosition, _endItemPosition);
  74. }
  75. }