InputTool.cs 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. using System.Windows;
  2. using System.Windows.Input;
  3. using System.Windows.Documents;
  4. namespace SHJX.Service.WorkFlowEdit
  5. {
  6. public class InputTool : IInputTool
  7. {
  8. protected DiagramView View { get; private set; }
  9. protected IDiagramController Controller { get { return View.Controller; } }
  10. protected Point? MouseDownPoint { get; set; }
  11. protected DiagramItem MouseDownItem { get; set; }
  12. public InputTool(DiagramView view)
  13. {
  14. View = view;
  15. }
  16. public virtual void OnMouseDown(MouseButtonEventArgs e)
  17. {
  18. MouseDownItem = (e.OriginalSource as DependencyObject).FindParent<DiagramItem>();
  19. MouseDownPoint = e.GetPosition(View);
  20. e.Handled = true;
  21. }
  22. public virtual void OnMouseMove(MouseEventArgs e)
  23. {
  24. if (e.LeftButton == MouseButtonState.Pressed && MouseDownPoint.HasValue)
  25. {
  26. if (MouseDownItem != null)
  27. {
  28. View.DragTool.BeginDrag(MouseDownPoint.Value, MouseDownItem, DragThumbKinds.Center);
  29. }
  30. else
  31. {
  32. View.Selection.Clear();
  33. View.DragAdorner = CreateRubberbandAdorner();
  34. }
  35. MouseDownItem = null;
  36. MouseDownPoint = null;
  37. }
  38. e.Handled = true;
  39. }
  40. public virtual void OnMouseUp(MouseButtonEventArgs e)
  41. {
  42. if (MouseDownPoint == null)
  43. return;
  44. var item = (e.Source as DependencyObject).FindParent<DiagramItem>();
  45. SelectItem(item);
  46. e.Handled = true;
  47. }
  48. public virtual void OnPreviewKeyDown(KeyEventArgs e)
  49. {
  50. if (e.Key == Key.Escape)
  51. {
  52. e.Handled = true;
  53. if (View.DragAdorner != null && View.DragAdorner.IsMouseCaptured)
  54. View.DragAdorner.ReleaseMouseCapture();
  55. else
  56. View.Selection.Clear();
  57. }
  58. }
  59. protected virtual void SelectItem(DiagramItem item)
  60. {
  61. var sel = View.Selection;
  62. if (Keyboard.Modifiers == ModifierKeys.Control)
  63. {
  64. if (item != null && item.CanSelect)
  65. {
  66. if (item.IsSelected)
  67. sel.Remove(item);
  68. else
  69. sel.Add(item);
  70. }
  71. }
  72. else if (Keyboard.Modifiers == ModifierKeys.Shift)
  73. {
  74. if (item != null && item.CanSelect)
  75. sel.Add(item);
  76. }
  77. else
  78. {
  79. if (item != null && item.CanSelect)
  80. sel.Set(item);
  81. else
  82. sel.Clear();
  83. }
  84. }
  85. protected virtual Adorner CreateRubberbandAdorner()
  86. {
  87. return new RubberbandAdorner(View, MouseDownPoint.Value);
  88. }
  89. }
  90. }