- Use samskivert Predicate.

- Type safety.


git-svn-id: svn+ssh://src.earth.threerings.net/vilya/trunk@65 c613c5cb-e716-0410-b11b-feb51c14d237
This commit is contained in:
Ray Greenwell
2006-08-28 18:53:03 +00:00
parent 37b60db7a7
commit 4b9ec23221
2 changed files with 31 additions and 41 deletions
@@ -33,6 +33,7 @@ import java.util.Iterator;
import javax.swing.event.MouseInputAdapter; import javax.swing.event.MouseInputAdapter;
import com.samskivert.util.ObserverList; import com.samskivert.util.ObserverList;
import com.samskivert.util.Predicate;
import com.samskivert.util.QuickSort; import com.samskivert.util.QuickSort;
import com.threerings.media.FrameManager; import com.threerings.media.FrameManager;
@@ -67,18 +68,6 @@ public abstract class CardPanel extends VirtualMediaPanel
/** The selection mode in which the user can select multiple cards. */ /** The selection mode in which the user can select multiple cards. */
public static final int MULTIPLE = 2; public static final int MULTIPLE = 2;
/**
* A predicate class for {@link CardSprite}s. Provides control over which
* cards are selectable, playable, etc.
*/
public static interface CardSpritePredicate
{
/**
* Evaluates the specified sprite.
*/
public boolean evaluate (CardSprite sprite);
}
/** /**
* A listener for card selection/deselection. * A listener for card selection/deselection.
*/ */
@@ -182,7 +171,7 @@ public abstract class CardPanel extends VirtualMediaPanel
* may be selected (if null, all cards may be selected). Changing the * may be selected (if null, all cards may be selected). Changing the
* predicate does not change the current selection. * predicate does not change the current selection.
*/ */
public void setHandSelectionPredicate (CardSpritePredicate pred) public void setHandSelectionPredicate (Predicate<CardSprite> pred)
{ {
_handSelectionPredicate = pred; _handSelectionPredicate = pred;
@@ -197,7 +186,7 @@ public abstract class CardPanel extends VirtualMediaPanel
public CardSprite getSelectedHandSprite () public CardSprite getSelectedHandSprite ()
{ {
return _selectedHandSprites.size() == 0 ? return _selectedHandSprites.size() == 0 ?
null : (CardSprite)_selectedHandSprites.get(0); null : _selectedHandSprites.get(0);
} }
/** /**
@@ -206,7 +195,7 @@ public abstract class CardPanel extends VirtualMediaPanel
*/ */
public CardSprite[] getSelectedHandSprites () public CardSprite[] getSelectedHandSprites ()
{ {
return (CardSprite[])_selectedHandSprites.toArray( return _selectedHandSprites.toArray(
new CardSprite[_selectedHandSprites.size()]); new CardSprite[_selectedHandSprites.size()]);
} }
@@ -234,9 +223,10 @@ public abstract class CardPanel extends VirtualMediaPanel
sprite.setLocation(sprite.getX(), getHandY(sprite)); sprite.setLocation(sprite.getX(), getHandY(sprite));
// notify the observers // notify the observers
ObserverList.ObserverOp op = new ObserverList.ObserverOp() { ObserverList.ObserverOp<CardSelectionObserver> op =
public boolean apply (Object obs) { new ObserverList.ObserverOp<CardSelectionObserver>() {
((CardSelectionObserver)obs).cardSpriteSelected(sprite); public boolean apply (CardSelectionObserver obs) {
obs.cardSpriteSelected(sprite);
return true; return true;
} }
}; };
@@ -258,9 +248,10 @@ public abstract class CardPanel extends VirtualMediaPanel
sprite.setLocation(sprite.getX(), getHandY(sprite)); sprite.setLocation(sprite.getX(), getHandY(sprite));
// notify the observers // notify the observers
ObserverList.ObserverOp op = new ObserverList.ObserverOp() { ObserverList.ObserverOp<CardSelectionObserver> op =
public boolean apply (Object obs) { new ObserverList.ObserverOp<CardSelectionObserver>() {
((CardSelectionObserver)obs).cardSpriteDeselected(sprite); public boolean apply (CardSelectionObserver obs) {
obs.cardSpriteDeselected(sprite);
return true; return true;
} }
}; };
@@ -322,7 +313,7 @@ public abstract class CardPanel extends VirtualMediaPanel
// fade them in at proper locations and layers // fade them in at proper locations and layers
long cardDuration = fadeDuration / size; long cardDuration = fadeDuration / size;
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
CardSprite cs = (CardSprite)_handSprites.get(i); CardSprite cs = _handSprites.get(i);
cs.setLocation(getHandX(size, i), _handLocation.y); cs.setLocation(getHandX(size, i), _handLocation.y);
cs.setRenderOrder(i); cs.setRenderOrder(i);
cs.addSpriteObserver(_handSpriteObserver); cs.addSpriteObserver(_handSpriteObserver);
@@ -366,7 +357,7 @@ public abstract class CardPanel extends VirtualMediaPanel
// set the sprites // set the sprites
int len = Math.min(_handSprites.size(), hand.size()); int len = Math.min(_handSprites.size(), hand.size());
for (int i = 0; i < len; i++) { for (int i = 0; i < len; i++) {
CardSprite cs = (CardSprite)_handSprites.get(i); CardSprite cs = _handSprites.get(i);
cs.setCard((Card)hand.get(i)); cs.setCard((Card)hand.get(i));
} }
} }
@@ -461,7 +452,7 @@ public abstract class CardPanel extends VirtualMediaPanel
addSprite(sprites[i]); addSprite(sprites[i]);
// create a path sequence containing flight, pause, and drop // create a path sequence containing flight, pause, and drop
ArrayList paths = new ArrayList(); ArrayList<Path> paths = new ArrayList<Path>();
Point hp2 = new Point(getHandX(size, idx), _handLocation.y), Point hp2 = new Point(getHandX(size, idx), _handLocation.y),
hp1 = new Point(hp2.x, hp2.y - _selectedCardOffset); hp1 = new Point(hp2.x, hp2.y - _selectedCardOffset);
paths.add(new LinePath(hp1, flightDuration)); paths.add(new LinePath(hp1, flightDuration));
@@ -694,7 +685,7 @@ public abstract class CardPanel extends VirtualMediaPanel
// Move each card to its proper position (and, optionally, layer) // Move each card to its proper position (and, optionally, layer)
int size = _handSprites.size(); int size = _handSprites.size();
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
CardSprite cs = (CardSprite)_handSprites.get(i); CardSprite cs = _handSprites.get(i);
if (!isManaged(cs)) { if (!isManaged(cs)) {
continue; continue;
} }
@@ -727,7 +718,7 @@ public abstract class CardPanel extends VirtualMediaPanel
int size = _handSprites.size(); int size = _handSprites.size();
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
CardSprite cs = (CardSprite)_handSprites.get(i); CardSprite cs = _handSprites.get(i);
if (!cs.isMoving()) { if (!cs.isMoving()) {
cs.setLocation(cs.getX(), getHandY(cs)); cs.setLocation(cs.getX(), getHandY(cs));
} }
@@ -776,7 +767,7 @@ public abstract class CardPanel extends VirtualMediaPanel
{ {
return _handSelectionMode != NONE && return _handSelectionMode != NONE &&
(_handSelectionPredicate == null || (_handSelectionPredicate == null ||
_handSelectionPredicate.evaluate(sprite)); _handSelectionPredicate.isMatch(sprite));
} }
/** /**
@@ -792,10 +783,8 @@ public abstract class CardPanel extends VirtualMediaPanel
// otherwise, look for a sprite that fits the predicate and isn't the // otherwise, look for a sprite that fits the predicate and isn't the
// parameter // parameter
int size = _handSprites.size(); for (CardSprite cs : _handSprites) {
for (int i = 0; i < size; i++) { if (cs != sprite && _handSelectionPredicate.isMatch(cs)) {
CardSprite cs = (CardSprite)_handSprites.get(i);
if (cs != sprite && _handSelectionPredicate.evaluate(cs)) {
return false; return false;
} }
} }
@@ -817,7 +806,7 @@ public abstract class CardPanel extends VirtualMediaPanel
// lower them just enough // lower them just enough
int size = _boardSprites.size(), adjustment = layer - highest; int size = _boardSprites.size(), adjustment = layer - highest;
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
CardSprite cs = (CardSprite)_boardSprites.get(i); CardSprite cs = _boardSprites.get(i);
cs.setRenderOrder(cs.getRenderOrder() + adjustment); cs.setRenderOrder(cs.getRenderOrder() + adjustment);
} }
} }
@@ -831,8 +820,7 @@ public abstract class CardPanel extends VirtualMediaPanel
// the sprites down to (the layer of the first card in the hand) // the sprites down to (the layer of the first card in the hand)
int size = _boardSprites.size(), highest = 0; int size = _boardSprites.size(), highest = 0;
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
highest = Math.max(highest, highest = Math.max(highest, _boardSprites.get(i).getRenderOrder());
((CardSprite)_boardSprites.get(i)).getRenderOrder());
} }
return highest; return highest;
} }
@@ -894,21 +882,23 @@ public abstract class CardPanel extends VirtualMediaPanel
protected CardSprite _activeCardSprite; protected CardSprite _activeCardSprite;
/** The sprites for cards within the hand. */ /** The sprites for cards within the hand. */
protected ArrayList _handSprites = new ArrayList(); protected ArrayList<CardSprite> _handSprites = new ArrayList<CardSprite>();
/** The sprites for cards within the hand that have been selected. */ /** The sprites for cards within the hand that have been selected. */
protected ArrayList _selectedHandSprites = new ArrayList(); protected ArrayList<CardSprite> _selectedHandSprites =
new ArrayList<CardSprite>();
/** The current selection mode for the hand. */ /** The current selection mode for the hand. */
protected int _handSelectionMode; protected int _handSelectionMode;
/** The predicate that determines which cards are selectable (if null, all /** The predicate that determines which cards are selectable (if null, all
* cards are selectable). */ * cards are selectable). */
protected CardSpritePredicate _handSelectionPredicate; protected Predicate<CardSprite> _handSelectionPredicate;
/** Observers of hand card selection/deselection. */ /** Observers of hand card selection/deselection. */
protected ObserverList _handSelectionObservers = new ObserverList( protected ObserverList<CardSelectionObserver> _handSelectionObservers =
ObserverList.FAST_UNSAFE_NOTIFY); new ObserverList<CardSelectionObserver>(
ObserverList.FAST_UNSAFE_NOTIFY);
/** The location of the center of the hand's upper edge. */ /** The location of the center of the hand's upper edge. */
protected Point _handLocation = new Point(); protected Point _handLocation = new Point();
@@ -923,7 +913,7 @@ public abstract class CardPanel extends VirtualMediaPanel
protected int _selectedCardOffset; protected int _selectedCardOffset;
/** The sprites for cards on the board. */ /** The sprites for cards on the board. */
protected ArrayList _boardSprites = new ArrayList(); protected ArrayList<CardSprite> _boardSprites = new ArrayList<CardSprite>();
/** The hand sprite observer instance. */ /** The hand sprite observer instance. */
protected HandSpriteObserver _handSpriteObserver = protected HandSpriteObserver _handSpriteObserver =
@@ -26,7 +26,7 @@ import com.threerings.util.StreamableArrayList;
/** /**
* Instances of this class represent hands of cards. * Instances of this class represent hands of cards.
*/ */
public class Hand extends StreamableArrayList public class Hand extends StreamableArrayList<Card>
{ {
/** /**
* Adds all of the specified cards to this hand. * Adds all of the specified cards to this hand.