Package VASSAL.counters
Class Stack
java.lang.Object
VASSAL.search.AbstractImageFinder
VASSAL.counters.Stack
- All Implemented Interfaces:
PropertySource,GamePiece,StateMergeable,ImageSearchTarget
- Direct Known Subclasses:
Deck
A Stack is a collection of pieces in the same location that can often be moved with a single drag-and-drop.
Because it implements the
A standard Stack will only contain pieces that are "stackable" (i.e. doesn't have a "Does Not Stack"
GamePiece interface, a Stack is formally a kind of GamePiece itself, which
can lead to confusion when the terms pieces, GamePieces, etc are used loosely/interchangeably. The kind of "pieces"
a Stack contains are the "regular" kind of pieces that have a BasicPiece plus an optional group of
Decorator traits.
A standard Stack will only contain pieces that are "stackable" (i.e. doesn't have a "Does Not Stack"
Immobilized
trait, so that Properties.NO_STACK is false) and share the same X/Y position on the same Map, and all
stackable pieces on a Map will always be part of some Stack -- even single stackable pieces will have
a Stack created to contain them. Stacks should contain only pieces from the same visual layer (see
LayeredPieceCollection), but presently bad behaviors can still develop (e.g. a piece
uses a Dynamic Property to control its Game Piece Layer, and ends up changing layers without the Stack noticing) --
ideally we should straighten that out in future versions.
Deck is a further extension of Stack which utilizes the piece-grouping code but overrides other aspects
(e.g. can allow pieces regardless of their "stackability", has different drag-and-drop behavior, etc) to create
a different kind of grouping.-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidAdds a piece to the stack.voidSeeAbstractImageFinderTells each of the pieces in the stack to add its images to the collectionasList()A list of the pieces in the stack.bottomPiece(String playerId)voidStackMetricsencapsulate information on how to draw expanded/unexpanded views of stacks.getId()Each GamePiece must have a unique String identifierintgetLayer()And the translated name for this piecegetLocalizedProperty(Object key)Stacks themselves do not have any properties, so always return null.getMap()intReturn the number of pieces that could possible be drawn in the stack, regardless of visibility to any particular playergetName()The plain English name for this piecegetName(boolean localized)Stacks cannot contain other stacks/decks, nor be contained in them, so parent is always null.Finds the piece "above" the one providedgetPieceAt(int index)Finds the piece "underneath" the one providedintDeprecated, for removal: This API element is subject to removal in a future version.Deprecated, for removal: This API element is subject to removal in a future version.Deprecated, for removal: This API element is subject to removal in a future version.Returns pieces in the order in which they are visible to the player -- topmost first In other words, selected pieces first, then unselected pieces from the top to the bottom.getProperty(Object key)Stacks themselves do not have any properties, so always return null.getShape()StackMetricsencapsulate information on how to draw expanded/unexpanded views of stacks.getStackMetrics(Map m)StackMetricsencapsulate information on how to draw expanded/unexpanded views of stacks.getState()Encodes the game state information of the stack into a stringgetType()intFinds the index of a piece in the stackvoidInserts a child GamePiece at a given index.voidinsertChild(GamePiece child, int index)Adds a GamePiece to this Stack.protected voidinsertPieceAt(GamePiece p, int index)Insert a piece at a particular point in the stackbooleanProcesses a key command for this stack, by sending it to the top visible piece in the stack.voidmergeState(String newState, String oldState)Compute the difference betweennewStateandoldStateand apply that difference to the current stateprotected intnVisible()pieceAdded(GamePiece p)Perform some action on a GamePiece that has just been added to this StackPerform some action on a GamePiece that has just been removed this StackvoidvoidMarks the stack as emptyprotected voidremovePieceAt(int index)voidselectNext(GamePiece c)Finds and selects (in the UI) the next piece in the stack after this onestatic voidStackMetricsencapsulate information on how to draw expanded/unexpanded views of stacks.voidsetExpanded(boolean b)Sets the expansion state of the stack.voidEach GamePiece must have a unique String identifiervoidvoidRequired for interface but won't be needed for stacksvoidsetPosition(Point p)voidsetProperty(Object key, Object val)Stacks themselves ignore property sets -- usesetPropertyOnContents(java.lang.Object,java.lang.Object)to apply a property to the members of a stack.voidsetPropertyOnContents(Object key, Object val)Calls setProperty() on each piece in this stackvoidDecodes the game state information of the stack from a stringtopPiece()CAUTION: returns the top VISIBLE piece in the stack, or null if none is visible.toString()Methods inherited from class VASSAL.search.AbstractImageFinder
addLocalImageNames, getAllImageNames, getLocalImageNames
-
Field Details
-
TYPE
- See Also:
- Constant Field Values
-
HAS_LAYER_MARKER
- See Also:
- Constant Field Values
-
LAYER_NOT_SET
public static final int LAYER_NOT_SET- See Also:
- Constant Field Values
-
INCR
protected static final int INCR- See Also:
- Constant Field Values
-
contents
-
pieceCount
protected int pieceCount -
map
-
pos
-
layer
protected int layer
-
-
Constructor Details
-
Stack
public Stack() -
Stack
Creates a Stack to contain a specific stackable piece.- Parameters:
p- piece to make a stack for
-
-
Method Details
-
getPiecesIterator
-
getPieces
Deprecated, for removal: This API element is subject to removal in a future version.useasList()- Returns:
- an Enumeration of the pieces in the stack, from the bottom up This is a clone of the contents so add/remove operations during read won't affect it.
-
asList
A list of the pieces in the stack. -
getPiecesReverseIterator
-
getPiecesInReverseOrder
@Deprecated(since="2020-08-06", forRemoval=true) public Enumeration<GamePiece> getPiecesInReverseOrder()Deprecated, for removal: This API element is subject to removal in a future version.Return an enumeration of the pieces in the start, from the top down- Returns:
- Reverse order Enumerator
-
getPiecesInVisibleOrderIterator
Returns pieces in the order in which they are visible to the player -- topmost first In other words, selected pieces first, then unselected pieces from the top to the bottom.- Returns:
- iterator
-
getPiecesInVisibleOrder
@Deprecated(since="2020-08-06", forRemoval=true) public Enumeration<GamePiece> getPiecesInVisibleOrder()Deprecated, for removal: This API element is subject to removal in a future version.Returns pieces in the order in which they are visible to the player -- topmost first In other words, selected pieces first, then unselected pieces from the top to the bottom. -
getLayer
public int getLayer()- Returns:
- the visual layer we're bound to, or LAYER_NOT_SET if it we haven't been bound yet. Keeps the stack oriented to its correct Game Piece Layer - see
LayeredPieceCollection
-
remove
- Parameters:
p- Piece to remove from the stack
-
removePieceAt
protected void removePieceAt(int index)- Parameters:
index- Index of piece to remove from the stack
-
pieceRemoved
Perform some action on a GamePiece that has just been removed this Stack- Parameters:
p- GamePiece- Returns:
- a
Commandthat performs the equivalent action when executed
-
insertPieceAt
Insert a piece at a particular point in the stack- Parameters:
p- piece to insertindex- place to insert it
-
removeAll
public void removeAll()Marks the stack as empty -
indexOf
Finds the index of a piece in the stack- Parameters:
p- Piece to locate- Returns:
- The index of the piece, or -1 if it is not present in the stack
-
getPieceAt
- Parameters:
index- Index in the stack- Returns:
- the piece at the specified index
-
add
Adds a piece to the stack. If the piece already exists in the stack, moves it to the top- Parameters:
c- Stack to add piece to
-
insertChild
Adds a GamePiece to this Stack. Slightly more efficient thaninsert(VASSAL.counters.GamePiece, int)because it assumes the piece does not already belong to this Stack.- Parameters:
child- GamePiece to insertindex- Insert Index
-
getPieceCount
public int getPieceCount()- Returns:
- the number of pieces in the stack
-
getMaximumVisiblePieceCount
public int getMaximumVisiblePieceCount()Return the number of pieces that could possible be drawn in the stack, regardless of visibility to any particular player- Returns:
- Piece Count
-
insert
Inserts a child GamePiece at a given index. If the child piece already belongs to this Stack, it will be repositioned to the given index.- Parameters:
p- GamePiece to insertpos- Insert position
-
pieceAdded
Perform some action on a GamePiece that has just been added to this Stack- Parameters:
p- Game Piece- Returns:
- a
Commandthat performs the equivalent action when executed
-
draw
If theobsparameter is aMap, delegate drawing of this Stack to theStackMetricsof that Map. Ifobsis not a Map, use the default StackMetrics- Specified by:
drawin interfaceGamePiece- Parameters:
g- target Graphics objectx- x-location of the center of the piecey- y-location of the center of the pieceobs- the Component on which this piece is being drawnzoom- the scaling factor.- See Also:
StackMetrics.draw(VASSAL.counters.Stack, java.awt.Graphics, int, int, java.awt.Component, double),getDefaultMetrics()
-
getName
- Parameters:
localized- if true, use the localized names- Returns:
- a comma-separated list of the names of the pieces in this Stack
-
getName
Description copied from interface:GamePieceThe plain English name for this piece -
getLocalizedName
Description copied from interface:GamePieceAnd the translated name for this piece- Specified by:
getLocalizedNamein interfaceGamePiece- Returns:
- a comma-separated list of the (localized) names of the pieces in this Stack
-
boundingBox
- Specified by:
boundingBoxin interfaceGamePiece- Returns:
- bounding box for the stack (minimum rectangle to contain the bounding boxes of all the pieces inside)
-
getShape
-
selectNext
Finds and selects (in the UI) the next piece in the stack after this one- Parameters:
c- Starting piece
-
getPieceBeneath
Finds the piece "underneath" the one provided- Parameters:
c- Starting piece- Returns:
- piece underneath it, or null if none.
-
getPieceAbove
Finds the piece "above" the one provided- Parameters:
c- Starting piece- Returns:
- piece above it, or null if none.
-
topPiece
CAUTION: returns the top VISIBLE piece in the stack, or null if none is visible.- Returns:
- the top visible piece in this stack
-
topPiece
- Parameters:
playerId- Player Id to check- Returns:
- the top piece in this stack that is visible to the player with the given id
- See Also:
GameModule.getUserId()
-
bottomPiece
- Parameters:
playerId- Player Id to Check- Returns:
- the bottom piece in this stack that is visible to the player with the given id
- See Also:
GameModule.getUserId()
-
bottomPiece
- Returns:
- the bottom visible piece in this stack
-
nVisible
protected int nVisible()- Returns:
- Number of GamePieces that are visible to me
-
keyEvent
Processes a key command for this stack, by sending it to the top visible piece in the stack.- Specified by:
keyEventin interfaceGamePiece- Parameters:
stroke- keystroke to process- Returns:
- Command encapsulating anything that happened as a result
- See Also:
ForwardToKeyBuffer
-
isExpanded
public boolean isExpanded()- Returns:
- true if stack has been visually expanded by the player
-
setExpanded
public void setExpanded(boolean b)Sets the expansion state of the stack. Players can expand (and un-expand) stacks by e.g. double-clicking on them. Expanded stacks are generally shown with the pieces drawn further apart, the better to see the individual pieces. When a stack is expanded, drag-and-drop operations can affect an individual piece rather than only the whole group.- Parameters:
b- true if stack should be expanded, false if not
-
getState
Encodes the game state information of the stack into a string -
setState
Decodes the game state information of the stack from a string -
mergeState
Compute the difference betweennewStateandoldStateand apply that difference to the current state- Specified by:
mergeStatein interfaceStateMergeable- Parameters:
newState- New StateoldState- Old State
-
getType
- Specified by:
getTypein interfaceGamePiece- Returns:
- the encoding type
- See Also:
BasicCommandEncoder
-
setProperty
Stacks themselves ignore property sets -- usesetPropertyOnContents(java.lang.Object,java.lang.Object)to apply a property to the members of a stack.- Specified by:
setPropertyin interfaceGamePiece- Parameters:
key- String key of property to be changedval- Object containing new value of the property
-
toString
-
setPropertyOnContents
Calls setProperty() on each piece in this stack- Parameters:
key- Property Keyval- Property Value
-
getProperty
Stacks themselves do not have any properties, so always return null.- Specified by:
getPropertyin interfaceGamePiece- Specified by:
getPropertyin interfacePropertySource- Parameters:
key- String key of property to be returned- Returns:
- always null
-
getLocalizedProperty
Stacks themselves do not have any properties, so always return null.- Specified by:
getLocalizedPropertyin interfacePropertySource- Parameters:
key- String key of property to be returned- Returns:
- always null
-
setMap
-
getMap
-
getPosition
- Specified by:
getPositionin interfaceGamePiece- Returns:
- current X/Y position of stack
-
setPosition
- Specified by:
setPositionin interfaceGamePiece- Parameters:
p- Sets the location of this stack on itsMap
-
getParent
Stacks cannot contain other stacks/decks, nor be contained in them, so parent is always null. -
setParent
Required for interface but won't be needed for stacks -
getId
Description copied from interface:GamePieceEach GamePiece must have a unique String identifier- Specified by:
getIdin interfaceGamePiece- Returns:
- stack's unique ID
- See Also:
GameState.getNewPieceId()
-
setId
Description copied from interface:GamePieceEach GamePiece must have a unique String identifier- Specified by:
setIdin interfaceGamePiece- Parameters:
id- sets unique ID for this piece- See Also:
GameState.getNewPieceId()
-
setDefaultMetrics
StackMetricsencapsulate information on how to draw expanded/unexpanded views of stacks. This method sets the default metrics for the module, but each map can have its own configuration, which can be found in the [Stacking options] subcomponent of the Map in the Editor.- Parameters:
s- default stack metrics for the module
-
getStackMetrics
StackMetricsencapsulate information on how to draw expanded/unexpanded views of stacks. This method retrieves the appropriate stack metrics to use for a given map- Parameters:
m- a map- Returns:
- stack metrics for the map, if provided, or the default one for the module.
-
getStackMetrics
StackMetricsencapsulate information on how to draw expanded/unexpanded views of stacks. This method retrieves the appropriate stack metrics to use the stack, based on its map- Returns:
- stack metrics for the map, if provided, or the default one for the module.
-
getDefaultMetrics
StackMetricsencapsulate information on how to draw expanded/unexpanded views of stacks. This method retrieves the default stack metrics for the module.- Returns:
- default stack metrics for the module
-
addImageNamesRecursively
SeeAbstractImageFinderTells each of the pieces in the stack to add its images to the collection- Specified by:
addImageNamesRecursivelyin interfaceImageSearchTarget- Overrides:
addImageNamesRecursivelyin classAbstractImageFinder- Parameters:
s- Collection to add image names to
-
asList()