Package VASSAL.build.module.map
Class PieceMover
java.lang.Object
VASSAL.search.AbstractImageFinder
VASSAL.build.AbstractBuildable
VASSAL.build.module.map.PieceMover
- All Implemented Interfaces:
MouseListener,Comparator<GamePiece>,EventListener,Buildable,GameComponent,PropertyNameSource,ValidityChecker,ImageSearchTarget
public class PieceMover
extends AbstractBuildable
implements MouseListener, GameComponent, Comparator<GamePiece>
PieceMover handles the "Drag and Drop" of pieces and stacks, onto or within a Map window. It implements
MouseListener and handles dragging and dropping of both individual pieces, stacks, and groups of
pieces/stacks. It is a subcomponent of Map.
For the selection/deselection of pieces and band-selecting pieces by "dragging a lasso around them", see
For the selection/deselection of pieces and band-selecting pieces by "dragging a lasso around them", see
KeyBufferer.-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classCommon functionality for DragHandler for cases with and without drag image support.static classVASSAL's front-line drag handler for drag-and-drop of pieces.static classSpecial MacOSX variant of DragHandler, because of differences in how device scaling is handled.static classFallback drag-handler when DragImage not supported by JRE. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic StringThe Preferences key for auto-reporting moves.protected Pointprotected GamePieceprotected PieceFinderprotected PieceFinderstatic Stringstatic Stringprotected Stringprotected Mapprotected LaunchButtonprotected Stringprotected Stringstatic Stringprotected Comparator<GamePiece>protected PieceVisitorDispatcherFields inherited from class VASSAL.build.AbstractBuildable
buildComponents, validator -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidReports the image files we use (forremove-unwanted-image-files and/or search)voidAdds this component to its parent map.protected CommandapplyKeyAfterMove(List<GamePiece> pieces, KeyStroke key)Applies a key command to each of a list of pieces.protected voidapplyKeyAfterMove(List<GamePiece> pieces, Command comm, KeyStroke key)Deprecated, for removal: This API element is subject to removal in a future version.protected booleanChecks if event has already been consumed -- so we will return false if this event has already been handled by one of the fancier Drag Gesture Recognizers.intImplement Comparator to sort the contents of the drag buffer before completing the drag.protected PieceFinderReturns thePieceFinderinstance that will select aGamePiecefor processing when the user clicks on the map.protected PieceFinderWhen the user *completes* a drag-and-drop operation, the pieces being dragged will either be:
(1) combined in a stack with an existing piece (or stack, or deck) on the map
(2) placed alone in a brand new stack
(3) or, in the case of non-stacking pieces, placed on the map without stack.protected MovementReporterCreates aMovementReporterfor a collection of commands containing AddPiece and MovePiece commands, which will supply auto-report messages corresponding to those commandsprotected PieceVisitorDispatcherWhen the user *starts* a potential drag-and-drop operation by clicking on the map, a piece from the map is selected by the dragTargetSelector.String[]Lists all the buildFile (XML) attribute names for this component.PieceMover has nothing to save/restore in a save file.protected voidPieceMover manages the "Mark All Pieces Unmoved" button for the map.booleanprotected booleanHandles marking pieces as "moved" or "not moved", based on Global Options settings.voidvoidvoidvoidThis "deep legacy" listener is used for faking drag-and-drop on Java 1.1 systems.voidMouse button has been released -- if we can still handle the event (i.e.protected CommandmovedPiece(GamePiece p, Point loc)Invoked just BEFORE a piece is moved.movePieces(Map map, Point p)This is the key method for handling the "Drop" part of Drag and Drop.protected voidperformDrop(Point p)Moves the group of dragged (in the DragBuffer) pieces to the target point (p).protected voidWhen doing a "deep legacy" fake drag-and-drop, place the clicked-on piece into theDragBufferprotected voidselectMovablePieces(Point point)Deprecated, for removal: This API element is subject to removal in a future version.voidsetAttribute(String key, Object value)Sets a buildFile (XML) attribute value for this component.protected voidDeprecated, for removal: This API element is subject to removal in a future version.setOldLocations(GamePiece)to return generated Commandsprotected CommandPopulates the "OldLocations" properties (e.g.voidsetup(boolean gameStarting)Detects when a game is starting, for purposes of managing the mark-unmoved button.protected booleanChecks Global Options settings (and if necessary, the user preference) about whether we mark moved pieces as "moved" or not.Methods inherited from class VASSAL.build.AbstractBuildable
add, addImageNamesRecursively, build, getAllDescendantComponents, getAllDescendantComponentsOf, getBuildables, getBuildComponents, getBuildElement, getComponents, getComponentsOf, getPropertyNames, validateMethods inherited from class VASSAL.search.AbstractImageFinder
getAllImageNames, getLocalImageNamesMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface java.util.Comparator
equals, reversed, thenComparing, thenComparing, thenComparing, thenComparingDouble, thenComparingInt, thenComparingLong
-
Field Details
-
AUTO_REPORT
The Preferences key for auto-reporting moves.- See Also:
- Constant Field Values
-
NAME
- See Also:
- Constant Field Values
-
HOTKEY
- See Also:
- Constant Field Values
-
map
-
dragBegin
-
dragging
-
markUnmovedButton
-
markUnmovedText
-
markUnmovedIcon
-
ICON_NAME
- See Also:
- Constant Field Values
-
iconName
-
dragTargetSelector
-
dropTargetSelector
-
selectionProcessor
-
pieceSorter
-
-
Constructor Details
-
PieceMover
public PieceMover()
-
-
Method Details
-
addTo
Adds this component to its parent map. Add ourselves as a mouse listener, drag gesture listener, etc. -
createMovementReporter
Creates aMovementReporterfor a collection of commands containing AddPiece and MovePiece commands, which will supply auto-report messages corresponding to those commands- Parameters:
c- Command -- presumably including some AddPiece and MovePiece commands.- Returns:
- MovementReporter to auto-report the command(s)
-
addLocalImageNames
Reports the image files we use (forremove-unwanted-image-files and/or search)- Specified by:
addLocalImageNamesin interfaceImageSearchTarget- Overrides:
addLocalImageNamesin classAbstractImageFinder- Parameters:
s- Collection to add image names to
-
createDropTargetSelector
When the user *completes* a drag-and-drop operation, the pieces being dragged will either be:
(1) combined in a stack with an existing piece (or stack, or deck) on the map
(2) placed alone in a brand new stack
(3) or, in the case of non-stacking pieces, placed on the map without stack.
For each of the stacks/pieces being dragged, we need to determine which of those outcomes applies. This "drop target selector" will be fed, one by one, all of the pieces on the map, and for each one must check whether it would make a valid "merge target" for the current dragged piece under consideration (the PieceFinder'sPieceFinder.select(VASSAL.build.module.Map, VASSAL.counters.GamePiece, java.awt.Point)method provides the map and location, and the PieceMover'sdraggingfield tells the "dragged piece under consideration". Each method is to returns the target piece/stack/deck to merge with if it has been passed a valid target, or null if it is not a valid match.
The Map'sMap.findAnyPiece(Point, PieceFinder)method, which feeds this, will be responsible for iterating through the list of possible pieces in proper visual order so that we check the pieces "visually on top" first.- Returns:
- a
PieceFinderinstance that determines whichGamePiece(if any) to combine the being-dragged pieces with.
-
createSelectionProcessor
When the user *starts* a potential drag-and-drop operation by clicking on the map, a piece from the map is selected by the dragTargetSelector. What happens to that piece is determined by thePieceVisitorDispatcherinstance returned by this method. The default implementation does the following:
(1) If a Deck, add the (single) top piece to the drag buffer
(2) If a stack, add it to the drag buffer. If the stack is a member of the "currently selected pieces" (i.e.KeyBuffer), then add any other already-selected pieces and stacks to the drag buffer as well.
(3) Otherwise, add the piece and any other multi-selected pieces and stacks to the drag buffer.- Returns:
- Dispatcher
- See Also:
createDragTargetSelector()
-
createDragTargetSelector
Returns thePieceFinderinstance that will select aGamePiecefor processing when the user clicks on the map. The default implementation is to return the first piece whose shape contains the point clicked on.- Returns:
- Piece Finder
-
setup
public void setup(boolean gameStarting)Detects when a game is starting, for purposes of managing the mark-unmoved button.- Specified by:
setupin interfaceGameComponent- Parameters:
gameStarting- if true, a game is starting. If false, then a game is ending
-
getRestoreCommand
PieceMover has nothing to save/restore in a save file.- Specified by:
getRestoreCommandin interfaceGameComponent- Returns:
- null
-
initButton
protected void initButton()PieceMover manages the "Mark All Pieces Unmoved" button for the map. -
getAttributeNames
Description copied from class:AbstractBuildableLists all the buildFile (XML) attribute names for this component. If this component is ALSO anAbstractConfigurable, then this list of attributes determines the appropriate attribute order forAbstractConfigurable.getAttributeDescriptions()andAbstractConfigurable.getAttributeTypes().- Specified by:
getAttributeNamesin classAbstractBuildable- Returns:
- a list of all buildFile (XML) attribute names for this component
-
getAttributeValueString
- Specified by:
getAttributeValueStringin classAbstractBuildable- Parameters:
key- the name of the attribute. Will be one of those listed inAbstractBuildable.getAttributeNames()- Returns:
- a String representation of the XML buildFile attribute with the given name. When initializing a module,
this String value will loaded from the XML and passed to
AbstractBuildable.setAttribute(java.lang.String, java.lang.Object). It is also frequently used for checking the current value of an attribute.
-
setAttribute
Description copied from class:AbstractBuildableSets a buildFile (XML) attribute value for this component. Thekeyparameter will be one of those listed inAbstractBuildable.getAttributeNames(). If thevalueparameter is a String, it will be the value returned byAbstractBuildable.getAttributeValueString(java.lang.String)for the samekey. If the implementing class extendsAbstractConfigurable, thenvaluewill be an instance of the corresponding Class listed inAbstractConfigurable.getAttributeTypes()- Specified by:
setAttributein classAbstractBuildable- Parameters:
key- the name of the attribute. Will be one of those listed inAbstractBuildable.getAttributeNames()value- If thevalueparameter is a String, it will be the value returned byAbstractBuildable.getAttributeValueString(java.lang.String)for the samekey. If the implementing class extendsAbstractConfigurable, thenvaluecan also be an instance of the corresponding Class listed inAbstractConfigurable.getAttributeTypes()
-
isMultipleSelectionEvent
-
movedPiece
Invoked just BEFORE a piece is moved. Sets the "OldLocations" properties for the piece. Marks the piece as "moved" if it has changed positions, and removes the piece from its old stack, if any.- Returns:
- Command encapsulating anything this method did, for replay in log file or on other clients
-
setOldLocation
Deprecated, for removal: This API element is subject to removal in a future version.setOldLocations(GamePiece)to return generated Commands- Parameters:
p- Piece
-
setOldLocations
Populates the "OldLocations" properties (e.g. OldMap, OldZone, etc) for the piece (or for a stack, for all the pieces contained in it), based on their present locations, in preparation for moving them to a new location.- Parameters:
p- Piece (could be a stack)- Returns:
- Command encapsulating any changes made, for replay in log file or on other clients
-
markMoved
Handles marking pieces as "moved" or "not moved", based on Global Options settings. Updates the "moved" property of the pieces, if they have one.- Parameters:
p- Piece (could be a Stack)hasMoved- True if piece has just moved, false if it is to be reset to not-moved status- Returns:
- Command encapsulating any changes made, for replay in log file or on other clients
-
shouldMarkMoved
protected boolean shouldMarkMoved()Checks Global Options settings (and if necessary, the user preference) about whether we mark moved pieces as "moved" or not.- Returns:
- true if we should mark a moved piece as "moved", false if not.
-
movePieces
This is the key method for handling the "Drop" part of Drag and Drop.
(1) Moves each piece in theDragBufferto its proper destination, based on the mouse having been released at point "p" (if multiple pieces were being dragged after being e.g. band-selected, each individual piece's destination point will vary with the piece's offset from the anhor point that started the drag). This also involves removing each piece from any stacks/decks it was part of, and possibly removing it from its old map if it is changing maps.
(2) As each piece is moved, finds appropriate "merge targets" (pieces that should be combined with it in a stack) if they exist, or forms new stacks where needed. Adds the piece to old or new stacks as appropriate, or directly to the map if non-stacking. If the piece has been moved between maps (or onto a map for the first time), the piece is also added to the map's piece collection.
(3) If auto-reporting of moves is enabled, creates the report.
(4) Applies any apply-on-move keystroke (from the "Key command to apply to all units ending movement on this map" field of the map) to each piece, as appropriate.
(5) Returns a command to encapsulate any and all changes made, for replay in log file or on other clients- Parameters:
map- Mapp- Point mouse released- Returns:
- Command encapsulating all changes, for replay in log file or on other clients.
-
applyKeyAfterMove
@Deprecated(since="2020-08-06", forRemoval=true) protected void applyKeyAfterMove(List<GamePiece> pieces, Command comm, KeyStroke key)Deprecated, for removal: This API element is subject to removal in a future version.UseapplyKeyAfterMove(List, KeyStroke)to return Commands -
applyKeyAfterMove
Applies a key command to each of a list of pieces.- Parameters:
pieces- List of pieceskey- keystroke to apply- Returns:
- Command that encapsulates the effects of the key command applied.
-
mousePressed
This "deep legacy" listener is used for faking drag-and-drop on Java 1.1 systems. On most systems, the mouse event will be "consumed" by the Drag Gesture Recognizer, causing canHandleEvent() to return false.- Specified by:
mousePressedin interfaceMouseListener- Parameters:
e- Event
-
selectMovablePieces
When doing a "deep legacy" fake drag-and-drop, place the clicked-on piece into theDragBuffer -
selectMovablePieces
Deprecated, for removal: This API element is subject to removal in a future version. -
canHandleEvent
Checks if event has already been consumed -- so we will return false if this event has already been handled by one of the fancier Drag Gesture Recognizers. Also screens out double-clicks and anything with modifier keys down.- Parameters:
e- mouse event- Returns:
- Should we treat this event as part of a "deep legacy" drag and drop?
-
isClick
- Returns:
- true if this point is "close enough" to the point at which the user initially pressed the mouse button to be considered a mouse click (such that no drag-and-drop moves are processed)
-
mouseReleased
Mouse button has been released -- if we can still handle the event (i.e. we haven't picked up some exotic modifier key during the drag, etc), then we perform the drop.- Specified by:
mouseReleasedin interfaceMouseListener- Parameters:
e- Mouse Event
-
performDrop
Moves the group of dragged (in the DragBuffer) pieces to the target point (p).- Parameters:
p- Point that mouse has been dragged to.
-
mouseEntered
- Specified by:
mouseEnteredin interfaceMouseListener
-
mouseExited
- Specified by:
mouseExitedin interfaceMouseListener
-
mouseClicked
- Specified by:
mouseClickedin interfaceMouseListener
-
compare
Implement Comparator to sort the contents of the drag buffer before completing the drag. This sorts the contents to be in the same order as the pieces were in their original parent stack.- Specified by:
comparein interfaceComparator<GamePiece>
-
applyKeyAfterMove(List, KeyStroke)to return Commands