20 package net.sf.gridarta.model.mapmanager;
23 import java.io.IOException;
24 import java.util.Collections;
25 import java.util.Iterator;
26 import java.util.List;
27 import java.util.concurrent.CopyOnWriteArrayList;
41 import org.jetbrains.annotations.NotNull;
42 import org.jetbrains.annotations.Nullable;
82 private final List<MapControl<G, A, R>>
mapControls =
new CopyOnWriteArrayList<>();
110 public void facesReloaded() {
117 faceObjectProviders.addFaceObjectProvidersListener(faceObjectProvidersListener);
129 mapControls.add(mapControl);
131 listener.mapCreated(mapControl, interactive);
152 if (currentMapControl != null) {
156 if (!mapControls.isEmpty()) {
157 return mapControls.get(mapControls.size() - 1);
167 listener.mapClosing(mapControl);
169 assert mapControl.getUseCounter() <= 0;
170 mapControls.remove(mapControl);
171 if (currentMapControl == mapControl) {
172 setCurrentMap(mapControls.isEmpty() ? null : mapControls.get(0));
175 listener.mapClosed(mapControl);
177 mapControl.getMapModel().mapClosed();
186 final MapFile tmpMapFile = mapControl.getMapModel().getMapFile();
187 if (tmpMapFile != null && tmpMapFile.
equals(mapFile)) {
188 mapControl.acquire();
218 }
catch (
final OutOfMemoryError ex) {
219 throw new IOException(
"out of memory", ex);
222 int outOfMapBoundsDeleted = 0;
223 final StringBuilder outOfMapBoundsObjects =
new StringBuilder();
227 while (it.hasNext()) {
230 final int minX = gameObject.
getHead().getMapX() + gameObject.
getMinX();
231 final int minY = gameObject.
getHead().getMapY() + gameObject.
getMinY();
232 final int maxX = gameObject.
getHead().getMapX() + gameObject.
getMaxX();
233 final int maxY = gameObject.
getHead().getMapY() + gameObject.
getMaxY();
234 if (minX < 0 || minY < 0 || maxX >= mapWidth || maxY >= mapHeight) {
236 if (gameObject.
isHead()) {
237 outOfMapBoundsDeleted++;
238 if (outOfMapBoundsDeleted <= DELETED_OBJECTS_TO_REPORT) {
239 outOfMapBoundsObjects.append(
'\n');
240 outOfMapBoundsObjects.append(gameObject.
getBestName());
241 outOfMapBoundsObjects.append(
" at ");
242 outOfMapBoundsObjects.append(minX).append(
'/').append(minY);
243 if (minX != maxX || minY != maxY) {
244 outOfMapBoundsObjects.append(
"..");
245 outOfMapBoundsObjects.append(maxX).append(
'/').append(maxY);
247 }
else if (outOfMapBoundsDeleted == DELETED_OBJECTS_TO_REPORT + 1) {
248 outOfMapBoundsObjects.append(
"\n...");
254 if (interactive && outOfMapBoundsDeleted > 0) {
263 if (currentMapControl == mapControl) {
267 currentMapControl = mapControl;
270 listener.currentMapChanged(currentMapControl);
277 return Collections.unmodifiableList(mapControls);
293 listenerList.
add(listener);
298 listenerList.
remove(listener);
303 final MapFile mapFile = mapControl.getMapModel().getMapFile();
304 if (mapFile == null) {
311 mapModel.beginTransaction(
"revert");
314 mapArchObject.beginTransaction();
318 mapArchObject.endTransaction();
323 mapModel.endTransaction();
325 mapModel.resetModified();
331 if (mapControl == null) {
336 if (mapFile == null) {
340 return mapFile.
getFile().getParentFile();
345 mapControl.release();
346 if (mapControl.getUseCounter() <= 0) {
MapControl< G, A, R > newMap(@Nullable final List< G > objects, @NotNull final A mapArchObject, @Nullable final MapFile mapFile, final boolean interactive)
T getHead()
Return the head part of a multi-part object.
boolean isInContainer()
Check whether this GameObject is in a Container (in Gridarta sense, which means being in a MapSquare ...
void closeMap(@NotNull final MapControl< G, A, R > mapControl)
int getMaxX()
Determines the maximum x-coordinate of any part relative to the head part.
This class contains methods for converting relative map paths to absolute map paths and vice versa...
MapReader< G, A > decodeMapFile(@NotNull final File file, final boolean interactive)
Load a map file.
A MapModel reflects the data of a map.
A MapManager manages all opened maps.
T [] getListeners()
Returns an array of all the listeners.
Reading and writing of maps, handling of paths.
Settings that apply to a project.
final ProjectSettings projectSettings
The project settings instance.
List< MapControl< G, A, R > > getOpenedMaps()
MapReader< G, A > newMapReader(@NotNull File file)
Create a new MapReader instance.
A factory for creating MapReader instances.
MapFile getMapFile(@NotNull final AbsoluteMapPath mapPath)
Returns a MapFile instance from an AbsoluteMapPath.
final MapReaderFactory< G, A > mapReaderFactory
The gridarta objects factory instance.
AbstractMapManager(@NotNull final MapReaderFactory< G, A > mapReaderFactory, @NotNull final ProjectSettings projectSettings, @NotNull final FaceObjectProviders faceObjectProviders, @NotNull final PathManager pathManager)
Create a new map manager.
void release(@NotNull final MapControl< G, A, R > mapControl)
List< G > getGameObjects()
Return the decoded game objects.
MapModel< G, A, R > getMapModel()
Returns the map model.
Abstract base class for MapManager implementations.
Base package of all Gridarta classes.
Interface for classes that read map files.
Reflects a game object (object on a map).
boolean equals(@Nullable final Object obj)
MapControl< G, A, R > openMapFile(@NotNull final File file, final boolean interactive)
Interface for listeners listening to MapManager changes.
int getMaxY()
Determines the maximum y-coordinate of any part relative to the head part.
void remove(@NotNull final T listener)
Removes a listener.
int getMinX()
Determines the minimum x-coordinate of any part relative to the head part.
void setFileControl(@NotNull final FileControl< G, A, R > fileControl)
MapControl< G, A, R > getOpenMap()
GameObjects are the objects based on Archetypes found on maps.
void add(@NotNull final T listener)
Adds a listener.
MapControl< G, A, R > currentMapControl
The current top map we are working with.
FileControl< G, A, R > fileControl
The main control.
void revert(@NotNull final MapControl< G, A, R > mapControl)
final PathManager pathManager
The PathManager for converting File instances.
MapControl< G, A, R > getCurrentMap()
Returns the current top map we are working with.
void removeMapManagerListener(@NotNull final MapManagerListener< G, A, R > listener)
String getBestName()
Returns the name which is best appropriate to describe this GameObject.
boolean isHead()
Returns whether this object is a single-part object or the head of the multi-part object...
Type-safe version of EventListenerList.
Interface for listeners interested in FaceObjectProviders related events.
A getMapArchObject()
Returns the Map Arch Object with the meta information about the map.
Provider for faces of GameObjects and Archetypes.
abstract MapControl< G, A, R > createMapControl(@Nullable List< G > objects, @NotNull A mapArchObject, @Nullable MapFile mapFile)
Creates a new MapControl instance.
void setCurrentMap(@Nullable final MapControl< G, A, R > mapControl)
The face is the appearance of an object.
Currently nothing more than a marker interface for unification.
MapFile getMapFile()
Returns the map file.
File getFile()
Returns a File for this map file.
void reportOutOfMapBoundsDeleted(@NotNull File file, int outOfMapBoundsDeleted, @NotNull StringBuilder outOfMapBoundsObjects)
File getMapsDirectory()
Returns the default maps directory.
final EventListenerList2< MapManagerListener< G, A, R > > listenerList
The MapManagerListeners to inform of changes.
int getMinY()
Determines the minimum y-coordinate of any part relative to the head part.
final List< MapControl< G, A, R > > mapControls
All open maps.
void addMapManagerListener(@NotNull final MapManagerListener< G, A, R > listener)
static final int DELETED_OBJECTS_TO_REPORT
The maximum number of deleted game objects to report.
A getMapArchObject()
Returns the MapArchObject read from this MapFileDecode.
void facesReloaded()
Called whenever the faces have been reloaded.
The location of a map file with a map directory.
MapControl< G, A, R > openMapFile(@NotNull final MapFile mapFile, final boolean interactive)
Interface for MapArchObjects.