Gridarta Editor
GrowSelectionAction.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.List;
24 import javax.swing.Action;
37 import net.sf.japi.swing.action.ActionMethod;
38 import org.jetbrains.annotations.NotNull;
39 import org.jetbrains.annotations.Nullable;
40 
45 public class GrowSelectionAction<G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> implements EditorAction, MapViewManagerListener<G, A, R> {
46 
50  @Nullable
52 
57  @Nullable
58  private Action action;
59 
63  @NotNull
65 
66  @Override
67  public void mapGridChanged(@NotNull final MapGridEvent e) {
68  updateAction();
69  }
70 
71  @Override
72  public void mapGridResized(@NotNull final MapGridEvent e) {
73  updateAction();
74  }
75 
76  };
77 
81  @ActionMethod
82  public void growSelection() {
83  doGrowSelection(true);
84  }
85 
86  @Override
87  public void setAction(@NotNull final Action action, @NotNull final String name) {
88  this.action = action;
89  updateAction();
90  }
91 
95  private void updateAction() {
96  if (action != null) {
97  //noinspection ConstantConditions
98  action.setEnabled(doGrowSelection(false));
99  }
100  }
101 
107  private boolean doGrowSelection(final boolean performAction) {
108  final MapView<G, A, R> mapView = currentMapView;
109  if (mapView == null) {
110  return false;
111  }
112 
113  final MapGrid mapGrid = mapView.getMapGrid();
114  if (mapGrid.getSelectedRec() == null) {
115  return false;
116  }
117 
118  final List<MapSquare<G, A, R>> selectedSquares = mapView.getSelectedSquares();
119  if (selectedSquares.isEmpty()) {
120  return false;
121  }
122 
123  if (performAction) {
124  final MapModel<G, A, R> mapModel = mapView.getMapControl().getMapModel();
125  final MapArchObject<A> mapArchObject = mapModel.getMapArchObject();
126  final Point point = new Point();
127  mapGrid.beginTransaction();
128  try {
129  for (final MapSquare<G, A, R> mapSquare : selectedSquares) {
130  for (int dy = -1; dy <= 1; dy++) {
131  for (int dx = -1; dx <= 1; dx++) {
132  mapSquare.getMapLocation(point, dx, dy);
133  if (mapArchObject.isPointValid(point)) {
134  mapGrid.select(point, SelectionMode.ADD);
135  }
136  }
137  }
138  }
139  } finally {
140  mapGrid.endTransaction();
141  }
142  }
143 
144  return true;
145  }
146 
147  @Override
148  public void activeMapViewChanged(@Nullable final MapView<G, A, R> mapView) {
149  if (currentMapView != null) {
150  currentMapView.getMapGrid().removeMapGridListener(mapGridListener);
151  }
152  currentMapView = mapView;
153  if (currentMapView != null) {
154  currentMapView.getMapGrid().addMapGridListener(mapGridListener);
155  }
156  updateAction();
157  }
158 
159  @Override
160  public void mapViewCreated(@NotNull final MapView<G, A, R> mapView) {
161  }
162 
163  @Override
164  public void mapViewClosing(@NotNull final MapView<G, A, R> mapView) {
165  }
166 
167 }
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
Graphical User Interface of Gridarta.
MapView< G, A, R > currentMapView
The active map view, or.
void mapViewCreated(@NotNull final MapView< G, A, R > mapView)
This event handler is called when a map view was created.
MapGrid getMapGrid()
Returns the MapGrid of this view.
MapControl< G, A, R > getMapControl()
Return the controller of this view.
void setAction(@NotNull final Action action, @NotNull final String name)
Sets the Action instance for this editor action.
void growSelection()
Grows the current map selection by one map square.
Action action
The action associated with this editor action.
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.
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 mapViewClosing(@NotNull final MapView< G, A, R > mapView)
This event handler is called when a map view is to be closed.
Base classes for rendering maps.
void beginTransaction()
Starts a new transaction.
Definition: MapGrid.java:756
A getMapArchObject()
Returns the Map Arch Object with the meta information about the map.
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
ADD
All squares that are preselected get selected.
Modes that describe how squares get selected.
void activeMapViewChanged(@Nullable final MapView< G, A, R > mapView)
This event handler is called when the current map view has changed.
void updateAction()
Updates the action&#39;s enabled state.
An EditorAction that grows the current selection by one map square.
final MapGridListener mapGridListener
The MapGridListener attached to currentMapView.
This event is created by MapGrid.
boolean doGrowSelection(final boolean performAction)
Grows the current map selection by one map square.