Gridarta Editor
CopyBuffer.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.copybuffer;
21 
22 import java.awt.Point;
23 import java.awt.Rectangle;
24 import java.util.Collection;
25 import java.util.HashSet;
26 import java.util.List;
41 import net.sf.gridarta.utils.Size2D;
42 import org.jetbrains.annotations.NotNull;
43 
52 public class CopyBuffer<G extends GameObject<G, A, R>, A extends MapArchObject<A>, R extends Archetype<G, A, R>> {
53 
57  @NotNull
59 
63  @NotNull
64  private final MapModel<G, A, R> mapModel;
65 
69  @NotNull
71 
75  @NotNull
77 
86  public CopyBuffer(@NotNull final MapViewSettings mapViewSettings, @NotNull final GameObjectFactory<G, A, R> gameObjectFactory, @NotNull final MapArchObjectFactory<A> mapArchObjectFactory, @NotNull final MapModelFactory<G, A, R> mapModelFactory, @NotNull final InsertionModeSet<G, A, R> insertionModeSet) {
87  this.mapViewSettings = mapViewSettings;
88  final A mapArchObject = mapArchObjectFactory.newMapArchObject(false);
89  mapArchObject.setMapName("cb");
90  mapModel = mapModelFactory.newMapModel(mapArchObject);
91  this.gameObjectFactory = gameObjectFactory;
92  this.insertionModeSet = insertionModeSet;
93  }
94 
100  public void addMapModelListener(@NotNull final MapModelListener<G, A, R> listener) {
101  mapModel.addMapModelListener(listener);
102  }
103 
109  public void removeMapModelListener(@NotNull final MapModelListener<G, A, R> listener) {
110  mapModel.removeMapModelListener(listener);
111  }
112 
118  public void clear(@NotNull final MapView<G, A, R> mapView, @NotNull final Rectangle selectedRec) {
119  copyNCut(mapView, selectedRec, CopyMode.DO_CLEAR);
120  }
121 
127  public void cut(@NotNull final MapView<G, A, R> mapView, @NotNull final Rectangle selectedRec) {
128  copyNCut(mapView, selectedRec, CopyMode.DO_CUT);
129  }
130 
136  public void copy(@NotNull final MapView<G, A, R> mapView, @NotNull final Rectangle selectedRec) {
137  copyNCut(mapView, selectedRec, CopyMode.DO_COPY);
138  }
139 
147  private void copyNCut(@NotNull final MapView<G, A, R> mapView, @NotNull final Rectangle selectedRec, @NotNull final CopyMode copyMode) {
148  mapModel.beginTransaction("Cut / Clear");
149  try {
150  copyMode.prepare(mapModel, new Size2D(selectedRec.width, selectedRec.height));
151 
152  final MapModel<G, A, R> mapModel2 = mapView.getMapControl().getMapModel();
153  mapModel2.beginTransaction("Cut / Clear"); // TODO: I18N/L10N
154  try {
155  final Collection<G> gameObjectsToDelete = new HashSet<>();
156  final Point pos = new Point();
157  for (final MapSquare<G, A, R> square : mapView.getSelectedSquares()) {
158  square.getMapLocation(pos, -selectedRec.x, -selectedRec.y);
159  for (final G gameObject : square) {
160  copyMode.process(mapModel, gameObject, mapViewSettings.isEditType(gameObject), gameObjectsToDelete, pos, gameObjectFactory, insertionModeSet);
161  }
162  }
163 
164  for (final GameObject<G, A, R> gameObject : gameObjectsToDelete) {
165  gameObject.remove();
166  }
167  } finally {
168  mapModel2.endTransaction();
169  }
170  } finally {
171  mapModel.endTransaction();
172  }
173  }
174 
180  public void paste(@NotNull final MapView<G, A, R> mapView, @NotNull final Point startLocation) {
181  final MapModel<G, A, R> mapModel2 = mapView.getMapControl().getMapModel();
182  final MapArchObject<A> mapArchObject = mapModel2.getMapArchObject();
183  mapModel2.beginTransaction("Paste"); // TODO: I18N/L10N
184  try {
185  final Point pos = new Point();
186  for (final MapSquare<G, A, R> square : mapModel) {
187  square.getMapLocation(pos);
188  pos.translate(startLocation.x, startLocation.y);
189  if (mapArchObject.isPointValid(pos)) {
190  for (final BaseObject<G, A, R, ?> gameObject : square) {
191  if (!gameObject.isMulti()) {
192  mapModel2.insertBaseObject(gameObject, pos, true, false, insertionModeSet.getTopmostInsertionMode());
193  }
194  }
195  }
196  }
197 
198  for (final MapSquare<G, A, R> square : mapModel) {
199  square.getMapLocation(pos);
200  pos.translate(startLocation.x, startLocation.y);
201  if (mapArchObject.isPointValid(pos)) {
202  for (final BaseObject<G, A, R, ?> gameObject : square) {
203  if (gameObject.isMulti()) {
204  mapModel2.insertBaseObject(gameObject, pos, true, false, insertionModeSet.getTopmostInsertionMode());
205  }
206  }
207  }
208  }
209  } finally {
210  mapModel2.endTransaction();
211  }
212  }
213 
220  public void pasteTiled(@NotNull final MapView<G, A, R> mapView, @NotNull final Iterable<MapSquare<G, A, R>> selectedSquares, @NotNull final Point origin) {
221  final Point sourcePoint = new Point();
222  final Point destinationPoint = new Point();
223  final MapModel<G, A, R> mapModel2 = mapView.getMapControl().getMapModel();
224  final Size2D mapModelSize = mapModel.getMapArchObject().getMapSize();
225  mapModel2.beginTransaction("Paste"); // TODO: I18N/L10N
226  try {
227  for (final MapSquare<G, A, R> destinationMapSquare : selectedSquares) {
228  sourcePoint.x = MathUtils.mod(destinationMapSquare.getMapX() - origin.x, mapModelSize.getWidth());
229  sourcePoint.y = MathUtils.mod(destinationMapSquare.getMapY() - origin.y, mapModelSize.getHeight());
230  final MapSquare<G, A, R> sourceMapSquare = mapModel.getMapSquare(sourcePoint);
231  for (final BaseObject<G, A, R, ?> gameObject : sourceMapSquare) {
232  if (!gameObject.isMulti()) {
233  destinationPoint.x = destinationMapSquare.getMapX();
234  destinationPoint.y = destinationMapSquare.getMapY();
235  mapModel2.insertBaseObject(gameObject, destinationPoint, true, false, insertionModeSet.getTopmostInsertionMode());
236  }
237  }
238  }
239 
240  for (final MapSquare<G, A, R> destinationMapSquare : selectedSquares) {
241  sourcePoint.x = MathUtils.mod(destinationMapSquare.getMapX() - origin.x, mapModelSize.getWidth());
242  sourcePoint.y = MathUtils.mod(destinationMapSquare.getMapY() - origin.y, mapModelSize.getHeight());
243  final MapSquare<G, A, R> sourceMapSquare = mapModel.getMapSquare(sourcePoint);
244  for (final BaseObject<G, A, R, ?> gameObject : sourceMapSquare) {
245  if (gameObject.isMulti()) {
246  destinationPoint.x = destinationMapSquare.getMapX();
247  destinationPoint.y = destinationMapSquare.getMapY();
248  mapModel2.insertBaseObject(gameObject, destinationPoint, true, false, insertionModeSet.getTopmostInsertionMode());
249  }
250  }
251  }
252  } finally {
253  mapModel2.endTransaction();
254  }
255  }
256 
262  @NotNull
263  public List<G> getAllGameObjects() {
264  return mapModel.getAllGameObjects();
265  }
266 
271  public boolean isEmpty() {
272  return mapModel.isEmpty();
273  }
274 
275 }
A factory for creating MapModel instances.
int getMapX()
Returns the x coordinate on the map.
Definition: MapSquare.java:107
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
final MapViewSettings mapViewSettings
The map view settings instance.
Definition: CopyBuffer.java:58
Graphical User Interface of Gridarta.
Utility class for mathematical functions.
Definition: MathUtils.java:26
final MapModel< G, A, R > mapModel
Internal map model to store the cut / copied game objects.
Definition: CopyBuffer.java:64
void endTransaction()
End a transaction.
void copyNCut(@NotNull final MapView< G, A, R > mapView, @NotNull final Rectangle selectedRec, @NotNull final CopyMode copyMode)
copyNCut implements clear, cut and copy in one function (since they are so similar).
CopyBuffer(@NotNull final MapViewSettings mapViewSettings, @NotNull final GameObjectFactory< G, A, R > gameObjectFactory, @NotNull final MapArchObjectFactory< A > mapArchObjectFactory, @NotNull final MapModelFactory< G, A, R > mapModelFactory, @NotNull final InsertionModeSet< G, A, R > insertionModeSet)
Create the copy buffer.
Definition: CopyBuffer.java:86
void clear(@NotNull final MapView< G, A, R > mapView, @NotNull final Rectangle selectedRec)
Executing the Clear command.
Interface for listeners listening on MapModel events.
Factory for creating MapArchObject instances.
boolean isEditType(int editType)
Get information on the current state of edit type.
void cut(@NotNull final MapView< G, A, R > mapView, @NotNull final Rectangle selectedRec)
Executing the Cut command.
void addMapModelListener(@NotNull MapModelListener< G, A, R > listener)
Register a map listener.
final InsertionModeSet< G, A, R > insertionModeSet
The InsertionModeSet to use.
Definition: CopyBuffer.java:76
boolean isEmpty()
Returns whether this copy buffer contains any game objects.
G insertBaseObject(@NotNull BaseObject< G, A, R, ?> baseObject, @NotNull Point pos, boolean allowMany, boolean join, @NotNull InsertionMode< G, A, R > insertionMode)
Inserts a BaseObject to a map.
Base package of all Gridarta classes.
void pasteTiled(@NotNull final MapView< G, A, R > mapView, @NotNull final Iterable< MapSquare< G, A, R >> selectedSquares, @NotNull final Point origin)
Executing the Paste Tiled command.
Reflects a game object (object on a map).
Definition: GameObject.java:36
Abstract factory for creating GameObject instances.
void addMapModelListener(@NotNull final MapModelListener< G, A, R > listener)
Adds a MapModelListener to be notified about changes of the cut/copied game objects.
final GameObjectFactory< G, A, R > gameObjectFactory
The GameObjectFactory to use.
Definition: CopyBuffer.java:70
Container for settings that affect the rendering of maps.
MapSquare< G, A, R > getMapSquare(@NotNull Point pos)
Get the square at a specified location.
InsertionMode< G, A, R > getTopmostInsertionMode()
Returns the "topmost" insertion mode.
GameObjects are the objects based on Archetypes found on maps.
int getWidth()
Returns the width of the area.
Definition: Size2D.java:96
void removeMapModelListener(@NotNull final MapModelListener< G, A, R > listener)
Removes a MapModelListener to be notified about changes of the cut/copied game objects.
List< G > getAllGameObjects()
Return all game objects.
Base classes for rendering maps.
void removeMapModelListener(@NotNull MapModelListener< G, A, R > listener)
Unregister a map listener.
A getMapArchObject()
Returns the Map Arch Object with the meta information about the map.
A map view consists of a map grid and a map cursor, and is attached to a map control.
Definition: MapView.java:43
boolean isEmpty()
Returns whether the map is empty.
void beginTransaction(@NotNull String name)
Starts a new transaction.
List< G > getAllGameObjects()
Returns all game objects.
int getHeight()
Returns the height of the area.
Definition: Size2D.java:104
Common base implementation of CopyBuffer.
Definition: CopyBuffer.java:52
void paste(@NotNull final MapView< G, A, R > mapView, @NotNull final Point startLocation)
Executing the Paste command.
The class Size2D represents a 2d rectangular area.
Definition: Size2D.java:30
void copy(@NotNull final MapView< G, A, R > mapView, @NotNull final Rectangle selectedRec)
Executing the Copy command.
static int mod(final int numerator, final int denominator)
Calculates the remainder of.
Definition: MathUtils.java:40