Skip to content

Commit

Permalink
libtiled-java: Added support for manipulating non-consecutive tile ID…
Browse files Browse the repository at this point in the history
…s in a tileset.

This implementation adds a new tiles field in TileSet whose values are "synchronized" on marshal and unmarshal operations via "class defined" event callbacks with the generated internalTiles attribute in TileSetData.
See https://docs.oracle.com/javase/7/docs/api/javax/xml/bind/Marshaller.html.
  • Loading branch information
stephaneseng committed Aug 27, 2017
1 parent 83cfbf6 commit a4db8de
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,12 @@
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.TreeMap;

import javax.imageio.ImageIO;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
Expand Down Expand Up @@ -75,13 +79,15 @@ public class TileSet extends TileSetData implements Iterable<Tile> {
private File tilebmpFile;
private Color transparentColor;
private Image tileSetImage;
private TreeMap<Integer, Tile> tiles;

/**
* Default constructor
*/
public TileSet() {
super();
this.tiles = new ArrayList<>();
this.internalTiles = new ArrayList<>();
this.tiles = new TreeMap<>();
}

/**
Expand Down Expand Up @@ -258,7 +264,7 @@ public void setTransparentColor(Color color) {
*/
public int addTile(Tile t) {
if (t.getId() < 0) {
t.setId(tiles.size());
t.setId(getMaxTileId() + 1);
}

if (tileWidth < t.getWidth()) {
Expand All @@ -269,7 +275,7 @@ public int addTile(Tile t) {
tileHeight = t.getHeight();
}

tiles.add(t);
tiles.put(t.getId(), t);
t.setTileSet(this);

return t.getId();
Expand All @@ -295,7 +301,7 @@ public void addNewTile(Tile t) {
* @param i the index to remove
*/
public void removeTile(int i) {
tiles.set(i, null);
tiles.remove(i);
}

/**
Expand All @@ -314,7 +320,11 @@ public int size() {
* @return the maximum tile id, or -1 when there are no tiles
*/
public int getMaxTileId() {
return tiles.size() - 1;
try {
return tiles.lastKey();
} catch (NoSuchElementException e) {
return -1;
}
}

/**
Expand All @@ -324,7 +334,7 @@ public int getMaxTileId() {
*/
@Override
public Iterator<Tile> iterator() {
return tiles.iterator();
return tiles.values().iterator();
}

/**
Expand Down Expand Up @@ -386,6 +396,32 @@ public Color getTransparentColor() {
return transparentColor;
}

/**
* JAXB class defined event callback, invoked before marshalling XML data.
*
* @param marshaller the marshaller doing the marshalling.
*/
public void beforeMarshal(Marshaller marshaller) {
internalTiles = new ArrayList<>();
for (java.util.Map.Entry<Integer, Tile> entry : tiles.entrySet()) {
internalTiles.add(entry.getValue());
}
}

/**
* JAXB class defined event callback, invoked after unmarshalling XML data.
*
* @param unmarshaller the unmarshaller doing the unmarshalling.
* @param parent the parent instance that will reference this instance,
* or null if this instance is the XML root.
*/
public void afterUnmarshal(Unmarshaller unmarshaller, Object parent) {
tiles = new TreeMap<>();
for (Tile tile : getInternalTiles()) {
tiles.put(tile.getId(), tile);
}
}

/** {@inheritDoc} */
@Override
public String toString() {
Expand Down
2 changes: 1 addition & 1 deletion util/java/libtiled-java/src/main/resources/bindings.xjb
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@
</jxb:property>
</jxb:bindings>
<jxb:bindings node="xs:element[@name='tile']">
<jxb:property name="tiles">
<jxb:property name="internalTiles">
<jxb:baseType name="Tile"/>
</jxb:property>
</jxb:bindings>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@
import org.mapeditor.core.Orientation;
import org.mapeditor.core.StaggerAxis;
import org.mapeditor.core.StaggerIndex;
import org.mapeditor.core.Tile;
import org.mapeditor.core.TileLayer;
import org.mapeditor.core.TileSet;

public class MapReaderTest {

Expand Down Expand Up @@ -114,6 +116,30 @@ public void testReadingCsvMap() throws Exception {
assertNotNull(layer.getTileAt(0, 0));
}

@Test
public void testReadingCsvMapEmbeddedImageCollection() throws Exception {
// Arrange
URL url = getUrlFromResources("csvmap_embedded_image_collection/csvmap_embedded_image_collection.tmx");

// Act
Map map = new TMXMapReader().readMap(url.getPath());

// Assert
assertEquals(Orientation.ORTHOGONAL, map.getOrientation());
assertEquals(3, map.getWidth());
assertEquals(1, map.getHeight());
assertEquals(32, map.getTileWidth());
assertEquals(32, map.getTileHeight());
assertEquals(1, map.getLayerCount());

TileLayer layer = (TileLayer) map.getLayer(0);
assertNotNull(layer.getTileAt(0, 0));
assertNotNull(layer.getTileAt(2, 0));

TileSet tileset = layer.getMap().getTileSets().get(0);
assertEquals(3, tileset.getMaxTileId());
}

@Test
public void testReadingDesertMap() throws Exception {
// Arrange
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<map version="1.0" tiledversion="1.0.2" orientation="orthogonal" renderorder="right-down" width="3" height="1" tilewidth="32" tileheight="32" nextobjectid="1">
<tileset firstgid="1" name="embedded_image_collection" tilewidth="32" tileheight="32" tilecount="2" columns="0">
<grid orientation="orthogonal" width="1" height="1"/>
<tile id="0">
<image width="32" height="32" source="csvmap_embedded_image_collection_1.png"/>
</tile>
<tile id="3">
<image width="32" height="32" source="csvmap_embedded_image_collection_2.png"/>
</tile>
</tileset>
<layer name="Tile Layer 1" width="3" height="1">
<data encoding="csv">
1,0,4
</data>
</layer>
</map>
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit a4db8de

Please sign in to comment.