Skip to content

Commit

Permalink
Merge pull request #4763 from kwvanderlinde/bugfix/2433-copy-drawings…
Browse files Browse the repository at this point in the history
…-when-copying-maps

Give DrawnElement a copy constructor
  • Loading branch information
cwisniew authored May 7, 2024
2 parents 926c06a + f452a8f commit 4b3f0a1
Show file tree
Hide file tree
Showing 20 changed files with 472 additions and 206 deletions.
11 changes: 10 additions & 1 deletion src/main/java/net/rptools/maptool/model/Zone.java
Original file line number Diff line number Diff line change
Expand Up @@ -662,7 +662,16 @@ public Zone(Zone zone, boolean keepIds) {
playerAlias = zone.playerAlias;

for (final var entry : drawablesByLayer.entrySet()) {
entry.getValue().addAll(zone.drawablesByLayer.get(entry.getKey()));
final var otherDrawables = zone.drawablesByLayer.get(entry.getKey());
final var thisDrawables = entry.getValue();

for (final var element : otherDrawables) {
final var copy = new DrawnElement(element);
if (!keepIds) {
copy.getDrawable().setId(new GUID());
}
thisDrawables.add(copy);
}
}

if (!zone.labels.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,12 @@
* implementing classes.
*/
public abstract class AbstractDrawing implements Drawable, ImageObserver {
/** The unique identifier for this drawable. It is immutable. */
private final GUID id;
/**
* The unique identifier for this drawable.
*
* <p>It should not typically be changed except to give copies a new ID.
*/
private GUID id;

private String layer;
private String name;
Expand All @@ -47,6 +51,13 @@ protected AbstractDrawing(GUID id) {
this.id = id;
}

protected AbstractDrawing(AbstractDrawing other) {
// The only thing we don't preserve is the ID.
this.id = other.id;
this.layer = other.layer;
this.name = other.name;
}

@Override
public void draw(Zone zone, Graphics2D g, Pen pen) {
if (pen == null) {
Expand Down Expand Up @@ -101,6 +112,11 @@ public GUID getId() {
return id;
}

@Override
public void setId(GUID guid) {
this.id = guid;
}

public void setLayer(Zone.Layer layer) {
this.layer = layer != null ? layer.name() : null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ protected AbstractTemplate(GUID id) {
super(id);
}

protected AbstractTemplate(AbstractTemplate other) {
super(other);
this.radius = other.radius;
this.vertex = new ZonePoint(other.vertex);
}

/*---------------------------------------------------------------------------------------------
* Class Variables
*-------------------------------------------------------------------------------------------*/
Expand Down
26 changes: 26 additions & 0 deletions src/main/java/net/rptools/maptool/model/drawing/BlastTemplate.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import javax.annotation.Nonnull;
import net.rptools.maptool.model.GUID;
import net.rptools.maptool.model.Zone;
import net.rptools.maptool.model.ZonePoint;
import net.rptools.maptool.server.proto.drawing.BlastTemplateDto;
import net.rptools.maptool.server.proto.drawing.DrawableDto;

Expand All @@ -48,10 +49,21 @@ public BlastTemplate(GUID id, int offsetX, int offsetY) {
this.offsetY = offsetY;
}

public BlastTemplate(BlastTemplate other) {
super(other);
this.offsetX = other.offsetX;
this.offsetY = other.offsetY;
}

/*---------------------------------------------------------------------------------------------
* Instance Methods
*-------------------------------------------------------------------------------------------*/

@Override
public Drawable copy() {
return new BlastTemplate(this);
}

private Rectangle makeShape(Zone zone) {
if (zone == null) {
return new Rectangle();
Expand Down Expand Up @@ -177,4 +189,18 @@ public DrawableDto toDto() {

return DrawableDto.newBuilder().setBlastTemplate(dto).build();
}

public static BlastTemplate fromDto(BlastTemplateDto dto) {
var id = GUID.valueOf(dto.getId());
var drawable = new BlastTemplate(id, dto.getOffsetX(), dto.getOffsetY());
drawable.setRadius(dto.getRadius());
var vertex = dto.getVertex();
drawable.setVertex(new ZonePoint(vertex.getX(), vertex.getY()));
drawable.setDirection(AbstractTemplate.Direction.valueOf(dto.getDirection()));
if (dto.hasName()) {
drawable.setName(dto.getName().getValue());
}
drawable.setLayer(Zone.Layer.valueOf(dto.getLayer()));
return drawable;
}
}
23 changes: 23 additions & 0 deletions src/main/java/net/rptools/maptool/model/drawing/BurstTemplate.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import javax.annotation.Nonnull;
import net.rptools.maptool.model.GUID;
import net.rptools.maptool.model.Zone;
import net.rptools.maptool.model.ZonePoint;
import net.rptools.maptool.server.proto.drawing.BurstTemplateDto;
import net.rptools.maptool.server.proto.drawing.DrawableDto;

Expand All @@ -41,10 +42,19 @@ public BurstTemplate(GUID id) {
super(id);
}

public BurstTemplate(BurstTemplate other) {
super(other);
}

/*---------------------------------------------------------------------------------------------
* Instance Methods
*-------------------------------------------------------------------------------------------*/

@Override
public Drawable copy() {
return new BurstTemplate(this);
}

private Rectangle makeVertexShape(Zone zone) {
int gridSize = zone.getGrid().getSize();
return new Rectangle(getVertex().x, getVertex().y, gridSize, gridSize);
Expand Down Expand Up @@ -123,4 +133,17 @@ public DrawableDto toDto() {

return DrawableDto.newBuilder().setBurstTemplate(dto).build();
}

public static BurstTemplate fromDto(BurstTemplateDto dto) {
var id = GUID.valueOf(dto.getId());
var drawable = new BurstTemplate(id);
drawable.setRadius(dto.getRadius());
var vertex = dto.getVertex();
drawable.setVertex(new ZonePoint(vertex.getX(), vertex.getY()));
if (dto.hasName()) {
drawable.setName(dto.getName().getValue());
}
drawable.setLayer(Zone.Layer.valueOf(dto.getLayer()));
return drawable;
}
}
40 changes: 30 additions & 10 deletions src/main/java/net/rptools/maptool/model/drawing/ConeTemplate.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,27 +35,33 @@
*/
public class ConeTemplate extends RadiusTemplate {

public ConeTemplate() {}

public ConeTemplate(GUID id) {
super(id);
}

/*---------------------------------------------------------------------------------------------
* Instance Variables
*-------------------------------------------------------------------------------------------*/

/**
* The dirction to paint. The ne,se,nw,sw paint a quadrant and the n,w,e,w paint along the spine
* of the selected vertex. Saved as a string as a hack to get around the hessian library's problem
* w/ serializing enumerations.
*/
private String direction = Direction.SOUTH_EAST.name();

public ConeTemplate() {}

public ConeTemplate(GUID id) {
super(id);
}

public ConeTemplate(ConeTemplate other) {
super(other);
this.direction = other.direction;
}

/*---------------------------------------------------------------------------------------------
* Instance Methods
*-------------------------------------------------------------------------------------------*/

@Override
public Drawable copy() {
return new ConeTemplate(this);
}

/**
* Get the direction for this ConeTemplate.
*
Expand Down Expand Up @@ -344,4 +350,18 @@ public DrawableDto toDto() {

return DrawableDto.newBuilder().setConeTemplate(dto).build();
}

public static ConeTemplate fromDto(ConeTemplateDto dto) {
var id = GUID.valueOf(dto.getId());
var drawable = new ConeTemplate(id);
drawable.setRadius(dto.getRadius());
var vertex = dto.getVertex();
drawable.setVertex(new ZonePoint(vertex.getX(), vertex.getY()));
drawable.setDirection(AbstractTemplate.Direction.valueOf(dto.getDirection()));
if (dto.hasName()) {
drawable.setName(dto.getName().getValue());
}
drawable.setLayer(Zone.Layer.valueOf(dto.getLayer()));
return drawable;
}
}
25 changes: 25 additions & 0 deletions src/main/java/net/rptools/maptool/model/drawing/Cross.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,18 @@ public Cross(GUID id, int startX, int startY, int endX, int endY) {
endPoint = new Point(endX, endY);
}

public Cross(Cross other) {
super(other);

this.startPoint = new Point(other.startPoint);
this.endPoint = new Point(other.endPoint);
}

@Override
public Drawable copy() {
return new Cross(this);
}

@Override
public @Nonnull Area getArea(Zone zone) {
return new Area(getBounds(zone));
Expand All @@ -62,6 +74,19 @@ public DrawableDto toDto() {
return DrawableDto.newBuilder().setCrossDrawable(dto).build();
}

public static Cross fromDto(CrossDrawableDto dto) {
var id = GUID.valueOf(dto.getId());
var startPoint = dto.getStartPoint();
var endPoint = dto.getEndPoint();
var drawable =
new Cross(id, startPoint.getX(), startPoint.getY(), endPoint.getX(), endPoint.getY());
if (dto.hasName()) {
drawable.setName(dto.getName().getValue());
}
drawable.setLayer(Zone.Layer.valueOf(dto.getLayer()));
return drawable;
}

@Override
public java.awt.Rectangle getBounds(Zone zone) {
if (bounds == null) {
Expand Down
Loading

0 comments on commit 4b3f0a1

Please sign in to comment.