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


// @(#)AnimatedTextWidget.java, 1.16, 11/15/96





package marimba.gui;





import java.awt.*;





import marimba.persist.*;





/**


 * Scrolling text widget.


 *


 * @author	Arthur van Hoff


 * @version 	1.16, 11/15/96


 */


public class AnimatedTextWidget extends AnimatedWidget {


    /**


     * The text that is being animated.


     * @see #getText


     * @see #setText


     */


    public String  text = "Message";





    /**


     * The text that is being displayed when 'text' is ready animating.


     * @see #getNextText


     * @see #setNextText


     */


    public String  nextText = "Message";





    /**


     * The current x-position of the text being displayed.


     */


    public float  strx;





    /**


     * The current y-position of the text being displayed.


     */


    public float  stry;





    /**


     * The number of pixels to be skipped in the x-direction.


     * This also determines the direction of the animation.


     * @see #getDx


     * @see #setDx


     * @see #setDirection


     */


    public int  dx = 10;





    /**


     * The number of pixels to be skipped in the y-direction.


     * This also determines the direction of the animation.


     * @see #getDy


     * @see #setDy


     * @see #setDirection


     */


    public int  dy = 0;








    /**


     * Constructor.


     */


    public AnimatedTextWidget() {


	dx = 10;


    }





    /**


     * Get the properties of this widget.


     */


    public void getProperties(PropertyList list) {


	super.getProperties(list);


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


	list.setInteger("dy", dy, 0);


	list.setString("text", text, "");


    }





    /**


     * Set the properties of this widget.


     */


    public void setProperties(PropertyList list) {


	super.setProperties(list);


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


	dy = list.getInteger("dy", 0);


	text = nextText = list.getString("text", "");


    }





    /**


     * Get the value of the text.


     * @see #text


     */


    public String getText() {


	return text;


    }





    /**


     * Set the text. The text will change immediately.


     * @see #text


     */


    public void setText(String text) {


	if (text != null) {


	    this.text = text;


	    nextText = text;


	}


    }





    /**


     * Get the text that will be displayed when the current


     * message is scrolled off the screen.


     * @see #nextText


     */


    public String getNextText() {


	return nextText;


    }





    /**


     * Get the number of pixels to be skipped in the x-direction.


     * @see #dx


     */


    public int getDx() {


	return dx;


    }





    /**


     * Set the number of pixels to be skipped in the x-direction.


     * @see #dx


     */


    public void setDx(int dx) {


	this.dx = dx;


    }





    /**


     * Get the number of pixels to be skipped in the y-direction.


     * @see #dy


     */


    public int getDy() {


	return dy;


    }





    /**


     * Set the number of pixels to be skipped in the y-direction.


     * @see #dy


     */


    public void setDy(int dy) {


	this.dy = dy;


    }





    /**


     * Set the direction.


     * @see #dx


     * @see #dy


     */


    public void setDirection(int dx, int dy) {


	this.dx = dx;


	this.dy = dy;


    }





    /**


     * Set the next text. The text won't be displayed


     * immediately, it will be displayed as soon


     * as the current message is scrolled off screen.


     * @see #nextText


     */


    public void setNextText(String text) {


	nextText = (text == null) ? "" : text;


    }





    /**


     * Advance the animation,


     * when this AnimatedTextWidget is not disabled.


     */


    public void advance() {


	FontMetrics fm = getFontMetrics(font);


	


	frame++;


	if (dx != 0) {


	    strx += (float)dx/fps;





	    int w = fm.stringWidth(text);


	    if (w > 0) {


		if (strx + w < 0) {


		    strx = width + strx + w;


		    text = nextText;


		} else if (strx > width) {


		    strx -= width + w;


		    text = nextText;


		}


	    }


	} else {


	    strx = (width - fm.stringWidth(text)) / 2;


	}


	if (dy != 0) {


	    stry += (float)dy/fps;





	    int h = fm.getHeight();


	    if (h > 0) {


		if (stry + h < 0) {


		    stry = height + stry + h;


		    text = nextText;


		} else if (stry > height) {


		    stry -= height + h;


		    text = nextText;


		}


	    }


	} else {


	    stry = (height - fm.getHeight())/2 + fm.getAscent() - fm.getHeight();


	}


	repaint();


    }





    /**


     * Paint the message.


     */


    public synchronized void paint(Graphics g) {


	FontMetrics fm = getFontMetrics(font);


	g.drawString(text, (int)strx, (int)stry + fm.getHeight());


    }


}


