package pathanimation;

import javafx.animation.*;
import javafx.animation.transition.*;
import javafx.scene.*;
import javafx.scene.image.*;
import javafx.scene.paint.Color;
import javafx.scene.shape.*;
import javafx.stage.Stage;


package class ShipScenario extends Scenario {

    public-read override var image = Image { url: "{__DIR__}resources/boat-small.png" };
        
    def ship = ImageView {
        smooth: true
        image: Image { url: "{__DIR__}resources/boat.png" };
        translateX: 60
        translateY: 315
    };

    // Path the ship is animated along
    def path = Path {
        elements: [
            MoveTo       {         x: 130          y: 420 },
            CubicCurveTo { controlX1: 220  controlY1: 390
                           controlX2: 220  controlY2: 456
                                   x: 310          y: 420 },
            CubicCurveTo { controlX1: 400  controlY1: 390
                           controlX2: 460  controlY2: 490
                                   x: 525          y: 420 },
            CubicCurveTo { controlX1: 590  controlY1: 355
                           controlX2: 640  controlY2: 455
                                   x: 730          y: 420 },
        ]
    };

    // The blue sea
    def sea = Path {
        fill: Color.BLUE
        elements: [
            path.elements,
            CubicCurveTo { controlX1: 820  controlY1: 390
                           controlX2: 820  controlY2: 455
                                   x: 905          y: 420 },
            CubicCurveTo { controlX1: 995  controlY1: 390
                           controlX2: 1060 controlY2: 490
                                   x: 1120         y: 420 },
            VLineTo      {                         y: 600 },
            HLineTo      {         x: -100                },
            VLineTo      {                         y: 420 },
            CubicCurveTo { controlX1: -9   controlY1: 355
                           controlX2: 45   controlY2: 455
                                   x: 130          y: 420 },
            ClosePath {},
        ]
    };
    
    // Animates ship along the path
    def anim = PathTransition {
        path: AnimationPath.createFromPath(path)
        orientation: OrientationType.ORTHOGONAL_TO_TANGENT
        node: ship
        interpolate: Interpolator.LINEAR
        duration: 8s
        repeatCount: Timeline.INDEFINITE
    };

    // Animates the path itself
    def move = TranslateTransition {
        fromX: 0
        fromY: 0
        toX: -600
        toY: 0
        node: bind group
        interpolate: Interpolator.LINEAR
        duration: 8s
        repeatCount: Timeline.INDEFINITE
    };

    def group = Group {
        content: [sea, ship]
    };

    public-read override var node = bind Group {
        content: [
            ImageView {
                smooth: true
                image: Image { url: "{__DIR__}resources/{Scenario.PLATFORM}/boat-bg.png" };
            },
            group,
        ]
    }

    public override function play() {
        anim.play();
        move.play();
    }

    public override function pause() {
        anim.pause();
        move.pause();
    }

    public-read override var running = bind (anim.running and not anim.paused);
}