Gridarta Editor
EnterMap.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.mapactions;
21 
22 import java.awt.Component;
23 import java.awt.Dimension;
24 import java.awt.Point;
25 import java.awt.Rectangle;
26 import java.io.IOException;
27 import javax.swing.JOptionPane;
28 import javax.swing.JScrollPane;
42 import net.sf.gridarta.utils.Size2D;
43 import net.sf.japi.swing.action.ActionBuilder;
44 import net.sf.japi.swing.action.ActionBuilderFactory;
45 import org.jetbrains.annotations.NotNull;
46 import org.jetbrains.annotations.Nullable;
47 
52 public class EnterMap<G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> {
53 
57  @NotNull
58  private static final ActionBuilder ACTION_BUILDER = ActionBuilderFactory.getInstance().getActionBuilder("net.sf.gridarta");
59 
63  @NotNull
64  private final Component parent;
65 
69  @NotNull
70  private final Direction @NotNull [] directionMap;
71 
75  @NotNull
77 
81  @NotNull
83 
91  public EnterMap(@NotNull final Component parent, @NotNull final Direction @NotNull [] directionMap, @NotNull final FileControl<G, A, R> fileControl, @NotNull final MapViewsManager<G, A, R> mapViewsManager) {
92  this.parent = parent;
93  this.directionMap = directionMap.clone();
94  this.fileControl = fileControl;
95  this.mapViewsManager = mapViewsManager;
96  }
97 
107  public boolean enterMap(@NotNull final MapView<G, A, R> mapView, @NotNull final MapPath mapPath, @NotNull final Direction direction, @Nullable final Point destinationPoint) {
108  final MapFile mapFile;
109  try {
110  mapFile = mapView.getMapControl().getMapModel().getMapFile(mapPath);
111  } catch (final SameMapException ignored) {
112  // path points to the same map
113  if (destinationPoint != null) {
114  showLocation(mapView, destinationPoint);
115  }
116  return true;
117  } catch (final UnsavedMapException ignored) {
118  ACTION_BUILDER.showMessageDialog(parent, "enterExitNotSaved", mapPath);
119  return false;
120  }
121  return enterMap(mapView, mapFile, destinationPoint, direction);
122  }
123 
133  public boolean enterMap(@Nullable final MapView<G, A, R> mapView, @NotNull final MapFile mapFile, @Nullable final Point destinationPoint, @NotNull final Direction direction) {
134  final MapView<G, A, R> newMapView;
135  try {
136  newMapView = mapViewsManager.openMapFileWithView(mapFile, null);
137  } catch (final IOException ex) {
138  fileControl.reportLoadError(mapFile.getFile(), ex.getMessage());
139  return false;
140  }
141 
142  if (destinationPoint != null) {
143  showLocation(newMapView, destinationPoint);
144  } else if (mapView != null) {
145  newMapView.getScrollPane().getViewport().setViewPosition(calculateNewViewPosition(mapView.getScrollPane(), newMapView.getScrollPane(), direction));
146  newMapView.getMapCursor().setLocation(calculateNewCursorLocation(mapView.getMapCursor().getLocation(), newMapView.getMapControl().getMapModel().getMapArchObject().getMapSize(), direction));
147  }
148 
149  if (mapView != null && ACTION_BUILDER.showOnetimeConfirmDialog(parent, JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, "enterExitClose") == JOptionPane.YES_OPTION) {
150  // only close current map if a new file was opened and user wants to close it
151  mapViewsManager.closeMapView(mapView);
152  }
153 
154  return true;
155  }
156 
162  private void showLocation(@NotNull final MapView<G, A, R> mapView, @NotNull final Point point) {
163  final Point point2 = point.x == -1 && point.y == -1 ? mapView.getMapControl().getMapModel().getMapArchObject().getEnter() : point;
164  mapView.centerMapView(point2);
165  mapView.setCursorLocation(point2);
166  }
167 
176  public boolean enterExit(@NotNull final MapView<G, A, R> mapView, @NotNull final GameObject<G, A, R> exit, final boolean allowRandomMapParameters) {
177  final MapLocation mapLocation;
178  try {
179  mapLocation = new MapLocation(exit, allowRandomMapParameters);
180  } catch (final NoExitPathException ex) {
181  ACTION_BUILDER.showMessageDialog(parent, "enterExitRandomDestination", ex.getMessage());
182  return false;
183  }
184 
185  return enterMap(mapView, mapLocation.getMapPath(), Direction.NORTH, mapLocation.getMapCoordinate());
186  }
187 
195  @NotNull
196  private Point calculateNewViewPosition(@NotNull final JScrollPane oldMapView, @NotNull final JScrollPane newMapView, @NotNull final Direction direction) {
197  final Dimension newViewSize = newMapView.getViewport().getViewSize();
198  final Rectangle oldViewRectangle = oldMapView.getViewport().getViewRect();
199 
200  final Rectangle scrollTo;
201  switch (directionMap[direction.ordinal()]) {
202  case SOUTH:
203  scrollTo = new Rectangle(oldViewRectangle.x, 0, oldViewRectangle.width, oldViewRectangle.height);
204  break;
205 
206  case NORTH:
207  scrollTo = new Rectangle(oldViewRectangle.x, newViewSize.height - oldViewRectangle.height, oldViewRectangle.width, oldViewRectangle.height);
208  break;
209 
210  case EAST:
211  scrollTo = new Rectangle(0, oldViewRectangle.y, oldViewRectangle.width, oldViewRectangle.height);
212  break;
213 
214  case WEST:
215  scrollTo = new Rectangle(newViewSize.width - oldViewRectangle.width, oldViewRectangle.y, oldViewRectangle.width, oldViewRectangle.height);
216  break;
217 
218  case NORTH_EAST:
219  scrollTo = new Rectangle(0, newViewSize.height - oldViewRectangle.height, oldViewRectangle.width, oldViewRectangle.height);
220  break;
221 
222  case SOUTH_EAST:
223  scrollTo = new Rectangle(0, 0, oldViewRectangle.width, oldViewRectangle.height);
224  break;
225 
226  case SOUTH_WEST:
227  scrollTo = new Rectangle(newViewSize.width - oldViewRectangle.width, 0, oldViewRectangle.width, oldViewRectangle.height);
228  break;
229 
230  case NORTH_WEST:
231  scrollTo = new Rectangle(newViewSize.width - oldViewRectangle.width, newViewSize.height - oldViewRectangle.height, oldViewRectangle.width, oldViewRectangle.height);
232  break;
233 
234  case UP:
235  case DOWN:
236  scrollTo = new Rectangle(oldViewRectangle.x, oldViewRectangle.y, oldViewRectangle.width, oldViewRectangle.height);
237  break;
238 
239  default:
240  throw new AssertionError();
241  }
242 
243  if (scrollTo.x + scrollTo.width > newViewSize.width) {
244  scrollTo.x = newViewSize.width - scrollTo.width;
245  }
246  if (scrollTo.x < 0) {
247  scrollTo.x = 0;
248  }
249  if (scrollTo.y + scrollTo.height > newViewSize.height) {
250  scrollTo.y = newViewSize.height - scrollTo.height;
251  }
252  if (scrollTo.y < 0) {
253  scrollTo.y = 0;
254  }
255  return scrollTo.getLocation();
256  }
257 
265  @NotNull
266  public static Point calculateNewCursorLocation(@NotNull final Point oldCursorLocation, @NotNull final Size2D mapSize, @NotNull final Direction direction) {
267  return new Point((oldCursorLocation.x + direction.getDx() + mapSize.getWidth()) % mapSize.getWidth(), (oldCursorLocation.y + direction.getDy() + mapSize.getHeight()) % mapSize.getHeight());
268  }
269 
270 }
net.sf.gridarta.model.maplocation.MapLocation.getMapPath
MapPath getMapPath()
Returns the map path.
Definition: MapLocation.java:116
net.sf.gridarta.model.direction.Direction
A direction.
Definition: Direction.java:28
net.sf.gridarta.model.mapmanager
Definition: AbstractMapManager.java:20
net.sf.gridarta.model.mapmodel.SameMapException
Exception thrown if the destination path points to the source map.
Definition: SameMapException.java:26
net.sf.gridarta.model.mapmodel.MapModel.getMapArchObject
A getMapArchObject()
Returns the Map Arch Object with the meta information about the map.
net.sf.gridarta.gui.map.mapview.MapView.getMapControl
MapControl< G, A, R > getMapControl()
Return the controller of this view.
net.sf.gridarta
Base package of all Gridarta classes.
net.sf
net.sf.gridarta.model.mapmanager.FileControl
Definition: FileControl.java:30
net.sf.gridarta.gui.map.mapactions.EnterMap.mapViewsManager
final MapViewsManager< G, A, R > mapViewsManager
The MapViewsManager.
Definition: EnterMap.java:82
net.sf.gridarta.model.mapmodel
Definition: AboveFloorInsertionMode.java:20
net.sf.gridarta.model.archetype
Definition: AbstractArchetype.java:20
net.sf.gridarta.model.mapmanager.FileControl.reportLoadError
void reportLoadError(@Nullable File file, @NotNull String message)
net.sf.gridarta.model.gameobject.GameObject
Reflects a game object (object on a map).
Definition: GameObject.java:36
net.sf.gridarta.gui.map.mapactions.EnterMap.calculateNewViewPosition
Point calculateNewViewPosition(@NotNull final JScrollPane oldMapView, @NotNull final JScrollPane newMapView, @NotNull final Direction direction)
Calculate the view position for the new viewport.
Definition: EnterMap.java:196
net.sf.gridarta.gui.map.mapactions.EnterMap
Helper class for entering maps.
Definition: EnterMap.java:52
net.sf.gridarta.gui
Graphical User Interface of Gridarta.
net.sf.gridarta.model.direction.Direction.NORTH
NORTH
North.
Definition: Direction.java:33
net.sf.gridarta.gui.map.mapview.MapView.getMapCursor
MapCursor< G, A, R > getMapCursor()
Returns the MapCursor of this view.
net.sf.gridarta.gui.map.mapactions.EnterMap.directionMap
final Direction[] directionMap
Maps map relative direction to map window direction.
Definition: EnterMap.java:70
net.sf.gridarta.model.maplocation.MapLocation.getMapCoordinate
Point getMapCoordinate()
Returns the map coordinate.
Definition: MapLocation.java:125
net.sf.gridarta.model.gameobject
GameObjects are the objects based on Archetypes found on maps.
Definition: AbstractGameObject.java:20
net.sf.gridarta.gui.map.mapactions.EnterMap.EnterMap
EnterMap(@NotNull final Component parent, @NotNull final Direction @NotNull[] directionMap, @NotNull final FileControl< G, A, R > fileControl, @NotNull final MapViewsManager< G, A, R > mapViewsManager)
Creates a new instance.
Definition: EnterMap.java:91
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.model.maparchobject.MapArchObject
Interface for MapArchObjects.
Definition: MapArchObject.java:40
net.sf.gridarta.model.maplocation.NoExitPathException
Exception thrown if a game object does not specify a valid exit path.
Definition: NoExitPathException.java:29
net.sf.gridarta.gui.map.mapview
Definition: AbstractMapView.java:20
net.sf.gridarta.gui.map.mapactions.EnterMap.enterMap
boolean enterMap(@NotNull final MapView< G, A, R > mapView, @NotNull final MapPath mapPath, @NotNull final Direction direction, @Nullable final Point destinationPoint)
Enters a map wanted.
Definition: EnterMap.java:107
net.sf.gridarta.gui.map.mapview.MapView
A map view consists of a map grid and a map cursor, and is attached to a map control.
Definition: MapView.java:43
net.sf.gridarta.model.mapmodel.MapPath
Represents a maps directory local map path.
Definition: MapPath.java:31
net.sf.gridarta.model.maplocation.MapLocation
Represents a location on a map consisting of a map path and a map coordinate.
Definition: MapLocation.java:42
net.sf.gridarta.gui.map.mapactions.EnterMap.enterExit
boolean enterExit(@NotNull final MapView< G, A, R > mapView, @NotNull final GameObject< G, A, R > exit, final boolean allowRandomMapParameters)
Opens the map an exit game object points to.
Definition: EnterMap.java:176
net.sf.gridarta.gui.map.mapactions.EnterMap.calculateNewCursorLocation
static Point calculateNewCursorLocation(@NotNull final Point oldCursorLocation, @NotNull final Size2D mapSize, @NotNull final Direction direction)
Calculate the map cursor location for the new viewport.
Definition: EnterMap.java:266
net.sf.gridarta.model.mapmodel.UnsavedMapException
Exception thrown if an operation is attempted on an unsaved map.
Definition: UnsavedMapException.java:26
net.sf.gridarta.gui.map.mapview.MapView.getScrollPane
JScrollPane getScrollPane()
Returns the JScrollPane of this map view.
net.sf.gridarta.model.mapcursor.MapCursor.setLocation
void setLocation(@NotNull final Point p)
Move cursor to a new location.
Definition: MapCursor.java:236
net.sf.gridarta.gui.map.mapactions.EnterMap.ACTION_BUILDER
static final ActionBuilder ACTION_BUILDER
Action Builder to create Actions.
Definition: EnterMap.java:58
net.sf.gridarta.gui.map.mapactions.EnterMap.showLocation
void showLocation(@NotNull final MapView< G, A, R > mapView, @NotNull final Point point)
Scrolls a map view to make a give tile visible.
Definition: EnterMap.java:162
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.mapactions.EnterMap.parent
final Component parent
The component for showing dialog boxes.
Definition: EnterMap.java:64
net.sf.gridarta.model
net.sf.gridarta.model.archetype.Archetype
Reflects an Archetype.
Definition: Archetype.java:41
net.sf.gridarta.gui.map
Base classes for rendering maps.
Definition: AbstractPerMapDialogManager.java:20
net.sf.gridarta.model.mapcontrol.MapControl.getMapModel
MapModel< G, A, R > getMapModel()
Returns the map model.
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.mapactions.EnterMap.enterMap
boolean enterMap(@Nullable final MapView< G, A, R > mapView, @NotNull final MapFile mapFile, @Nullable final Point destinationPoint, @NotNull final Direction direction)
Enters a map.
Definition: EnterMap.java:133
net.sf.gridarta.gui.map.mapview.MapViewsManager
Stores all existing MapViews.
Definition: MapViewsManager.java:47
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.model.maplocation
Definition: MapLocation.java:20
net.sf.gridarta.model.direction
Definition: Direction.java:20
net.sf.gridarta.gui.map.mapactions.EnterMap.fileControl
final FileControl< G, A, R > fileControl
The FileControl.
Definition: EnterMap.java:76