Skip to content

Commit

Permalink
Token no longer has a nullable facing.
Browse files Browse the repository at this point in the history
A token still may not have a facing, but this must now be determined by `Token.hasFacing()`. `Token.getFacing()` will
never return `null` but will provide the default of -90°, which removes the case work that several callers had to
consistently perform.

In order to remove a token's facing, `Token.removeFacing()` can now be used as `Token.setFacing()` no longer accepts
`null`.

Also remove some unused and redundant code in related places.
  • Loading branch information
kwvanderlinde committed Sep 27, 2024
1 parent ae4cce9 commit d8987b1
Show file tree
Hide file tree
Showing 10 changed files with 28 additions and 111 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,7 @@ public Object childEvaluate(
if (functionName.equalsIgnoreCase("getTokenFacing")) {
FunctionUtil.checkNumberParam(functionName, parameters, 0, 2);
Token token = FunctionUtil.getTokenFromParam(resolver, functionName, parameters, 0, 1);
if (token.getFacing() == null) {
if (!token.hasFacing()) {
return ""; // XXX Should be -1 instead of a string?
}
return BigDecimal.valueOf(token.getFacing());
Expand Down
59 changes: 3 additions & 56 deletions src/main/java/net/rptools/maptool/client/tool/DefaultTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

import java.awt.dnd.DragSource;
import java.awt.event.*;
import java.awt.geom.AffineTransform;
import java.util.Map;
import java.util.Set;
import javax.swing.*;
Expand Down Expand Up @@ -322,62 +321,10 @@ public void mouseWheelMoved(MouseWheelEvent e) {
if (!AppUtil.playerOwns(token)) {
continue;
}
Integer facing = token.getFacing();
if (facing == null) {
facing = -90; // natural alignment
}
if (SwingUtil.isControlDown(e)) {
// Modify on the fly the rotation point
if (e.isAltDown()) {
int x = token.getX();
int y = token.getY();
int w = token.getWidth();
int h = token.getHeight();

double xc = x + w / 2;
double yc = y + h / 2;

facing += e.getWheelRotation() > 0 ? 5 : -5;
token.setFacing(facing);
int a = token.getFacingInDegrees();
double r = Math.toRadians(a);

System.out.println("Angle: " + a);
System.out.println("Origin x,y: " + x + ", " + y);
System.out.println("Origin bounds: " + token.getBounds(renderer.getZone()));
// System.out.println("Anchor x,y: " + token.getAnchor().x + ", " +
// token.getAnchor().y);

// x = (int) ((x + w) - w * Math.cos(r));
// y = (int) (y - w * Math.sin(r));

// double x1 = (x - xc) * Math.cos(r) - (y - yc) * Math.sin(r) + xc;
// double y1 = (y - yc) * Math.cos(r) + (x - xc) * Math.sin(r) + yc;

// x = (int) (x * Math.cos(r) - y * Math.sin(r));
// y = (int) (y * Math.cos(r) + x * Math.sin(r));

AffineTransform at = new AffineTransform();
at.translate(x, y);
at.rotate(r, x + w, y);

x = (int) at.getTranslateX();
y = (int) at.getTranslateY();

// token.setX(x);
// token.setY(y);
// renderer.flush(token);
// MapTool.serverCommand().putToken(getZone().getId(), token);

// token.setX(0);
// token.setY(0);

System.out.println("New x,y: " + x + ", " + y);
System.out.println("New bounds: " + token.getBounds(renderer.getZone()).toString());

} else {
facing += e.getWheelRotation() > 0 ? 5 : -5;
}
int facing = token.getFacing();
if (SwingUtil.isControlDown(e)) {
facing += e.getWheelRotation() > 0 ? 5 : -5;
} else {
int[] facingArray = getZone().getGrid().getFacingAngles();
int facingIndex = TokenUtil.getIndexNearestTo(facingArray, facing);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public void actionPerformed(ActionEvent e) {
if (token == null) {
continue;
}
token.setFacing(null);
token.removeFacing();
renderer.flush(token);
}
// Go back to the pointer tool
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1092,11 +1092,8 @@ private void handleKeyRotate(int direction, boolean freeRotate) {
if (!AppUtil.playerOwns(token)) {
continue;
}
Integer facing = token.getFacing();
// TODO: this should really be a per grid setting
if (facing == null) {
facing = -90; // natural alignment
}

int facing = token.getFacing();
if (freeRotate) {
facing += direction * 5;
} else {
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/net/rptools/maptool/client/tool/StampTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.Set;
import javax.annotation.Nullable;
import javax.swing.AbstractAction;
Expand Down Expand Up @@ -1046,7 +1045,7 @@ public void paintOverlay(ZoneRenderer renderer, Graphics2D g) {
if (token.hasFacing() && token.getShape() == Token.TokenShape.TOP_DOWN) {
// untested when anchor != (0,0)
// rotate the resize image with the stamp.
double theta = Math.toRadians(-token.getFacing() - 90);
double theta = Math.toRadians(token.getFacingInDegrees());
double anchorX =
-scaledWidth / 2 + resizeImg.getWidth() - (token.getAnchor().x * scale);
double anchorY =
Expand Down Expand Up @@ -1303,7 +1302,7 @@ public TokenResizeOp(
// theta is the rotation angle clockwise from the positive x-axis to compensate for the +ve
// y-axis pointing downwards in zone space and an unrotated token has facing of -90.
// theta == 0 => token has default rotation.
int theta = -Objects.requireNonNullElse(tokenBeingResized.getFacing(), -90) - 90;
int theta = tokenBeingResized.getFacingInDegrees();
double radians = Math.toRadians(theta);
this.down = new Vector2(-Math.sin(radians), Math.cos(radians));
this.right = new Vector2(Math.cos(radians), Math.sin(radians));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -693,7 +693,7 @@ public void actionPerformed(ActionEvent e) {
ZoneRenderer renderer = MapTool.getFrame().getCurrentZoneRenderer();
for (GUID tokenGUID : selectedTokenSet) {
Token token = renderer.getZone().getToken(tokenGUID);
token.setFacing(null);
token.removeFacing();
MapTool.serverCommand().putToken(renderer.getZone().getId(), token);
}
renderer.repaint();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1496,7 +1496,7 @@ protected void showBlockedMoves(Graphics2D g, PlayerView view, Set<SelectionSet>

if (token.hasFacing() && token.getShape() == Token.TokenShape.TOP_DOWN) {
at.rotate(
Math.toRadians(-token.getFacing() - 90),
Math.toRadians(token.getFacingInDegrees()),
scaledWidth / 2 - token.getAnchor().x * scale - offsetx,
scaledHeight / 2
- token.getAnchor().y * scale
Expand Down Expand Up @@ -2191,7 +2191,7 @@ protected void renderTokens(
double sy = scaledHeight / 2 + y - (token.getAnchor().y * scale);
tokenBounds.transform(
AffineTransform.getRotateInstance(
Math.toRadians(-token.getFacing() - 90), sx, sy)); // facing
Math.toRadians(token.getFacingInDegrees()), sx, sy)); // facing
// defaults
// to
// down,
Expand Down Expand Up @@ -2394,7 +2394,7 @@ protected void renderTokens(
// (token.getAnchor().y * scale) - offsety);

at.rotate(
Math.toRadians(-token.getFacing() - 90),
Math.toRadians(token.getFacingInDegrees()),
location.scaledWidth / 2 - (token.getAnchor().x * scale) - offsetx,
location.scaledHeight / 2 - (token.getAnchor().y * scale) - offsety);
// facing defaults to down, or -90 degrees
Expand Down Expand Up @@ -2502,9 +2502,7 @@ protected void renderTokens(
Token.TokenShape tokenType = token.getShape();
switch (tokenType) {
case FIGURE:
if (token.getHasImageTable()
&& token.hasFacing()
&& AppPreferences.getForceFacingArrow() == false) {
if (token.getHasImageTable() && AppPreferences.getForceFacingArrow() == false) {
break;
}
Shape arrow = getFigureFacingArrow(token.getFacing(), footprintBounds.width / 2);
Expand Down Expand Up @@ -2719,7 +2717,7 @@ protected void renderTokens(
// Rotated
clippedG.translate(sp.x, sp.y);
clippedG.rotate(
Math.toRadians(-token.getFacing() - 90),
Math.toRadians(token.getFacingInDegrees()),
width / 2 - (token.getAnchor().x * scale),
height / 2 - (token.getAnchor().y * scale)); // facing defaults to down, or -90
// degrees
Expand Down Expand Up @@ -3376,7 +3374,7 @@ private BufferedImage getTokenImage(Token token) {
MapTool.getCampaign().getLookupTableMap().get(token.getImageTableName());
if (lookupTable != null) {
try {
LookupEntry result = lookupTable.getLookup(token.getFacing().toString());
LookupEntry result = lookupTable.getLookup(Integer.toString(token.getFacing()));
if (result != null) {
image = ImageManager.getImage(result.getImageId(), this);
}
Expand Down
7 changes: 0 additions & 7 deletions src/main/java/net/rptools/maptool/model/Grid.java
Original file line number Diff line number Diff line change
Expand Up @@ -406,9 +406,6 @@ public void setSize(int size) {
-visionRange, -visionRange, visionRange * 2, visionRange * 2));
break;
case BEAM:
if (token.getFacing() == null) {
token.setFacing(0);
}
// Make at least 1 pixel on each side, so it's at least visible at 100% zoom.
var pixelWidth = Math.max(2, width * getSize() / zone.getUnitsPerCell());
Shape lineShape = new Rectangle2D.Double(0, -pixelWidth / 2, visionRange, pixelWidth);
Expand All @@ -421,10 +418,6 @@ public void setSize(int size) {
.createTransformedShape(visibleShape));
break;
case CONE:
if (token.getFacing() == null) {
token.setFacing(0);
}

Arc2D cone =
new Arc2D.Double(
-visionRange,
Expand Down
6 changes: 0 additions & 6 deletions src/main/java/net/rptools/maptool/model/IsometricGrid.java
Original file line number Diff line number Diff line change
Expand Up @@ -351,9 +351,6 @@ public Area getShapedArea(
visibleArea = new Area(new Polygon(x, y, 4));
break;
case BEAM:
if (token.getFacing() == null) {
token.setFacing(0);
}
var pixelWidth = Math.max(2, width * getSize() / getZone().getUnitsPerCell());
Shape visibleShape = new Rectangle2D.Double(0, -pixelWidth / 2, visionRange, pixelWidth);

Expand All @@ -374,9 +371,6 @@ public Area getShapedArea(

break;
case CONE:
if (token.getFacing() == null) {
token.setFacing(0);
}
// Rotate the vision range by 45 degrees for isometric view
visionRange = (float) Math.sin(Math.toRadians(45)) * visionRange;
// Get the cone, use degreesFromIso to convert the facing from isometric to plan
Expand Down
35 changes: 12 additions & 23 deletions src/main/java/net/rptools/maptool/model/Token.java
Original file line number Diff line number Diff line change
Expand Up @@ -848,14 +848,18 @@ public boolean hasFacing() {
return facing != null;
}

public void setFacing(Integer facing) {
while (facing != null && (facing > 180 || facing < -179)) {
public void setFacing(int facing) {
while (facing > 180 || facing < -179) {
facing += facing > 180 ? -360 : 0;
facing += facing < -179 ? 360 : 0;
}
this.facing = facing;
}

public void removeFacing() {
this.facing = null;
}

/**
* Facing is in the map space where 0 degrees is along the X axis to the right and proceeding CCW
* for positive angles.
Expand All @@ -864,8 +868,9 @@ public void setFacing(Integer facing) {
*
* @return null or angle in degrees
*/
public Integer getFacing() {
return facing;
public int getFacing() {
// -90° is natural alignment. TODO This should really be a per grid setting
return facing == null ? -90 : facing;
}

/**
Expand All @@ -874,24 +879,8 @@ public Integer getFacing() {
*
* @return angle in degrees
*/
public Integer getFacingInDegrees() {
if (facing == null) {
return 0;
} else {
return -(facing + 90);
}
}

public Integer getFacingInRealDegrees() {
if (facing == null) {
return 270;
}

if (facing >= 0) {
return facing;
} else {
return facing + 360;
}
public int getFacingInDegrees() {
return -getFacing() - 90;
}

public boolean getHasSight() {
Expand Down Expand Up @@ -2728,7 +2717,7 @@ public void updateProperty(Zone zone, Update update, List<TokenPropertyValueDto>
setFacing(parameters.get(0).getIntValue());
break;
case removeFacing:
setFacing(null);
removeFacing();
break;
case clearAllOwners:
clearAllOwners();
Expand Down

0 comments on commit d8987b1

Please sign in to comment.