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-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.gui.map.mapview;
21 
22 import java.awt.Point;
23 import java.io.File;
24 import java.io.IOException;
25 import java.util.IdentityHashMap;
26 import java.util.Iterator;
27 import java.util.List;
28 import java.util.Map;
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 IdentityHashMap<>();
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  assert getMapViews(mapControl) <= 0;
158  mapViews.remove(mapControl);
159  }
160 
161  };
162 
171  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) {
172  this.mapViewSettings = mapViewSettings;
173  this.mapViewFactory = mapViewFactory;
174  this.mapManager = mapManager;
175  this.pathManager = pathManager;
176  mapViewSettings.addMapViewSettingsListener(mapViewSettingsListener);
177  mapManager.addMapManagerListener(mapManagerListener);
178  pickmapManager.addMapManagerListener(mapManagerListener);
179  }
180 
181  @Deprecated
182  public void setFileControl(@NotNull final FileControl<G, A, R> fileControl) {
183  this.fileControl = fileControl;
184  }
185 
191  public void addMapViewsListener(@NotNull final MapControl<G, A, R> mapControl, @NotNull final MapViewsListener<G, A, R> listener) {
192  getMapViewsInt(mapControl).addMapViewsListener(listener);
193  }
194 
200  public void removeMapViewsListener(@NotNull final MapControl<G, A, R> mapControl, @NotNull final MapViewsListener<G, A, R> listener) {
201  getMapViewsInt(mapControl).removeMapViewsListener(listener);
202  }
203 
209  @Nullable
210  public MapView<G, A, R> getMapViewFrame(@NotNull final MapControl<G, A, R> mapControl) {
211  return getMapViewsInt(mapControl).getMapViewFrame();
212  }
213 
219  public int getMapViews(@NotNull final MapControl<G, A, R> mapControl) {
220  return getMapViewsInt(mapControl).getMapViews();
221  }
222 
232  @NotNull
233  public MapView<G, A, R> newMapView(@NotNull final MapControl<G, A, R> mapControl, @Nullable final Point viewPosition, @Nullable final Point centerSquare) {
234  mapControl.getMapModel().addActiveEditType(mapViewSettings.getEditType());
235  return getMapViewsInt(mapControl).newMapView(mapControl, viewPosition, centerSquare, mapViewFactory);
236  }
237 
242  public void closeView(@NotNull final MapView<G, A, R> mapView) {
243  getMapViewsInt(mapView.getMapControl()).closeView(mapView);
244  }
245 
250  public void setFocus(@NotNull final MapView<G, A, R> mapView) {
251  getMapViewsInt(mapView.getMapControl()).setFocus(mapView);
252  }
253 
260  public Iterator<MapView<G, A, R>> getMapViewIterator(@NotNull final MapControl<G, A, R> mapControl) {
261  return getMapViewsInt(mapControl).getMapViewIterator();
262  }
263 
268  public void repaintAllViews(@NotNull final MapControl<G, A, R> mapControl) {
269  getMapViewsInt(mapControl).repaintAllViews();
270  }
271 
277  @NotNull
278  private MapViews<G, A, R> getMapViewsInt(@NotNull final MapControl<G, A, R> mapControl) {
279  final MapViews<G, A, R> existingMapViews = mapViews.get(mapControl);
280  if (existingMapViews != null) {
281  return existingMapViews;
282  }
283 
284  final MapViews<G, A, R> newMapViews = new MapViews<>();
285  mapViews.put(mapControl, newMapViews);
286  return newMapViews;
287  }
288 
298  public void newMapWithView(@Nullable final List<G> objects, @NotNull final A mapArchObject, @Nullable final Point viewPosition, @Nullable final MapFile mapFile) {
299  final MapControl<G, A, R> mapControl = mapManager.newMap(objects, mapArchObject, mapFile, true);
300  try {
301  newMapView(mapControl, viewPosition, null);
302  } finally {
303  mapManager.release(mapControl);
304  }
305  }
306 
312  public void closeMapView(@NotNull final MapView<G, A, R> mapView) {
313  final MapControl<G, A, R> mapControl = mapView.getMapControl();
314  if (getMapViews(mapControl) <= 1) {
315  if (!fileControl.confirmSaveChanges(mapControl)) {
316  return;
317  }
318  closeView(mapView);
319  assert mapControl.getUseCounter() <= 0;
320  mapManager.closeMap(mapControl);
321  } else {
322  closeView(mapView);
323  assert mapControl.getUseCounter() > 0;
324  }
325  }
326 
337  @NotNull
338  public MapView<G, A, R> openMapFileWithView(@NotNull final MapFile mapFile, @Nullable final Point viewPosition, @Nullable final Point centerSquare) throws IOException {
339  final MapControl<G, A, R> mapControl = mapManager.openMapFile(mapFile, true);
340  try {
341  return newMapView(mapControl, viewPosition, centerSquare);
342  } finally {
343  mapManager.release(mapControl);
344  }
345  }
346 
356  public void openMapFileWithView(@NotNull final File file, @Nullable final Point viewPosition, @Nullable final Point centerSquare) throws IOException {
357  openMapFileWithView(pathManager.getMapFile(file), viewPosition, centerSquare);
358  }
359 
363  public void refreshAllMaps() {
364  for (final MapControl<G, A, R> mapControl : mapManager.getOpenedMaps()) {
365  repaintAllViews(mapControl);
366  }
367  }
368 
373  public void addEditType(final int newType) {
374  for (final MapControl<G, A, R> mapControl : mapManager.getOpenedMaps()) {
375  mapControl.getMapModel().addActiveEditType(newType); // calculate new type
376  }
377  refreshAllMaps();
378  }
379 
380 }
int getEditType()
Returns the currently set edit type.
final PathManager pathManager
The PathManager for converting File instance.
void addMapViewsListener(@NotNull final MapControl< G, A, R > mapControl, @NotNull final MapViewsListener< G, A, R > listener)
Adds a MapViewsListener to be notified of events.
void removeMapViewsListener(@NotNull final MapControl< G, A, R > mapControl, @NotNull final MapViewsListener< G, A, R > listener)
Removes a MapViewsListener to be notified of events.
This class contains methods for converting relative map paths to absolute map paths and vice versa...
A MapManager manages all opened maps.
Definition: MapManager.java:37
void closeMapView(@NotNull final MapView< G, A, R > mapView)
Invoked when the user wants to close a map view.
void release(@NotNull MapControl< G, A, R > mapControl)
Releases a MapControl instance.
Reading and writing of maps, handling of paths.
MapControl< G, A, R > newMap(@Nullable List< G > objects, @NotNull A mapArchObject, @Nullable MapFile mapFile, boolean interactive)
Creates a new map control without view.
void addEditType(final int newType)
Calculate a new type for all opened maps.
void repaintAllViews(@NotNull final MapControl< G, A, R > mapControl)
Repaints all MapViews of a MapControl.
int getUseCounter()
Returns the use counter.
MapView< G, A, R > newMapView(@NotNull final MapControl< G, A, R > mapControl, @Nullable final Point viewPosition, @Nullable final Point centerSquare)
Creates a new map view.
void refreshAllMaps()
Refreshes all map views.
MapFile getMapFile(@NotNull final AbsoluteMapPath mapPath)
Returns a MapFile instance from an AbsoluteMapPath.
A list of MapViews for one MapControl instance.
Definition: MapViews.java:42
void newMapWithView(@Nullable final List< G > objects, @NotNull final A mapArchObject, @Nullable final Point viewPosition, @Nullable final MapFile mapFile)
Create a new map control and an initial map view.
void openMapFileWithView(@NotNull final File file, @Nullable final Point viewPosition, @Nullable final Point centerSquare)
Load a map file and create a map view.
Base package of all Gridarta classes.
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.
final MapManager< G, A, R > mapManager
The MapManager to use.
Reflects a game object (object on a map).
Definition: GameObject.java:36
final MapManagerListener< G, A, R > mapManagerListener
The MapManagerListener attached to mapManager.
MapView< G, A, R > openMapFileWithView(@NotNull final MapFile mapFile, @Nullable final Point viewPosition, @Nullable final Point centerSquare)
Load a map file and create a map view.
int getMapViews(@NotNull final MapControl< G, A, R > mapControl)
Returns the number of views of a MapControl.
MapViews< G, A, R > getMapViewsInt(@NotNull final MapControl< G, A, R > mapControl)
Returns the MapViews instance for a MapControl.
Interface for listeners listening to MapManager changes.
Interface for event listeners that are interested in changes on MapViewSettings.
Container for settings that affect the rendering of maps.
GameObjects are the objects based on Archetypes found on maps.
final MapViewSettings mapViewSettings
The MapViewSettings instance.
MapView< G, A, R > getMapViewFrame(@NotNull final MapControl< G, A, R > mapControl)
Returns the last used view of a MapControl.
Currently nothing more than a marker interface for unification.
Definition: MapControl.java:35
void closeMap(@NotNull MapControl< G, A, R > mapControl)
Closes the given map, ignoring modified status.
final MapViewSettingsListener mapViewSettingsListener
The listener tracking alpha types changes to repaint map views after changes.
FileControl< G, A, R > fileControl
The FileControl to use.
void setFileControl(@NotNull final FileControl< G, A, R > fileControl)
MapControl< G, A, R > openMapFile(@NotNull MapFile mapFile, boolean interactive)
Loads a map file.
Iterator< MapView< G, A, R > > getMapViewIterator(@NotNull final MapControl< G, A, R > mapControl)
Returns an Iterator returning all MapViews of a MapControl.
boolean confirmSaveChanges(@NotNull MapControl< G, A, R > mapControl)
Asks the user whether to save changes of a map.
void closeView(@NotNull final MapView< G, A, R > mapView)
Closes a view of a MapControl.
final Map< MapControl< G, A, R >, MapViews< G, A, R > > mapViews
All existing MapViews.
List< MapControl< G, A, R > > getOpenedMaps()
Returns all opened maps.
The location of a map file with a map directory.
Definition: MapFile.java:31
final MapViewFactory< G, A, R > mapViewFactory
The MapViewFactory to use.
void setFocus(@NotNull final MapView< G, A, R > mapView)
Sets a MapView as the main view.