/*
 * Toggle.fx
 *
 * Created on Feb 8, 2010, 10:20:11 PM
 */

package mdot_gui;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;
import javafx.scene.paint.RadialGradient;
import javafx.scene.paint.Stop;
import javafx.scene.shape.Rectangle;

/**
 * @author Student
 */

public class ToggleButton extends GenericButton{

    public var imageButton1: Image;
    public var imageButton2: Image;
    public var onclickButton1Action: function():Void;
    public var onclickButton2Action: function():Void;

    var size = Constants.BUTTON_SIZE * Constants.BUTTON_SCALE;
    var activeImage:Image = imageButton1;
    var toggle:Integer = 0;

    public override function create(): Node {
        Group{

                blocksMouse: true;
                content:[

                    Rectangle {
                    width: size
                    height: size
                    arcWidth: size / 4
                    arcHeight: size / 4
                    fill: null
                    stroke: bind if (active) then Color.YELLOW else Color.MEDIUMBLUE
                    strokeWidth: 4
                },
                 Rectangle {
                    width: size
                    height: size
                    arcWidth: size / 4
                    arcHeight: size / 4
                    fill: RadialGradient {
                        centerX: 0.5
                        centerY: 0.5
                        stops: [
                            Stop { offset: 0.0 color: Color.DARKBLUE },
                            Stop { offset: 0.8 color: Color.BLACK }
                        ]
                    }
                },

                    ImageView {
                        x: (size - imageButton1.width) / 2;
                        y: (size - imageButton1.height) /2;

                        image: bind activeImage;
                    }
                ]

            onMousePressed: function(ev: MouseEvent) {

                //Toggle
                if(toggle == 0){
                    activeImage = imageButton2;

                    //Action defined by user for Button1
                    onclickButton1Action();
                }
                else if (toggle == 1)
                {
                    activeImage = imageButton1;

                     //Action defined by user for Button2
                    onclickButton2Action();
                }

                //Change toggle for next time
                toggle = (toggle + 1) mod 2;
            }
        }
    }
}
