MoveResizeTool.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. using System;
  2. using System.Linq;
  3. using System.Windows;
  4. using System.Windows.Input;
  5. using System.Windows.Documents;
  6. using System.Windows.Controls;
  7. namespace SHJX.Service.WorkFlowEdit
  8. {
  9. public class MoveResizeTool : IMoveResizeTool
  10. {
  11. public DragThumbKinds DragKind { get; protected set; }
  12. public Rect[] InitialBounds { get; protected set; }
  13. public DiagramItem[] DragItems { get; protected set; }
  14. //public bool SnapToGrid { get; set; }
  15. public Size MoveGridCell { get; set; }
  16. public Size ResizeGridCell { get; set; }
  17. protected DiagramView View { get; private set; }
  18. protected IDiagramController Controller { get { return View.Controller; } }
  19. protected Point Start { get; set; }
  20. public MoveResizeTool(DiagramView view)
  21. {
  22. View = view;
  23. }
  24. public virtual void BeginDrag(Point start, DiagramItem item, DragThumbKinds kind)
  25. {
  26. Start = start;
  27. DragKind = kind;
  28. if (kind == DragThumbKinds.Center)
  29. {
  30. if (!item.CanMove || !IsMovable(item))
  31. return;
  32. if (!View.Selection.Contains(item))
  33. View.Selection.Set(item);
  34. DragItems = View.Selection.Where(p => p.CanMove && IsMovable(p)).ToArray();
  35. }
  36. else
  37. {
  38. DragItems = new DiagramItem[] { item };
  39. }
  40. InitialBounds = DragItems.Select(p => p.Bounds).ToArray();
  41. View.DragAdorner = CreateAdorner();
  42. }
  43. protected bool IsMovable(DiagramItem item)
  44. {
  45. return !(item is LinkBase);
  46. }
  47. public virtual void DragTo(Vector vector)
  48. {
  49. vector = UpdateVector(vector);
  50. for (int i = 0; i < DragItems.Length; i++)
  51. {
  52. var item = DragItems[i];
  53. var rect = InitialBounds[i];
  54. if (DragKind == DragThumbKinds.Center)
  55. {
  56. Canvas.SetLeft(item, rect.X + vector.X);
  57. Canvas.SetTop(item, rect.Y + vector.Y);
  58. }
  59. else
  60. {
  61. if ((DragKind & DragThumbKinds.Left) != DragThumbKinds.None)
  62. {
  63. item.Width = Math.Max(item.MinWidth, rect.Width - vector.X);
  64. Canvas.SetLeft(item, Math.Min(rect.X + vector.X, rect.Right - item.MinWidth));
  65. }
  66. if ((DragKind & DragThumbKinds.Top) != DragThumbKinds.None)
  67. {
  68. item.Height = Math.Max(item.MinHeight, rect.Height - vector.Y);
  69. Canvas.SetTop(item, Math.Min(rect.Y + vector.Y, rect.Bottom - item.MinHeight));
  70. }
  71. if ((DragKind & DragThumbKinds.Right) != DragThumbKinds.None)
  72. {
  73. item.Width = Math.Max(0, rect.Width + vector.X);
  74. }
  75. if ((DragKind & DragThumbKinds.Bottom) != DragThumbKinds.None)
  76. {
  77. item.Height = Math.Max(0, rect.Height + vector.Y);
  78. }
  79. }
  80. }
  81. }
  82. public virtual bool CanDrop()
  83. {
  84. return true;
  85. }
  86. public virtual void EndDrag(bool doCommit)
  87. {
  88. if (doCommit)
  89. {
  90. var bounds = DragItems.Select(p => p.Bounds).ToArray();
  91. Controller.UpdateItemsBounds(DragItems, bounds);
  92. }
  93. else
  94. {
  95. RestoreBounds();
  96. }
  97. DragItems = null;
  98. InitialBounds = null;
  99. }
  100. protected virtual Adorner CreateAdorner()
  101. {
  102. return new MoveResizeAdorner(View, Start) { Cursor = GetCursor() };
  103. }
  104. protected Cursor GetCursor()
  105. {
  106. switch (DragKind)
  107. {
  108. case DragThumbKinds.Center:
  109. return Cursors.SizeAll;
  110. case DragThumbKinds.Bottom:
  111. case DragThumbKinds.Top:
  112. return Cursors.SizeNS;
  113. case DragThumbKinds.Left:
  114. case DragThumbKinds.Right:
  115. return Cursors.SizeWE;
  116. case DragThumbKinds.TopLeft:
  117. case DragThumbKinds.BottomRight:
  118. return Cursors.SizeNWSE;
  119. case DragThumbKinds.TopRight:
  120. case DragThumbKinds.BottomLeft:
  121. return Cursors.SizeNESW;
  122. }
  123. return null;
  124. }
  125. protected virtual Vector UpdateVector(Vector vector)
  126. {
  127. Size cell;
  128. if (DragKind == DragThumbKinds.Center)
  129. cell = MoveGridCell;
  130. else
  131. cell = ResizeGridCell;
  132. if (cell.Width > 0 && cell.Height > 0)
  133. {
  134. var x = Math.Round(vector.X / cell.Width) * cell.Width;
  135. var y = Math.Round(vector.Y / cell.Height) * cell.Height;
  136. return new Vector(x, y);
  137. }
  138. else
  139. return vector;
  140. }
  141. protected virtual void RestoreBounds()
  142. {
  143. for (int i = 0; i < DragItems.Length; i++)
  144. {
  145. var item = DragItems[i];
  146. var rect = InitialBounds[i];
  147. Canvas.SetLeft(item, rect.X);
  148. Canvas.SetTop(item, rect.Y);
  149. item.Width = rect.Width;
  150. item.Height = rect.Height;
  151. }
  152. }
  153. }
  154. }