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-2023 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) {
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 {
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 }
net.sf.gridarta.model.mapmodel.InsertionModeSet.getTopmostInsertionMode
InsertionMode getTopmostInsertionMode()
Returns the "topmost" insertion mode.
Definition: InsertionModeSet.java:98
net.sf.gridarta.utils.Size2D.getWidth
int getWidth()
Returns the width of the area.
Definition: Size2D.java:96
net.sf.gridarta.model.mapmodel.MapModel
A MapModel reflects the data of a map.
Definition: MapModel.java:75
net.sf.gridarta.model.gameobject.GameObjectFactory
Abstract factory for creating GameObject instances.
Definition: GameObjectFactory.java:31
net.sf.gridarta.model.mapmodel.MapModel.getMapArchObject
A getMapArchObject()
Returns the Map Arch Object with the meta information about the map.
net.sf.gridarta.model.mapmodel.InsertionModeSet
A set of InsertionModes.
Definition: InsertionModeSet.java:33
net.sf.gridarta.gui.copybuffer.CopyBuffer.isEmpty
boolean isEmpty()
Returns whether this copy buffer contains any game objects.
Definition: CopyBuffer.java:271
net.sf.gridarta.model.mapmodel.MapModel.insertBaseObject
G insertBaseObject(@NotNull BaseObject< G, A, R, ?> baseObject, @NotNull Point pos, boolean allowMany, boolean join, @NotNull InsertionMode insertionMode)
Inserts a BaseObject to a map.
net.sf.gridarta.model.mapmodel.MapSquare.getMapX
int getMapX()
Returns the x coordinate on the map.
Definition: MapSquare.java:107
net.sf.gridarta.model.mapmodel.MapModel.isEmpty
boolean isEmpty()
Returns whether the map is empty.
net.sf.gridarta
Base package of all Gridarta classes.
net.sf.gridarta.model.mapmodel.MapSquare
A single Map Square.
Definition: MapSquare.java:45
net.sf.gridarta.model.mapmodel.MapModel.endTransaction
void endTransaction()
End a transaction.
net.sf.gridarta.model.mapviewsettings
Definition: AbstractMapViewSettings.java:20
net.sf.gridarta.model.maparchobject.MapArchObject.isPointValid
boolean isPointValid(@Nullable Point pos)
Checks whether the given coordinate is within map bounds.
net.sf
net.sf.gridarta.model.mapmodel.MapModel.beginTransaction
void beginTransaction(@NotNull String name)
Starts a new transaction.
net.sf.gridarta.utils.MathUtils
Utility class for mathematical functions.
Definition: MathUtils.java:26
net.sf.gridarta.model.mapmodel
Definition: AboveFloorInsertionMode.java:20
net.sf.gridarta.model.mapmodel.MapModelFactory
A factory for creating MapModel instances.
Definition: MapModelFactory.java:35
net.sf.gridarta.model.archetype
Definition: AbstractArchetype.java:20
net.sf.gridarta.model.gameobject.GameObject
Reflects a game object (object on a map).
Definition: GameObject.java:36
net.sf.gridarta.model.mapviewsettings.MapViewSettings
Container for settings that affect the rendering of maps.
Definition: MapViewSettings.java:30
net.sf.gridarta.model.mapmodel.MapModel.addMapModelListener
void addMapModelListener(@NotNull MapModelListener< G, A, R > listener)
Register a map listener.
net.sf.gridarta.gui.copybuffer.CopyBuffer.getAllGameObjects
List< G > getAllGameObjects()
Returns all game objects.
Definition: CopyBuffer.java:263
net.sf.gridarta.gui
Graphical User Interface of Gridarta.
net.sf.gridarta.model.gameobject
GameObjects are the objects based on Archetypes found on maps.
Definition: AbstractGameObject.java:20
net
net.sf.gridarta.gui.copybuffer.CopyBuffer.addMapModelListener
void addMapModelListener(@NotNull final MapModelListener< G, A, R > listener)
Adds a MapModelListener to be notified about changes of the cut/copied game objects.
Definition: CopyBuffer.java:100
net.sf.gridarta.utils.Size2D.getHeight
int getHeight()
Returns the height of the area.
Definition: Size2D.java:104
net.sf.gridarta.gui.copybuffer.CopyBuffer.mapViewSettings
final MapViewSettings mapViewSettings
The map view settings instance.
Definition: CopyBuffer.java:58
net.sf.gridarta.gui.copybuffer.CopyBuffer.removeMapModelListener
void removeMapModelListener(@NotNull final MapModelListener< G, A, R > listener)
Removes a MapModelListener to be notified about changes of the cut/copied game objects.
Definition: CopyBuffer.java:109
net.sf.gridarta.model.mapmodel.MapModel.removeMapModelListener
void removeMapModelListener(@NotNull MapModelListener< G, A, R > listener)
Unregister a map listener.
net.sf.gridarta.model.maparchobject.MapArchObject
Interface for MapArchObjects.
Definition: MapArchObject.java:40
net.sf.gridarta.model.mapviewsettings.MapViewSettings.isEditType
boolean isEditType(int editType)
Get information on the current state of edit type.
net.sf.gridarta.gui.copybuffer.CopyBuffer.copy
void copy(@NotNull final MapView< G, A, R > mapView, @NotNull final Rectangle selectedRec)
Executes the Copy command.
Definition: CopyBuffer.java:136
net.sf.gridarta.gui.map.mapview
Definition: AbstractMapView.java:20
net.sf.gridarta.gui.map.mapview.MapView
A map view consists of a map grid and a map cursor, and is attached to a map control.
Definition: MapView.java:43
net.sf.gridarta.gui.copybuffer.CopyBuffer
Common base implementation of CopyBuffer.
Definition: CopyBuffer.java:52
net.sf.gridarta.model.baseobject.BaseObject
Definition: BaseObject.java:34
net.sf.gridarta.gui.copybuffer.CopyBuffer.cut
void cut(@NotNull final MapView< G, A, R > mapView, @NotNull final Rectangle selectedRec)
Executes the Cut command.
Definition: CopyBuffer.java:127
net.sf.gridarta.model.mapmodel.MapModelListener
Interface for listeners listening on MapModel events.
Definition: MapModelListener.java:36
net.sf.gridarta.model.mapmodel.MapModel.getMapSquare
MapSquare< G, A, R > getMapSquare(@NotNull Point pos)
Get the square at a specified location.
net.sf.gridarta.gui.copybuffer.CopyBuffer.paste
void paste(@NotNull final MapView< G, A, R > mapView, @NotNull final Point startLocation)
Executes the Paste command.
Definition: CopyBuffer.java:180
net.sf.gridarta.gui.copybuffer.CopyBuffer.mapModel
final MapModel< G, A, R > mapModel
Internal map model to store the cut / copied game objects.
Definition: CopyBuffer.java:64
net.sf.gridarta.model
net.sf.gridarta.model.archetype.Archetype
Reflects an Archetype.
Definition: Archetype.java:41
net.sf.gridarta.gui.copybuffer.CopyBuffer.gameObjectFactory
final GameObjectFactory< G, A, R > gameObjectFactory
The GameObjectFactory to use.
Definition: CopyBuffer.java:70
net.sf.gridarta.gui.copybuffer.CopyBuffer.insertionModeSet
final InsertionModeSet< G, A, R > insertionModeSet
The InsertionModeSet to use.
Definition: CopyBuffer.java:76
net.sf.gridarta.model.baseobject
Definition: AbstractBaseObject.java:20
net.sf.gridarta.gui.map
Base classes for rendering maps.
Definition: AbstractPerMapDialogManager.java:20
net.sf.gridarta.gui.copybuffer.CopyBuffer.CopyBuffer
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)
Creates a new instance.
Definition: CopyBuffer.java:86
net.sf.gridarta.model.maparchobject
Definition: AbstractMapArchObject.java:20
net.sf.gridarta.utils.MathUtils.mod
static int mod(final int numerator, final int denominator)
Calculates the remainder of.
Definition: MathUtils.java:40
net.sf.gridarta.gui.copybuffer.CopyMode
Mode for operations.
Definition: CopyMode.java:39
net.sf.gridarta.utils.Size2D
The class Size2D represents a 2d rectangular area.
Definition: Size2D.java:30
net.sf.gridarta.model.mapmodel.MapModel.getAllGameObjects
List< G > getAllGameObjects()
Returns all game objects.
net.sf.gridarta.utils
Definition: ActionBuilderUtils.java:20
net.sf.gridarta.gui.copybuffer.CopyBuffer.copyNCut
void copyNCut(@NotNull final MapView< G, A, R > mapView, @NotNull final Rectangle selectedRec, @NotNull final CopyMode copyMode)
Implements clear, cut and copy in one function (since they are so similar).
Definition: CopyBuffer.java:147
net.sf.gridarta.gui.copybuffer.CopyBuffer.pasteTiled
void pasteTiled(@NotNull final MapView< G, A, R > mapView, @NotNull final Iterable< MapSquare< G, A, R >> selectedSquares, @NotNull final Point origin)
Executes the Paste Tiled command.
Definition: CopyBuffer.java:220
net.sf.gridarta.model.maparchobject.MapArchObjectFactory
Factory for creating MapArchObject instances.
Definition: MapArchObjectFactory.java:28
net.sf.gridarta.gui.copybuffer.CopyBuffer.clear
void clear(@NotNull final MapView< G, A, R > mapView, @NotNull final Rectangle selectedRec)
Executes the Clear command.
Definition: CopyBuffer.java:118