/*
This is a simple sample with a linear moving sphere.
It can be done with conventional keyframe animation as well.
*/


  /** Creates the initial state from information in the current scene
    and/or displays a dialog to allow configuring the script.

    @return the initial state or null if the state could not be created.
            in the latter case the script should have displayed a dialog
            to inform the user about the reason of the failure.
  */
  public Serializable createInitialState(window)
  {
    scene = window.getScene();
    result = new HashMap();
    ball = scene.getObject("Sphere 1");
    result.put("Pos", new Vec3 (ball.getCoordinates().getOrigin()));

    return result;
  }


  /** Take <CODE>initial</CODE> state and one <CODE>previous</CODE> state
    and create a new state which is appropriate if time goes the
    <CODE>timestep</CODE> interval further.

    <P>The <CODE>timestep</CODE> may be negative iff
    {@link #isBackwardTimeAllowed()} returns
    true for the same combination of parameters.<BR>
    <CODE>initial</CODE> and <CODE>previous</CODE> may be identical (if this
    is the first step).<BR>

    <P>The <CODE>initial</CODE> state is given because it may contain
    additional configuration data which does not need to be stored in
    every state object.

    <P>The current implementation always uses a timestep of 1/fps or
    -1/fps where fps are the frames per second of the current scene. (TODO(MB)!)

    @return a new (or recycled) state object or null if no state could be
            created.
  */
  public Object executeTimeStep(initial, previous, timestep, scene)
  {
    statemap = (HashMap) previous;
    result = new HashMap();
    result.put("Pos", ((Vec3)statemap.get("Pos")).plus(new Vec3(timestep*0.2, 0, 0)));

    return result;
  }


  /** Returns true iff a new state can be created from an old state which
    lies in the future of the new one.<BR>
    This is only needed in interactive mode. During animation rendering
    time always goes forward.<BR>
    The parameters have the same meaning as for {@link #executeTimeStep()}.
  */
  public boolean isBackwardTimeAllowed(initial, previous, timestep, scene)
  { return true; }


  /** Returns the minimal timestep allowed for these states.
    The parameters have the same meaning as for {@link #executeTimeStep()}.
    <P>Ignored by current implementation!  (TODO(MB)!)
  */
  public double getMinimalTimeStep(initial, previous, scene)
  {return 0;}


  /** Returns the maximal timestep allowed for these states.
    The parameters have the same meaning as for {@link #executeTimeStep()}.
    <P>Ignored by current implementation!  (TODO(MB)!)
  */
  public double getMaximalTimeStep(initial, previous, scene)
  {return -1;}




  /** Realizes the state, meaning to set the scene according to the state
    information in <CODE>state</CODE> and <CODE>initial</CODE>.
  */
  public void realizeState(initial, state, scene)
  {
    statemap = (HashMap) state;
    ball = scene.getObject("Sphere 1");
    ball.getCoordinates().setOrigin(new Vec3(statemap.get("Pos")));
  }
