Gridarta Editor
ShrinkMapSizeUtils.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.gui.dialog.shrinkmapsize;
21 
23 import net.sf.gridarta.utils.Size2D;
24 import org.jetbrains.annotations.NotNull;
25 
30 public class ShrinkMapSizeUtils {
31 
35  public static final int SHRINK_EAST = 2;
36 
40  public static final int SHRINK_SOUTH = 4;
41 
45  private ShrinkMapSizeUtils() {
46  }
47 
53  public static void shrinkMap(@NotNull final MapModel<?, ?, ?> mapModel, final int shrinkFlags) {
54  final Size2D mapSize = mapModel.getMapArchObject().getMapSize();
55  int filledWidth = mapSize.getWidth();
56  int filledHeight = mapSize.getHeight();
57  if ((shrinkFlags & SHRINK_EAST) != 0) {
58  while (filledWidth > 1 && mapModel.isAreaEmpty(filledWidth - 1, 0, 1, filledHeight)) {
59  filledWidth--;
60  }
61  }
62  if ((shrinkFlags & SHRINK_SOUTH) != 0) {
63  while (filledHeight > 1 && mapModel.isAreaEmpty(0, filledHeight - 1, filledWidth, 1)) {
64  filledHeight--;
65  }
66  }
67  mapModel.beginTransaction("Shrink Map Size");
68  try {
69  mapModel.getMapArchObject().setMapSize(new Size2D(filledWidth, filledHeight));
70  } finally {
71  mapModel.endTransaction();
72  }
73  }
74 
80  public static int getShrinkFlags(@NotNull final MapModel<?, ?, ?> mapModel) {
81  final Size2D mapSize = mapModel.getMapArchObject().getMapSize();
82  final int mapWidth = mapSize.getWidth();
83  final int mapHeight = mapSize.getHeight();
84  int shrinkFlags = 0;
85  /*
86  if (mapModel.isAreaEmpty(0, 0, mapWidth, 1)) {
87  shrinkFlags |= SHRINK_NORTH;
88  }
89  */
90  if (mapModel.isAreaEmpty(mapWidth - 1, 0, 1, mapHeight)) {
91  shrinkFlags |= SHRINK_EAST;
92  }
93  if (mapModel.isAreaEmpty(0, mapHeight - 1, mapWidth, 1)) {
94  shrinkFlags |= SHRINK_SOUTH;
95  }
96  /*
97  if (mapModel.isAreaEmpty(0, 0, 1, mapHeight)) {
98  shrinkFlags |= SHRINK_WEST;
99  }
100  */
101  return shrinkFlags;
102  }
103 
104 }
net.sf.gridarta.utils.Size2D.getWidth
int getWidth()
Returns the width of the area.
Definition: Size2D.java:96
net.sf.gridarta.model.mapmodel.MapModel
A MapModel reflects the data of a map.
Definition: MapModel.java:75
net.sf.gridarta
Base package of all Gridarta classes.
net.sf.gridarta.gui.dialog.shrinkmapsize.ShrinkMapSizeUtils.getShrinkFlags
static int getShrinkFlags(@NotNull final MapModel<?, ?, ?> mapModel)
Returns which borders contain empty squares.
Definition: ShrinkMapSizeUtils.java:80
net.sf
net.sf.gridarta.model.mapmodel
Definition: AboveFloorInsertionMode.java:20
net.sf.gridarta.gui.dialog.shrinkmapsize.ShrinkMapSizeUtils.ShrinkMapSizeUtils
ShrinkMapSizeUtils()
Private constructor to prevent instantiation.
Definition: ShrinkMapSizeUtils.java:45
net
net.sf.gridarta.utils.Size2D.getHeight
int getHeight()
Returns the height of the area.
Definition: Size2D.java:104
net.sf.gridarta.gui.dialog.shrinkmapsize.ShrinkMapSizeUtils.SHRINK_SOUTH
static final int SHRINK_SOUTH
Flag value: remove empty space from south border.
Definition: ShrinkMapSizeUtils.java:40
net.sf.gridarta.gui.dialog.shrinkmapsize.ShrinkMapSizeUtils.SHRINK_EAST
static final int SHRINK_EAST
Flag value: remove empty space from east border.
Definition: ShrinkMapSizeUtils.java:35
net.sf.gridarta.model
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.gui.dialog.shrinkmapsize.ShrinkMapSizeUtils
Utility class to remove empty squares from a map's border.
Definition: ShrinkMapSizeUtils.java:30
net.sf.gridarta.gui.dialog.shrinkmapsize.ShrinkMapSizeUtils.shrinkMap
static void shrinkMap(@NotNull final MapModel<?, ?, ?> mapModel, final int shrinkFlags)
Removes empty squares from a map's border.
Definition: ShrinkMapSizeUtils.java:53