Make AbstractMedia implement Comparable instead of using a comparator. Added

naturalOrder() which allows media to use a more consistent "same render order"
order resolution value than Object.hashCode().


git-svn-id: svn+ssh://src.earth.threerings.net/nenya/trunk@248 ed5b42cb-e716-0410-a449-f6a68f950b19
This commit is contained in:
Michael Bayne
2007-05-15 17:45:18 +00:00
parent f7dfe51a9c
commit acea29604d
4 changed files with 41 additions and 33 deletions
@@ -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<AbstractMedia>
{
/** 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<AbstractMedia>
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 <code>super.toString()</code>) to append the derived class
@@ -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<? extends AbstractMedia> createMediaList ();
protected abstract ComparableArrayList<? extends AbstractMedia> createMediaList ();
/**
* Dispatches all queued media notifications.
@@ -295,8 +295,8 @@ public abstract class AbstractMediaManager
new ArrayList<Tuple<ObserverList,ObserverOp>>();
/** Our render-order sorted list of media. */
@SuppressWarnings("unchecked") protected SortableArrayList<AbstractMedia> _media =
(SortableArrayList<AbstractMedia>)createMediaList();
@SuppressWarnings("unchecked") protected ComparableArrayList<AbstractMedia> _media =
(ComparableArrayList<AbstractMedia>)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<AbstractMedia> RENDER_ORDER =
new Comparator<AbstractMedia>() {
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();
}
};
}
@@ -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<? extends AbstractMedia> createMediaList ()
protected ComparableArrayList<? extends AbstractMedia> createMediaList ()
{
return (_anims = new SortableArrayList<Animation>());
return (_anims = new ComparableArrayList<Animation>());
}
protected SortableArrayList<Animation> _anims;
protected ComparableArrayList<Animation> _anims;
}
@@ -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<? extends AbstractMedia> createMediaList ()
protected ComparableArrayList<? extends AbstractMedia> createMediaList ()
{
return (_sprites = new SortableArrayList<Sprite>());
return (_sprites = new ComparableArrayList<Sprite>());
}
protected SortableArrayList<Sprite> _sprites;
protected ComparableArrayList<Sprite> _sprites;
}