Gridarta Editor
MapGridTest.java
Go to the documentation of this file.
1 /*
2  * Gridarta MMORPG map editor for Crossfire, Daimonin and similar games.
3  * Copyright (C) 2000-2023 The Gridarta Developers.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 package net.sf.gridarta.model.mapgrid;
21 
22 import java.awt.Point;
23 import net.sf.gridarta.utils.Size2D;
24 import org.junit.Assert;
25 import org.junit.Test;
26 
31 public class MapGridTest {
32 
36  @Test
38  final MapGrid mapGrid = new MapGrid(new Size2D(4, 3));
39  checkSelectionBorder(mapGrid, "0000" + "0000" + "0000");
40 
41  mapGrid.select(new Point(1, 1), SelectionMode.ADD);
42  checkSelectionBorder(mapGrid, "0000" + "0f00" + "0000");
43 
44  mapGrid.select(new Point(2, 1), SelectionMode.ADD);
45  checkSelectionBorder(mapGrid, "0000" + "0d70" + "0000");
46 
47  mapGrid.select(new Point(3, 1), SelectionMode.ADD);
48  checkSelectionBorder(mapGrid, "0000" + "0d57" + "0000");
49 
50  mapGrid.select(new Point(3, 1), SelectionMode.SUB);
51  checkSelectionBorder(mapGrid, "0000" + "0d70" + "0000");
52 
53  mapGrid.selectArea(new Point(0, 0), new Point(3, 2), SelectionMode.FLIP);
54  checkSelectionBorder(mapGrid, "9553" + "a00a" + "c556");
55 
56  mapGrid.select(new Point(1, 1), SelectionMode.FLIP);
57  checkSelectionBorder(mapGrid, "9153" + "820a" + "c456");
58 
59  mapGrid.select(new Point(1, 1), SelectionMode.SUB);
60  checkSelectionBorder(mapGrid, "9553" + "a00a" + "c556");
61 
62  mapGrid.selectArea(new Point(0, 0), new Point(3, 2), SelectionMode.ADD);
63  checkSelectionBorder(mapGrid, "9113" + "8002" + "c446");
64 
65  mapGrid.resize(new Size2D(2, 1));
66  checkSelectionBorder(mapGrid, "d7");
67 
68  mapGrid.resize(new Size2D(3, 4));
69  checkSelectionBorder(mapGrid, "d700" + "0000" + "0000");
70 
71  mapGrid.selectArea(new Point(0, 0), new Point(2, 3), SelectionMode.ADD);
72  checkSelectionBorder(mapGrid, "913" + "802" + "802" + "c46");
73 
74  mapGrid.resize(new Size2D(4, 3));
75  checkSelectionBorder(mapGrid, "9130" + "8020" + "c460");
76  }
77 
86  private static void checkSelectionBorder(final MapGrid mapGrid, final String expectedBorder) {
87  final StringBuilder sb = new StringBuilder();
88  final Size2D size = mapGrid.getSize();
89  for (int y = 0; y < size.getHeight(); y++) {
90  for (int x = 0; x < size.getWidth(); x++) {
91  final int flags = mapGrid.getFlags(x, y);
92  int value = 0;
93  if ((flags & MapGrid.GRID_FLAG_SELECTION_NORTH) != 0) {
94  value |= 1;
95  }
96  if ((flags & MapGrid.GRID_FLAG_SELECTION_EAST) != 0) {
97  value |= 2;
98  }
99  if ((flags & MapGrid.GRID_FLAG_SELECTION_SOUTH) != 0) {
100  value |= 4;
101  }
102  if ((flags & MapGrid.GRID_FLAG_SELECTION_WEST) != 0) {
103  value |= 8;
104  }
105  sb.append(Integer.toHexString(value));
106  }
107  }
108  Assert.assertEquals(expectedBorder, sb.toString());
109  }
110 
111 }
net.sf.gridarta.utils.Size2D.getWidth
int getWidth()
Returns the width of the area.
Definition: Size2D.java:96
net.sf.gridarta.model.mapgrid.MapGrid.getFlags
int getFlags(final int x, final int y)
Returns the flags of a square.
Definition: MapGrid.java:476
net.sf.gridarta
Base package of all Gridarta classes.
net.sf
net.sf.gridarta.model.mapgrid.MapGrid.GRID_FLAG_SELECTION_EAST
static final int GRID_FLAG_SELECTION_EAST
Selection - is set for squares at the east edge of the selected area.
Definition: MapGrid.java:144
net.sf.gridarta.model.mapgrid.SelectionMode.SUB
SUB
All squares that are preselected get unselected.
Definition: SelectionMode.java:36
net.sf.gridarta.model.mapgrid.SelectionMode.FLIP
FLIP
All squares that are preselected change state of selection.
Definition: SelectionMode.java:41
net.sf.gridarta.model.mapgrid.MapGrid.GRID_FLAG_SELECTION_SOUTH
static final int GRID_FLAG_SELECTION_SOUTH
Selection - is set for squares at the south edge of the selected area.
Definition: MapGrid.java:149
net
net.sf.gridarta.utils.Size2D.getHeight
int getHeight()
Returns the height of the area.
Definition: Size2D.java:104
net.sf.gridarta.model.mapgrid.MapGrid.getSize
Size2D getSize()
Returns size of grid.
Definition: MapGrid.java:504
net.sf.gridarta.model.mapgrid.MapGrid.GRID_FLAG_SELECTION_WEST
static final int GRID_FLAG_SELECTION_WEST
Selection - is set for squares at the west edge of the selected area.
Definition: MapGrid.java:154
net.sf.gridarta.model.mapgrid.SelectionMode
Modes that describe how squares get selected.
Definition: SelectionMode.java:26
net.sf.gridarta.model.mapgrid.MapGridTest.checkSelectionBorder
static void checkSelectionBorder(final MapGrid mapGrid, final String expectedBorder)
Checks that the map border selection flags of a MapGrid instance is as expected.
Definition: MapGridTest.java:86
net.sf.gridarta.model.mapgrid.MapGrid.select
void select(@NotNull final Point pos, @NotNull final SelectionMode selectionMode)
Selects or deselects a single square.
Definition: MapGrid.java:408
net.sf.gridarta.model.mapgrid.MapGridTest
Test for MapGrid.
Definition: MapGridTest.java:31
net.sf.gridarta.model.mapgrid.MapGrid
2D-Grid containing flags for selection, pre-selection, cursor, warnings and errors.
Definition: MapGrid.java:46
net.sf.gridarta.model.mapgrid.SelectionMode.ADD
ADD
All squares that are preselected get selected.
Definition: SelectionMode.java:31
net.sf.gridarta.model.mapgrid.MapGrid.resize
void resize(@NotNull final Size2D newSize)
Resizes the MapGrid.
Definition: MapGrid.java:215
net.sf.gridarta.model.mapgrid.MapGrid.GRID_FLAG_SELECTION_NORTH
static final int GRID_FLAG_SELECTION_NORTH
Selection - is set for squares at the north edge of the selected area.
Definition: MapGrid.java:139
net.sf.gridarta.model.mapgrid.MapGrid.selectArea
void selectArea(@NotNull final Point pos1, @NotNull final Point pos2, @NotNull final SelectionMode selectionMode)
Selects or deselects all squares in an area.
Definition: MapGrid.java:418
net.sf.gridarta.utils.Size2D
The class Size2D represents a 2d rectangular area.
Definition: Size2D.java:30
net.sf.gridarta.utils
Definition: ActionBuilderUtils.java:20
net.sf.gridarta.model.mapgrid.MapGridTest.testSelectionBorderUpdates
void testSelectionBorderUpdates()
Test case for selection border updates.
Definition: MapGridTest.java:37