Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*-
* #%L
* This file is part of libtiled-java.
* %%
* Copyright (C) 2004 - 2016 Thorbjørn Lindeijer
* Copyright (C) 2004 - 2016 Adam Turk
* Copyright (C) 2016 Mike Thomas
* %%
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
* #L%
*/
package tiled.io;
import java.awt.Color;
import java.awt.Rectangle;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import java.util.Set;
import java.util.TreeSet;
import java.util.zip.DeflaterOutputStream;
import java.util.zip.GZIPOutputStream;
import tiled.core.AnimatedTile;
import tiled.core.Map;
import tiled.core.MapLayer;
import tiled.core.MapObject;
import tiled.core.ObjectGroup;
import tiled.core.Sprite;
import tiled.core.Tile;
import tiled.core.TileLayer;
import tiled.core.TileSet;
import tiled.io.xml.XMLWriter;
import tiled.util.Base64;
/**
* A writer for Tiled's TMX map format.
*
* @author Thorbjørn Lindeijer
* @author Adam Turk
* @author Mike Thomas
* @version 0.17
*/
public class TMXMapWriter {
private static final int LAST_BYTE = 0x000000FF;
private static final boolean ENCODE_LAYER_DATA = true;
private static final boolean COMPRESS_LAYER_DATA = ENCODE_LAYER_DATA;
private HashMap firstGidPerTileset;
public static class Settings {
@Deprecated
public static final String LAYER_COMPRESSION_METHOD_GZIP = "gzip";
public static final String LAYER_COMPRESSION_METHOD_ZLIB = "zlib";
public String layerCompressionMethod = LAYER_COMPRESSION_METHOD_ZLIB;
}
public Settings settings = new Settings();
/**
* Saves a map to an XML file.
*
* @param map a {@link tiled.core.Map} object.
* @param filename the filename of the map file
* @throws java.io.IOException if any.
*/
public void writeMap(Map map, String filename) throws IOException {
OutputStream os = new FileOutputStream(filename);
if (filename.endsWith(".tmx.gz")) {
os = new GZIPOutputStream(os);
}
Writer writer = new OutputStreamWriter(os, Charset.forName("UTF-8"));
XMLWriter xmlWriter = new XMLWriter(writer);
xmlWriter.startDocument();
writeMap(map, xmlWriter, filename);
xmlWriter.endDocument();
writer.flush();
if (os instanceof GZIPOutputStream) {
((GZIPOutputStream) os).finish();
}
}
/**
* Saves a tileset to an XML file.
*
* @param set a {@link tiled.core.TileSet} object.
* @param filename the filename of the tileset file
* @throws java.io.IOException if any.
*/
public void writeTileset(TileSet set, String filename) throws IOException {
OutputStream os = new FileOutputStream(filename);
Writer writer = new OutputStreamWriter(os, Charset.forName("UTF-8"));
XMLWriter xmlWriter = new XMLWriter(writer);
xmlWriter.startDocument();
writeTileset(set, xmlWriter, filename);
xmlWriter.endDocument();
writer.flush();
}
/**
*
writeMap.
*
* @param map a {@link tiled.core.Map} object.
* @param out a {@link java.io.OutputStream} object.
* @throws java.lang.Exception if any.
*/
public void writeMap(Map map, OutputStream out) throws Exception {
Writer writer = new OutputStreamWriter(out, Charset.forName("UTF-8"));
XMLWriter xmlWriter = new XMLWriter(writer);
xmlWriter.startDocument();
writeMap(map, xmlWriter, "/.");
xmlWriter.endDocument();
writer.flush();
}
/**
*
writeTileset.
*
* @param set a {@link tiled.core.TileSet} object.
* @param out a {@link java.io.OutputStream} object.
* @throws java.lang.Exception if any.
*/
public void writeTileset(TileSet set, OutputStream out) throws Exception {
Writer writer = new OutputStreamWriter(out, Charset.forName("UTF-8"));
XMLWriter xmlWriter = new XMLWriter(writer);
xmlWriter.startDocument();
writeTileset(set, xmlWriter, "/.");
xmlWriter.endDocument();
writer.flush();
}
private void writeMap(Map map, XMLWriter w, String wp) throws IOException {
w.writeDocType("map", null, "http://mapeditor.org/dtd/1.0/map.dtd");
w.startElement("map");
w.writeAttribute("version", "1.0");
Map.Orientation orientation = map.getOrientation();
w.writeAttribute("orientation", String.valueOf(orientation));
w.writeAttribute("width", map.getWidth());
w.writeAttribute("height", map.getHeight());
w.writeAttribute("tilewidth", map.getTileWidth());
w.writeAttribute("tileheight", map.getTileHeight());
switch (orientation) {
case HEXAGONAL:
w.writeAttribute("hexsidelength", map.getHexSideLength());
case STAGGERED:
w.writeAttribute("staggeraxis", String.valueOf(map.getStaggerAxis()));
w.writeAttribute("staggerindex", String.valueOf(map.getStaggerIndex()));
}
writeProperties(map.getProperties(), w);
firstGidPerTileset = new HashMap<>();
int firstgid = 1;
for (TileSet tileset : map.getTileSets()) {
setFirstGidForTileset(tileset, firstgid);
writeTilesetReference(tileset, w, wp);
firstgid += tileset.getMaxTileId() + 1;
}
for (MapLayer layer : map) {
writeMapLayer(layer, w, wp);
}
firstGidPerTileset = null;
w.endElement();
}
private static void writeProperties(Properties props, XMLWriter w) throws
IOException {
if (!props.isEmpty()) {
final Set