License text

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER 
 * Copyright  2008, 2010 Oracle and/or its affiliates.  All rights reserved. 
 * Use is subject to license terms.
 * 
 * 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, 
 *     this list of conditions and the following disclaimer. 
 *
 *   * Redistributions in binary form must reproduce the above copyright 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 Corporation 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. 
 */

    

package sl;

import java.util.Random;
import javafx.animation.Interpolator;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.fxd.FXDContent;
import javafx.fxd.FXDLoader;
import javafx.lang.FX;
import javafx.scene.CustomNode;
import javafx.scene.Group;
import javafx.scene.input.MouseEvent;
import javafx.scene.Node;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.scene.transform.Scale;
import javafx.util.Math;
import sl.Main;
import sl.Player;

/**
 * @author JavaFX Samples Team
 */
public var dim = bind Math.min(Main.sceneheight - Main.sceneheight /10 - Main.sceneheight/20, Main.scenewidth - Main.scenewidth /10 - Main.scenewidth/20);//600;
var sqSide = bind dim / 10;

public var fxdContent:FXDContent;

//public var fxdContentTitle:Node;
public var fxdContentTitle:FXDContent;
var fxTitleNode:Group;

public var player0: Player = Main.bd.players[0];
public var player1: Player = Main.bd.players[1];

public var previousdiceValue:Integer;
var diceval: Integer = 1; //

public var shiftwidth = bind Math.max(0, (Main.scenewidth - dim - sqSide )/2);
public var shifty = bind (sqSide * 3/4) + Math.max(0, (Main.sceneheight - dim - sqSide )/2);
var t:Timeline = Timeline {
    repeatCount: 1
    keyFrames: [
         KeyFrame {
            time: 1s
            canSkip: true
            values: [
                diceval => 6  tween Interpolator.LINEAR
            ]
        }
         KeyFrame {
            time: 2s
            canSkip: true
            values: [
                diceval => 1  tween Interpolator.LINEAR
            ]
            action:function(){
                    var rand:Random = Random{};
                    diceval = rand.nextInt(5) + 1;
                    Main.gd.updateGame(diceval);
            }
        }
    ]

};



function getFXDGroup(fxdc:FXDContent, id:String):Group {

    var g = fxdc.getGroup(id);
    if (g.parent instanceof Group) {
        delete g from (g.parent as Group).content;
    }
    g
}

public class TurnandDicePanel extends CustomNode {
    var closebuttonstroke:Integer = 2;

    public override function create():Node {
            //make the dice code -start
        fxdContent = FXDLoader.loadContent("{__DIR__}Snakes_Ladders_Dice_All_Faces.fxz");
        fxdContentTitle = FXDLoader.loadContent("{__DIR__}SnakesLaddersGameTitleCloseControl.fxz"); // loads the content
        var gTitle:Group = Group{
            transforms: [
                Scale {
                    x: bind Math.min(1, dim/544);
                    y: bind Math.min(1, dim/460);
                    pivotX: 0
                    pivotY: 0
                    }
                ]
                translateX: bind shiftwidth
                content: [
                    getFXDGroup(fxdContentTitle, "snakes_ladders_title")
                ]
            }
        var gclose:Group = Group{
            transforms: [
                Scale {
                    x: bind Math.min(1, dim/544);
                    y: bind Math.min(1, dim/460);
                    pivotX: 0
                    pivotY: 0
                }
            ]
            translateX: bind shiftwidth + Math.min(1, dim/460)
            onMouseClicked:function(e:MouseEvent):Void {
             if ("{__PROFILE__}" != "browser")
                FX.exit();
             }
          content: [
              getFXDGroup(fxdContentTitle, "close_control")
            ]
        }
        var gBase1:Group = getFXDGroup(fxdContent, "dice_cube");
        var gBase:Group = Group{
            transforms: [
                Scale {
                    x: bind Math.max(0.75 * sqSide / 80, Math.min(1, shiftwidth /80))
                    y: bind Math.max(0.75 * sqSide / 80, Math.min(1, shiftwidth /80))
                    pivotX: 0
                    pivotY: 0
                }
            ]
            content: [
                gBase1
             ]
             translateY: bind shifty;
             onMouseClicked:function(e:MouseEvent):Void {
                if(true){
                    t.playFromStart();
                }//end if
             }
        };
            //the following code is  rather cumbersome
            //done this way to adjust to and avoid modification
            //to the UE designed FXD image
            var gFaces:Group = Group{};

            var g6:Group = getFXDGroup(fxdContent, "dice_image_six");
            var g5:Group = getFXDGroup(fxdContent, "dice_image_five");
            var g4:Group = getFXDGroup(fxdContent, "dice_image_four");
            var g3:Group = getFXDGroup(fxdContent, "dice_image_three");
            var g2:Group = getFXDGroup(fxdContent, "dice_image_two");
            var g1:Group = getFXDGroup(fxdContent, "dice_image_one");

            g2.visible =true;
            g3.visible =true;
            g4.visible =true;
            g5.visible =true;
            g6.visible =true;

            var gf1:Group = Group{
                visible:bind (diceval == 1)
                content: [g1]
            };
            var gf2:Group = Group{
                visible: bind (diceval == 2)
                content: [ g2 ]
            };
            var gf3:Group = Group{
                visible: bind (diceval == 3)
                content: [ g3 ]
            };
             var gf4:Group = Group{
                visible: bind (diceval == 4)
                content: [ g4 ]
            };
             var gf5:Group = Group{
                visible: bind (diceval == 5)
                content: [ g5 ]
            };
             var gf6:Group = Group{
                visible: bind (diceval == 6)
                content: [ g6 ]
            };
            insert gf1 into gFaces.content;
            insert gf2 into gFaces.content;
            insert gf3 into gFaces.content;
            insert gf4 into gFaces.content;
            insert gf5 into gFaces.content;
            insert gf6 into gFaces.content;
            var gDice:Group = Group{
                transforms: [
                        Scale {
                            x: bind Math.max(0.75 * sqSide / 80, Math.min(1, shiftwidth /80))
                            y: bind Math.max(0.75 * sqSide / 80, Math.min(1, shiftwidth /80))
                            pivotX: 0
                            pivotY: 0
                        }
                 ]
                 content: [
                    gFaces
                 ]
                translateY: bind shifty;
            };
            //make the dice code -end 
            return Group {
                content: [
                    gTitle,
                    gclose,
                    gBase,
                    gDice,
                   Text {
                        font: bind Font {
                            size: sqSide / 3
                        }
                        x: bind sqSide
                        y: bind dim + shifty + 10
                        fill: bind Main.bd.players[ Main.gd.prevturn ].col
                        content: bind "Dice Val: { previousdiceValue }"
                    }
                ]
            };
        }
}