Class Stack

java.lang.Object
VASSAL.counters.Stack
All Implemented Interfaces:
PropertySource, GamePiece, StateMergeable
Direct Known Subclasses:
Deck

public class Stack
extends Object
implements GamePiece, StateMergeable
A collection of GamePieces which can be moved as a single unit
  • Field Details

  • Constructor Details

    • Stack

      public Stack()
    • Stack

      public Stack​(GamePiece p)
  • Method Details

    • getPiecesIterator

      public Iterator<GamePiece> getPiecesIterator()
    • getPieces

      @Deprecated(since="2020-08-06", forRemoval=true) public Enumeration<GamePiece> getPieces()
      Deprecated, for removal: This API element is subject to removal in a future version.
      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

      public List<GamePiece> asList()
      Returns:
      an unmodifiable List which is a defensive copy of GamePieces contained in this Stack
    • getPiecesReverseIterator

      public Iterator<GamePiece> 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

      public Iterator<GamePiece> getPiecesInVisibleOrderIterator()
    • 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 layer we're bound to, or LAYER_NOT_SET if it we haven't been bound yet
    • remove

      public void remove​(GamePiece p)
    • removePieceAt

      protected void removePieceAt​(int index)
    • pieceRemoved

      public Command pieceRemoved​(GamePiece p)
      Perform some action on a GamePiece that has just been removed this Stack
      Parameters:
      p - GamePiece
      Returns:
      a Command that performs the equivalent action when executed
    • insertPieceAt

      protected void insertPieceAt​(GamePiece p, int index)
    • removeAll

      public void removeAll()
    • indexOf

      public int indexOf​(GamePiece p)
    • getPieceAt

      public GamePiece getPieceAt​(int index)
    • add

      public void add​(GamePiece c)
      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

      public void insertChild​(GamePiece child, int index)
      Adds a GamePiece to this Stack. Slightly more efficient than insert(VASSAL.counters.GamePiece, int) because it assumes the piece does not already belong to this Stack.
      Parameters:
      child - GamePiece to insert
      index - Insert Index
    • getPieceCount

      public int getPieceCount()
    • 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

      public void insert​(GamePiece p, int pos)
      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 insert
      pos - Insert position
    • pieceAdded

      public Command pieceAdded​(GamePiece p)
      Perform some action on a GamePiece that has just been added to this Stack
      Parameters:
      p - Game Piece
      Returns:
      a Command that performs the equivalent action when executed
    • draw

      public void draw​(Graphics g, int x, int y, Component obs, double zoom)
      If the obs parameter is a Map, delegate drawing of this Stack to the StackMetrics of that Map. If obs is not a Map, use the default StackMetrics
      Specified by:
      draw in interface GamePiece
      x - x-location of the center of the piece
      y - y-location of the center of the piece
      obs - the Component on which this piece is being drawn
      zoom - the scaling factor.
      See Also:
      StackMetrics.draw(VASSAL.counters.Stack, java.awt.Graphics, int, int, java.awt.Component, double), getDefaultMetrics()
    • getName

      public String getName​(boolean localized)
      Return a comma-separated list of the names of the pieces in this Stack
    • getName

      public String getName()
      Description copied from interface: GamePiece
      The plain English name for this piece
      Specified by:
      getName in interface GamePiece
    • getLocalizedName

      public String getLocalizedName()
      Description copied from interface: GamePiece
      And the translated name for this piece
      Specified by:
      getLocalizedName in interface GamePiece
    • boundingBox

      public Rectangle boundingBox()
      Description copied from interface: GamePiece
      The area which this GamePiece occupies when drawn at the point (0,0)
      Specified by:
      boundingBox in interface GamePiece
    • getShape

      public Shape getShape()
      Description copied from interface: GamePiece
      The shape of the piece from the user's viewpoint. This defines the area in which the user must click to select or move the piece, for example. Like GamePiece.boundingBox(), it assumes the position is (0,0) and must be translated to the actual location where the piece is being drawn.
      Specified by:
      getShape in interface GamePiece
    • selectNext

      public void selectNext​(GamePiece c)
    • getPieceBeneath

      public GamePiece getPieceBeneath​(GamePiece p)
    • getPieceAbove

      public GamePiece getPieceAbove​(GamePiece p)
    • topPiece

      public GamePiece topPiece()
      Returns:
      the top visible piece in this stack
    • topPiece

      public GamePiece topPiece​(String playerId)
      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

      public GamePiece bottomPiece​(String playerId)
      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

      public GamePiece bottomPiece()
      Returns:
      the bottom visible piece in this stack
    • nVisible

      protected int nVisible()
      Returns:
      Number of GamePieces that are visible to me
    • keyEvent

      public Command keyEvent​(KeyStroke stroke)
      Description copied from interface: GamePiece
      Keyboard events are forward to this method when a piece is selected The GamePiece can respond in any way it likes
      Specified by:
      keyEvent in interface GamePiece
      Returns:
      a Command that, when executed, will invoke the same response. Usually a ChangePiece command.
      See Also:
      ForwardToKeyBuffer
    • isExpanded

      public boolean isExpanded()
    • setExpanded

      public void setExpanded​(boolean b)
    • getState

      public String getState()
      Description copied from interface: GamePiece
      The state information is information that can change during the course of a game. State information is saved when the game is saved and is transferred between players on the server. For example, the relative order of pieces in a stack is state information, but whether the stack is expanded is not
      Specified by:
      getState in interface GamePiece
    • setState

      public void setState​(String s)
      Specified by:
      setState in interface GamePiece
    • mergeState

      public void mergeState​(String newState, String oldState)
      Compute the difference between newState and oldState and appy that difference to the current state
      Specified by:
      mergeState in interface StateMergeable
      Parameters:
      newState - New State
      oldState - Old State
    • getType

      public String getType()
      Description copied from interface: GamePiece
      The type information is information that does not change during the course of a game. Image file names, popup menu command names, etc., all should be reflected in the type.
      Specified by:
      getType in interface GamePiece
      See Also:
      BasicCommandEncoder
    • setProperty

      public void setProperty​(Object key, Object val)
      Description copied from interface: GamePiece
      Other properties, possibly game-specific, can be associated with a piece. The properties may or may not need to be encoded in the piece's GamePiece.getState() method.
      Specified by:
      setProperty in interface GamePiece
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • setPropertyOnContents

      public void setPropertyOnContents​(Object key, Object val)
      Calls setProperty() on each piece in this stack
      Parameters:
      key - Property Key
      val - Property Value
    • getProperty

      public Object getProperty​(Object key)
      Specified by:
      getProperty in interface GamePiece
      Specified by:
      getProperty in interface PropertySource
    • getLocalizedProperty

      public Object getLocalizedProperty​(Object key)
      Specified by:
      getLocalizedProperty in interface PropertySource
    • setMap

      public void setMap​(Map map)
      Description copied from interface: GamePiece
      Each GamePiece belongs to a single Map
      Specified by:
      setMap in interface GamePiece
    • getMap

      public Map getMap()
      Specified by:
      getMap in interface GamePiece
    • getPosition

      public Point getPosition()
      Specified by:
      getPosition in interface GamePiece
      Returns:
      the location of this piece on its owning Map
    • setPosition

      public void setPosition​(Point p)
      Specified by:
      setPosition in interface GamePiece
    • getParent

      public Stack getParent()
      Specified by:
      getParent in interface GamePiece
      Returns:
      the Stack to which this piece belongs, or null if it doesn't belong to a stack.
    • setParent

      public void setParent​(Stack s)
      Specified by:
      setParent in interface GamePiece
    • getId

      public String getId()
      Description copied from interface: GamePiece
      Each GamePiece must have a unique String identifier
      Specified by:
      getId in interface GamePiece
      See Also:
      GameState.getNewPieceId()
    • setId

      public void setId​(String id)
      Specified by:
      setId in interface GamePiece
    • setDefaultMetrics

      public static void setDefaultMetrics​(StackMetrics s)
    • getStackMetrics

      public StackMetrics getStackMetrics​(Map m)
    • getStackMetrics

      public StackMetrics getStackMetrics()
    • getDefaultMetrics

      public StackMetrics getDefaultMetrics()