Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decompose drawing, expose, and topology tools #5011

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import java.awt.Point;
import java.awt.geom.Ellipse2D;
import java.awt.geom.PathIterator;
import java.math.BigDecimal;
import java.util.List;
Expand Down Expand Up @@ -274,13 +275,8 @@ private JsonObject boundsToJSON(Zone map, AbstractDrawing d) {
private String getDrawbleType(AbstractDrawing d) {
if (d instanceof LineSegment) {
return "Line";
} else if (d instanceof ShapeDrawable) {
String shape = ((ShapeDrawable) d).getShape().getClass().getSimpleName();
if ("Float".equalsIgnoreCase(shape)) {
return "Oval";
} else {
return shape;
}
} else if (d instanceof ShapeDrawable sd) {
return sd.getShapeTypeName();
} else if (d instanceof DrawablesGroup) {
return "Group";
} else {
Expand All @@ -299,14 +295,15 @@ private JsonArray pathToJSON(AbstractDrawing d) {
pinfo.add(info);
}
return pinfo;
} else if (d instanceof ShapeDrawable) {
String shape = ((ShapeDrawable) d).getShape().getClass().getSimpleName();
if ("Float".equalsIgnoreCase(shape)) {
} else if (d instanceof ShapeDrawable sd) {
var shape = sd.getShape();
if (shape instanceof Ellipse2D) {
// We don't support converting ellipses to path.
return new JsonArray();
} else {
// Convert shape into path
JsonArray pinfo = new JsonArray();
final PathIterator pathIter = ((ShapeDrawable) d).getShape().getPathIterator(null);
final PathIterator pathIter = shape.getPathIterator(null);
float[] coords = new float[6];
JsonObject lastinfo = new JsonObject();
while (!pathIter.isDone()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -879,7 +879,7 @@ public void actionPerformed(ActionEvent e) {
}
Toolbox toolbox = MapTool.getFrame().getToolbox();

FacingTool tool = (FacingTool) toolbox.getTool(FacingTool.class);
FacingTool tool = toolbox.getTool(FacingTool.class);
tool.init(
renderer.getZone().getToken(renderer.getSelectedTokenSet().iterator().next()),
renderer.getSelectedTokenSet());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -648,7 +648,7 @@ public void actionPerformed(ActionEvent e) {
return;
}
Toolbox toolbox = MapTool.getFrame().getToolbox();
FacingTool tool = (FacingTool) toolbox.getTool(FacingTool.class);
FacingTool tool = toolbox.getTool(FacingTool.class);
tool.init(
renderer.getZone().getToken(renderer.getSelectedTokenSet().iterator().next()),
renderer.getSelectedTokenSet());
Expand Down
88 changes: 20 additions & 68 deletions src/main/java/net/rptools/maptool/client/tool/ToolHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.event.ActionEvent;
import java.awt.geom.AffineTransform;
import java.awt.geom.PathIterator;
import java.awt.geom.Line2D;
import java.text.NumberFormat;
import javax.swing.AbstractAction;
import javax.swing.SwingUtilities;
Expand Down Expand Up @@ -49,69 +47,31 @@ public void actionPerformed(ActionEvent e) {
}
};

public static void drawDiamondMeasurement(ZoneRenderer renderer, Graphics2D g, Shape diamond) {
double[] north = null;
double[] west = null;
double[] east = null;
PathIterator path = diamond.getPathIterator(getPaintTransform(renderer));
while (!path.isDone()) {
double[] coords = new double[2];
int segType = path.currentSegment(coords);
if (segType != PathIterator.SEG_CLOSE) {
if (north == null) {
north = coords;
}
if (west == null) {
west = coords;
}
if (east == null) {
east = coords;
}
if (coords[1] < north[1]) {
north = coords;
}
if (coords[0] < west[0]) {
west = coords;
}
if (coords[0] > east[0]) {
east = coords;
}
}
path.next();
}
// Measure
int nx = (int) north[0];
int ny = (int) north[1];
int ex = (int) east[0];
int ey = (int) east[1];
int wx = (int) west[0];
int wy = (int) west[1];
public static void drawIsoRectangleMeasurement(
ZoneRenderer renderer, Graphics2D g, ScreenPoint north, ScreenPoint west, ScreenPoint east) {
if (g != null) {
g.setColor(Color.white);
g.setStroke(new BasicStroke(3));
g.drawLine(nx, ny - 20, nx, ny - 10);
g.drawLine(nx, ny - 15, ex, ey - 15);
g.drawLine(ex, ey - 20, ex, ey - 10);
g.drawLine(nx, ny - 15, wx, wy - 15);
g.drawLine(wx, wy - 20, wx, wy - 10);
g.draw(new Line2D.Double(north.x, north.y - 20, north.x, north.y - 10));
g.draw(new Line2D.Double(north.x, north.y - 15, east.x, east.y - 15));
g.draw(new Line2D.Double(east.x, east.y - 20, east.x, east.y - 10));
g.draw(new Line2D.Double(north.x, north.y - 15, west.x, west.y - 15));
g.draw(new Line2D.Double(west.x, west.y - 20, west.x, west.y - 10));

g.setColor(Color.black);
g.setStroke(new BasicStroke(1));
g.drawLine(nx, ny - 20, nx, ny - 10);
g.drawLine(nx, ny - 15, ex, ey - 15);
g.drawLine(ex, ey - 20, ex, ey - 10);
g.drawLine(nx, ny - 15, wx, wy - 15);
g.drawLine(wx, wy - 20, wx, wy - 10);
// g.setPaintMode();
// Same points, but in thin black.
g.draw(new Line2D.Double(north.x, north.y - 20, north.x, north.y - 10));
g.draw(new Line2D.Double(north.x, north.y - 15, east.x, east.y - 15));
g.draw(new Line2D.Double(east.x, east.y - 20, east.x, east.y - 10));
g.draw(new Line2D.Double(north.x, north.y - 15, west.x, west.y - 15));
g.draw(new Line2D.Double(west.x, west.y - 20, west.x, west.y - 10));

String displayString =
NumberFormat.getInstance()
.format(
isometricDistance(renderer, new ScreenPoint(nx, ny), new ScreenPoint(ex, ey)));
GraphicsUtil.drawBoxedString(g, displayString, nx + 25, ny - 25);
displayString =
NumberFormat.getInstance()
.format(
isometricDistance(renderer, new ScreenPoint(nx, ny), new ScreenPoint(wx, wy)));
GraphicsUtil.drawBoxedString(g, displayString, nx - 25, ny - 25);
NumberFormat.getInstance().format(isometricDistance(renderer, north, east));
GraphicsUtil.drawBoxedString(g, displayString, (int) (north.x + 25), (int) (north.y - 25));
displayString = NumberFormat.getInstance().format(isometricDistance(renderer, north, west));
GraphicsUtil.drawBoxedString(g, displayString, (int) (north.x - 25), (int) (north.y - 25));
}
}

Expand Down Expand Up @@ -214,17 +174,9 @@ private static double euclideanDistance(ZoneRenderer renderer, ScreenPoint p1, S

private static double isometricDistance(ZoneRenderer renderer, ScreenPoint p1, ScreenPoint p2) {
double b = p2.y - p1.y;
// return b;
return 2 * b * renderer.getZone().getUnitsPerCell() / renderer.getScaledGridSize();
}

protected static AffineTransform getPaintTransform(ZoneRenderer renderer) {
AffineTransform transform = new AffineTransform();
transform.translate(renderer.getViewOffsetX(), renderer.getViewOffsetY());
transform.scale(renderer.getScale(), renderer.getScale());
return transform;
}

protected static AbstractAction getDeleteTokenAction() {
return deleteTokenAction;
}
Expand Down
125 changes: 74 additions & 51 deletions src/main/java/net/rptools/maptool/client/tool/Toolbox.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,23 +44,20 @@ public Tool getSelectedTool() {
return currentTool;
}

public Tool getTool(Class<? extends Tool> toolClass) {
return toolMap.get(toolClass);
@SuppressWarnings("unchecked")
public <T extends Tool> T getTool(Class<? extends T> toolClass) {
return (T) toolMap.get(toolClass);
}

public Tool createTool(Class<? extends Tool> toolClass) {
Tool tool;
public <T extends Tool> T createTool(Class<T> toolClass) {
T tool;
try {
Constructor<? extends Tool> constructor = toolClass.getDeclaredConstructor();
Constructor<T> constructor = toolClass.getDeclaredConstructor();
tool = constructor.newInstance();
// tool = constructor.newInstance((Object) null);

if (tool.hasGroup()) {
buttonGroup.add(tool);
}

addTool(tool);
toolMap.put(toolClass, tool);
tool.setToolbox(this);

return tool;
} catch (InstantiationException e) {
MapTool.showError(I18N.getText("msg.error.toolCannotInstantiate", toolClass.getName()), e);
Expand All @@ -76,28 +73,35 @@ public Tool createTool(Class<? extends Tool> toolClass) {
return null;
}

/**
* Add {@code tool} to the toolbox.
*
* <p>This tool will not be registered by its class, so methods like {@link #getTool(Class)} will
* not be able to find it.
*
* @param tool The tool to add.
*/
public void addTool(Tool tool) {
if (tool.hasGroup()) {
buttonGroup.add(tool);
}
tool.setToolbox(this);
}

public void setTargetRenderer(final ZoneRenderer renderer) {
// Need to be synchronous with the timing of the invokes within this method
EventQueue.invokeLater(
() -> {
final Tool oldTool = currentTool;

// Disconnect the current tool from the current renderer
setSelectedTool((Tool) null);

// Update the renderer
EventQueue.invokeLater(() -> currentRenderer = renderer);
// Attach the old tool to the new renderer
setSelectedTool(oldTool);
detach();
currentRenderer = renderer;
attach();
});
}

public void setSelectedTool(Class<? extends Tool> toolClass) {
Tool tool = toolMap.get(toolClass);
if (tool != null && tool.isAvailable()) {
tool.setSelected(true);
setSelectedTool(tool);
}
setSelectedTool(tool);
}

public void setSelectedTool(final Tool tool) {
Expand All @@ -106,35 +110,54 @@ public void setSelectedTool(final Tool tool) {
if (tool == currentTool) {
return;
}
if (currentTool != null && currentRenderer != null) {
currentTool.removeListeners(currentRenderer);
// currentTool.addGridBasedKeys(currentRenderer, false);
currentTool.detachFrom(currentRenderer);

if (currentTool instanceof ZoneOverlay) {
currentRenderer.removeOverlay((ZoneOverlay) currentTool);
}
}
// Update
currentTool = tool;

if (currentTool != null) {
if (currentRenderer != null) {
// We have a renderer at this point so we can figure out the grid type and add its
// keystrokes
// to the PointerTool.
// currentTool.addGridBasedKeys(currentRenderer, true);
currentTool.addListeners(currentRenderer);
currentTool.attachTo(currentRenderer);

if (currentTool instanceof ZoneOverlay) {
currentRenderer.addOverlay((ZoneOverlay) currentTool);
}
}
if (MapTool.getFrame() != null) {
MapTool.getFrame().setStatusMessage(I18N.getText(currentTool.getInstructions()));
}

detach();
var accepted = makeCurrent(tool);
if (accepted) {
attach();
}
});
}

private void attach() {
if (currentTool != null) {
if (currentRenderer != null) {
// We have a renderer at this point so we can figure out the grid type and add its
// keystrokes to the PointerTool.
// currentTool.addGridBasedKeys(currentRenderer, true);
currentTool.addListeners(currentRenderer);
currentTool.attachTo(currentRenderer);

if (currentTool instanceof ZoneOverlay) {
currentRenderer.addOverlay((ZoneOverlay) currentTool);
}
}
}
}

private void detach() {
if (currentTool != null && currentRenderer != null) {
currentTool.removeListeners(currentRenderer);
// currentTool.addGridBasedKeys(currentRenderer, false);
currentTool.detachFrom(currentRenderer);

if (currentTool instanceof ZoneOverlay) {
currentRenderer.removeOverlay((ZoneOverlay) currentTool);
}
}
}

private boolean makeCurrent(Tool tool) {
if (tool == null || !tool.isAvailable()) {
return false;
}

currentTool = tool;
tool.setSelected(true);
if (MapTool.getFrame() != null) {
MapTool.getFrame().setStatusMessage(I18N.getText(currentTool.getInstructions()));
}

return true;
}
}
Loading
Loading