Legal Terms and Copyright Notice
/* 
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
 * 
 * Copyright © 2010, Oracle and/or its affiliates. All rights reserved.
 * Oracle is a registered trademark of Oracle Corporation and/or its affiliates. 
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates. 
 * Other names may be trademarks of their respective owners.
 * 
 * This file is available and licensed under the following license:
 * 
 * Redistribution and use in source and binary forms, with or without modification, 
 * are permitted provided that the following conditions are met:

 *   *  Redistributions of source code must retain the above copyright notice, 
        trademark notice, this list of conditions, and the following disclaimer.

 *   *  Redistributions in binary form must reproduce the above copyright notice, 
        trademark notice, this list of conditions, and the following disclaimer in 
        the documentation and/or other materials provided with the distribution.

 *   *  Neither the name of Oracle nor the names of its contributors may be used 
        to endorse or promote products derived from this software without specific 
        prior written permission.
 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
 * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES LOSS OF USE, 
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 */
/*
 * TubeFill.fx
 *
 * Created on Feb 29, 2008, 3:18:34 PM
 */

package tubefilling;

import javafx.animation.Interpolator;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.scene.CustomNode;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.paint.Color;
import javafx.scene.paint.LinearGradient;
import javafx.scene.paint.Paint;
import javafx.scene.paint.Stop;
import javafx.scene.shape.Ellipse;
import javafx.scene.shape.Line;
import javafx.scene.shape.Rectangle;
import javafx.scene.shape.ShapeSubtract;
import javafx.scene.text.Font;
import javafx.scene.text.Text;

/**
 * @author Vaibhav Choudhary
 */

public class TubeFill extends CustomNode {

    public var fillHeight = 0.0;
    public var fill = 0.0;
    public var op = 0.0;
    public var grad: Paint;
    public var t = Timeline {
        repeatCount: 1
        keyFrames: [
            KeyFrame {
                time: 20s
                canSkip: true
                values: bind [
                    fill => fillHeight tween Interpolator.LINEAR
                    op => 1.0 tween Interpolator.LINEAR
                ]
            }
        ]
    }
    init {
        t.playFromStart();
    }

    public override function create(): Node {
        return Group {
            content: [
                Group {
                    //Instance of creating the y-axis tick lines and text
                    content: for(i in [0 .. 9]) { [
                            Line {
                                startX: 214 ,
                                startY: 290 - i * 20
                                endX: 217 ,
                                endY: 290 - i * 20
                                strokeWidth: 1
                                stroke: Color.BLACK
                            },
                            Text {
                                font: Font {
                                    name: "Arial Bold"
                                    size: 11
                                    letterSpacing: 0.15
                                }
                                x: 230,
                                y: 295 - i * 20
                                content: "{i * 20}"
                            }
                        ]
                    }

                }
                // Instance of creating ellipse for top of tube
                Ellipse {
                    stroke: Color.LIGHTGRAY
                    opacity: 0.5
                    cache: true
                    strokeWidth: 4
                    centerX: 185,
                    centerY: 98
                    radiusX: 34,
                    radiusY: 7
                    fill: LinearGradient {
                        startX: 0.0
                        startY: 0.0
                        endX: 1.0
                        endY: 0.0
                        stops: [
                            Stop {
                                color: Color.TRANSPARENT
                                offset: 0.0
                            },
                            Stop {
                                color: Color.LIGHTGRAY
                                offset: 0.5
                            },
                            Stop {
                                color: Color.TRANSPARENT
                                offset: 1.0
                            },

                        ]
                    }
                }
                 // Instance of creating ellipse for top of liquid
                Ellipse {
                    translateY: bind 91 - fill
                    opacity: bind op
                    cache: true
                    stroke: Color.LIGHTGRAY
                    strokeWidth: 2
                    opacity: 1.0
                    centerX: 185,
                    centerY: 199
                    radiusX: 28,
                    radiusY: 7
                    fill: grad
                }

                 /* Instance of liquid column: rectangle with top ellipse
                cut out */
                ShapeSubtract {
                    cache: true
                    translateY: bind 90 - fill
                    fill: grad
                    opacity: 1.0
                    a: bind Rectangle {
                        opacity: 0.2
                        x: 156,
                        y: 200
                        width: 60,
                        height: fill
                        fill: Color.BLACK
                    },
                    b: Ellipse {
                        stroke: Color.BLACK
                        opacity: 0.1
                        centerX: 184,
                        centerY: 200
                        radiusX: 30,
                        radiusY: 7
                        fill: Color.BLACK
                    }
                }
                 /*Instance of tube sides with cutouts for ellipse at
                top of tube */
                ShapeSubtract {
                    cache: true
                    fill: LinearGradient {
                        startX: 0.0
                        startY: 0.0
                        endX: 1.0
                        endY: 0.0
                        stops: [
                            Stop {
                                color: Color.TRANSPARENT
                                offset: 0.0
                            },
                            Stop {
                                color: Color.WHITE
                                offset: 0.3
                            },
                            Stop {
                                color: Color.BLACK
                                offset: 1.0
                            },

                        ]
                    }
                    opacity: 0.4
                    a: Rectangle {
                        x: 150,
                        y: 100
                        width: 70,
                        height: 190
                        fill: Color.BLACK
                    },
                    b: Ellipse {
                        stroke: Color.BLACK
                        opacity: 0.3
                        centerX: 184,
                        centerY: 98
                        radiusX: 34,
                        radiusY: 7
                        fill: Color.BLACK
                    }
                },
                 /* Instance of thin rectangle to create heavy line
                at bottom of bar above country label */
                Rectangle {
                    x: 148,
                    y: 290
                    width: 81,
                    height: 3
                    fill: Color.BLACK

                },
            ]
        };
    }
}