Skip to content

Commit

Permalink
Refactor DTO log for Drawables
Browse files Browse the repository at this point in the history
Now each implementation of `Drawable` has its own `fromDto()` method that focuses only on that implementation's
needs. `Drawable.fromDto()` simply delegates to each implementation based on the type case of the parent DTO.
  • Loading branch information
kwvanderlinde committed Apr 17, 2024
1 parent 26dca5c commit 5b4e34a
Show file tree
Hide file tree
Showing 16 changed files with 235 additions and 193 deletions.
15 changes: 15 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 Down Expand Up @@ -188,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;
}
}
14 changes: 14 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 Down Expand Up @@ -132,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;
}
}
14 changes: 14 additions & 0 deletions src/main/java/net/rptools/maptool/model/drawing/ConeTemplate.java
Original file line number Diff line number Diff line change
Expand Up @@ -350,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;
}
}
13 changes: 13 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 @@ -74,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
211 changes: 18 additions & 193 deletions src/main/java/net/rptools/maptool/model/drawing/Drawable.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,9 @@

import java.awt.Graphics2D;
import java.awt.geom.Area;
import java.util.ArrayList;
import javax.annotation.Nonnull;
import net.rptools.maptool.model.CellPoint;
import net.rptools.maptool.model.GUID;
import net.rptools.maptool.model.Zone;
import net.rptools.maptool.model.ZonePoint;
import net.rptools.maptool.server.Mapper;
import net.rptools.maptool.server.proto.drawing.DrawableDto;
import org.apache.logging.log4j.LogManager;

Expand Down Expand Up @@ -50,198 +46,27 @@ public interface Drawable {
DrawableDto toDto();

static Drawable fromDto(DrawableDto drawableDto) {
switch (drawableDto.getDrawableTypeCase()) {
case SHAPE_DRAWABLE -> {
var dto = drawableDto.getShapeDrawable();
var shape = Mapper.map(dto.getShape());
var id = GUID.valueOf(dto.getId());
var drawable = new ShapeDrawable(id, shape, dto.getUseAntiAliasing());
if (dto.hasName()) drawable.setName(dto.getName().getValue());
drawable.setLayer(Zone.Layer.valueOf(dto.getLayer()));
return drawable;
}
case RECTANGLE_DRAWABLE -> {
var dto = drawableDto.getRectangleDrawable();
var id = GUID.valueOf(dto.getId());
var startPoint = dto.getStartPoint();
var endPoint = dto.getEndPoint();
var drawable =
new Rectangle(
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;
}
case OVAL_DRAWABLE -> {
var dto = drawableDto.getOvalDrawable();
var id = GUID.valueOf(dto.getId());
var startPoint = dto.getStartPoint();
var endPoint = dto.getEndPoint();
var drawable =
new Oval(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;
}
case CROSS_DRAWABLE -> {
var dto = drawableDto.getCrossDrawable();
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;
}
case DRAWN_LABEL -> {
var dto = drawableDto.getDrawnLabel();
var id = GUID.valueOf(dto.getId());
var bounds = dto.getBounds();
var drawable =
new DrawnLabel(id, dto.getText(), Mapper.map(dto.getBounds()), dto.getFont());
if (dto.hasName()) drawable.setName(dto.getName().getValue());
drawable.setLayer(Zone.Layer.valueOf(dto.getLayer()));
return drawable;
}
case LINE_SEGMENT -> {
var dto = drawableDto.getLineSegment();
var id = GUID.valueOf(dto.getId());
var drawable = new LineSegment(id, dto.getWidth(), dto.getSquareCap());
var points = drawable.getPoints();
var pointDtos = dto.getPointsList();
pointDtos.forEach(p -> points.add(Mapper.map(p)));
if (dto.hasName()) drawable.setName(dto.getName().getValue());
drawable.setLayer(Zone.Layer.valueOf(dto.getLayer()));
return drawable;
}
case DRAWABLES_GROUP -> {
var dto = drawableDto.getDrawablesGroup();
var id = GUID.valueOf(dto.getId());
var elements = new ArrayList<DrawnElement>();
var elementDtos = dto.getDrawnElementsList();
elementDtos.forEach(e -> elements.add(DrawnElement.fromDto(e)));
var drawable = new DrawablesGroup(id, elements);
if (dto.hasName()) drawable.setName(dto.getName().getValue());
drawable.setLayer(Zone.Layer.valueOf(dto.getLayer()));
return drawable;
}
case RADIUS_CELL_TEMPLATE -> {
var dto = drawableDto.getRadiusCellTemplate();
var id = GUID.valueOf(dto.getId());
var drawable = new RadiusCellTemplate(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;
}
case LINE_CELL_TEMPLATE -> {
var dto = drawableDto.getLineCellTemplate();
var id = GUID.valueOf(dto.getId());
var drawable = new LineCellTemplate(id);
drawable.setRadius(dto.getRadius());
var vertex = dto.getVertex();
drawable.setVertex(new ZonePoint(vertex.getX(), vertex.getY()));
var pathVertex = dto.getPathVertex();
drawable.setPathVertex(new ZonePoint(pathVertex.getX(), pathVertex.getY()));
if (dto.hasName()) {
drawable.setName(dto.getName().getValue());
}
drawable.setLayer(Zone.Layer.valueOf(dto.getLayer()));
return drawable;
}
case RADIUS_TEMPLATE -> {
var dto = drawableDto.getRadiusTemplate();
var id = GUID.valueOf(dto.getId());
var drawable = new RadiusTemplate(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;
}
case BURST_TEMPLATE -> {
var dto = drawableDto.getBurstTemplate();
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;
}
case CONE_TEMPLATE -> {
var dto = drawableDto.getConeTemplate();
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;
}
case BLAST_TEMPLATE -> {
var dto = drawableDto.getBlastTemplate();
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;
}
case LINE_TEMPLATE -> {
var dto = drawableDto.getLineTemplate();
var id = GUID.valueOf(dto.getId());
var drawable = new LineTemplate(id);
drawable.setRadius(dto.getRadius());
var vertex = dto.getVertex();
drawable.setVertex(new ZonePoint(vertex.getX(), vertex.getY()));
if (!dto.getQuadrant().isEmpty()) {
drawable.setQuadrant(AbstractTemplate.Quadrant.valueOf(dto.getQuadrant()));
}
drawable.setMouseSlopeGreater(dto.getMouseSlopeGreater());
var pathVertex = dto.getPathVertex();
drawable.setPathVertex(new ZonePoint(pathVertex.getX(), pathVertex.getY()));
drawable.setDoubleWide(dto.getDoubleWide());
if (dto.hasName()) drawable.setName(dto.getName().getValue());
drawable.setLayer(Zone.Layer.valueOf(dto.getLayer()));
return drawable;
}
case WALL_TEMPLATE -> {
var dto = drawableDto.getWallTemplate();
var id = GUID.valueOf(dto.getId());
var drawable = new WallTemplate(id);
drawable.setRadius(dto.getRadius());
var vertex = dto.getVertex();
drawable.setVertex(new ZonePoint(vertex.getX(), vertex.getY()));
drawable.setMouseSlopeGreater(dto.getMouseSlopeGreater());
var pathVertex = dto.getPathVertex();
drawable.setPathVertex(new ZonePoint(pathVertex.getX(), pathVertex.getY()));
drawable.setDoubleWide(dto.getDoubleWide());
if (dto.hasName()) drawable.setName(dto.getName().getValue());
drawable.setLayer(Zone.Layer.valueOf(dto.getLayer()));

var cellpoints = new ArrayList<CellPoint>();
for (var point : dto.getPointsList())
cellpoints.add(new CellPoint(point.getX(), point.getY()));
drawable.setPath(cellpoints);

return drawable;
}
return switch (drawableDto.getDrawableTypeCase()) {
case SHAPE_DRAWABLE -> ShapeDrawable.fromDto(drawableDto.getShapeDrawable());
case RECTANGLE_DRAWABLE -> Rectangle.fromDto(drawableDto.getRectangleDrawable());
case OVAL_DRAWABLE -> Oval.fromDto(drawableDto.getOvalDrawable());
case CROSS_DRAWABLE -> Cross.fromDto(drawableDto.getCrossDrawable());
case DRAWN_LABEL -> DrawnLabel.fromDto(drawableDto.getDrawnLabel());
case LINE_SEGMENT -> LineSegment.fromDto(drawableDto.getLineSegment());
case DRAWABLES_GROUP -> DrawablesGroup.fromDto(drawableDto.getDrawablesGroup());
case RADIUS_CELL_TEMPLATE -> RadiusCellTemplate.fromDto(drawableDto.getRadiusCellTemplate());
case LINE_CELL_TEMPLATE -> LineCellTemplate.fromDto(drawableDto.getLineCellTemplate());
case RADIUS_TEMPLATE -> RadiusTemplate.fromDto(drawableDto.getRadiusTemplate());
case BURST_TEMPLATE -> BurstTemplate.fromDto(drawableDto.getBurstTemplate());
case CONE_TEMPLATE -> ConeTemplate.fromDto(drawableDto.getConeTemplate());
case BLAST_TEMPLATE -> BlastTemplate.fromDto(drawableDto.getBlastTemplate());
case LINE_TEMPLATE -> LineTemplate.fromDto(drawableDto.getLineTemplate());
case WALL_TEMPLATE -> WallTemplate.fromDto(drawableDto.getWallTemplate());
default -> {
LogManager.getLogger(Drawable.class)
.warn("unknown DrawableDto type: " + drawableDto.getDrawableTypeCase());
return null;
yield null;
}
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,19 @@ public DrawableDto toDto() {
return DrawableDto.newBuilder().setDrawablesGroup(dto).build();
}

public static DrawablesGroup fromDto(DrawablesGroupDto dto) {
var id = GUID.valueOf(dto.getId());
var elements = new ArrayList<DrawnElement>();
var elementDtos = dto.getDrawnElementsList();
elementDtos.forEach(e -> elements.add(DrawnElement.fromDto(e)));
var drawable = new DrawablesGroup(id, elements);
if (dto.hasName()) {
drawable.setName(dto.getName().getValue());
}
drawable.setLayer(Zone.Layer.valueOf(dto.getLayer()));
return drawable;
}

@Override
protected void draw(Zone zone, Graphics2D g) {
// This should never be called
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/net/rptools/maptool/model/drawing/DrawnLabel.java
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,15 @@ public DrawableDto toDto() {

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

public static DrawnLabel fromDto(DrawnLabelDto dto) {
var id = GUID.valueOf(dto.getId());
var bounds = dto.getBounds();
var drawable = new DrawnLabel(id, dto.getText(), Mapper.map(dto.getBounds()), dto.getFont());
if (dto.hasName()) {
drawable.setName(dto.getName().getValue());
}
drawable.setLayer(Zone.Layer.valueOf(dto.getLayer()));
return drawable;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -470,4 +470,19 @@ public DrawableDto toDto() {

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

public static LineCellTemplate fromDto(LineCellTemplateDto dto) {
var id = GUID.valueOf(dto.getId());
var drawable = new LineCellTemplate(id);
drawable.setRadius(dto.getRadius());
var vertex = dto.getVertex();
drawable.setVertex(new ZonePoint(vertex.getX(), vertex.getY()));
var pathVertex = dto.getPathVertex();
drawable.setPathVertex(new ZonePoint(pathVertex.getX(), pathVertex.getY()));
if (dto.hasName()) {
drawable.setName(dto.getName().getValue());
}
drawable.setLayer(Zone.Layer.valueOf(dto.getLayer()));
return drawable;
}
}
13 changes: 13 additions & 0 deletions src/main/java/net/rptools/maptool/model/drawing/LineSegment.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,19 @@ public DrawableDto toDto() {
return DrawableDto.newBuilder().setLineSegment(dto).build();
}

public static LineSegment fromDto(LineSegmentDrawableDto dto) {
var id = GUID.valueOf(dto.getId());
var drawable = new LineSegment(id, dto.getWidth(), dto.getSquareCap());
var points = drawable.getPoints();
var pointDtos = dto.getPointsList();
pointDtos.forEach(p -> points.add(Mapper.map(p)));
if (dto.hasName()) {
drawable.setName(dto.getName().getValue());
}
drawable.setLayer(Zone.Layer.valueOf(dto.getLayer()));
return drawable;
}

private Area createLineArea() {
if (points.size() < 1) {
return null;
Expand Down
Loading

0 comments on commit 5b4e34a

Please sign in to comment.