Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Media;
- using System.Windows.Shapes;
- namespace VoidwalkerEngine.Editor.Controls.Blueprint
- {
- public class BlueprintConnection
- {
- public UserControl Parent { get; set; }
- public FrameworkElement Origin { get; set; }
- public FrameworkElement Destination { get; set; }
- public double OriginControlTension { get; set; } = 1.25;
- public double DestinationControlTension { get; set; } = 1.25;
- public Path Connection { get; set; }
- private bool _isConnectionCreated;
- public BlueprintConnection()
- {
- }
- public void Rebuild()
- {
- if (!_isConnectionCreated)
- {
- CreateConnection();
- return;
- }
- CalculateConnectionPoints(out Point start, out Point end);
- this.Connection.Width = this.Parent.Width;
- this.Connection.Height = this.Parent.Height;
- PathGeometry pathGeometry = (PathGeometry)this.Connection.Data;
- PathFigure figure = pathGeometry.Figures[0];
- figure.StartPoint = start;
- BezierSegment segment = (BezierSegment)figure.Segments[0];
- CalculateControlPoints(start,end,out Point controlPoint1, out Point controlPoint2);
- segment.Point1 = controlPoint1;
- segment.Point2 = controlPoint2;
- segment.Point3 = end;
- }
- private void CalculateControlPoints(Point start, Point end, out Point startControlPoint, out Point endControlPoint)
- {
- /**
- * This needs to detect the orientation of the origin relative to its destination,
- * and after doing so, change the formula based uppon that orientation.
- * Right now it only does a left to right, in which the origin X is less
- * than the destination X
- */
- startControlPoint = new Point(start.X * 1.25, start.Y);
- endControlPoint = new Point(end.X / 1.25, end.Y);
- }
- private void CreateConnection()
- {
- CalculateConnectionPoints(out Point start, out Point end);
- CalculateControlPoints(start,end, out Point controlPoint1, out Point controlPoint2);
- Path path = new Path
- {
- Width = this.Parent.Width,
- Height = this.Parent.Height,
- Stroke = Brushes.White,
- StrokeThickness = 2.0,
- Data = new PathGeometry()
- {
- Figures = new PathFigureCollection()
- {
- new PathFigure()
- {
- StartPoint = start,
- Segments = new PathSegmentCollection()
- {
- new BezierSegment()
- {
- Point1 = controlPoint1,
- Point2 = controlPoint2,
- Point3 = end
- }
- }
- }
- }
- }
- };
- Panel.SetZIndex(path, -1);
- this.Connection = path;
- _isConnectionCreated = true;
- }
- private void CalculateConnectionPoints(out Point start, out Point end)
- {
- start = Origin.TransformToAncestor(this.Parent).Transform(new Point(0, 0));
- start = new Point(start.X + (Origin.Width / 2D), start.Y + (Origin.Height / 2D));
- end = Destination.TransformToAncestor(this.Parent).Transform(new Point(0, 0));
- end = new Point(end.X + (Destination.ActualWidth / 2D), end.Y + (Destination.ActualHeight / 2D));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement