diff --git a/src/java/com/threerings/media/AbstractMedia.java b/src/java/com/threerings/media/AbstractMedia.java index dac1bdf3..dd18fd5b 100644 --- a/src/java/com/threerings/media/AbstractMedia.java +++ b/src/java/com/threerings/media/AbstractMedia.java @@ -37,7 +37,7 @@ import com.samskivert.util.StringUtil; * Something that can be rendered on the media panel. */ public abstract class AbstractMedia - implements Shape + implements Shape, Comparable { /** A {@link #_renderOrder} value at or above which, indicates that this * media is in the HUD (heads up display) and should not scroll when the @@ -118,54 +118,62 @@ public abstract class AbstractMedia return _bounds; } - // documentation inherited from interface Shape + // from interface Shape public boolean contains (double x, double y) { return _bounds.contains(x, y); } - // documentation inherited from interface Shape + // from interface Shape public boolean contains (Point2D p) { return _bounds.contains(p); } - // documentation inherited from interface Shape + // from interface Shape public boolean intersects (double x, double y, double w, double h) { return _bounds.intersects(x, y, w, h); } - // documentation inherited from interface Shape + // from interface Shape public boolean intersects (Rectangle2D r) { return _bounds.intersects(r); } - // documentation inherited from interface Shape + // from interface Shape public boolean contains (double x, double y, double w, double h) { return _bounds.contains(x, y, w, h); } - // documentation inherited from interface Shape + // from interface Shape public boolean contains (Rectangle2D r) { return _bounds.contains(r); } - // documentation inherited from interface Shape + // from interface Shape public PathIterator getPathIterator (AffineTransform at) { return _bounds.getPathIterator(at); } - // documentation inherited from interface Shape + // from interface Shape public PathIterator getPathIterator (AffineTransform at, double flatness) { return _bounds.getPathIterator(at, flatness); } + // from interface Comparable + public int compareTo (AbstractMedia other) + { + int result = _renderOrder - other._renderOrder; + // if render order is same, use their natural order + return (result != 0) ? result : naturalOrder() - other.naturalOrder(); + } + /** * Sets the render order associated with this media. Media * can be rendered in two layers; those with negative render order and @@ -336,6 +344,16 @@ public abstract class AbstractMedia } } + /** + * Returns a value used to "naturally" order media within the same render order layer. The + * default behavior, for legacy reasons is {@link #hashCode} which is not consistent across + * sessions. + */ + protected int naturalOrder () + { + return hashCode(); + } + /** * This should be overridden by derived classes (which should be sure * to call super.toString()) to append the derived class diff --git a/src/java/com/threerings/media/AbstractMediaManager.java b/src/java/com/threerings/media/AbstractMediaManager.java index 32fd8865..366eec9c 100644 --- a/src/java/com/threerings/media/AbstractMediaManager.java +++ b/src/java/com/threerings/media/AbstractMediaManager.java @@ -27,9 +27,9 @@ import java.awt.Shape; import java.util.ArrayList; import java.util.Comparator; +import com.samskivert.util.ComparableArrayList; import com.samskivert.util.ObserverList.ObserverOp; import com.samskivert.util.ObserverList; -import com.samskivert.util.SortableArrayList; import com.samskivert.util.Tuple; /** @@ -166,7 +166,7 @@ public abstract class AbstractMediaManager } _media.remove(media); - _media.insertSorted(media, RENDER_ORDER); + _media.insertSorted(media); } /** @@ -195,7 +195,7 @@ public abstract class AbstractMediaManager } media.init(this); - int ipos = _media.insertSorted(media, RENDER_ORDER); + int ipos = _media.insertSorted(media); // if we've started our tick but have not yet painted our media, we need to take care that // this newly added media will be ticked before our upcoming render @@ -270,7 +270,7 @@ public abstract class AbstractMediaManager } /** Type safety jockeying. */ - protected abstract SortableArrayList createMediaList (); + protected abstract ComparableArrayList createMediaList (); /** * Dispatches all queued media notifications. @@ -295,8 +295,8 @@ public abstract class AbstractMediaManager new ArrayList>(); /** Our render-order sorted list of media. */ - @SuppressWarnings("unchecked") protected SortableArrayList _media = - (SortableArrayList)createMediaList(); + @SuppressWarnings("unchecked") protected ComparableArrayList _media = + (ComparableArrayList)createMediaList(); /** The position in our media list that we're ticking (while in the middle of a call to {@link * #tick}) otherwise -1. */ @@ -304,14 +304,4 @@ public abstract class AbstractMediaManager /** The tick stamp if the manager is in the midst of a call to {@link #tick}, else 0. */ protected long _tickStamp; - - /** Used to sort media by render order. */ - protected static final Comparator RENDER_ORDER = - new Comparator() { - public int compare (AbstractMedia am1, AbstractMedia am2) { - int result = am1._renderOrder - am2._renderOrder; - // if render order is same, use hashcode to keep them stable relative to each other - return (result != 0) ? result : am1.hashCode() - am2.hashCode(); - } - }; } diff --git a/src/java/com/threerings/media/animation/AnimationManager.java b/src/java/com/threerings/media/animation/AnimationManager.java index be15a8f7..d8c40ff0 100644 --- a/src/java/com/threerings/media/animation/AnimationManager.java +++ b/src/java/com/threerings/media/animation/AnimationManager.java @@ -21,7 +21,7 @@ package com.threerings.media.animation; -import com.samskivert.util.SortableArrayList; +import com.samskivert.util.ComparableArrayList; import com.threerings.media.AbstractMedia; import com.threerings.media.AbstractMediaManager; @@ -71,10 +71,10 @@ public class AnimationManager extends AbstractMediaManager } @Override // from AbstractMediaManager - protected SortableArrayList createMediaList () + protected ComparableArrayList createMediaList () { - return (_anims = new SortableArrayList()); + return (_anims = new ComparableArrayList()); } - protected SortableArrayList _anims; + protected ComparableArrayList _anims; } diff --git a/src/java/com/threerings/media/sprite/SpriteManager.java b/src/java/com/threerings/media/sprite/SpriteManager.java index 2acb80f9..e8ac9788 100644 --- a/src/java/com/threerings/media/sprite/SpriteManager.java +++ b/src/java/com/threerings/media/sprite/SpriteManager.java @@ -29,7 +29,7 @@ import java.util.List; import java.util.Iterator; import com.samskivert.util.Predicate; -import com.samskivert.util.SortableArrayList; +import com.samskivert.util.ComparableArrayList; import com.threerings.media.AbstractMedia; import com.threerings.media.AbstractMediaManager; @@ -248,10 +248,10 @@ public class SpriteManager extends AbstractMediaManager // } @Override // from AbstractMediaManager - protected SortableArrayList createMediaList () + protected ComparableArrayList createMediaList () { - return (_sprites = new SortableArrayList()); + return (_sprites = new ComparableArrayList()); } - protected SortableArrayList _sprites; + protected ComparableArrayList _sprites; }