package disasters;

import java.awt.*;
import marimba.gui.*;
import marimba.persist.*;

/**
 * A picture command button that animates when pressed.
 *
 * @author	Danny Goodman
 * @version 1.0 12/06/96
 */
public class AnimatedButtonWidget extends CommandButtonWidget {

    public int fps = 10;
    public int startframe = 1;
    public int endframe = 8;
    public boolean preload = true;
    public String animSrc = "~builder/jon";
    public AnimatedImageWidget anim = null;

    /**
     * Default constructor presets CommandButtonWidget
     * class properties for convenience.
     */
    public AnimatedButtonWidget() {
	    setGroup("groupA");
	    setSticky(true);
        setMode(PICTURE);
	    setSource("~builder/audio.gif");
    }

    /**
     * Get the properties of this widget.
     */
    public void getProperties(PropertyList list) {
	    // store super class properties in the list
	    super.getProperties(list);

	    // store additional properties in the list
	    list.setInteger("fps", fps, 10);
	    list.setInteger("startframe", startframe, 1);
	    list.setInteger("endframe", endframe, 8);
	    list.setBoolean("preload", preload, true);
	    list.setURL("animSrc", animSrc, "~builder.jon");
    }

    /**
     * Set the properties of this widget.
     */
    public void setProperties(PropertyList list) {
	    // retrieve super class properties from the list
	    super.setProperties(list);

	    // retrieve additional properties from list
	    fps = list.getInteger("fps", 10);
	    startframe = list.getInteger("startframe", 1);
	    endframe = list.getInteger("endframe", 8);
	    preload = list.getBoolean("preload", true);
	    animSrc = list.getURL("animSrc", "~builder/jon");
    }


    /**
     *  Create a new animated image widget with all the necessary
     *  properties. Set offset and size to fit inside a pressed,
     *  beveled CommandButton.
     */
    public AnimatedImageWidget newAnimatedImage() {
        AnimatedImageWidget aiw = new AnimatedImageWidget(animSrc, fps, startframe, endframe);
	    aiw.preload = preload;
	    aiw.resize(width-2, height-2);
	    aiw.imgx = 2;
	    aiw.imgy = 2;
	    aiw.start();
	    return aiw;
    }

    /**
     *  Generalized method calls.
     */

    public void enableAnimation() {
        enableAnimation(true);
    }

    public void disableAnimation() {
        enableAnimation(false);
    }

    /**
     *  Main animation starter/stopper
     */
    public void enableAnimation(boolean go) {
	    if (go) {
	        if (anim == null) {
	            add(anim = newAnimatedImage());
	        }
	    } else {
	        if (anim != null) {
	            remove(anim);
	            anim = null;
	        }
	    }
    }

    /**
     *  Make sure animated image tracks any resizing
     *  done to the AnimatedButtonWidget while editing
     *  in Bongo.
     */
    public void layout() {
        super.layout();
        if (anim != null) {
            anim.reshape(0,0,width-2,height-2);
        }
    }

    /**
     *  Start/stop animation when the value of the button changes.
     */

    public void repaint() {
        enableAnimation(getBooleanValue());
        super.repaint();
    }

}