//
// $Id: PathSequence.java,v 1.1 2003/05/16 02:22:52 ray Exp $

package com.threerings.media.util;

import java.awt.Graphics2D;
import java.awt.Rectangle;

import java.util.ArrayList;
import java.util.List;

import com.threerings.media.Log;

/**
 * Used to create a path that is a sequence of several other paths.
 */
public class PathSequence
    implements Path
{
    /**
     * Conveniently construct a path sequence with the two specified
     * paths.
     */
    public PathSequence (Path first, Path second)
    {
        this(new ArrayList());
        _paths.add(first);
        _paths.add(second);
    }

    /**
     * Construct a path sequence with the list of paths.
     * Note: Paths may be added to the end of the list while
     * the pathable is still traversing earlier paths!
     */
    public PathSequence (List paths)
    {
        _paths = paths;
    }

    // documentation inherited from interface Path
    public void init (Pathable pable, long tickStamp)
    {
        _pable = pable;
        initNextPath(tickStamp, tickStamp);
    }

    // documentation inherited from interface Path
    public boolean tick (Pathable pable, long tickStamp)
    {
        if (pable != _pable) {
            Log.warning("PathSequence ticked with different path than " +
                "it was inited with.");
        }
        return _curPath.tick(_pableRep, tickStamp);
    }

    // documentation inherited from interface Path
    public void fastForward (long timeDelta)
    {
        _lastInit += timeDelta;
        _curPath.fastForward(timeDelta);
    }

    // documentation inherited from interface Path
    public void paint (Graphics2D gfx)
    {
        // for now..
        _curPath.paint(gfx);
    }

    // documentation inherited from interface Path
    public void wasRemoved (Pathable pable)
    {
        _curPath.wasRemoved(_pableRep);
    }

    /**
     * Initialize and start the next path in the sequence.
     */
    protected void initNextPath (long initStamp, long tickStamp)
    {
        if (_paths.size() == 0) {
            _pable.pathCompleted(tickStamp);

        } else {
            _curPath = (Path) _paths.remove(0);
            _lastInit = initStamp;

            _curPath.init(_pableRep, initStamp);
            _curPath.tick(_pableRep, tickStamp);
        }
    }

    /** The list of paths. */
    protected List _paths;

    /** The timestamp at which we last inited a path. */
    protected long _lastInit;

    /** The current path we're pathing. */
    protected Path _curPath;

    /** The pathable we're duping bigtime. */
    protected Pathable _pable;

    /** A fake pathable that we pass to the subpaths. */
    protected Pathable _pableRep = new Pathable() {
        public void pathCompleted (long timeStamp)
        {
            long initStamp;
            // if we just finished a timedpath, we can figure out how
            // long ago it really finished and init the next path
            // at that time in the past.
            if (_curPath instanceof TimedPath) {
                initStamp = _lastInit + ((TimedPath) _curPath)._duration;
            } else {
                // we don't know
                initStamp = timeStamp;
            }

            initNextPath(initStamp, timeStamp);
        }

        public int getX ()
        {
            return _pable.getX();
        }

        public int getY ()
        {
            return _pable.getY();
        }

        public Rectangle getBounds ()
        {
            return _pable.getBounds();
        }

        public void setLocation (int x, int y)
        {
            _pable.setLocation(x, y);
        }

        public void setOrientation (int orient)
        {
            _pable.setOrientation(orient);
        }

        public int getOrientation ()
        {
            return _pable.getOrientation();
        }

        public void pathBeginning ()
        {
            _pable.pathBeginning();
        }
    };
}

@@ -26,17 +26,17 @@
 ava,v 1.
-1
+2
  2003/05
@@ -40,23 +40,23 @@
 /05/
-16 02:22:52 ray
+20 04:09:13 mdb
  Exp
@@ -1007,16 +1007,679 @@
  pable;%0A
+        _pableRep = new DelegatingPathable(_pable) %7B%0A            public void pathCompleted (long timeStamp) %7B%0A                long initStamp;%0A                // if we just finished a timed path, we can figure out how%0A                // long ago it really finished and init the next path at%0A                // that time in the past.%0A                if (_curPath instanceof TimedPath) %7B%0A                    initStamp = _lastInit + ((TimedPath) _curPath)._duration;%0A                %7D else %7B%0A                    // we don't know%0A                    initStamp = timeStamp;%0A                %7D%0A                initNextPath(initStamp, timeStamp);%0A            %7D%0A        %7D;%0A
         
@@ -3320,32 +3320,32 @@
 he subpaths. */%0A
+
     protected Pa
@@ -3364,1264 +3364,8 @@
 eRep
- = new Pathable() %7B%0A        public void pathCompleted (long timeStamp)%0A        %7B%0A            long initStamp;%0A            // if we just finished a timedpath, we can figure out how%0A            // long ago it really finished and init the next path%0A            // at that time in the past.%0A            if (_curPath instanceof TimedPath) %7B%0A                initStamp = _lastInit + ((TimedPath) _curPath)._duration;%0A            %7D else %7B%0A                // we don't know%0A                initStamp = timeStamp;%0A            %7D%0A%0A            initNextPath(initStamp, timeStamp);%0A        %7D%0A%0A        public int getX ()%0A        %7B%0A            return _pable.getX();%0A        %7D%0A%0A        public int getY ()%0A        %7B%0A            return _pable.getY();%0A        %7D%0A%0A        public Rectangle getBounds ()%0A        %7B%0A            return _pable.getBounds();%0A        %7D%0A%0A        public void setLocation (int x, int y)%0A        %7B%0A            _pable.setLocation(x, y);%0A        %7D%0A%0A        public void setOrientation (int orient)%0A        %7B%0A            _pable.setOrientation(orient);%0A        %7D%0A%0A        public int getOrientation ()%0A        %7B%0A            return _pable.getOrientation();%0A        %7D%0A%0A        public void pathBeginning ()%0A        %7B%0A            _pable.pathBeginning();%0A        %7D%0A    %7D
 ;%0A%7D%0A

Use our handy dandy new DelegatingPathable.