Also transform the TileFactory when transforming a dimension

master
Captain Chaos 2022-08-20 12:34:06 +02:00
parent 0bb514a5f8
commit 43261c7052
9 changed files with 75 additions and 62 deletions

View File

@ -5,6 +5,7 @@
package org.pepsoft.worldpainter;
import org.pepsoft.minecraft.Direction;
import org.pepsoft.worldpainter.heightMaps.TransformingHeightMap;
import javax.vecmath.Point3i;
import java.awt.*;
@ -52,6 +53,8 @@ public abstract class CoordinateTransform {
public abstract float transform(float angle);
public abstract HeightMap transform(HeightMap heightMap);
public boolean isScaling() {
return false;
}
@ -92,6 +95,11 @@ public abstract class CoordinateTransform {
return angle;
}
@Override
public HeightMap transform(HeightMap heightMap) {
return heightMap.scaled(scale);
}
@Override
public boolean isScaling() {
return true;
@ -148,6 +156,11 @@ public abstract class CoordinateTransform {
}
return angle;
}
@Override
public HeightMap transform(HeightMap heightMap) {
return TransformingHeightMap.build().withHeightMap(heightMap).withName(heightMap.getName()).withRotation(HALF_PI).now();
}
};
public static final CoordinateTransform ROTATE_180_DEGREES = new CoordinateTransform() {
@ -191,6 +204,11 @@ public abstract class CoordinateTransform {
}
return angle;
}
@Override
public HeightMap transform(HeightMap heightMap) {
return TransformingHeightMap.build().withHeightMap(heightMap).withName(heightMap.getName()).withRotation(PI).now();
}
};
public static final CoordinateTransform ROTATE_CLOCKWISE_270_DEGREES = new CoordinateTransform() {
@ -236,6 +254,11 @@ public abstract class CoordinateTransform {
}
return angle;
}
@Override
public HeightMap transform(HeightMap heightMap) {
return TransformingHeightMap.build().withHeightMap(heightMap).withName(heightMap.getName()).withRotation(PI * 3 / 2).now();
}
};
public static final CoordinateTransform NOOP = new CoordinateTransform() {
@ -279,6 +302,11 @@ public abstract class CoordinateTransform {
return angle;
}
@Override
public HeightMap transform(HeightMap heightMap) {
return heightMap;
}
@Override
public boolean isScaling() {
return false;

View File

@ -1620,6 +1620,8 @@ public class Dimension extends InstanceKeeper implements TileProvider, Serializa
}
}
tileFactory.transform(transform);
if (overlayCoords != null) {
overlayCoords = transform.transform(overlayCoords);
overlayOffsetX = overlayCoords.x - (lowestX << TILE_SIZE_BITS);

View File

@ -152,6 +152,11 @@ public class HeightMapTileFactory extends AbstractTileFactory {
theme.apply(tile, x, y);
}
@Override
public void transform(CoordinateTransform transform) {
heightMap = transform.transform(heightMap);
}
protected final void setRandomise(boolean randomise) {
this.randomise = randomise;
}

View File

@ -19,4 +19,5 @@ public interface TileFactory extends Serializable, TileProvider {
void setSeed(long seed);
Tile createTile(int x, int y);
void applyTheme(Tile tile, int x, int y);
void transform(CoordinateTransform transform);
}

View File

@ -6,9 +6,11 @@
package org.pepsoft.worldpainter;
import java.awt.Point;
import javax.vecmath.Point3i;
import org.pepsoft.minecraft.Direction;
import org.pepsoft.worldpainter.heightMaps.TransformingHeightMap;
import javax.vecmath.Point3i;
import java.awt.*;
/**
*
@ -49,6 +51,11 @@ public final class Translation extends CoordinateTransform {
public float transform(float angle) {
return angle;
}
@Override
public HeightMap transform(HeightMap heightMap) {
return TransformingHeightMap.build().withHeightMap(heightMap).withName(heightMap.getName()).withOffset(dx, dy).now();
}
private final int dx, dy;
}

View File

@ -32,7 +32,7 @@ import java.awt.geom.Point2D;
* @author pepijn
*/
public class TransformingHeightMap extends DelegatingHeightMap {
public TransformingHeightMap(String name, HeightMap baseHeightMap, float scaleX, float scaleY, int offsetX, int offsetY, int rotation) {
public TransformingHeightMap(String name, HeightMap baseHeightMap, float scaleX, float scaleY, int offsetX, int offsetY, float rotation) {
super("baseHeightMap");
setName(name);
setHeightMap(0, baseHeightMap);
@ -41,7 +41,22 @@ public class TransformingHeightMap extends DelegatingHeightMap {
this.offsetX = offsetX;
this.offsetY = offsetY;
this.rotation = rotation;
recalculate();
if ((scaleX == 1.0f) && (scaleY == 1.0f) && (rotation == 0)) {
translateOnly = true;
transform = null;
} else {
translateOnly = false;
transform = new AffineTransform();
if ((scaleX != 1.0f) || (scaleY != 1.0f)) {
transform.scale(1 / scaleX, 1 / scaleY);
}
if ((offsetX != 0) || (offsetY != 0)) {
transform.translate(-offsetX, -offsetY);
}
if (rotation != 0) {
transform.rotate(-rotation);
}
}
}
public HeightMap getBaseHeightMap() {
@ -56,47 +71,22 @@ public class TransformingHeightMap extends DelegatingHeightMap {
return offsetX;
}
public void setOffsetX(int offsetX) {
this.offsetX = offsetX;
recalculate();
}
public int getOffsetY() {
return offsetY;
}
public void setOffsetY(int offsetY) {
this.offsetY = offsetY;
recalculate();
}
public float getScaleX() {
return scaleX;
}
public void setScaleX(float scaleX) {
this.scaleX = scaleX;
recalculate();
}
public float getScaleY() {
return scaleY;
}
public void setScaleY(float scaleY) {
this.scaleY = scaleY;
recalculate();
}
public int getRotation() {
public float getRotation() {
return rotation;
}
public void setRotation(int rotation) {
this.rotation = rotation;
recalculate();
}
@Override
public float doGetHeight(int x, int y) {
if (translateOnly) {
@ -170,36 +160,17 @@ public class TransformingHeightMap extends DelegatingHeightMap {
return children[0].getRange();
}
private void recalculate() {
if ((scaleX == 1.0f) && (scaleY == 1.0f) && (rotation == 0)) {
translateOnly = true;
transform = null;
} else {
translateOnly = false;
transform = new AffineTransform();
if ((scaleX != 1.0f) || (scaleY != 1.0f)) {
transform.scale(1 / scaleX, 1 / scaleY);
}
if ((offsetX != 0) || (offsetY != 0)) {
transform.translate(-offsetX, -offsetY);
}
if (rotation != 0) {
transform.rotate(-rotation / DOUBLE_PI);
}
}
}
public static TransformingHeightMapBuilder build() {
return new TransformingHeightMapBuilder();
}
private int offsetX, offsetY, rotation;
private float scaleX, scaleY;
private AffineTransform transform;
private boolean translateOnly;
private final int offsetX, offsetY;
private final float scaleX, scaleY;
private final AffineTransform transform;
private final boolean translateOnly;
private final float rotation;
private static final long serialVersionUID = 1L;
private static final double DOUBLE_PI = Math.PI * 2;
private static final Icon ICON_TRANSFORMING_HEIGHTMAP = IconUtils.loadScaledIcon("org/pepsoft/worldpainter/icons/transform.png");
public static class TransformingHeightMapBuilder {
@ -231,7 +202,7 @@ public class TransformingHeightMap extends DelegatingHeightMap {
return this;
}
public TransformingHeightMapBuilder withRotation(int rotation) {
public TransformingHeightMapBuilder withRotation(float rotation) {
this.rotation = rotation;
return this;
}
@ -243,7 +214,6 @@ public class TransformingHeightMap extends DelegatingHeightMap {
private String name;
private HeightMap baseHeightMap;
private int offsetX, offsetY;
private float scaleX = 1.0f, scaleY = 1.0f;
private int rotation;
private float scaleX = 1.0f, scaleY = 1.0f, rotation;
}
}

View File

@ -104,7 +104,7 @@ public class ImportHeightMapOp extends AbstractOperation<World2> {
if (scale != 100) {
heightMap.setSmoothScaling(true);
}
adjustedHeightMap = new TransformingHeightMap(heightMap.getName() + " transformed", heightMap, scale / 100.0f, scale / 100.0f, offsetX, offsetY, 0);
adjustedHeightMap = new TransformingHeightMap(heightMap.getName() + " transformed", heightMap, scale / 100.0f, scale / 100.0f, offsetX, offsetY, 0.0f);
}
importer.setHeightMap(adjustedHeightMap);
importer.setImageFile(heightMap.getImageFile());

View File

@ -188,7 +188,7 @@ public class ImportHeightMapDialog extends WorldPainterDialog implements Documen
if (scale != 100) {
((BitmapHeightMap) heightMap).setSmoothScaling(true);
}
heightMap = new TransformingHeightMap(heightMap.getName() + " transformed", heightMap, scale / 100.0f, scale / 100.0f, offsetX, offsetY, 0);
heightMap = new TransformingHeightMap(heightMap.getName() + " transformed", heightMap, scale / 100.0f, scale / 100.0f, offsetX, offsetY, 0.0f);
}
if (checkBoxInvert.isSelected()) {
heightMap = new DifferenceHeightMap(new ConstantHeightMap((float) (Math.pow(2, bitDepth) - 1)), heightMap);

View File

@ -178,7 +178,7 @@ public class HeightMapEditor extends javax.swing.JFrame implements HeightMapProp
insertMenu.add(menuItem);
menuItem = new JMenuItem("Transformation");
menuItem.addActionListener(actionEvent -> {
TransformingHeightMap transformingHeightMap = new TransformingHeightMap(heightMap.getName(), heightMap, 1.0f, 1.0f, 0, 0, 0);
TransformingHeightMap transformingHeightMap = new TransformingHeightMap(heightMap.getName(), heightMap, 1.0f, 1.0f, 0, 0, 0.0f);
replace(parent, heightMap, transformingHeightMap);
});
insertMenu.add(menuItem);
@ -302,7 +302,7 @@ public class HeightMapEditor extends javax.swing.JFrame implements HeightMapProp
replaceMenu.add(menuItem);
menuItem = new JMenuItem("Transformation");
menuItem.addActionListener(actionEvent -> {
TransformingHeightMap transformingHeightMap = new TransformingHeightMap(null, new ConstantHeightMap(1.0f), 1.0f, 1.0f, 0, 0, 0);
TransformingHeightMap transformingHeightMap = new TransformingHeightMap(null, new ConstantHeightMap(1.0f), 1.0f, 1.0f, 0, 0, 0.0f);
replace(parent, heightMap, transformingHeightMap);
});
replaceMenu.add(menuItem);