20 package net.sf.gridarta.actions;
23 import java.io.IOException;
24 import java.util.ArrayList;
25 import java.util.Collection;
26 import java.util.List;
27 import java.util.regex.Matcher;
41 import org.jetbrains.annotations.NotNull;
42 import org.jetbrains.annotations.Nullable;
71 this.tileLinks = tileLinks.clone();
86 if (mapModel.getMapFile() == null) {
91 final List<MapControl<G, A, R>> mapControls =
new ArrayList<>(tilePaths.length);
109 if (mapControl != null) {
110 mapManager.
release(mapControl);
126 for (@Nullable
final String tilePath : tilePaths) {
128 if (tilePath != null && !tilePath.isEmpty()) {
131 }
catch (
final IOException ex) {
132 throw new CannotLoadMapFileException(tilePath, ex);
137 mapControls.add(mapControl);
149 if (mapControl != null) {
151 if (mapControl.getMapModel().isModified()) {
154 }
catch (
final IOException e) {
155 final MapFile mapFile = mapControl.getMapModel().getMapFile();
156 assert mapFile != null;
157 throw new CannotSaveMapFileException(mapFile.
getFile(), e);
169 boolean repeatFlag =
true;
172 for (
int i = 0; i < mapControls.size(); i++) {
174 if (mapControl != null) {
175 final TileLink tileLink = tileLinks[i];
177 for (
final MapLink mapLink : mapLinks) {
178 final Direction direction = mapLink.getMapDirection();
179 if (mapControls.get(direction.ordinal()) == null) {
181 if (!tilePath.isEmpty()) {
185 }
catch (
final IOException ex) {
186 throw new CannotLoadMapFileException(tilePath, ex);
205 final Size2D mapSize = mapModel.getMapArchObject().getMapSize();
207 if (mapControl != null) {
211 if (!mapSize.
equals(otherMapSize)) {
212 throw new MapSizeMismatchException(tmpMapModel.
getMapFile().getFile(), mapSize, otherMapSize);
214 for (
int ii = 0; ii < 2; ii++) {
230 for (
int i = 0; i < tilePaths.length; i++) {
232 if (mapControl != null) {
235 final String canonicalMapPath1 = mapModel.getMapFile().getFile().getPath();
236 final String link1 =
getTilePath(canonicalMapPath1, canonicalMapPath2, mapsDirectory);
238 tilePaths[i] = link1;
240 final String link2 =
getTilePath(canonicalMapPath2, canonicalMapPath1, mapsDirectory);
247 mapArchObject2.
setTilePath(tileLinks[i].getRevLink(), link2 == null ?
"" : link2);
267 final MapFile mapFile = mapModel.getMapFile();
268 if (mapFile == null) {
284 private static String
getTilePath(@NotNull
final String base, @NotNull
final String link, @NotNull
final File mapsDirectory)
throws CannotSaveMapFileException {
287 final CharSequence mapDirectory;
289 mapDirectory = mapsDirectory.getCanonicalPath();
290 }
catch (
final IOException e) {
291 throw new CannotSaveMapFileException(mapsDirectory, e);
293 final String first = base.substring(mapDirectory.length(), pos).trim();
294 final String second = link.substring(mapDirectory.length(), pos2).trim();
296 final String sep = Matcher.quoteReplacement(File.separator);
299 if (first.isEmpty()) {
300 return link.substring(mapDirectory.length()).trim().replaceAll(sep,
"/");
303 if (first.compareTo(second) == 0) {
304 return link.substring(pos2 + 1).trim().replaceAll(sep,
"/");
307 if (second.startsWith(first)) {
308 return link.substring(pos + 1).trim().replaceAll(sep,
"/");
311 return link.substring(mapDirectory.length()).trim().replaceAll(sep,
"/");
321 final int slashIndex1 = base.lastIndexOf(
'/');
322 final int separatorIndex1 = base.lastIndexOf(File.separator);
323 int pos = slashIndex1;
324 if (separatorIndex1 > slashIndex1) {
325 pos = separatorIndex1;
static MapPath newMapPath(@NotNull final String string)
Creates a MapPath instance from string representation.
A MapModel reflects the data of a map.
A MapManager manages all opened maps.
void release(@NotNull MapControl< G, A, R > mapControl)
Releases a MapControl instance.
boolean equals(@Nullable final Object obj)
MapControl< G, A, R > loadMapControl(@NotNull final MapModel< G, A, R > mapModel, @NotNull final String path)
Loads an adjacent MapControl, ignoring any (I/O-)errors.
Exception thrown if a map file cannot be saved.
void endTransaction()
End a transaction.
void beginTransaction()
Starts a new transaction.
void attachTiledMaps(@NotNull final MapModel< G, A, R > mapModel, @NotNull final String[] tilePaths, @NotNull final File mapsDirectory, final boolean performAction)
Updates tile paths of a map.
MapModel< G, A, R > getMapModel()
Returns the map model.
Exception thrown if a map file cannot be loaded.
Base package of all Gridarta classes.
Reflects a game object (object on a map).
Size2D getMapSize()
Returns the map size.
GameObjects are the objects based on Archetypes found on maps.
Utility class for MapPath related functions.
static String getTilePath(@NotNull final String base, @NotNull final String link, @NotNull final File mapsDirectory)
Returns the map path for a tile of a map and a tile path.
void fillAdjacentMaps(@NotNull final List< MapControl< G, A, R >> mapControls)
Fills missing adjacent map slots.
void updateTilePaths(@NotNull final MapModel< G, A, R > mapModel, @NotNull final List< MapControl< G, A, R >> mapControls, @NotNull final String[] tilePaths, @NotNull final File mapsDirectory)
Updates map tile paths to match the loaded maps.
final TileLink [] tileLinks
The tile links for the attach map algorithm.
A getMapArchObject()
Returns the Map Arch Object with the meta information about the map.
Exception thrown if an operation is attempted on an unsaved map.
Currently nothing more than a marker interface for unification.
void endTransaction()
Ends a transaction.
MapFile getMapFile()
Returns the map file.
final MapManager< G, A, R > mapManager
The mainControl to use.
File getFile()
Returns a File for this map file.
void validateMapSizes(@NotNull final MapModel< G, A, R > mapModel, @NotNull final Iterable< MapControl< G, A, R >> mapControls)
Validates all links to check that attached maps have matching width/height.
AttachTiledMaps(@NotNull final MapManager< G, A, R > mapManager, @NotNull final TileLink[] tileLinks)
Creates a new instance.
void setTilePath(@NotNull Direction direction, @NotNull String tilePath)
Sets a tile path.
void saveAdjacentMaps(@NotNull final Iterable< MapControl< G, A, R >> mapControls)
Saves adjacent maps.
void beginTransaction(@NotNull String name)
Starts a new transaction.
void loadAdjacentMaps(@NotNull final MapModel< G, A, R > mapModel, @NotNull final Collection< MapControl< G, A, R >> mapControls, @NotNull final String[] tilePaths)
Loads adjacent map files by "filling" them by checking the "side" path links of the loaded ones...
MapControl< G, A, R > openMapFile(@NotNull MapFile mapFile, boolean interactive)
Loads a map file.
static int getLastSlashIndex(@NotNull final String base)
Returns the index of the last '/' or File#separator within a string.
The location of a map file with a map directory.
Exception thrown if the size of a map file is unexpected.
Interface for MapArchObjects.
Attaches maps to adjacent tiled maps.
The class Size2D represents a 2d rectangular area.