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

libtiled-java - Tiled editor parity #2207

Merged
merged 9 commits into from
Oct 30, 2020
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,21 @@ public class MapObject extends MapObjectData implements Cloneable {
private Image image;
private Image scaledImage;
private Tile tile;
private boolean flipHorizontal;
private boolean flipVertical;
private boolean flipDiagonal;

/**
* <p>Constructor for MapObject.</p>
*/
public MapObject() {
super();
this.properties = new Properties();
this.name = "Object";
this.name = "";
this.type = "";
this.imageSource = "";
this.flipHorizontal = false;
this.flipVertical = false;
}

/**
Expand Down Expand Up @@ -208,6 +213,15 @@ public void setTile(Tile tile) {
this.tile = tile;
}

public boolean getFlipHorizontal() { return flipHorizontal; }
public void setFlipHorizontal(boolean flip) { this.flipHorizontal = flip; }

public boolean getFlipVertical() { return flipVertical; }
public void setFlipVertical(boolean flip) { this.flipVertical = flip; }

public boolean getFlipDiagonal() { return flipDiagonal; }
public void setFlipDiagonal(boolean flip) { this.flipDiagonal = flip; }

/**
* Returns the image to be used when drawing this object. This image is
* scaled to the size of the object.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ public void resize(int width, int height, int dx, int dy) {
* @param y Tile-space y coordinate
* @return <code>true</code> if tile at (x, y) is flipped horizontally
*/
public boolean isFlippedHorizontaly(int x, int y) {
public boolean isFlippedHorizontally(int x, int y) {
return getBounds().contains(x, y) &&
(flags[y][x] & (int)TMXMapReader.FLIPPED_HORIZONTALLY_FLAG) != 0;
}
Expand All @@ -558,7 +558,7 @@ public boolean isFlippedVertically(int x, int y) {
* @param y Tile-space y coordinate
* @return <code>true</code> if tile at (x, y) is flipped diagonally
*/
public boolean isFlippedDiagonaly(int x, int y) {
public boolean isFlippedDiagonally(int x, int y) {
return getBounds().contains(x, y) &&
(flags[y][x] & (int)TMXMapReader.FLIPPED_DIAGONALLY_FLAG) != 0;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import java.awt.Color;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Path2D;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
Expand Down Expand Up @@ -79,9 +80,9 @@
*/
public class TMXMapReader {

public static long FLIPPED_HORIZONTALLY_FLAG = 0xFFFFFFFF80000000L;
public static long FLIPPED_VERTICALLY_FLAG = 0xFFFFFFFF40000000L;
public static long FLIPPED_DIAGONALLY_FLAG = 0xFFFFFFFF20000000L;
public static long FLIPPED_HORIZONTALLY_FLAG = 0x0000000080000000L;
public static long FLIPPED_VERTICALLY_FLAG = 0x0000000040000000L;
public static long FLIPPED_DIAGONALLY_FLAG = 0x0000000020000000L;

public static long ALL_FLAGS = FLIPPED_HORIZONTALLY_FLAG
| FLIPPED_VERTICALLY_FLAG
Expand Down Expand Up @@ -142,6 +143,15 @@ private static int getAttribute(Node node, String attribname, int def) {
}
}

private static float getFloatAttribute(Node node, String attribname, float def) {
final String attr = getAttributeValue(node, attribname);
if (attr != null) {
return Float.parseFloat(attr);
} else {
return def;
}
}

private static double getDoubleAttribute(Node node, String attribname, double def) {
final String attr = getAttributeValue(node, attribname);
if (attr != null) {
Expand Down Expand Up @@ -334,6 +344,7 @@ private TileSet unmarshalTileset(Node t) throws Exception {
}

private MapObject readMapObject(Node t) throws Exception {
final int id = getAttribute(t, "id", 0);
final String name = getAttributeValue(t, "name");
final String type = getAttributeValue(t, "type");
final String gid = getAttributeValue(t, "gid");
Expand All @@ -345,6 +356,9 @@ private MapObject readMapObject(Node t) throws Exception {

MapObject obj = new MapObject(x, y, width, height, rotation);
obj.setShape(obj.getBounds());
if (id != 0) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I reckon the id defaults to 0 anyway, in which case this condition is not adding any value and should just be removed.

obj.setId(id);
}
if (name != null) {
obj.setName(name);
}
Expand All @@ -353,13 +367,16 @@ private MapObject readMapObject(Node t) throws Exception {
}
if (gid != null) {
long tileId = Long.parseLong(gid);
if (tileId > Integer.MAX_VALUE) {
if ((tileId & ALL_FLAGS) != 0) {
// Read out the flags
// TODO: Save these flags somewhere
long flippedHorizontally = tileId & FLIPPED_HORIZONTALLY_FLAG;
long flippedVertically = tileId & FLIPPED_VERTICALLY_FLAG;
long flippedDiagonally = tileId & FLIPPED_DIAGONALLY_FLAG;

obj.setFlipHorizontal(flippedHorizontally != 0);
obj.setFlipVertical(flippedVertically != 0);
obj.setFlipDiagonal(flippedDiagonally != 0);

// Clear the flags
tileId &= ~(FLIPPED_HORIZONTALLY_FLAG
| FLIPPED_VERTICALLY_FLAG
Expand Down Expand Up @@ -401,6 +418,8 @@ private MapObject readMapObject(Node t) throws Exception {
shape.closePath();
obj.setShape(shape);
obj.setBounds((Rectangle2D.Double) shape.getBounds2D());
} else if ("point".equalsIgnoreCase(child.getNodeName())) {
obj.setPoint(new Point());
}
}

Expand Down Expand Up @@ -487,6 +506,61 @@ private Tile unmarshalTile(TileSet set, Node t, String baseDir)
return tile;
}

private Group unmarshalGroup(Node t) throws Exception {
Group g = null;
try {
g = unmarshalClass(t, Group.class);
} catch (JAXBException e) {
// todo: replace with log message
e.printStackTrace();
return g;
}

final int offsetX = getAttribute(t, "x", 0);
final int offsetY = getAttribute(t, "y", 0);
g.setOffset(offsetX, offsetY);

String opacity = getAttributeValue(t, "opacity");
if (opacity != null) {
g.setOpacity(Float.parseFloat(opacity));
}

final int locked = getAttribute(t, "locked", 0);
if (locked != 0) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just call g.setLocked(locked) if the result is the same.

g.setLocked(1);
}

g.getLayers().clear();

// Load the layers and objectgroups
for (Node sibs = t.getFirstChild(); sibs != null;
sibs = sibs.getNextSibling()) {
if ("group".equals(sibs.getNodeName())) {
Group group = unmarshalGroup(sibs);
if (group != null) {
g.getLayers().add(group);
}
} else if ("layer".equals(sibs.getNodeName())) {
TileLayer layer = readLayer(sibs);
if (layer != null) {
g.getLayers().add(layer);
}
} else if ("objectgroup".equals(sibs.getNodeName())) {
ObjectGroup group = unmarshalObjectGroup(sibs);
if (group != null) {
g.getLayers().add(group);
}
} else if ("imagelayer".equals(sibs.getNodeName())) {
ImageLayer imageLayer = unmarshalImageLayer(sibs);
if (imageLayer != null) {
g.getLayers().add(imageLayer);
}
}
}

return g;
}

private ObjectGroup unmarshalObjectGroup(Node t) throws Exception {
ObjectGroup og = null;
try {
Expand All @@ -501,6 +575,11 @@ private ObjectGroup unmarshalObjectGroup(Node t) throws Exception {
final int offsetY = getAttribute(t, "y", 0);
og.setOffset(offsetX, offsetY);

final int locked = getAttribute(t, "locked", 0);
if (locked != 0) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please also remove this condition and just call og.setLocked(locked).

og.setLocked(1);
}

// Manually parse the objects in object group
og.getObjects().clear();

Expand Down Expand Up @@ -535,11 +614,14 @@ private ImageLayer unmarshalImageLayer(Node t) throws Exception {
* @throws Exception
*/
private TileLayer readLayer(Node t) throws Exception {
final int layerId = getAttribute(t, "id", 0);
final int layerWidth = getAttribute(t, "width", map.getWidth());
final int layerHeight = getAttribute(t, "height", map.getHeight());

TileLayer ml = new TileLayer(layerWidth, layerHeight);

ml.setId(layerId);

final int offsetX = getAttribute(t, "x", 0);
final int offsetY = getAttribute(t, "y", 0);
final int visible = getAttribute(t, "visible", 1);
Expand Down Expand Up @@ -611,19 +693,6 @@ private TileLayer readLayer(Node t) throws Exception {
String gid = csvTileIds[x + y * ml.getWidth()];
long tileId = Long.parseLong(gid);

if (tileId > Integer.MAX_VALUE) {
// Read out the flags
// TODO: Save these flags somewhere
long flippedHorizontally = tileId & FLIPPED_HORIZONTALLY_FLAG;
long flippedVertically = tileId & FLIPPED_VERTICALLY_FLAG;
long flippedDiagonally = tileId & FLIPPED_DIAGONALLY_FLAG;

// Clear the flags
tileId &= ~(FLIPPED_HORIZONTALLY_FLAG
| FLIPPED_VERTICALLY_FLAG
| FLIPPED_DIAGONALLY_FLAG);
}

setTileAtFromTileId(ml, y, x, (int) tileId);
}
}
Expand Down Expand Up @@ -675,6 +744,11 @@ private TileLayer readLayer(Node t) throws Exception {
// todo: something to keep in mind at this level?
ml.setVisible(visible == 1);

final int locked = getAttribute(t, "locked", 0);
if (locked != 0) {
ml.setLocked(1);
}

return ml;
}

Expand Down Expand Up @@ -743,9 +817,15 @@ private void buildMap(Document doc) throws Exception {
map.addTileset(tileset);
}

// Load the layers and objectgroups
// Load the layers and groups
for (Node sibs = mapNode.getFirstChild(); sibs != null;
sibs = sibs.getNextSibling()) {
if ("group".equals(sibs.getNodeName())) {
Group group = unmarshalGroup(sibs);
if (group != null) {
map.addLayer(group);
}
}
if ("layer".equals(sibs.getNodeName())) {
TileLayer layer = readLayer(sibs);
if (layer != null) {
Expand Down
Loading