 |
Gridarta Editor
|
Go to the documentation of this file.
20 package net.sf.gridarta.model.mapgrid;
22 import java.awt.Point;
23 import java.awt.Rectangle;
24 import java.util.ArrayList;
25 import java.util.Collection;
26 import java.util.List;
27 import java.util.concurrent.CopyOnWriteArrayList;
29 import org.jetbrains.annotations.NotNull;
30 import org.jetbrains.annotations.Nullable;
75 private final Rectangle
recChange =
new Rectangle();
160 private final Collection<MapGridListener>
listenerList =
new CopyOnWriteArrayList<>();
220 final int[][] newGridFlags =
new int[newSize.getWidth()][newSize.getHeight()];
221 final Point pos =
new Point();
229 for (pos.x = 0; pos.x < minSize.
getWidth(); pos.x++) {
230 for (pos.y = 0; pos.y < minSize.
getHeight(); pos.y++) {
231 newGridFlags[pos.x][pos.y] =
gridFlags[pos.x][pos.y];
246 listener.mapGridChanged(e);
256 listener.mapGridResized(e);
281 }
catch (
final ArrayIndexOutOfBoundsException ignored) {
313 public void preSelect(@NotNull
final Point start, @NotNull
final Point end) {
334 public void updatePreSelect(@NotNull
final Point start, @NotNull
final Point oldEnd, @NotNull
final Point newEnd) {
335 final Point old1 =
new Point(Math.min(start.x, oldEnd.x), Math.min(start.y, oldEnd.y));
336 final Point old2 =
new Point(Math.max(start.x, oldEnd.x), Math.max(start.y, oldEnd.y));
337 final Point new1 =
new Point(Math.min(start.x, newEnd.x), Math.min(start.y, newEnd.y));
338 final Point new2 =
new Point(Math.max(start.x, newEnd.x), Math.max(start.y, newEnd.y));
342 if (old1.x < new1.x) {
346 if (new2.x < old2.x) {
350 if (old1.y < new1.y) {
354 if (new2.y < old2.y) {
360 if (new1.x < old1.x) {
364 if (old2.x < new2.x) {
368 if (new1.y < old1.y) {
372 if (old2.y < new2.y) {
378 assert old1.x == new1.x;
379 assert old2.x == new2.x;
380 assert old1.y == new1.y;
381 assert old2.y == new2.y;
393 public void unPreSelect(@NotNull
final Point start, @NotNull
final Point end) {
422 switch (selectionMode) {
444 private void calculateRec(@NotNull
final Point p1, @NotNull
final Point p2) {
692 private void setFlags(
final int minX,
final int minY,
final int maxX,
final int maxY,
final int flags) {
693 for (
int x = minX; x <= maxX; x++) {
694 for (
int y = minY; y <= maxY; y++) {
695 if ((
gridFlags[x][y] & flags) != flags) {
715 private void unsetFlags(
final int minX,
final int minY,
final int maxX,
final int maxY,
final int flags) {
716 for (
int x = minX; x <= maxX; x++) {
717 for (
int y = minY; y <= maxY; y++) {
738 private void toggleFlags(
final int minX,
final int minY,
final int maxX,
final int maxY,
final int flags) {
739 for (
int x = minX; x <= maxX; x++) {
740 for (
int y = minY; y <= maxY; y++) {
764 throw new IllegalStateException(
"A transaction must only be used by one thread.");
778 throw new IllegalStateException(
"Tried to end a transaction but no transaction was open.");
816 private void updateSelectionFlag(
final int x,
final int y,
final boolean newState,
final int dx,
final int dy,
final int flag,
final int dFlag) {
845 final List<Point> selection =
new ArrayList<>();
846 if (selectedRec !=
null) {
847 for (
int x = selectedRec.x; x < selectedRec.x + selectedRec.width; x++) {
848 for (
int y = selectedRec.y; y < selectedRec.y + selectedRec.height; y++) {
850 selection.add(
new Point(x, y));
857 return selection.toArray(
new Point[0]);
void addMapGridListener(@NotNull final MapGridListener listener)
Registers a MapGridListener.
int getWidth()
Returns the width of the area.
void toggleFlags(final int minX, final int minY, final int maxX, final int maxY, final int flags)
Flips flags in a rectangle and generate a grid change event.
int getFlags(final int x, final int y)
Returns the flags of a square.
void endTransaction()
Ends a transaction.
void invertSelection()
Inverts all selected squares.
void fireMapGridResizeEvent()
Informs all registered listeners that the size of MapGrid has changed.
Base package of all Gridarta classes.
static final int GRID_FLAG_SELECTION
Selection - marks all selected squares.
final Collection< MapGridListener > listenerList
The MapGridListeners to inform of changes.
void updatePreSelect(@NotNull final Point start, @NotNull final Point oldEnd, @NotNull final Point newEnd)
Update the pre-selection rectangle.
boolean hasError(@NotNull final Point p)
Checks if a square has the error flag set.
int transactionDepth
The transaction depth.
static final int GRID_FLAG_INFORMATION
Flag to highlight as information.
void clearErrors()
Clears all error flags.
void unsetFlags(final int minX, final int minY, final int maxX, final int maxY, final int flags)
Resets flags in a rectangle and generate a grid change event.
static final int GRID_FLAG_SELECTION_EAST
Selection - is set for squares at the east edge of the selected area.
void updateSelectionFlag(final int x, final int y, final boolean newState, final int dx, final int dy, final int flag, final int dFlag)
Updates the border selection flags of a square and one adjacent square.
static final int GRID_FLAG_CONNECTION
Flag to highlight as part of a connection group.
Rectangle getSelectedRec()
Returns the smallest rectangle containing selection.
void unSetCursor(@NotNull final Point pos)
Un-highlights the given cursor position.
final Point cornerMin
Left upper coordinates of rectangle that is being processed.
Rectangle getRecChange()
Returns a rectangle where the grid was changed.
void updateSelectionFlag(final int x, final int y, final boolean newState)
Updates the border selection flags of a square and its adjacent squares.
void setFlags(final int minX, final int minY, final int maxX, final int maxY, final int flags)
Sets flags in a rectangle and generate a grid change event.
void beginTransaction()
Starts a new transaction.
static final int GRID_FLAG_SELECTION_SOUTH
Selection - is set for squares at the south edge of the selected area.
void selectAll()
Marks all squares as selected.
int getHeight()
Returns the height of the area.
Rectangle cachedSelectedRec
The return value for getSelectedRec().
boolean cachedSelectedRecValid
If set, cachedSelectedRec is up-to-date.
Size2D getSize()
Returns size of grid.
static final int GRID_FLAG_SELECTION_WEST
Selection - is set for squares at the west edge of the selected area.
static final int GRID_FLAG_ERROR
Flag to highlight as error.
void beginRecChange()
Begins a set of changes.
Modes that describe how squares get selected.
void removeMapGridListener(@NotNull final MapGridListener listener)
Removes a MapGridListener.
static final int GRID_FLAG_CURSOR
Flag to highlight cursor position.
boolean endRecChange()
Ends the set of changes and store the bounding box for all recorded changes in recChange.
static final int GRID_FLAG_WARNING
Flag to highlight as warning.
void select(@NotNull final Point pos, @NotNull final SelectionMode selectionMode)
Selects or deselects a single square.
Point[] getSelection()
Returns the selection.
2D-Grid containing flags for selection, pre-selection, cursor, warnings and errors.
MapGrid(@NotNull final Size2D gridSize)
Creates a new instance.
boolean equals(@Nullable final Object obj)
void calculateCachedSelectedRec()
Makes sure the value of cachedSelectedRec if up-to-date.
int getFlags(@NotNull final Point p)
Returns the flags of a square.
Size2D getGridSize()
Returns a Size2D with the dimension of this grid.
void resize(@NotNull final Size2D newSize)
Resizes the MapGrid.
static final int GRID_FLAG_SELECTION_NORTH
Selection - is set for squares at the north edge of the selected area.
void updateRecChange(final int x, final int y)
Adds a point to the set of changes.
Interface for listeners listening to MapGridEvents.
void preSelect(@NotNull final Point start, @NotNull final Point end)
Rectangle defined by two points gets preselected.
void unPreSelect(@NotNull final Point start, @NotNull final Point end)
Pre-selection of rectangle defined by points gets deleted.
static final int GRID_FLAG_FATAL
Flag to highlight as fatal.
void selectArea(@NotNull final Point pos1, @NotNull final Point pos2, @NotNull final SelectionMode selectionMode)
Selects or deselects all squares in an area.
void setCursor(@NotNull final Point pos)
Highlights the given cursor position.
This event is created by MapGrid.
void fireMapGridChangedEvent()
Inform all registered listeners that the flags on MapGrid have changed.
void setError(final int x, final int y)
Sets the error flag at given coordinates.
void calculateRec(@NotNull final Point p1, @NotNull final Point p2)
Thread transactionThread
The thread that performs the current transaction.
The class Size2D represents a 2d rectangular area.
void unSelect()
Clears all selection and pre-selection flags from the grid.
final Point cornerMax
Right lower coordinates of rectangle that is being processed.
Point cachedCursorLoc
Cached location of the cursor.
int[][] gridFlags
2D-array to store grid flags.
static final int GRID_FLAG_SELECTING
Pre-selection - used to preselect squares.
final Rectangle recChange
Rectangle to store location of last grid change.