/*****************************************************************************
 *                        Web3d.org Copyright (c) 2001-2005
 *                               Java Source
 *
 * This source is licensed under the BSD license.
 * Please read docs/BSD.txt for the text of the license.
 *
 * This software comes with the standard NO WARRANTY disclaimer for any
 * purpose. Use it at your own risk. If there's a problem you get to fix it.
 *
 ****************************************************************************/

import java.util.Map;

import org.web3d.x3d.sai.*;


/**
 * An SAI script which moves an object around.  The object will move
 * from one location to another.
 *
 * @author Alan Hudson
 * @version
 */
public class MoveObjectScript
    implements X3DScriptImplementation, X3DFieldEventListener {

    /** The location of the box */
    private SFVec3f location;

    /** The roation of the box */
    private SFRotation rotation;

    /** The input pulse field */
    private SFTime inputField;

    /** A boolean to flip back and forth */
    private boolean flip = true;

    /** The first location */
    private float[] homeLocation = {0, 0, 0};
    private float[] homeRotation = {0, 0, 1, 0};

    /** The second location */
    private float[] otherLocation = {0, 2, 0};
    private float[] otherRotation = {0, 1, 0, 0.5f};

    public MoveObjectScript() {
    }

    //----------------------------------------------------------
    // Methods defined by X3DScriptImplementation
    //----------------------------------------------------------

    public void setBrowser(Browser browser) {
        // ignored as we don't need any browser functionality
    }

    public void setFields(X3DScriptNode externalView, Map fields) {
        // Get the fields we need for processing

        location = (SFVec3f)fields.get("location");
        rotation = (SFRotation)fields.get("orientation");

        inputField = (SFTime)fields.get("pulse");

        // Express interest in pulse changes
        inputField.addX3DEventListener(this);
    }

    public void initialize() {
    }

    public void eventsProcessed() {
    }

    public void shutdown() {
        inputField.removeX3DEventListener(this);
    }

    //----------------------------------------------------------
    // Methods defined by X3DFieldEventListener
    //----------------------------------------------------------

    public void readableFieldChanged(X3DFieldEvent evt) {
        if(flip) {
            flip = false;
            location.setValue(otherLocation);
            rotation.setValue(otherRotation);
        }
        else {
            flip = true;
            location.setValue(homeLocation);
            rotation.setValue(homeRotation);
        }
    }
}
