package nl.utwente.viskell.ui.components;

import javafx.geometry.Point2D;
import javafx.scene.shape.CubicCurve;
import nl.utwente.ewi.caes.tactilefx.control.TactilePane;

/**
 * This class represent a Connection-Line visual object in the UI. Each UI
 * element that uses Connection-Line properties should extend this class.
 * <p>
 * For Lines that connect inputs and outputs of Blocks see Connection.
 * </p>
 */
public class ConnectionLine extends CubicCurve implements ComponentLoader {

    /**
     * Control offset for this bezier of this line. in simple terms: controls
     * the curviness of the line.
     *
     * A higher offset equates to a more curved line. The offset is a function
     * of the offset multiplier and the straight line length. The offset will
     * always be between the defined minimum and maximum.
     */
    public static final double BEZIER_CONTROL_OFFSET_MULTIPLIER = 0.7f;
    public static final double BEZIER_CONTROL_OFFSET_MINIMUM = 10f;
    public static final double BEZIER_CONTROL_OFFSET_MAXIMUM = 200f;

    /**
     * Constructs a new ConnectionLine from FXML.
     */
    public ConnectionLine() {
        this.loadFXML("ConnectionLine");

        TactilePane.setDraggable(this, false);
        TactilePane.setGoToForegroundOnContact(this, false);
        this.setMouseTransparent(true);
    }

    /**
     * Sets the start coordinates for this ConnectionLine object.
     *
     * @param point Coordinates local to this Line's parent.
     */
    public void setStartPositionParent(Point2D point) {
        setStartX(point.getX());
        setStartY(point.getY());
        updateBezierControlPoints();
    }

    /**
     * Sets the end coordinates for this ConnectionLine object.
     *
     * @param point coordinates local to this Line's parent.
     */
    public void setEndPositionParent(Point2D point) {
        setEndX(point.getX());
        setEndY(point.getY());
        updateBezierControlPoints();
    }

    /**
     * Returns the current bezier offset based on the current start and end
     * positions.
     */
    private double getCurrentBezierOffset() {
        double unclamped = BEZIER_CONTROL_OFFSET_MULTIPLIER
                * getStraightLineLength();
        return Math.max(Math.min(unclamped, BEZIER_CONTROL_OFFSET_MAXIMUM),
                BEZIER_CONTROL_OFFSET_MINIMUM);
    }

    /**
     * Returns the length of a direct line between the end and start point of
     * this CubicCurve
     */
    public double getStraightLineLength() {
        return Math.sqrt(getDeltaX() * getDeltaX() + getDeltaY() * getDeltaY());
    }

    /** Returns the difference between the start and end X posistion. */
    public double getDeltaX() {
        return this.getEndX() - this.getStartX();
    }

    /** Returns the difference between the start and end Y posistion. */
    public double getDeltaY() {
        return this.getEndY() - this.getStartY();
    }

    /**
     * Updates the Bezier offset (curviness) according to the current start and
     * end positions.
     */
    private void updateBezierControlPoints() {
        setControlX1(getStartX());
        setControlY1(getStartY() + getCurrentBezierOffset());
        setControlX2(getEndX());
        setControlY2(getEndY() - getCurrentBezierOffset());
    }
}

@@ -535,75 +535,21 @@
 ier 
-of this line. in simple terms: controls%0A     * the curviness
+curve
  of th
-e
+is
  lin
@@ -561,437 +561,152 @@
    *
-%0A     * A higher offset equates to a more curved line. The offset is a function%0A     * of the offset multiplier and the straight line length. The offset will%0A     * always be between the defined minimum and maximum.%0A     */%0A    public static final double BEZIER_CONTROL_OFFSET_MULTIPLIER = 0.7f;%0A    public static final double BEZIER_CONTROL_OFFSET_MINIMUM = 10f;%0A    public static final double BEZIER_CONTROL_OFFSET_MAXIMUM = 20
+ It determines how a far a line attempts to goes straight from its end points.%0A     */%0A    public static final double BEZIER_CONTROL_OFFSET = 15
 0f;%0A
@@ -1622,39 +1622,32 @@
 ;%0A    %7D%0A%0A    /**
-%0A     *
  Returns the cur
@@ -1699,23 +1699,16 @@
  and end
-%0A     *
  positio
@@ -1702,37 +1702,32 @@
 d end positions.
-%0A    
  */%0A    private 
@@ -1732,37 +1732,31 @@
 e double get
-Current
 Bezier
+Y
 Offset() %7B%0A 
@@ -1773,139 +1773,148 @@
 ble 
-unclamped = BEZIER_CONTROL_OFFSET_MULTIPLIER%0A                * getStraightLineLength();%0A        return Math.max(Math.min(unclamped,
+distX = Math.abs(this.getEndX() - this.getStartX());%0A        double distY = Math.abs(this.getEndY() - this.getStartY());%0A        if (distY %3C
  BEZ
@@ -1935,18 +1935,11 @@
 FSET
-_MAXIMUM),
+) %7B
 %0A   
@@ -1947,19 +1947,27 @@
         
-   
+if (distX %3C
  BEZIER_
@@ -1984,272 +1984,149 @@
 FSET
-_MINIMUM);
+) %7B
 %0A    
-%7D%0A%0A
     
-/**%0A
+ 
      
-* Returns the length of a direct line between the end and start point of%0A     * this CubicCurv
+  // short lines are extra flexibl
 e%0A     
-*/%0A
     
-public double getStraightLineLength() %7B%0A        return Math.sqrt(getDeltaX() * getDeltaX() + getDeltaY() * getDeltaY(
+       return Math.max(BEZIER_CONTROL_OFFSET/10, Math.max(distX, distY
 ));%0A    
 %7D%0A%0A 
@@ -2125,327 +2125,196 @@
     
-%7D%0A%0A
     
-/** Returns the difference between the start and end X posistion. */%0A    public double getDeltaX() %7B%0A        return this.getEndX() - this.getStartX()
+    %7D else %7B%0A                return BEZIER_CONTROL_OFFSET
 ;%0A    
-%7D%0A%0A
     
-/** Returns the difference between the start and end Y posistion. */%0A    public double getDeltaY() %7B%0A        return this.getEndY() - this.getStartY();
+    %7D%0A        %7D else %7B%0A            return Math.cbrt(distY / BEZIER_CONTROL_OFFSET) * BEZIER_CONTROL_OFFSET;%0A        %7D
 %0A   
@@ -2551,37 +2551,31 @@
 artY() + get
-Current
 Bezier
+Y
 Offset());%0A 
@@ -2646,21 +2646,15 @@
  get
-Current
 Bezier
+Y
 Offs

Rewrite of bezier curve point calculation