Gridarta Editor
MapCursorActions.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.mapcursor;
21 
22 import java.awt.Point;
23 import java.util.Set;
24 import javax.swing.Action;
40 import net.sf.gridarta.utils.Size2D;
41 import net.sf.japi.swing.action.ActionMethod;
42 import org.jetbrains.annotations.NotNull;
43 import org.jetbrains.annotations.Nullable;
44 
49 public class MapCursorActions<G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> implements EditorAction, MapCursorListener<G, A, R>, MapViewManagerListener<G, A, R> {
50 
55  private static final int BORDER = 3;
56 
60  @NotNull
61  private static final Direction[] DIRECTIONS = Direction.values();
62 
63  @NotNull
65 
69  @NotNull
70  private final Action[] aMoveCursor;
71 
75  @Nullable
77 
82  @NotNull
84 
85  @Override
86  public void mapSizeChanged(@NotNull final Size2D newSize) {
88  }
89 
90  @Override
91  public void mapSquaresChanged(@NotNull final Set<MapSquare<G, A, R>> mapSquares) {
92  // ignore
93  }
94 
95  @Override
96  public void mapObjectsChanged(@NotNull final Set<G> gameObjects, @NotNull final Set<G> transientGameObjects) {
97  // ignore
98  }
99 
100  @Override
101  public void errorsChanged(@NotNull final ErrorCollector<G, A, R> errors) {
102  // ignore
103  }
104 
105  @Override
106  public void mapFileChanged(@Nullable final MapFile oldMapFile) {
107  // ignore
108  }
109 
110  @Override
111  public void modifiedChanged() {
112  // ignore
113  }
114 
115  };
116 
120  public MapCursorActions(@NotNull final MapActions<G, A, R> mapActions) {
121  this.mapActions = mapActions;
122  aMoveCursor = new Action[DIRECTIONS.length];
123  refreshActions();
124  }
125 
129  @ActionMethod
130  public void moveCursorNorth() {
132  }
133 
137  @ActionMethod
138  public void moveCursorSouth() {
140  }
141 
145  @ActionMethod
146  public void moveCursorEast() {
147  doMoveCursor(true, Direction.EAST);
148  }
149 
153  @ActionMethod
154  public void moveCursorWest() {
155  doMoveCursor(true, Direction.WEST);
156  }
157 
161  @ActionMethod
162  public void moveCursorNorthEast() {
164  }
165 
169  @ActionMethod
170  public void moveCursorNorthWest() {
172  }
173 
177  @ActionMethod
178  public void moveCursorSouthEast() {
180  }
181 
185  @ActionMethod
186  public void moveCursorSouthWest() {
188  }
189 
193  private void refreshActions() {
194  for (final Direction direction : DIRECTIONS) {
195  final Action action = aMoveCursor[direction.ordinal()];
196  if (action != null) {
197  action.setEnabled(doMoveCursor(false, direction));
198  }
199  }
200  }
201 
208  private boolean doMoveCursor(final boolean performAction, @NotNull final Direction direction) {
209  final MapView<G, A, R> mapView = currentMapView;
210  if (mapView == null) {
211  return false;
212  }
213 
214  final MapCursor<G, A, R> mapCursor = mapView.getMapCursor();
215  if (!mapCursor.goTo(performAction, direction)) {
216  if (!mapActions.doEnterMap(performAction, direction)) {
217  return false;
218  }
219  } else if (performAction) {
220  final MapRenderer renderer = mapView.getRenderer();
221  final Point location = mapCursor.getLocation();
222  location.translate(BORDER * direction.getDx(), BORDER * direction.getDy());
223  renderer.scrollRectToVisible(renderer.getSquareBounds(location));
224  }
225 
226  return true;
227  }
228 
229  @Override
230  public void setAction(@NotNull final Action action, @NotNull final String name) {
231  if (name.startsWith("moveCursor")) {
232  boolean found = false;
233  for (final Direction direction : DIRECTIONS) {
234  if (name.equals("moveCursor" + direction.getId())) {
235  aMoveCursor[direction.ordinal()] = action;
236  found = true;
237  break;
238  }
239  }
240  if (!found) {
241  throw new IllegalArgumentException();
242  }
243  } else {
244  throw new IllegalArgumentException();
245  }
246  refreshActions();
247  }
248 
249  @Override
250  public void activeMapViewChanged(@Nullable final MapView<G, A, R> mapView) {
251  currentMapView = mapView;
252  refreshActions();
253  }
254 
255  @Override
256  public void mapViewCreated(@NotNull final MapView<G, A, R> mapView) {
257  mapView.getMapControl().getMapModel().addMapModelListener(mapModelListener);
258  }
259 
260  @Override
261  public void mapViewClosing(@NotNull final MapView<G, A, R> mapView) {
262  mapView.getMapControl().getMapModel().removeMapModelListener(mapModelListener);
263  }
264 
265  @Override
266  public void mapCursorChangedPos(@NotNull final Point location) {
267  refreshActions();
268  }
269 
270  @Override
271  public void mapCursorChangedMode() {
272  // ignore
273  }
274 
275  @Override
276  public void mapCursorChangedGameObject(@Nullable final MapSquare<G, A, R> mapSquare, @Nullable final G gameObject) {
277  // ignore
278  }
279 
280  @Override
281  public void mapCursorChangedSize() {
282  refreshActions();
283  }
284 
285 }
void mapCursorChangedMode()
This event handler is called when MapCursor changes mode (drag, select).
void activeMapViewChanged(@Nullable final MapView< G, A, R > mapView)
This event handler is called when the current map view has changed.
Rectangle getSquareBounds(@NotNull Point p)
Returns coordinates, length and width of map square.
void moveCursorNorth()
Action method for "move cursor north".
Graphical User Interface of Gridarta.
boolean doMoveCursor(final boolean performAction, @NotNull final Direction direction)
Executes the "move cursor" action.
This package contains the framework for validating maps.
void setAction(@NotNull final Action action, @NotNull final String name)
Sets the Action instance for this editor action.
boolean doEnterMap(final boolean performAction, @NotNull final Direction direction)
Executes the "enter map" action.
Interface for listeners listening on MapModel events.
MapCursor provides methods to move and drag on map.
Definition: MapCursor.java:57
void mapCursorChangedGameObject(@Nullable final MapSquare< G, A, R > mapSquare, @Nullable final G gameObject)
Called whenever the selected game object has changed.
void mapViewClosing(@NotNull final MapView< G, A, R > mapView)
This event handler is called when a map view is to be closed.
MapView< G, A, R > currentMapView
The active map view, or.
Point getLocation()
Get position of cursor.
Definition: MapCursor.java:226
Base package of all Gridarta classes.
Reflects a game object (object on a map).
Definition: GameObject.java:36
static final Direction [] DIRECTIONS
All Directions.
void moveCursorNorthEast()
Action method for "move cursor north east".
A global editor action.
void mapViewCreated(@NotNull final MapView< G, A, R > mapView)
This event handler is called when a map view was created.
GameObjects are the objects based on Archetypes found on maps.
MapRenderer getRenderer()
Returns the MapRenderer for this view.
void mapCursorChangedSize()
Called whenever the map cursor&#39;s map grid size has changed.
Interface for listeners interested in events related to MapViewManager instances. ...
void moveCursorSouth()
Action method for "move cursor south".
void refreshActions()
Enable/disable menu entries based on the current cursor state.
void moveCursorSouthWest()
Action method for "move cursor south west".
Manages actions in the "map" menu.
Definition: MapActions.java:70
Base classes for rendering maps.
void mapCursorChangedPos(@NotNull final Point location)
This event handler is called when MapCursor has moved.
void moveCursorEast()
Action method for "move cursor east".
final MapModelListener< G, A, R > mapModelListener
The map model listener used to detect map size changes in currentMapView.
MapCursor< G, A, R > getMapCursor()
Returns the MapCursor of this view.
void moveCursorSouthEast()
Action method for "move cursor south east".
static final int BORDER
The visible border around the cursor.
void moveCursorNorthWest()
Action method for "move cursor north west".
A map view consists of a map grid and a map cursor, and is attached to a map control.
Definition: MapView.java:43
boolean goTo(final boolean performAction, @NotNull final Direction dir)
Moves the cursor one square relative to current position.
Definition: MapCursor.java:389
An interface for classes that collect errors.
Common interface for renderers of map control instances.
Interface for listeners listening to MapCursor related events.
void moveCursorWest()
Action method for "move cursor west".
final Action [] aMoveCursor
Actions for "move cursor".
The location of a map file with a map directory.
Definition: MapFile.java:31
void scrollRectToVisible(@NotNull Rectangle aRect)
Ensures that a rectangular area is visible.
The class Size2D represents a 2d rectangular area.
Definition: Size2D.java:30
MapCursorActions(@NotNull final MapActions< G, A, R > mapActions)
Creates a new instance.