Skip to content

Commit

Permalink
Merge branch 'RPTools:develop' into j_develop_statsheet
Browse files Browse the repository at this point in the history
  • Loading branch information
Jmr3366 authored Nov 7, 2024
2 parents 52c8e27 + dc948f9 commit f9cd9a6
Show file tree
Hide file tree
Showing 59 changed files with 2,013 additions and 3,379 deletions.
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 @@ -21,9 +21,11 @@
import java.math.BigDecimal;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.zip.Inflater;
import net.rptools.maptool.client.AppPreferences;
import net.rptools.maptool.client.MapTool;
import net.rptools.maptool.language.I18N;
Expand All @@ -33,11 +35,17 @@
import net.rptools.parser.VariableResolver;
import net.rptools.parser.function.AbstractFunction;
import okhttp3.Headers;
import okhttp3.Interceptor;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.ResponseBody;
import okio.BufferedSource;
import okio.GzipSource;
import okio.InflaterSource;
import okio.Okio;

/**
* RESTful based functions REST.get, REST.post, REST.put, REST.patch, REST.delete
Expand Down Expand Up @@ -67,7 +75,7 @@ public static RESTfulFunctions getInstance() {
return instance;
}

private final OkHttpClient client = new OkHttpClient();
private final OkHttpClient client = buildClient();
private final Gson gson = new Gson();

@Override
Expand Down Expand Up @@ -236,6 +244,72 @@ private Object executeClientCall(String functionName, Request request, boolean f
}
}

private OkHttpClient buildClient() {
return new OkHttpClient.Builder()
.addInterceptor(
(Interceptor.Chain chain) -> {
var oldRequest = chain.request();

// If the macro has passed its own Accept-Encoding
// it's indicating it expects to somehow handle it itself.
if (oldRequest.header("Accept-Encoding") != null) {
return chain.proceed(oldRequest);
}

// Augment request saying we accept multiple content encodings
var newHeaders =
oldRequest
.headers()
.newBuilder()
.add("Accept-Encoding", "deflate")
.add("Accept-Encoding", "gzip")
.build();

var newRequest = oldRequest.newBuilder().headers(newHeaders).build();

var oldResponse = chain.proceed(newRequest);

// Replace the response's request with the original one
var responseBuilder = oldResponse.newBuilder().request(oldRequest);

// We might not have a body to decompress
var body = oldResponse.body();
if (body != null) {
BufferedSource source = body.source();
// The body may have been wrapped in an arbitrary encoding sequence
// and the server returns them in the order it encoded them
// so we wrap them with decoders in reverse order.
var encodings = oldResponse.headers().values("Content-Encoding");
Collections.reverse(encodings);
for (var encoding : encodings) {
if ("deflate".equalsIgnoreCase(encoding)) {
var inflater = new Inflater(true);
source = Okio.buffer(new InflaterSource(source, inflater));
} else if ("gzip".equalsIgnoreCase(encoding)) {
source = Okio.buffer(new GzipSource(source));
}
}

// Strip encoding and length headers as we've already handled them
var strippedHeaders =
oldResponse
.headers()
.newBuilder()
.removeAll("Content-Encoding")
.removeAll("Content-Length")
.build();
responseBuilder.headers(strippedHeaders);
var contentType = MediaType.parse(oldResponse.header("Content-Type"));
// Construct a new body with an inferred Content-Length
var newBody = ResponseBody.create(contentType, -1L, source);
responseBuilder.body(newBody);
}

return responseBuilder.build();
})
.build();
}

private Headers buildHeaders(Map<String, List<String>> headerMap) {
Headers.Builder headerBuilder = new Headers.Builder();

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
Loading

0 comments on commit f9cd9a6

Please sign in to comment.