Advertisement
Krythic

BlueprintConnection

Nov 8th, 2020
2,249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.87 KB | None | 0 0
  1. using System.Windows;
  2. using System.Windows.Controls;
  3. using System.Windows.Media;
  4. using System.Windows.Shapes;
  5.  
  6. namespace VoidwalkerEngine.Editor.Controls.Blueprint
  7. {
  8.     public class BlueprintConnection
  9.     {
  10.         public UserControl Parent { get; set; }
  11.         public FrameworkElement Origin { get; set; }
  12.         public FrameworkElement Destination { get; set; }
  13.         public double OriginControlTension { get; set; } = 1.25;
  14.         public double DestinationControlTension { get; set; } = 1.25;
  15.         public Path Connection { get; set; }
  16.         private bool _isConnectionCreated;
  17.  
  18.  
  19.         public BlueprintConnection()
  20.         {
  21.         }
  22.  
  23.         public void Rebuild()
  24.         {
  25.             if (!_isConnectionCreated)
  26.             {
  27.                 CreateConnection();
  28.                 return;
  29.             }
  30.             CalculateConnectionPoints(out Point start, out Point end);
  31.             this.Connection.Width = this.Parent.Width;
  32.             this.Connection.Height = this.Parent.Height;
  33.             PathGeometry pathGeometry = (PathGeometry)this.Connection.Data;
  34.             PathFigure figure = pathGeometry.Figures[0];
  35.             figure.StartPoint = start;
  36.             BezierSegment segment = (BezierSegment)figure.Segments[0];
  37.             CalculateControlPoints(start,end,out Point controlPoint1, out Point controlPoint2);
  38.             segment.Point1 = controlPoint1;
  39.             segment.Point2 = controlPoint2;
  40.             segment.Point3 = end;
  41.         }
  42.  
  43.         private void CalculateControlPoints(Point start, Point end, out Point startControlPoint, out Point endControlPoint)
  44.         {
  45.             /**
  46.              * This needs to detect the orientation of the origin relative to its destination,
  47.              * and after doing so, change the formula based uppon that orientation.
  48.              * Right now it only does a left to right, in which the origin X is less
  49.              * than the destination X
  50.              */
  51.             startControlPoint = new Point(start.X * OriginControlTension, start.Y);
  52.             endControlPoint = new Point(end.X / DestinationControlTension, end.Y);
  53.         }
  54.  
  55.         private void CreateConnection()
  56.         {
  57.             CalculateConnectionPoints(out Point start, out Point end);
  58.             CalculateControlPoints(start,end, out Point controlPoint1, out Point controlPoint2);
  59.             Path path = new Path
  60.             {
  61.                 Width = this.Parent.Width,
  62.                 Height = this.Parent.Height,
  63.                 Stroke = Brushes.White,
  64.                 StrokeThickness = 2.0,
  65.                 Data = new PathGeometry()
  66.                 {
  67.                     Figures = new PathFigureCollection()
  68.                 {
  69.                     new PathFigure()
  70.                     {
  71.                         StartPoint = start,
  72.                         Segments = new PathSegmentCollection()
  73.                         {
  74.                             new BezierSegment()
  75.                             {
  76.                                 Point1 = controlPoint1,
  77.                                 Point2 = controlPoint2,
  78.                                 Point3 = end
  79.                             }
  80.                         }
  81.                     }
  82.                 }
  83.                 }
  84.             };
  85.             Panel.SetZIndex(path, -1);
  86.             this.Connection = path;
  87.             _isConnectionCreated = true;
  88.  
  89.         }
  90.  
  91.         private void CalculateConnectionPoints(out Point start, out Point end)
  92.         {
  93.             start = Origin.TransformToAncestor(this.Parent).Transform(new Point(0, 0));
  94.             start = new Point(start.X + (Origin.Width / 2D), start.Y + (Origin.Height / 2D));
  95.             end = Destination.TransformToAncestor(this.Parent).Transform(new Point(0, 0));
  96.             end = new Point(end.X + (Destination.ActualWidth / 2D), end.Y + (Destination.ActualHeight / 2D));
  97.         }
  98.     }
  99. }
  100.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement