Gridarta Editor
MapViewsManager.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.map.mapview;
21 
22 import java.awt.Point;
23 import java.io.File;
24 import java.io.IOException;
25 import java.util.Iterator;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.WeakHashMap;
40 import org.jetbrains.annotations.NotNull;
41 import org.jetbrains.annotations.Nullable;
42 
47 public class MapViewsManager<G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> {
48 
52  @NotNull
54 
58  @NotNull
59  private final PathManager pathManager;
60 
64  @NotNull
66 
70  @NotNull
72 
76  @NotNull
78 
83  @NotNull
84  private final Map<MapControl<G, A, R>, MapViews<G, A, R>> mapViews = new WeakHashMap<>();
85 
90  @NotNull
92 
93  @Override
94  public void gridVisibleChanged(final boolean gridVisible) {
95  // ignore
96  }
97 
98  @Override
99  public void lightVisibleChanged(final boolean lightVisible) {
100  // ignore
101  }
102 
103  @Override
104  public void smoothingChanged(final boolean smoothing) {
105  // ignore
106  }
107 
108  @Override
109  public void tileStretchingChanged(final boolean tileStretching) {
110  // ignore
111  }
112 
113  @Override
114  public void doubleFacesChanged(final boolean doubleFaces) {
115  // ignore
116  }
117 
118  @Override
119  public void alphaTypeChanged(final int alphaType) {
120  // XXX THIS IS A BAD HACK
121  addEditType(alphaType);
122  }
123 
124  @Override
125  public void editTypeChanged(final int editType) {
126  addEditType(editType);
127  }
128 
129  @Override
130  public void autojoinChanged(final boolean autojoin) {
131  // ignore
132  }
133 
134  };
135 
139  @NotNull
141 
142  @Override
143  public void currentMapChanged(@Nullable final MapControl<G, A, R> mapControl) {
144  }
145 
146  @Override
147  public void mapCreated(@NotNull final MapControl<G, A, R> mapControl, final boolean interactive) {
148  }
149 
150  @Override
151  public void mapClosing(@NotNull final MapControl<G, A, R> mapControl) {
152  getMapViewsInt(mapControl).closeAllViews();
153  }
154 
155  @Override
156  public void mapClosed(@NotNull final MapControl<G, A, R> mapControl) {
157  }
158 
159  };
160 
169  public MapViewsManager(@NotNull final MapViewSettings mapViewSettings, @NotNull final MapViewFactory<G, A, R> mapViewFactory, @NotNull final MapManager<G, A, R> mapManager, @NotNull final MapManager<G, A, R> pickmapManager, @NotNull final PathManager pathManager) {
170  this.mapViewSettings = mapViewSettings;
171  this.mapViewFactory = mapViewFactory;
172  this.mapManager = mapManager;
173  this.pathManager = pathManager;
176  pickmapManager.addMapManagerListener(mapManagerListener);
177  }
178 
179  @Deprecated
180  public void setFileControl(@NotNull final FileControl<G, A, R> fileControl) {
181  this.fileControl = fileControl;
182  }
183 
189  public void addMapViewsListener(@NotNull final MapControl<G, A, R> mapControl, @NotNull final MapViewsListener<G, A, R> listener) {
190  getMapViewsInt(mapControl).addMapViewsListener(listener);
191  }
192 
198  public void removeMapViewsListener(@NotNull final MapControl<G, A, R> mapControl, @NotNull final MapViewsListener<G, A, R> listener) {
199  getMapViewsInt(mapControl).removeMapViewsListener(listener);
200  }
201 
207  @Nullable
208  public MapView<G, A, R> getMapViewFrame(@NotNull final MapControl<G, A, R> mapControl) {
209  return getMapViewsInt(mapControl).getMapViewFrame();
210  }
211 
217  private int getMapViews(@NotNull final MapControl<G, A, R> mapControl) {
218  return getMapViewsInt(mapControl).getMapViews();
219  }
220 
228  @NotNull
229  public MapView<G, A, R> newMapView(@NotNull final MapControl<G, A, R> mapControl, @Nullable final Point viewPosition) {
230  mapControl.getMapModel().addActiveEditType(mapViewSettings.getEditType());
231  return getMapViewsInt(mapControl).newMapView(mapControl, viewPosition, mapViewFactory);
232  }
233 
238  public void closeView(@NotNull final MapView<G, A, R> mapView) {
239  getMapViewsInt(mapView.getMapControl()).closeView(mapView);
240  }
241 
246  public void setFocus(@NotNull final MapView<G, A, R> mapView) {
247  getMapViewsInt(mapView.getMapControl()).setFocus(mapView);
248  }
249 
256  public Iterator<MapView<G, A, R>> getMapViewIterator(@NotNull final MapControl<G, A, R> mapControl) {
257  return getMapViewsInt(mapControl).getMapViewIterator();
258  }
259 
264  private void repaintAllViews(@NotNull final MapControl<G, A, R> mapControl) {
265  getMapViewsInt(mapControl).repaintAllViews();
266  }
267 
273  @NotNull
274  private MapViews<G, A, R> getMapViewsInt(@NotNull final MapControl<G, A, R> mapControl) {
275  final MapViews<G, A, R> existingMapViews = mapViews.get(mapControl);
276  if (existingMapViews != null) {
277  return existingMapViews;
278  }
279 
280  final MapViews<G, A, R> newMapViews = new MapViews<>();
281  mapViews.put(mapControl, newMapViews);
282  return newMapViews;
283  }
284 
294  public void newMapWithView(@Nullable final List<G> objects, @NotNull final A mapArchObject, @Nullable final Point viewPosition, @Nullable final MapFile mapFile) {
295  final MapControl<G, A, R> mapControl = mapManager.newMap(objects, mapArchObject, mapFile, true);
296  try {
297  newMapView(mapControl, viewPosition);
298  } finally {
299  mapManager.release(mapControl);
300  }
301  }
302 
308  public void closeMapView(@NotNull final MapView<G, A, R> mapView) {
309  final MapControl<G, A, R> mapControl = mapView.getMapControl();
310  if (getMapViews(mapControl) <= 1) {
311  if (!fileControl.confirmSaveChanges(mapControl)) {
312  return;
313  }
314  closeView(mapView);
315  assert mapControl.getUseCounter() <= 0;
316  mapManager.closeMap(mapControl);
317  } else {
318  closeView(mapView);
319  assert mapControl.getUseCounter() > 0;
320  }
321  }
322 
332  @NotNull
333  public MapView<G, A, R> openMapFileWithView(@NotNull final MapFile mapFile, @Nullable final Point viewPosition) throws IOException {
334  final MapControl<G, A, R> mapControl = mapManager.openMapFile(mapFile, true);
335  try {
336  final MapViews<G, A, R> mapViews = getMapViewsInt(mapControl);
337  final MapView<G, A, R> mapView = mapViews.getMapViewFrame();
338  if (mapView == null) {
339  return newMapView(mapControl, viewPosition);
340  }
341 
342  mapViews.raise(mapView);
343  return mapView;
344  } finally {
345  mapManager.release(mapControl);
346  }
347  }
348 
358  public void openMapFileWithView(@NotNull final File file, @Nullable final Point viewPosition, @Nullable final Point centerSquare) throws IOException {
359  final MapView<G, A, R> mapView = openMapFileWithView(pathManager.getMapFile(file), viewPosition);
360  if (viewPosition == null && centerSquare != null) {
361  mapView.centerMapView(centerSquare);
362  }
363  }
364 
368  private void refreshAllMaps() {
369  for (final MapControl<G, A, R> mapControl : mapManager.getOpenedMaps()) {
370  repaintAllViews(mapControl);
371  }
372  }
373 
378  private void addEditType(final int newType) {
379  for (final MapControl<G, A, R> mapControl : mapManager.getOpenedMaps()) {
380  mapControl.getMapModel().addActiveEditType(newType); // calculate new type
381  }
382  refreshAllMaps();
383  }
384 
385 }
net.sf.gridarta.model.mapmanager
Definition: AbstractMapManager.java:20
net.sf.gridarta.gui.map.mapview.MapViewsManager.repaintAllViews
void repaintAllViews(@NotNull final MapControl< G, A, R > mapControl)
Repaints all MapViews of a MapControl.
Definition: MapViewsManager.java:264
net.sf.gridarta.gui.map.mapview.MapViewsManager.mapViewSettingsListener
final MapViewSettingsListener mapViewSettingsListener
The listener tracking alpha types changes to repaint map views after changes.
Definition: MapViewsManager.java:91
net.sf.gridarta.model.mapmanager.MapManager
A MapManager manages all opened maps.
Definition: MapManager.java:37
net.sf.gridarta
Base package of all Gridarta classes.
net.sf.gridarta.gui.map.mapview.MapViewsManager.mapManagerListener
final MapManagerListener< G, A, R > mapManagerListener
The MapManagerListener attached to mapManager.
Definition: MapViewsManager.java:140
net.sf.gridarta.model.mapviewsettings.MapViewSettings.getEditType
int getEditType()
Returns the currently set edit type.
net.sf.gridarta.model.mapviewsettings
Definition: AbstractMapViewSettings.java:20
net.sf.gridarta.gui.map.mapview.MapViewsManager.pathManager
final PathManager pathManager
The PathManager for converting File instance.
Definition: MapViewsManager.java:59
net.sf.gridarta.model.mapviewsettings.MapViewSettings.addMapViewSettingsListener
void addMapViewSettingsListener(@NotNull MapViewSettingsListener listener)
Register a MapViewSettingsListener.
net.sf
net.sf.gridarta.gui.map.mapview.MapViewsManager.getMapViewIterator
Iterator< MapView< G, A, R > > getMapViewIterator(@NotNull final MapControl< G, A, R > mapControl)
Returns an Iterator returning all MapViews of a MapControl.
Definition: MapViewsManager.java:256
net.sf.gridarta.model.mapmanager.FileControl
Definition: FileControl.java:30
net.sf.gridarta.model.mapmanager.MapManagerListener
Interface for listeners listening to MapManager changes.
Definition: MapManagerListener.java:42
net.sf.gridarta.model.io.PathManager
This class contains methods for converting relative map paths to absolute map paths and vice versa.
Definition: PathManager.java:39
net.sf.gridarta.model.mapmodel
Definition: AboveFloorInsertionMode.java:20
net.sf.gridarta.gui.map.mapview.MapViewsManager.removeMapViewsListener
void removeMapViewsListener(@NotNull final MapControl< G, A, R > mapControl, @NotNull final MapViewsListener< G, A, R > listener)
Removes a MapViewsListener to be notified of events.
Definition: MapViewsManager.java:198
net.sf.gridarta.gui.map.mapview.MapViews
A list of MapViews for one MapControl instance.
Definition: MapViews.java:40
net.sf.gridarta.gui.map.mapview.MapViewsManager.addEditType
void addEditType(final int newType)
Calculates a new type for all opened maps.
Definition: MapViewsManager.java:378
net.sf.gridarta.model.mapmanager.MapManager.newMap
MapControl< G, A, R > newMap(@Nullable List< G > objects, @NotNull A mapArchObject, @Nullable MapFile mapFile, boolean interactive)
Creates a new map control without view.
net.sf.gridarta.gui.map.mapview.MapViewsManager.setFileControl
void setFileControl(@NotNull final FileControl< G, A, R > fileControl)
Definition: MapViewsManager.java:180
net.sf.gridarta.model.mapmanager.MapManager.addMapManagerListener
void addMapManagerListener(@NotNull MapManagerListener< G, A, R > listener)
Adds a MapManagerListener to be notified.
net.sf.gridarta.gui.map.mapview.MapViewsManager.MapViewsManager
MapViewsManager(@NotNull final MapViewSettings mapViewSettings, @NotNull final MapViewFactory< G, A, R > mapViewFactory, @NotNull final MapManager< G, A, R > mapManager, @NotNull final MapManager< G, A, R > pickmapManager, @NotNull final PathManager pathManager)
Creates a new instance.
Definition: MapViewsManager.java:169
net.sf.gridarta.model.archetype
Definition: AbstractArchetype.java:20
net.sf.gridarta.model.gameobject.GameObject
Reflects a game object (object on a map).
Definition: GameObject.java:36
net.sf.gridarta.model.mapviewsettings.MapViewSettings
Container for settings that affect the rendering of maps.
Definition: MapViewSettings.java:30
net.sf.gridarta.model.mapcontrol
Definition: DefaultMapControl.java:20
net.sf.gridarta.gui.map.mapview.MapViewsManager.closeView
void closeView(@NotNull final MapView< G, A, R > mapView)
Closes a view of a MapControl.
Definition: MapViewsManager.java:238
net.sf.gridarta.model.mapmanager.MapManager.getOpenedMaps
List< MapControl< G, A, R > > getOpenedMaps()
Returns all opened maps.
net.sf.gridarta.gui.map.mapview.MapViewsManager.refreshAllMaps
void refreshAllMaps()
Refreshes all map views.
Definition: MapViewsManager.java:368
net.sf.gridarta.model.gameobject
GameObjects are the objects based on Archetypes found on maps.
Definition: AbstractGameObject.java:20
net.sf.gridarta.gui.map.mapview.MapViewsManager.getMapViews
int getMapViews(@NotNull final MapControl< G, A, R > mapControl)
Returns the number of views of a MapControl.
Definition: MapViewsManager.java:217
net
net.sf.gridarta.gui.map.mapview.MapViewsManager.closeMapView
void closeMapView(@NotNull final MapView< G, A, R > mapView)
Invoked when the user wants to close a map view.
Definition: MapViewsManager.java:308
net.sf.gridarta.gui.map.mapview.MapViewsManager.getMapViewsInt
MapViews< G, A, R > getMapViewsInt(@NotNull final MapControl< G, A, R > mapControl)
Returns the MapViews instance for a MapControl.
Definition: MapViewsManager.java:274
net.sf.gridarta.model.mapmanager.FileControl.confirmSaveChanges
boolean confirmSaveChanges(@NotNull MapControl< G, A, R > mapControl)
Asks the user whether to save changes of a map.
net.sf.gridarta.gui.map.mapview.MapViewsManager.mapViewSettings
final MapViewSettings mapViewSettings
The MapViewSettings instance.
Definition: MapViewsManager.java:71
net.sf.gridarta.gui.map.mapview.MapViewsManager.newMapWithView
void newMapWithView(@Nullable final List< G > objects, @NotNull final A mapArchObject, @Nullable final Point viewPosition, @Nullable final MapFile mapFile)
Creates a new map control and an initial map view.
Definition: MapViewsManager.java:294
net.sf.gridarta.model.maparchobject.MapArchObject
Interface for MapArchObjects.
Definition: MapArchObject.java:40
net.sf.gridarta.model.io.PathManager.getMapFile
MapFile getMapFile(@NotNull final AbsoluteMapPath mapPath)
Returns a MapFile instance from an AbsoluteMapPath.
Definition: PathManager.java:72
net.sf.gridarta.model.mapmanager.MapManager.openMapFile
MapControl< G, A, R > openMapFile(@NotNull MapFile mapFile, boolean interactive)
Loads a map file.
net.sf.gridarta.gui.map.mapview.MapViewsManager.mapViewFactory
final MapViewFactory< G, A, R > mapViewFactory
The MapViewFactory to use.
Definition: MapViewsManager.java:77
net.sf.gridarta.gui.map.mapview.MapView< G, A, R >
net.sf.gridarta.gui.map.mapview.MapView.centerMapView
void centerMapView(@NotNull Point centerSquare)
Centers the map view on a map square.
net.sf.gridarta.gui.map.mapview.MapViewsListener< G, A, R >
net.sf.gridarta.model.mapmodel.MapFile
The location of a map file with a map directory.
Definition: MapFile.java:31
net.sf.gridarta.gui.map.mapview.MapViewsManager.newMapView
MapView< G, A, R > newMapView(@NotNull final MapControl< G, A, R > mapControl, @Nullable final Point viewPosition)
Creates a new map view.
Definition: MapViewsManager.java:229
net.sf.gridarta.model.io
Reading and writing of maps, handling of paths.
Definition: AbstractAnimationObjectsReader.java:20
net.sf.gridarta.model.mapmanager.MapManager.release
void release(@NotNull MapControl< G, A, R > mapControl)
Releases a MapControl instance.
net.sf.gridarta.gui.map.mapview.MapViewsManager.addMapViewsListener
void addMapViewsListener(@NotNull final MapControl< G, A, R > mapControl, @NotNull final MapViewsListener< G, A, R > listener)
Adds a MapViewsListener to be notified of events.
Definition: MapViewsManager.java:189
net.sf.gridarta.gui.map.mapview.MapViewsManager.mapViews
final Map< MapControl< G, A, R >, MapViews< G, A, R > > mapViews
All existing MapViews.
Definition: MapViewsManager.java:84
net.sf.gridarta.model
net.sf.gridarta.model.archetype.Archetype
Reflects an Archetype.
Definition: Archetype.java:41
net.sf.gridarta.model.mapviewsettings.MapViewSettingsListener
Interface for event listeners that are interested in changes on {}.
Definition: MapViewSettingsListener.java:31
net.sf.gridarta.gui.map.mapview.MapViewFactory< G, A, R >
net.sf.gridarta.model.mapcontrol.MapControl.getUseCounter
int getUseCounter()
Returns the use counter.
net.sf.gridarta.model.mapcontrol.MapControl
Currently nothing more than a marker interface for unification.
Definition: MapControl.java:35
net.sf.gridarta.gui.map.mapview.MapViewsManager.openMapFileWithView
void openMapFileWithView(@NotNull final File file, @Nullable final Point viewPosition, @Nullable final Point centerSquare)
Loads a map file and creates a map view.
Definition: MapViewsManager.java:358
net.sf.gridarta.model.maparchobject
Definition: AbstractMapArchObject.java:20
net.sf.gridarta.gui.map.mapview.MapViewsManager.openMapFileWithView
MapView< G, A, R > openMapFileWithView(@NotNull final MapFile mapFile, @Nullable final Point viewPosition)
Loads a map file and creates a map view.
Definition: MapViewsManager.java:333
net.sf.gridarta.gui.map.mapview.MapViewsManager.fileControl
FileControl< G, A, R > fileControl
The FileControl to use.
Definition: MapViewsManager.java:65
net.sf.gridarta.gui.map.mapview.MapViewsManager.setFocus
void setFocus(@NotNull final MapView< G, A, R > mapView)
Sets a MapView as the main view.
Definition: MapViewsManager.java:246
net.sf.gridarta.gui.map.mapview.MapViewsManager
Stores all existing MapViews.
Definition: MapViewsManager.java:47
net.sf.gridarta.gui.map.mapview.MapViewsManager.getMapViewFrame
MapView< G, A, R > getMapViewFrame(@NotNull final MapControl< G, A, R > mapControl)
Returns the last used view of a MapControl.
Definition: MapViewsManager.java:208
net.sf.gridarta.gui.map.mapview.MapViewsManager.mapManager
final MapManager< G, A, R > mapManager
The MapManager to use.
Definition: MapViewsManager.java:53
net.sf.gridarta.model.mapmanager.MapManager.closeMap
void closeMap(@NotNull MapControl< G, A, R > mapControl)
Closes the given map, ignoring modified status.