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-2015 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 }
void resize(@NotNull final Size2D newSize)
Resizes the MapGrid.
Definition: MapGrid.java:215
static final int GRID_FLAG_SELECTION_NORTH
Selection - is set for squares at the north edge of the selected area.
Definition: MapGrid.java:139
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
int getFlags(final int x, final int y)
Returns the flags of a square.
Definition: MapGrid.java:476
Size2D getSize()
Returns size of grid.
Definition: MapGrid.java:504
Base package of all Gridarta classes.
static final int GRID_FLAG_SELECTION_EAST
Selection - is set for squares at the east edge of the selected area.
Definition: MapGrid.java:144
int getWidth()
Returns the width of the area.
Definition: Size2D.java:96
2D-Grid containing flags for selection, pre-selection, cursor, warnings and errors.
Definition: MapGrid.java:45
FLIP
All squares that are preselected change state of selection.
static final int GRID_FLAG_SELECTION_SOUTH
Selection - is set for squares at the south edge of the selected area.
Definition: MapGrid.java:149
void select(@NotNull final Point pos, @NotNull final SelectionMode selectionMode)
Selects or deselects a single square.
Definition: MapGrid.java:408
ADD
All squares that are preselected get selected.
static void checkSelectionBorder(final MapGrid mapGrid, final String expectedBorder)
Checks that the map border selection flags of a MapGrid instance is as expected.
static final int GRID_FLAG_SELECTION_WEST
Selection - is set for squares at the west edge of the selected area.
Definition: MapGrid.java:154
Modes that describe how squares get selected.
void testSelectionBorderUpdates()
Test case for selection border updates.
SUB
All squares that are preselected get unselected.
int getHeight()
Returns the height of the area.
Definition: Size2D.java:104
The class Size2D represents a 2d rectangular area.
Definition: Size2D.java:30