Gridarta Editor
ShrinkSelectionAction.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.action;
21 
22 import java.awt.Point;
23 import java.util.IdentityHashMap;
24 import java.util.List;
25 import java.util.Map;
26 import javax.swing.Action;
39 import net.sf.japi.swing.action.ActionMethod;
40 import org.jetbrains.annotations.NotNull;
41 import org.jetbrains.annotations.Nullable;
42 
48 public class ShrinkSelectionAction<G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> implements EditorAction, MapViewManagerListener<G, A, R> {
49 
53  @Nullable
55 
60  @Nullable
61  private Action action;
62 
66  @NotNull
68 
69  @Override
70  public void mapGridChanged(@NotNull final MapGridEvent e) {
71  updateAction();
72  }
73 
74  @Override
75  public void mapGridResized(@NotNull final MapGridEvent e) {
76  updateAction();
77  }
78 
79  };
80 
84  @ActionMethod
85  public void shrinkSelection() {
86  doShrinkSelection(true);
87  }
88 
89  @Override
90  public void setAction(@NotNull final Action action, @NotNull final String name) {
91  this.action = action;
92  updateAction();
93  }
94 
98  private void updateAction() {
99  if (action != null) {
100  //noinspection ConstantConditions
101  action.setEnabled(doShrinkSelection(false));
102  }
103  }
104 
110  private boolean doShrinkSelection(final boolean performAction) {
111  final MapView<G, A, R> mapView = currentMapView;
112  if (currentMapView == null) {
113  return false;
114  }
115 
116  final MapGrid mapGrid = mapView.getMapGrid();
117  if (mapGrid.getSelectedRec() == null) {
118  return false;
119  }
120 
121  final List<MapSquare<G, A, R>> selectedSquares = mapView.getSelectedSquares();
122  if (selectedSquares.isEmpty()) {
123  return false;
124  }
125 
126  if (performAction) {
127  final MapModel<G, A, R> mapModel = mapView.getMapControl().getMapModel();
128  final MapArchObject<A> mapArchObject = mapModel.getMapArchObject();
129  final Point point = new Point();
130  final Map<MapSquare<G, A, R>, Void> mapSquaresToShrink = new IdentityHashMap<>();
131  mapGrid.beginTransaction();
132  try {
133  for (final MapSquare<G, A, R> mapSquare : selectedSquares) {
134 LOOP:
135  for (int dy = -1; dy <= 1; dy++) {
136  for (int dx = -1; dx <= 1; dx++) {
137  if (dx != 0 || dy != 0) {
138  mapSquare.getMapLocation(point, dx, dy);
139  if (mapArchObject.isPointValid(point) && (mapGrid.getFlags(point) & MapGrid.GRID_FLAG_SELECTION) == 0) {
140  mapSquaresToShrink.put(mapSquare, null);
141  break LOOP;
142  }
143  }
144  }
145  }
146  }
147  for (final MapSquare<G, A, R> mapSquare : mapSquaresToShrink.keySet()) {
148  mapSquare.getMapLocation(point);
149  mapGrid.select(point, SelectionMode.SUB);
150  }
151  } finally {
152  mapGrid.endTransaction();
153  }
154  }
155 
156  return true;
157  }
158 
159  @Override
160  public void activeMapViewChanged(@Nullable final MapView<G, A, R> mapView) {
161  if (currentMapView != null) {
162  currentMapView.getMapGrid().removeMapGridListener(mapGridListener);
163  }
164  currentMapView = mapView;
165  if (currentMapView != null) {
166  currentMapView.getMapGrid().addMapGridListener(mapGridListener);
167  }
168  updateAction();
169  }
170 
171  @Override
172  public void mapViewCreated(@NotNull final MapView<G, A, R> mapView) {
173  // ignore
174  }
175 
176  @Override
177  public void mapViewClosing(@NotNull final MapView<G, A, R> mapView) {
178  // ignore
179  }
180 
181 }
void activeMapViewChanged(@Nullable final MapView< G, A, R > mapView)
This event handler is called when the current map view has changed.
Rectangle getSelectedRec()
Returns the smallest rectangle containing selection.
Definition: MapGrid.java:514
List< MapSquare< G, A, R > > getSelectedSquares()
Returns the selected squares.
boolean isPointValid(@Nullable Point pos)
Checks whether the given coordinate is within map bounds.
A MapModel reflects the data of a map.
Definition: MapModel.java:75
An EditorAction that shrinks the current selection by one map square.
Graphical User Interface of Gridarta.
final MapGridListener mapGridListener
The MapGridListener attached to currentMapView.
MapGrid getMapGrid()
Returns the MapGrid of this view.
int getFlags(final int x, final int y)
Returns the flags of a square.
Definition: MapGrid.java:476
void mapViewCreated(@NotNull final MapView< G, A, R > mapView)
This event handler is called when a map view was created.
MapControl< G, A, R > getMapControl()
Return the controller of this view.
void shrinkSelection()
Grows the current map selection by one map square.
MapModel< G, A, R > getMapModel()
Returns the map model.
Base package of all Gridarta classes.
Reflects a game object (object on a map).
Definition: GameObject.java:36
Interface for listeners listening to MapGridEvents.
boolean doShrinkSelection(final boolean performAction)
Shrinks the current map selection by one map square.
A global editor action.
GameObjects are the objects based on Archetypes found on maps.
Interface for listeners interested in events related to MapViewManager instances. ...
2D-Grid containing flags for selection, pre-selection, cursor, warnings and errors.
Definition: MapGrid.java:45
void setAction(@NotNull final Action action, @NotNull final String name)
Sets the Action instance for this editor action.
Base classes for rendering maps.
void beginTransaction()
Starts a new transaction.
Definition: MapGrid.java:756
Action action
The action associated with this editor action.
A getMapArchObject()
Returns the Map Arch Object with the meta information about the map.
MapView< G, A, R > currentMapView
The active map view, or.
void select(@NotNull final Point pos, @NotNull final SelectionMode selectionMode)
Selects or deselects a single square.
Definition: MapGrid.java:408
void endTransaction()
Ends a transaction.
Definition: MapGrid.java:776
A map view consists of a map grid and a map cursor, and is attached to a map control.
Definition: MapView.java:43
void updateAction()
Updates the action&#39;s enabled state.
Modes that describe how squares get selected.
static final int GRID_FLAG_SELECTION
Selection - marks all selected squares.
Definition: MapGrid.java:98
SUB
All squares that are preselected get unselected.
void mapViewClosing(@NotNull final MapView< G, A, R > mapView)
This event handler is called when a map view is to be closed.
This event is created by MapGrid.