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-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.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[] directionMap;
71 
75  @NotNull
77 
81  @NotNull
83 
91  public EnterMap(@NotNull final Component parent, @NotNull final Direction[] 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, destinationPoint);
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  public 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  if (!mapView.getMapControl().getMapModel().getMapArchObject().isPointValid(point2)) {
165  ACTION_BUILDER.showMessageDialog(parent, "enterExitOutside");
166  }
167 
168  mapView.setCursorLocation(point2);
169  }
170 
179  public boolean enterExit(@NotNull final MapView<G, A, R> mapView, @NotNull final GameObject<G, A, R> exit, final boolean allowRandomMapParameters) {
180  final MapLocation mapLocation;
181  try {
182  mapLocation = new MapLocation(exit, allowRandomMapParameters);
183  } catch (final NoExitPathException ex) {
184  ACTION_BUILDER.showMessageDialog(parent, "enterExitRandomDestination", ex.getMessage());
185  return false;
186  }
187 
188  return enterMap(mapView, mapLocation.getMapPath(), Direction.NORTH, mapLocation.getMapCoordinate());
189  }
190 
199  @NotNull
200  private Point calculateNewViewPosition(@NotNull final JScrollPane oldMapView, @NotNull final JScrollPane newMapView, @NotNull final Direction direction) {
201  final Dimension newViewSize = newMapView.getViewport().getViewSize();
202  final Rectangle oldViewRectangle = oldMapView.getViewport().getViewRect();
203 
204  final Rectangle scrollTo;
205  switch (directionMap[direction.ordinal()]) {
206  case SOUTH:
207  scrollTo = new Rectangle(oldViewRectangle.x, 0, oldViewRectangle.width, oldViewRectangle.height);
208  break;
209 
210  case NORTH:
211  scrollTo = new Rectangle(oldViewRectangle.x, newViewSize.height - oldViewRectangle.height, oldViewRectangle.width, oldViewRectangle.height);
212  break;
213 
214  case EAST:
215  scrollTo = new Rectangle(0, oldViewRectangle.y, oldViewRectangle.width, oldViewRectangle.height);
216  break;
217 
218  case WEST:
219  scrollTo = new Rectangle(newViewSize.width - oldViewRectangle.width, oldViewRectangle.y, oldViewRectangle.width, oldViewRectangle.height);
220  break;
221 
222  case NORTH_EAST:
223  scrollTo = new Rectangle(0, newViewSize.height - oldViewRectangle.height, oldViewRectangle.width, oldViewRectangle.height);
224  break;
225 
226  case SOUTH_EAST:
227  scrollTo = new Rectangle(0, 0, oldViewRectangle.width, oldViewRectangle.height);
228  break;
229 
230  case SOUTH_WEST:
231  scrollTo = new Rectangle(newViewSize.width - oldViewRectangle.width, 0, oldViewRectangle.width, oldViewRectangle.height);
232  break;
233 
234  case NORTH_WEST:
235  scrollTo = new Rectangle(newViewSize.width - oldViewRectangle.width, newViewSize.height - oldViewRectangle.height, oldViewRectangle.width, oldViewRectangle.height);
236  break;
237 
238  case UP:
239  case DOWN:
240  scrollTo = new Rectangle(oldViewRectangle.x, oldViewRectangle.y, oldViewRectangle.width, oldViewRectangle.height);
241  break;
242 
243  default:
244  throw new AssertionError();
245  }
246 
247  if (scrollTo.x + scrollTo.width > newViewSize.width) {
248  scrollTo.x = newViewSize.width - scrollTo.width;
249  }
250  if (scrollTo.x < 0) {
251  scrollTo.x = 0;
252  }
253  if (scrollTo.y + scrollTo.height > newViewSize.height) {
254  scrollTo.y = newViewSize.height - scrollTo.height;
255  }
256  if (scrollTo.y < 0) {
257  scrollTo.y = 0;
258  }
259  return scrollTo.getLocation();
260  }
261 
270  @NotNull
271  public static Point calculateNewCursorLocation(@NotNull final Point oldCursorLocation, @NotNull final Size2D mapSize, @NotNull final Direction direction) {
272  return new Point((oldCursorLocation.x + direction.getDx() + mapSize.getWidth()) % mapSize.getWidth(), (oldCursorLocation.y + direction.getDy() + mapSize.getHeight()) % mapSize.getHeight());
273  }
274 
275 }
EnterMap(@NotNull final Component parent, @NotNull final Direction[] directionMap, @NotNull final FileControl< G, A, R > fileControl, @NotNull final MapViewsManager< G, A, R > mapViewsManager)
Creates a new instance.
Definition: EnterMap.java:91
JScrollPane getScrollPane()
Returns the JScrollPane of this map view.
Exception thrown if the destination path points to the source map.
void closeMapView(@NotNull final MapView< G, A, R > mapView)
Invoked when the user wants to close a map view.
Graphical User Interface of Gridarta.
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:271
Exception thrown if a game object does not specify a valid exit path.
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
final Direction [] directionMap
Maps map relative direction to map window direction.
Definition: EnterMap.java:70
final MapViewsManager< G, A, R > mapViewsManager
The MapViewsManager.
Definition: EnterMap.java:82
Represents a maps directory local map path.
Definition: MapPath.java:31
MapControl< G, A, R > getMapControl()
Return the controller of this view.
MapModel< G, A, R > getMapModel()
Returns the map model.
void setLocation(@NotNull final Point p)
Move cursor to a new location.
Definition: MapCursor.java:235
Base package of all Gridarta classes.
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:200
Reflects a game object (object on a map).
Definition: GameObject.java:36
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:179
void reportLoadError(@Nullable File file, @NotNull String message)
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.
MapPath getMapPath()
Returns the map path.
GameObjects are the objects based on Archetypes found on maps.
final Component parent
The component for showing dialog boxes.
Definition: EnterMap.java:64
Base classes for rendering maps.
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
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.
MapCursor< G, A, R > getMapCursor()
Returns the MapCursor of this view.
A map view consists of a map grid and a map cursor, and is attached to a map control.
Definition: MapView.java:43
Represents a location on a map consisting of a map path and a map coordinate.
final FileControl< G, A, R > fileControl
The FileControl.
Definition: EnterMap.java:76
static final ActionBuilder ACTION_BUILDER
Action Builder to create Actions.
Definition: EnterMap.java:58
Helper class for entering maps.
Definition: EnterMap.java:52
Point getMapCoordinate()
Returns the map coordinate.
The location of a map file with a map directory.
Definition: MapFile.java:31
The class Size2D represents a 2d rectangular area.
Definition: Size2D.java:30
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