// Copyright 1996, Marimba Inc. All Rights Reserved.


// @(#)AnimatedWidget.java, 1.12, 12/19/96





package marimba.gui;





import java.awt.*;


import java.net.*;





import marimba.persist.*;


import marimba.util.*;





/**


 * A basic animated widget.


 *


 * @author	Arthur van Hoff


 * @version 	1.12, 12/19/96


 */


public abstract class AnimatedWidget extends Widget implements TimerClient {


    /**


     * The frames per second that are displayed.


     * @see #getFps


     * @see #getValue


     * @see #getIntegerValue


     * @see #setFps


     */


    public int fps = 10;





    /**


     * The current frame of the animation that is displayed.


     * @see #getFrame


     * @see #setFrame


     * @see #setValue


     */


    public int frame;





    /**


     * The panel in which this animation must be displayed.


     */


    PlayerPanel player;








    /**


     * Get the properties of this widget.


     */


    public void getProperties(PropertyList list) {


	super.getProperties(list);


	list.setBoolean("disabled", disabled, false);


	list.setInteger("fps", fps, 10);


    }





    /**


     * Set the properties of this widget.


     */


    public void setProperties(PropertyList list) {


	super.setProperties(list);


	disabled = list.getBoolean("disabled", false);


	fps = list.getInteger("fps", 10);


    }





    /**


     * Get the Frames per second.


     * @see #fps


     */


    public int getIntegerValue() {


	return fps;


    }





    /**


     * Get the Frames per second.


     * @see #fps


     */


    public Object getValue() {


	return new Integer(getIntegerValue());


    }





    /**


     * Set the current frame.


     * @see #frame


     */


    public void setValue(Object value) {


	if (value instanceof Number) {


	    setValue(((Number)value).intValue());


	} else if (value instanceof String) {


	    try {


		setValue(Integer.parseInt((String)value));


	    } catch (NumberFormatException e) {


	    }


	} 


    }





    /**


     * Set the current frame.


     * @see #frame


     */


    public void setValue(int frame) {


	if (this.frame != frame) {


	    this.frame = frame;


	    repaint();


	}


    }





    /**


     * Get the frames per second.


     * @see #fps


     * @see #getIntegerValue


     */


    public int getFps() {


	return getIntegerValue();


    }





    /**


     * Set the Frames Per Second.


     * @see #fps


     * @see #setFPS


     */


    public void setFps(int fps) {


	if (this.fps != fps) {


	    Timer.master.remove(this);


	    this.fps = fps;


	    if (fps > 0) {


		Timer.master.add(this);


	    }


	}


    }





    /**


     * Another method for setting Frames Per Second,


     * 'fps' in capitals.


     * @see #setFps


     */


    public void setFPS(int fps) {


	setFps(fps);


    }





    /**


     * When disabling also stop the animation,


     * otherwise start the animation again.


     */


    public void disable(boolean disabled) {


	super.disable(disabled);


	if (this.disabled) {


	    stop();


	} else {


	    start();


	}


    }





    /**


     * Get the current frame.


     * @see #frame


     */


    public int getFrame() {


	return frame;


    }





    /**


     * Set the current frame.


     * @see #frame


     * @see #setValue


     */


    public void setFrame(int frame) {


	setValue(frame);


    }





    /**


     * Start the animation.


     * @see #stop


     * @see #advance


     */


    public void start() {


	player = getPlayerPanel();


	if ((fps > 0) && !disabled) {


	    Timer.master.add(this);


	}


    }





    /**


     * Stop the animation.


     * @see #start


     * @see #advance


     */


    public void stop() {


	Timer.master.remove(this);


	player = null;


    }





    /**


     * Advance the animation.


     * @see #start


     * @see #stop


     */


    public void advance() {


	setValue(frame + 1);


    }





    /**


     * Advance the animation.


     */


    public long tick(long tm, Object arg) {


	if (!disabled) {


	    if ((player != null) && !player.editing()) {


		advance();


	    }


	    return (fps > 0) ? (tm + 1000 / fps) : 0;


	}


	return 0;


    }





    /**


     * Handle mouse events.


     */


    public boolean handleEvent(Event evt) {


	switch (evt.id) {


	    case Event.MOUSE_DOWN:


		if (!disabled) {


		    action();


		}


		return false;





	    case Event.MOUSE_DRAG:


	    case Event.MOUSE_UP:


		return false;


	}


	return super.handleEvent(evt);


    }





    /**


     * Debugging


     */


    public void paramString(StringBuffer buf) {


	super.paramString(buf);


	buf.append(",fps=");


	buf.append(fps);


	buf.append(",frame=");


	buf.append(frame);


    }


}


