/* -----------------------------------------------------------------------
 * The "Hello World" applet.  This is a very simple applet that puts up
 * a button, which when pressed animates its movement off screen, then 
 * returns.
 * -----------------------------------------------------------------------
 */
package sub_arctic.test;

/* import various pieces of the sub_arctic toolkit that we need */
import sub_arctic.lib.*;
import sub_arctic.anim.*;
import sub_arctic.input.*;
import sub_arctic.constraints.std_cnstr;

/* We use a new subclass of interactor_applet (with is an applet capable of 
 * hosting a sub_arctic interface).
 */
public class hello_world_anim extends debug_interactor_applet 
  implements callback_object {

  /** Keeps track of whether or not we are in the middle of animating */
  boolean are_animating;

  /* This is the button on the interface */
  button    goodbye;

  /* sprite container we are using to do the animation */
  sprite_container container;

  /* the two transitions for our movements */
  transition trans1, trans2;

  /* initialization of sub_arctic interface when applet starts */
  public void build_ui(base_parent_interactor top) 
    {
      /* when we start, we are not animating */
      are_animating=false;

      /* create a button... it'll be at zero zero of its parent */
      goodbye = new button(0,0, "Goodbye", this);

      /* put the button in the sprite container ... we'll override the 
	   position with constraints in a second*/
      container=new sprite_container(0,0,goodbye,this);

      /* establish constraints to center it in the parent */
      container.set_x_constraint(std_cnstr.centered(PARENT.W(), 0));
      container.set_y_constraint(std_cnstr.centered(PARENT.H(), 0));

	/* make the button & container children of the top level */
      top.add_child(container);

    }

  /** Handle callback from the button and the sprite container. */
  public void callback(
    interactor from, 
    event      evt, 
    int        callb_num, 
    Object     callb_parm)
  {
      int x,y;
      time_interval t1, t2;
      pacer p;
      long now;

      x=container.x(); y=container.y();

      /* is it the button and are we not currently moving? */
      if ((from == goodbye) && (are_animating == false)) 
	{
	  /* get the current time*/
	  now = time_interval.now();

	  /* create a pacing function for an anticipation */
	  p=new slow_in_slow_out(0.35,0.2);

	  /* start the animation now and run it for 3 seconds */
	  t1 = new time_interval(now,now+3000);

	  /*create transition along cartoon line from here to off-screen */
	  trans1 = new transition(container,t1,new cartoon_line(x,y,x+800,y,p));

	  /* return time interval is right after the exit */
	  t2 = new time_interval(time_interval.AFTER_END_OF,trans1,0);
	  t2.set_duration(3000);

	  /* set up transition to move container along a reversed line */
	  trans2=new transition(container,t2,new cartoon_line(x+800,y,x,y,p));

	  /* set the two transitions on the container to start it going */
	  container.set_transition(trans1);
	  container.set_transition(trans2);

	  /* remove the constraints (since we are moving the object) */
	  container.set_x_constraint(NO_CONSTRAINT);
	  container.set_y_constraint(NO_CONSTRAINT);

	  /* we are now animating */
	  are_animating=true;
        }	

      /* if we get a callback from the sprite container */
      if (from == container) 
	{
	  /* is it the end of the second transition ?*/
	  if ((((transition)callb_parm) == trans2) && (callb_num == 1)) 
	    {
	      /* we aren't animating anymore */
	      are_animating=false;

	      /* put the constraints back */
	      container.set_x_constraint(std_cnstr.centered(PARENT.W(), 0));
	      container.set_y_constraint(std_cnstr.centered(PARENT.H(), 0));
	    }
        }
    }

  /* clean up the sub_arctic interface when the applet stops */
  public void stop()
    {
      remove_top_interactor();
    }
};