20 package net.sf.gridarta.model.mapcursor;
22 import java.awt.Dimension;
23 import java.awt.Point;
24 import java.awt.Rectangle;
25 import java.awt.geom.Rectangle2D;
39 import org.jetbrains.annotations.NotNull;
40 import org.jetbrains.annotations.Nullable;
64 private final Point
pos;
175 final Size2D gridSize = mapGrid.getGridSize();
177 final A mapArchObject = mapModel.getMapArchObject();
178 pos =
fixPoint(
new Point(mapArchObject.getEnterX(), mapArchObject.getEnterY()));
182 public void mapGridChanged(@NotNull
final MapGridEvent e) {
187 public void mapGridResized(@NotNull
final MapGridEvent e) {
188 final Size2D newSize = mapGrid.getGridSize();
189 final int newWidth = newSize.
getWidth();
190 final int newHeight = newSize.
getHeight();
193 mapRec.setSize(newWidth, newHeight);
195 if (dragging && !mapRec.contains(dragStart)) {
196 dragStart.x = Math.min(dragStart.x, mapRec.width - 1);
197 dragStart.y = Math.min(dragStart.y, mapRec.height - 1);
200 if (!mapRec.contains(pos)) {
201 pos.x = Math.min(pos.x, mapRec.width - 1);
202 pos.y = Math.min(pos.y, mapRec.height - 1);
211 mapGrid.addMapGridListener(mapGridListener);
212 mapGrid.setCursor(pos);
227 return new Point(pos);
238 final Point effectivePoint =
fixPoint(p);
239 if (!pos.equals(effectivePoint)) {
240 pos.setLocation(effectivePoint);
256 final boolean hasChanged;
258 if (p != null && mapRec.contains(p)) {
282 dragStart.setLocation(pos);
285 dragOffset.setSize(0, 0);
298 public boolean dragTo(@Nullable
final Point p) {
299 if (p != null && mapRec.contains(p) && dragging && !pos.equals(p)) {
302 final Point oldPos =
new Point(pos);
306 dragOffset.setSize(pos.x - dragStart.x, pos.y - dragStart.y);
342 if (forceSelect || !dragStart.equals(pos)) {
343 mapGrid.
selectArea(dragStart, pos, selectionMode);
380 return p != null && mapRec.contains(p);
389 public boolean goTo(
final boolean performAction, @NotNull
final Direction dir) {
390 if (dir.getDx() == 0 && dir.getDy() == 0) {
393 tmpPoint.setLocation(pos.x + dir.getDx(), pos.y + dir.getDy());
394 if (!mapRec.contains(tmpPoint)) {
420 listenerList.
add(listener);
428 listenerList.
remove(listener);
449 if (gameObject == null) {
450 this.gameObject = null;
472 if (mapSquare != null && mapSquare.getMapModel() ==
mapModel) {
473 pos.setLocation(mapSquare.getMapX(), mapSquare.getMapY());
489 if (transactionDepth == 0) {
490 transactionPos.setLocation(pos);
494 transactionMapRec.setRect(mapRec);
507 assert transactionDepth > 0;
510 if (transactionDepth == 0) {
513 final boolean changedGameObject = mapSquare != transactionMapSquare || gameObject !=
transactionGameObject;
514 final boolean changedSize = !mapRec.equals(transactionMapRec);
515 if (!pos.equals(transactionPos)) {
521 listener.mapCursorChangedMode();
529 if (changedGameObject) {
531 listener.mapCursorChangedGameObject(mapSquare, gameObject);
536 listener.mapCursorChangedSize();
549 assert transactionDepth > 0;
552 }
catch (
final IndexOutOfBoundsException ignored) {
563 assert transactionDepth > 0;
564 gameObject = mapSquare == null ? null : mapSquare.
getLast();
574 if (mapSquare == null || gameObject == null) {
578 final G newGameObject = mapSquare.
getPrev(gameObject);
579 if (newGameObject == null) {
586 gameObject = newGameObject;
601 if (mapSquare == null || gameObject == null) {
605 final G newGameObject = mapSquare.
getNext(gameObject);
606 if (newGameObject == null) {
613 gameObject = newGameObject;
635 final G insertedGameObject = mapModel.
insertArchToMap(gameObject, insertAtEnd ? null : this.gameObject, pos, join);
636 if (insertedGameObject != null) {
639 this.gameObject = insertedGameObject;
660 if (gameObject == null) {
665 if (mapSquare == null) {
672 G nextGameObject = mapSquare.
getNext(gameObject);
674 if (nextGameObject == null) {
675 nextGameObject = mapSquare.
getFirst();
677 if (nextGameObject != null) {
679 final G invGameObject = nextGameObject.getFirst();
680 if (invGameObject == null) {
683 nextGameObject = invGameObject;
688 this.gameObject = nextGameObject;
707 if (mapRec.contains(p)) {
711 return new Point(Math.max(Math.min(p.x, mapRec.width - 1), 0), Math.max(Math.min(p.y, mapRec.height - 1), 0));
Dimension getDragOffset()
Get offset from start position of dragging.
int getMapX()
Returns the x coordinate on the map.
void preSelect(@NotNull final Point start, @NotNull final Point end)
Rectangle defined by two points gets preselected.
boolean dragging
Gets set to.
void dragSelect(@NotNull final SelectionMode selectionMode, final boolean forceSelect)
Leave drag mode and select pre-selection using selectionMode.
MapSquare< G, A, R > getMapSquare()
A MapModel reflects the data of a map.
G getNext(@NotNull final G gameObject)
Return the GameObject succeeding a given game object.
T [] getListeners()
Returns an array of all the listeners.
final Point transactionPos
The value of pos at the start of the outermost map cursor transaction.
MapModel< G, A, R > getMapModel()
Returns the MapModel this map square is part of.
void selectMapSquare()
Selects the map square on the current map location.
void endTransaction()
End a transaction.
boolean selectBelow(final boolean performAction)
Moves the selected GameObject.
G transactionGameObject
The value of gameObject at the start of the outermost map cursor transaction.
final Rectangle mapRec
Used to test if coordinates are on the map.
void selectArea(@NotNull final Point pos1, @NotNull final Point pos2, @NotNull final SelectionMode selectionMode)
Selects or deselects all squares in an area.
void removeGameObject(@NotNull G gameObject, boolean join)
Delete an existing GameObject from the map.
Point fixPoint(@NotNull final Point p)
Returns a valid location that is on the map.
G getGameObject()
Returns the selected GameObject.
MapCursor provides methods to move and drag on map.
final Dimension dragOffset
Offset of dragging.
final void beginTransaction()
Start a new transaction.
void unSelect()
Clears all selection and pre-selection flags from the grid.
boolean selectAbove(final boolean performAction)
Moves the selected GameObject.
final void endTransaction()
End a transaction.
boolean isOnGrid(@Nullable final Point p)
Check if point is on grid.
boolean setLocationSafe(@Nullable final Point p)
Move cursor to a new location.
void unPreSelect(@NotNull final Point start, @NotNull final Point end)
Pre-selection of rectangle defined by points gets deleted.
Point getLocation()
Get position of cursor.
boolean dragTo(@Nullable final Point p)
When in drag mode and the point is on the map cursor is moved to this position.
void setLocation(@NotNull final Point p)
Move cursor to a new location.
Base package of all Gridarta classes.
void addMapCursorListener(@NotNull final MapCursorListener< G, A, R > listener)
Register a MapCursorListener.
Reflects a game object (object on a map).
Interface for listeners listening to MapGridEvents.
int transactionDepth
The nesting level of map cursor transactions.
void setCursor(@NotNull final Point pos)
Highlights the given cursor position.
void remove(@NotNull final T listener)
Removes a listener.
MapSquare< G, A, R > getMapSquare(@NotNull Point pos)
Get the square at a specified location.
GameObjects are the objects based on Archetypes found on maps.
void add(@NotNull final T listener)
Adds a listener.
G insertArchToMap(@NotNull BaseObject< G, A, R, ?> templateBaseObject, @Nullable G nextGameObject, @NotNull Point pos, boolean join)
Insert a game object to the map at a specified position.
int getWidth()
Returns the width of the area.
2D-Grid containing flags for selection, pre-selection, cursor, warnings and errors.
G getFirst(@NotNull final GameObjectMatcher gameObjectMatcher)
Returns the first occurrence of a matching game object.
final MapGrid mapGrid
Grid where cursor is bound to.
void unSetCursor(@NotNull final Point pos)
Un-highlights the given cursor position.
void dragRelease()
Leave drag mode and undo pre-selection.
void setGameObject(@Nullable final G gameObject)
Sets the selected GameObject.
void beginTransaction()
Starts a new transaction.
void selectTopmostGameObject()
Selects the last (top-most) GameObject on the current map square.
final EventListenerList2< MapCursorListener< G, A, R > > listenerList
The MapCursorListeners to inform of changes.
final MapModel< G, A, R > mapModel
The MapModel of this cursor.
MapSquare< G, A, R > transactionMapSquare
The value of mapSquare at the start of the outermost map cursor transaction.
Type-safe version of EventListenerList.
final Rectangle2D transactionMapRec
The value of mapRec at the start of the outermost map cursor transaction.
G getPrev(@NotNull final G gameObject)
Return the GameObject preceding a given game object.
boolean deleteSelectedGameObject(final boolean performAction, final boolean autoJoin)
Deletes the selected game object.
void endTransaction()
Ends a transaction.
MapSquare< G, A, R > mapSquare
The selected MapSquare.
boolean goTo(final boolean performAction, @NotNull final Direction dir)
Moves the cursor one square relative to current position.
final Point pos
Current cursor position.
void updatePreSelect(@NotNull final Point start, @NotNull final Point oldEnd, @NotNull final Point newEnd)
Update the pre-selection rectangle.
boolean insertGameObject(final boolean performAction, @NotNull final BaseObject< G, A, R, ?> gameObject, final boolean insertAtEnd, final boolean join)
Inserts a GameObject before the selected game object.
boolean transactionDragging
The value of dragging at the start of the outermost map cursor transaction.
void dragStart()
Set cursor to drag mode when it is active.
Modes that describe how squares get selected.
void beginTransaction(@NotNull String name)
Starts a new transaction.
Interface for listeners listening to MapCursor related events.
int getHeight()
Returns the height of the area.
boolean isDragging()
Returns whether the cursor is currently being dragged.
G getLast(@NotNull final GameObjectMatcher gameObjectMatcher)
Returns the last occurrence of a matching game object.
MapCursor(@NotNull final MapGrid mapGrid, @NotNull final MapModel< G, A, R > mapModel)
Construct a MapCursor.
final void deactivate()
Cursor gets deactivated.
void setMapSquare(@Nullable final MapSquare< G, A, R > mapSquare)
Sets the selected MapSquare.
void removeMapCursorListener(@NotNull final MapCursorListener< G, A, R > listener)
Remove a MapCursorListener.
final Point tmpPoint
Temporary point used in some methods.
int getMapY()
Returns the y coordinate on the map.
Interface for MapArchObjects.
G gameObject
The selected GameObject.
The class Size2D represents a 2d rectangular area.
This event is created by MapGrid.