-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
f1005c4
Added read/write for mapobject id
viitahenri 282f53c
added object rotation to TMXMapWriter
viitahenri 0967714
Don’t write name into object if it’s null (inline with the editor)
viitahenri 77f3058
Flip flags support for objects
viitahenri 3869e79
Fixed typo in tile flips
viitahenri 2a2fb16
1to1 with my needs of TMX 1.2 for now (not full 1.2 support on writer…
viitahenri fb5ad87
Write tilemap flags
viitahenri a42199c
Reading and writing Groups
viitahenri 2939b39
Changed Group layer data structure to be one list (like in MapData) t…
viitahenri File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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 | ||
|
@@ -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) { | ||
|
@@ -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"); | ||
|
@@ -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) { | ||
obj.setId(id); | ||
} | ||
if (name != null) { | ||
obj.setName(name); | ||
} | ||
|
@@ -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 | ||
|
@@ -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()); | ||
} | ||
} | ||
|
||
|
@@ -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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just call |
||
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 { | ||
|
@@ -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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please also remove this condition and just call |
||
og.setLocked(1); | ||
} | ||
|
||
// Manually parse the objects in object group | ||
og.getObjects().clear(); | ||
|
||
|
@@ -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); | ||
|
@@ -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); | ||
} | ||
} | ||
|
@@ -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; | ||
} | ||
|
||
|
@@ -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) { | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.