Class KeyBufferer

java.lang.Object
java.awt.event.MouseAdapter
VASSAL.build.module.map.KeyBufferer
All Implemented Interfaces:
MouseListener, MouseMotionListener, MouseWheelListener, EventListener, Buildable, Drawable

public class KeyBufferer extends MouseAdapter implements Buildable, MouseMotionListener, Drawable
Selects and unselects pieces on the map, using the mouse.

The KeyBufferer listens for mouse clicks and drags on its parent Map, determines whether the user is performing a "lasso"/"band-select" or just clicking, draws the selection rectangle where appropriate, and at the end of each relevant mouse action updates the KeyBuffer to maintain the list of which pieces are currently "Selected" in the UI for the parent map.

Its rather misleading name derives from the fact that the selected units are kept in a KeyBuffer, with the idea being that if the player now presses a key, any resulting key command would be applied to all the selected pieces, even though the actual tracking of keys getting pressed and key commands getting applied all happens elsewhere, i.e. in the KeyBuffer class). A more appropriate name might have been e.g. "MouseSelection".

If the user clicks on a GamePiece, that piece is added to the KeyBuffer. draw(Graphics, Map) is responsible for drawing the mouse selection rectangle, and mouseDragged(MouseEvent) is responsible for triggering repaint events as the selection rectangle is moved.

If you are looking for the drag-and-drop handler for dragging pieces on or between maps, instead see PieceMover.

See Also:
  • Field Details

    • map

      protected Map map
    • selection

      protected Rectangle selection
    • anchor

      protected Point anchor
    • color

      protected Color color
    • thickness

      protected int thickness
    • bandSelectPiece

      protected GamePiece bandSelectPiece
    • isLasso

      protected boolean isLasso
  • Constructor Details

    • KeyBufferer

      public KeyBufferer()
  • Method Details

    • addTo

      public void addTo(Buildable b)
      Adds us to our parent map - we register as a mouse listener and a drawable component
      Specified by:
      addTo in interface Buildable
      Parameters:
      b - Parent map
    • dragStarted

      public void dragStarted()
      If a drag operation begins, we clear any click-specific information
    • add

      public void add(Buildable b)
      Description copied from interface: Buildable
      Adds a child component. Both this method and Buildable.addTo(VASSAL.build.Buildable) are invoked when adding a child to a parent
      Specified by:
      add in interface Buildable
      Parameters:
      b - Child to attach to this buildable as a subcomponent
    • getBuildElement

      public Element getBuildElement(Document doc)
      Specified by:
      getBuildElement in interface Buildable
      Parameters:
      doc - XML file
      Returns:
      an XML element from which this component can be built
    • build

      public void build(Element e)
      Description copied from interface: Buildable
      Build the object from the XML representation of the Buildable
      Specified by:
      build in interface Buildable
      Parameters:
      e - the XML element containing the object data
    • isLasso

      public boolean isLasso()
      Returns:
      true if we've got a valid "selection lasso" drag operation going on (must have breached dragThreshold)
    • mousePressed

      public void mousePressed(MouseEvent e)
      Process mouse-pressed events. Depending on what's present where the button became pressed (i.e. movable piece? immovable piece? nothing?) we will determine whether this mouse event will be eligible to be a band-select action.
      Specified by:
      mousePressed in interface MouseListener
      Overrides:
      mousePressed in class MouseAdapter
      Parameters:
      e - Mouse Event
    • mouseReleased

      public void mouseReleased(MouseEvent e)
      When the mouse button is released while doing a band-select, we handle updating the selection based on the final size of the selection box.
      Specified by:
      mouseReleased in interface MouseListener
      Overrides:
      mouseReleased in class MouseAdapter
      Parameters:
      e - Mouse event at point of release
    • createDragSelector

      protected PieceVisitorDispatcher createDragSelector(boolean selecting, boolean altDown, Rectangle mapsel)
      This PieceVisitorDispatcher determines what to do with pieces on the map when the player finished dragging a rectangle to select pieces.
      Returns:
      the "DeckVisitor" used to process each piece.
    • repaintSelectionRect

      protected void repaintSelectionRect()
      Initiates a repaint on the selection rectangle
    • mouseDragged

      public void mouseDragged(MouseEvent e)
      If the mouse has dragged WHILE a band-select is in progress, sets the new location of the selection rectangle.
      Specified by:
      mouseDragged in interface MouseMotionListener
      Overrides:
      mouseDragged in class MouseAdapter
    • mouseMoved

      public void mouseMoved(MouseEvent e)
      Specified by:
      mouseMoved in interface MouseMotionListener
      Overrides:
      mouseMoved in class MouseAdapter
    • draw

      public void draw(Graphics g, Map map)
      Draws our current selection box
      Specified by:
      draw in interface Drawable
      Parameters:
      g - graphics object
      map - map
    • drawAboveCounters

      public boolean drawAboveCounters()
      Selection box is always drawn "above counters'
      Specified by:
      drawAboveCounters in interface Drawable
      Returns:
      true
    • isMandatory

      public boolean isMandatory()
      Description copied from interface: Buildable
      Is this component a reqired component within its parent?
      Specified by:
      isMandatory in interface Buildable
      Returns:
      true if component is mandatory
    • isUnique

      public boolean isUnique()
      Description copied from interface: Buildable
      Does this component need to be unique within it's parent?
      Specified by:
      isUnique in interface Buildable
      Returns: