PortBase.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. using System;
  2. using System.Linq;
  3. using System.Windows;
  4. using System.Windows.Controls;
  5. using System.Collections.Generic;
  6. namespace SHJX.Service.WorkFlowEdit
  7. {
  8. public abstract class PortBase : Control, IPort
  9. {
  10. #region Properties
  11. private List<ILink> _links = new List<ILink>();
  12. public ICollection<ILink> Links { get { return _links; } }
  13. public IEnumerable<ILink> IncomingLinks
  14. {
  15. get { return Links.Where(p => p.Target == this); }
  16. }
  17. public IEnumerable<ILink> OutgoingLinks
  18. {
  19. get { return Links.Where(p => p.Source == this); }
  20. }
  21. private Point _center;
  22. public Point Center
  23. {
  24. get { return _center; }
  25. protected set
  26. {
  27. if (_center != value && !double.IsNaN(value.X) && !double.IsNaN(value.Y))
  28. {
  29. _center = value;
  30. foreach (var link in Links)
  31. link.UpdatePath();
  32. }
  33. }
  34. }
  35. #region Sensitivity Property
  36. public double Sensitivity
  37. {
  38. get { return (double)GetValue(SensitivityProperty); }
  39. set { SetValue(SensitivityProperty, value); }
  40. }
  41. public static readonly DependencyProperty SensitivityProperty =
  42. DependencyProperty.Register("Sensitivity",
  43. typeof(double),
  44. typeof(PortBase),
  45. new FrameworkPropertyMetadata(5.0));
  46. #endregion
  47. #region CanAcceptIncomingLinks Property
  48. public bool CanAcceptIncomingLinks
  49. {
  50. get { return (bool)GetValue(CanAcceptIncomingLinksProperty); }
  51. set { SetValue(CanAcceptIncomingLinksProperty, value); }
  52. }
  53. public static readonly DependencyProperty CanAcceptIncomingLinksProperty =
  54. DependencyProperty.Register("CanAcceptIncomingLinks",
  55. typeof(bool),
  56. typeof(PortBase),
  57. new FrameworkPropertyMetadata(true));
  58. #endregion
  59. #region CanAcceptOutgoingLinks Property
  60. public bool CanAcceptOutgoingLinks
  61. {
  62. get { return (bool)GetValue(CanAcceptOutgoingLinksProperty); }
  63. set { SetValue(CanAcceptOutgoingLinksProperty, value); }
  64. }
  65. public static readonly DependencyProperty CanAcceptOutgoingLinksProperty =
  66. DependencyProperty.Register("CanAcceptOutgoingLinks",
  67. typeof(bool),
  68. typeof(PortBase),
  69. new FrameworkPropertyMetadata(true));
  70. #endregion
  71. #region CanCreateLink Property
  72. public bool CanCreateLink
  73. {
  74. get { return (bool)GetValue(CanCreateLinkProperty); }
  75. set { SetValue(CanCreateLinkProperty, value); }
  76. }
  77. public static readonly DependencyProperty CanCreateLinkProperty =
  78. DependencyProperty.Register("CanCreateLink",
  79. typeof(bool),
  80. typeof(PortBase),
  81. new FrameworkPropertyMetadata(false));
  82. #endregion
  83. #endregion
  84. protected PortBase()
  85. {
  86. }
  87. public virtual void UpdatePosition()
  88. {
  89. var canvas = VisualHelper.FindParent<Canvas>(this);
  90. if (canvas != null)
  91. Center = this.TransformToAncestor(canvas).Transform(new Point(this.ActualWidth / 2, this.ActualHeight / 2));
  92. else
  93. Center = new Point(Double.NaN, Double.NaN);
  94. }
  95. /// <summary>
  96. /// Calcluate the intersection point of the port bounds and the line between center and target point
  97. /// </summary>
  98. public abstract Point GetEdgePoint(Point target);
  99. /// <summary>
  100. /// Returns if the specified point is inside port sensivity area
  101. /// </summary>
  102. public abstract bool IsNear(Point point);
  103. protected override void OnPreviewMouseLeftButtonDown(System.Windows.Input.MouseButtonEventArgs e)
  104. {
  105. if (CanCreateLink)
  106. {
  107. var view = VisualHelper.FindParent<DiagramView>(this);
  108. if (view != null)
  109. {
  110. view.LinkTool.BeginDragNewLink(e.GetPosition(view), this);
  111. e.Handled = true;
  112. }
  113. }
  114. else
  115. base.OnMouseLeftButtonDown(e);
  116. }
  117. }
  118. }