Example usage for com.badlogic.gdx.maps.tiled TiledMapTileSet getProperties

List of usage examples for com.badlogic.gdx.maps.tiled TiledMapTileSet getProperties

Introduction

In this page you can find the example usage for com.badlogic.gdx.maps.tiled TiledMapTileSet getProperties.

Prototype

public MapProperties getProperties() 

Source Link

Usage

From source file:com.betmansmall.game.gameLogic.mapLoader.MapLoader.java

License:Apache License

/** Loads the specified tileset data, adding it to the collection of the specified map, given the XML element, the tmxFile and
 * an {@link ImageResolver} used to retrieve the tileset Textures.
 *
 * <p>/*from  w ww.j a v  a  2 s  . c o  m*/
 * Default tileset's property keys that are loaded by default are:
 * </p>
 *
 * <ul>
 * <li><em>firstgid</em>, (int, defaults to 1) the first valid global id used for tile numbering</li>
 * <li><em>imagesource</em>, (String, defaults to empty string) the tileset source image filename</li>
 * <li><em>imagewidth</em>, (int, defaults to 0) the tileset source image width</li>
 * <li><em>imageheight</em>, (int, defaults to 0) the tileset source image height</li>
 * <li><em>tilewidth</em>, (int, defaults to 0) the tile width</li>
 * <li><em>tileheight</em>, (int, defaults to 0) the tile height</li>
 * <li><em>margin</em>, (int, defaults to 0) the tileset margin</li>
 * <li><em>spacing</em>, (int, defaults to 0) the tileset spacing</li>
 * </ul>
 *
 * <p>
 * The values are extracted from the specified Tmx file, if a value can't be found then the default is used.
 * </p>
 * @param map the Map whose tilesets collection will be populated
 * @param element the XML element identifying the tileset to load
 * @param tmxFile the Filehandle of the tmx file
 * @param imageResolver the {@link ImageResolver} */
protected void loadTileSet(TiledMap map, Element element, FileHandle tmxFile, ImageResolver imageResolver) {
    if (element.getName().equals("tileset")) {
        String name = element.get("name", null);
        int firstgid = element.getIntAttribute("firstgid", 1);
        int tilewidth = element.getIntAttribute("tilewidth", 0);
        int tileheight = element.getIntAttribute("tileheight", 0);
        int spacing = element.getIntAttribute("spacing", 0);
        int margin = element.getIntAttribute("margin", 0);
        String source = element.getAttribute("source", null);

        int offsetX = 0;
        int offsetY = 0;

        String imageSource = "";
        int imageWidth = 0, imageHeight = 0;

        FileHandle image = null;
        if (source != null) {
            FileHandle tsx = getRelativeFileHandle(tmxFile, source);
            try {
                element = xml.parse(tsx);
                name = element.get("name", null);
                tilewidth = element.getIntAttribute("tilewidth", 0);
                tileheight = element.getIntAttribute("tileheight", 0);
                spacing = element.getIntAttribute("spacing", 0);
                margin = element.getIntAttribute("margin", 0);
                Element offset = element.getChildByName("tileoffset");
                if (offset != null) {
                    offsetX = offset.getIntAttribute("x", 0);
                    offsetY = offset.getIntAttribute("y", 0);
                }
                Element imageElement = element.getChildByName("image");
                if (imageElement != null) {
                    imageSource = imageElement.getAttribute("source");
                    imageWidth = imageElement.getIntAttribute("width", 0);
                    imageHeight = imageElement.getIntAttribute("height", 0);
                    image = getRelativeFileHandle(tsx, imageSource);
                }
            } catch (IOException e) {
                throw new GdxRuntimeException("Error parsing external tileset.");
            }
        } else {
            Element offset = element.getChildByName("tileoffset");
            if (offset != null) {
                offsetX = offset.getIntAttribute("x", 0);
                offsetY = offset.getIntAttribute("y", 0);
            }
            Element imageElement = element.getChildByName("image");
            if (imageElement != null) {
                imageSource = imageElement.getAttribute("source");
                imageWidth = imageElement.getIntAttribute("width", 0);
                imageHeight = imageElement.getIntAttribute("height", 0);
                image = getRelativeFileHandle(tmxFile, imageSource);
            }
        }

        TiledMapTileSet tileset = new TiledMapTileSet();
        tileset.setName(name);
        tileset.getProperties().put("firstgid", firstgid);
        if (image != null) {
            TextureRegion texture = imageResolver.getImage(image.path());

            MapProperties props = tileset.getProperties();
            props.put("imagesource", imageSource);
            props.put("imagewidth", imageWidth);
            props.put("imageheight", imageHeight);
            props.put("tilewidth", tilewidth);
            props.put("tileheight", tileheight);
            props.put("margin", margin);
            props.put("spacing", spacing);

            int stopWidth = texture.getRegionWidth() - tilewidth;
            int stopHeight = texture.getRegionHeight() - tileheight;

            int id = firstgid;

            for (int y = margin; y <= stopHeight; y += tileheight + spacing) {
                for (int x = margin; x <= stopWidth; x += tilewidth + spacing) {
                    TextureRegion tileRegion = new TextureRegion(texture, x, y, tilewidth, tileheight);
                    TiledMapTile tile = new StaticTiledMapTile(tileRegion);
                    tile.setId(id);
                    tile.setOffsetX(offsetX);
                    tile.setOffsetY(flipY ? -offsetY : offsetY);
                    tileset.putTile(id++, tile);
                }
            }
        } else {
            Array<Element> tileElements = element.getChildrenByName("tile");
            for (Element tileElement : tileElements) {
                Element imageElement = tileElement.getChildByName("image");
                if (imageElement != null) {
                    imageSource = imageElement.getAttribute("source");
                    imageWidth = imageElement.getIntAttribute("width", 0);
                    imageHeight = imageElement.getIntAttribute("height", 0);
                    image = getRelativeFileHandle(tmxFile, imageSource);
                }
                TextureRegion texture = imageResolver.getImage(image.path());
                TiledMapTile tile = new StaticTiledMapTile(texture);
                tile.setId(firstgid + tileElement.getIntAttribute("id"));
                tile.setOffsetX(offsetX);
                tile.setOffsetY(flipY ? -offsetY : offsetY);
                tileset.putTile(tile.getId(), tile);
            }
        }
        Array<Element> tileElements = element.getChildrenByName("tile");

        Array<AnimatedTiledMapTile> animatedTiles = new Array<AnimatedTiledMapTile>();

        for (Element tileElement : tileElements) {
            int localtid = tileElement.getIntAttribute("id", 0);
            TiledMapTile tile = tileset.getTile(firstgid + localtid);
            if (tile != null) {
                Element animationElement = tileElement.getChildByName("animation");
                if (animationElement != null) {

                    Array<StaticTiledMapTile> staticTiles = new Array<StaticTiledMapTile>();
                    IntArray intervals = new IntArray();
                    for (Element frameElement : animationElement.getChildrenByName("frame")) {
                        staticTiles.add((StaticTiledMapTile) tileset
                                .getTile(firstgid + frameElement.getIntAttribute("tileid")));
                        intervals.add(frameElement.getIntAttribute("duration"));
                    }

                    AnimatedTiledMapTile animatedTile = new AnimatedTiledMapTile(intervals, staticTiles);
                    animatedTile.setId(tile.getId());
                    animatedTiles.add(animatedTile);
                    tile = animatedTile;
                }

                String terrain = tileElement.getAttribute("terrain", null);
                if (terrain != null) {
                    tile.getProperties().put("terrain", terrain);
                }
                String probability = tileElement.getAttribute("probability", null);
                if (probability != null) {
                    tile.getProperties().put("probability", probability);
                }
                Element properties = tileElement.getChildByName("properties");
                if (properties != null) {
                    loadProperties(tile.getProperties(), properties);
                }
            }
        }

        for (AnimatedTiledMapTile tile : animatedTiles) {
            tileset.putTile(tile.getId(), tile);
        }

        Element properties = element.getChildByName("properties");
        if (properties != null) {
            loadProperties(tileset.getProperties(), properties);
        }
        map.getTileSets().addTileSet(tileset);
    }
}

From source file:com.skettios.loader.TmxMapLoaderFixed.java

License:Apache License

/**
 * Loads the specified tileset data, adding it to the collection of the specified map, given the XML element, the tmxFile and
 * an {@link ImageResolver} used to retrieve the tileset Textures.
 * <p/>//w  w  w  .ja v a 2s  . c  o m
 * <p>
 * Default tileset's property keys that are loaded by default are:
 * </p>
 * <p/>
 * <ul>
 * <li><em>firstgid</em>, (int, defaults to 1) the first valid global id used for tile numbering</li>
 * <li><em>imagesource</em>, (String, defaults to empty string) the tileset source image filename</li>
 * <li><em>imagewidth</em>, (int, defaults to 0) the tileset source image width</li>
 * <li><em>imageheight</em>, (int, defaults to 0) the tileset source image height</li>
 * <li><em>tilewidth</em>, (int, defaults to 0) the tile width</li>
 * <li><em>tileheight</em>, (int, defaults to 0) the tile height</li>
 * <li><em>margin</em>, (int, defaults to 0) the tileset margin</li>
 * <li><em>spacing</em>, (int, defaults to 0) the tileset spacing</li>
 * </ul>
 * <p/>
 * <p>
 * The values are extracted from the specified Tmx file, if a value can't be found then the default is used.
 * </p>
 *
 * @param map           the Map whose tilesets collection will be populated
 * @param element       the XML element identifying the tileset to load
 * @param tmxFile       the Filehandle of the tmx file
 * @param imageResolver the {@link ImageResolver}
 */
protected void loadTileSet(TiledMap map, Element element, FileHandle tmxFile, ImageResolver imageResolver) {
    if (element.getName().equals("tileset")) {
        String name = element.get("name", null);
        int firstgid = element.getIntAttribute("firstgid", 1);
        int tilewidth = element.getIntAttribute("tilewidth", 0);
        int tileheight = element.getIntAttribute("tileheight", 0);
        int spacing = element.getIntAttribute("spacing", 0);
        int margin = element.getIntAttribute("margin", 0);
        String source = element.getAttribute("source", null);

        int offsetX = 0;
        int offsetY = 0;

        String imageSource = "";
        int imageWidth = 0, imageHeight = 0;

        FileHandle image = null;
        if (source != null) {
            FileHandle tsx = getRelativeFileHandleFixed(tmxFile, source);
            try {
                element = xml.parse(tsx);
                name = element.get("name", null);
                tilewidth = element.getIntAttribute("tilewidth", 0);
                tileheight = element.getIntAttribute("tileheight", 0);
                spacing = element.getIntAttribute("spacing", 0);
                margin = element.getIntAttribute("margin", 0);
                Element offset = element.getChildByName("tileoffset");
                if (offset != null) {
                    offsetX = offset.getIntAttribute("x", 0);
                    offsetY = offset.getIntAttribute("y", 0);
                }
                Element imageElement = element.getChildByName("image");
                if (imageElement != null) {
                    imageSource = imageElement.getAttribute("source");
                    imageWidth = imageElement.getIntAttribute("width", 0);
                    imageHeight = imageElement.getIntAttribute("height", 0);
                    image = getRelativeFileHandleFixed(tsx, imageSource);
                }
            } catch (IOException e) {
                throw new GdxRuntimeException("Error parsing external tileset.");
            }
        } else {
            Element offset = element.getChildByName("tileoffset");
            if (offset != null) {
                offsetX = offset.getIntAttribute("x", 0);
                offsetY = offset.getIntAttribute("y", 0);
            }
            Element imageElement = element.getChildByName("image");
            if (imageElement != null) {
                imageSource = imageElement.getAttribute("source");
                imageWidth = imageElement.getIntAttribute("width", 0);
                imageHeight = imageElement.getIntAttribute("height", 0);
                image = getRelativeFileHandleFixed(tmxFile, imageSource);
            }
        }

        TiledMapTileSet tileset = new TiledMapTileSet();
        tileset.setName(name);
        tileset.getProperties().put("firstgid", firstgid);
        if (image != null) {
            TextureRegion texture = imageResolver.getImage(image.path());

            MapProperties props = tileset.getProperties();
            props.put("imagesource", imageSource);
            props.put("imagewidth", imageWidth);
            props.put("imageheight", imageHeight);
            props.put("tilewidth", tilewidth);
            props.put("tileheight", tileheight);
            props.put("margin", margin);
            props.put("spacing", spacing);

            int stopWidth = texture.getRegionWidth() - tilewidth;
            int stopHeight = texture.getRegionHeight() - tileheight;

            int id = firstgid;

            for (int y = margin; y <= stopHeight; y += tileheight + spacing) {
                for (int x = margin; x <= stopWidth; x += tilewidth + spacing) {
                    TextureRegion tileRegion = new TextureRegion(texture, x, y, tilewidth, tileheight);
                    TiledMapTile tile = new StaticTiledMapTile(tileRegion);
                    tile.setId(id);
                    tile.setOffsetX(offsetX);
                    tile.setOffsetY(-offsetY);
                    tileset.putTile(id++, tile);
                }
            }
        } else {
            Array<Element> tileElements = element.getChildrenByName("tile");
            for (Element tileElement : tileElements) {
                Element imageElement = tileElement.getChildByName("image");
                if (imageElement != null) {
                    imageSource = imageElement.getAttribute("source");
                    imageWidth = imageElement.getIntAttribute("width", 0);
                    imageHeight = imageElement.getIntAttribute("height", 0);
                    image = getRelativeFileHandleFixed(tmxFile, imageSource);
                }
                TextureRegion texture = imageResolver.getImage(image.path());
                TiledMapTile tile = new StaticTiledMapTile(texture);
                tile.setId(firstgid + tileElement.getIntAttribute("id"));
                tile.setOffsetX(offsetX);
                tile.setOffsetY(-offsetY);
                tileset.putTile(tile.getId(), tile);
            }
        }
        Array<Element> tileElements = element.getChildrenByName("tile");

        Array<AnimatedTiledMapTile> animatedTiles = new Array<AnimatedTiledMapTile>();

        for (Element tileElement : tileElements) {
            int localtid = tileElement.getIntAttribute("id", 0);
            TiledMapTile tile = tileset.getTile(firstgid + localtid);
            if (tile != null) {
                Element animationElement = tileElement.getChildByName("animation");
                if (animationElement != null) {

                    Array<StaticTiledMapTile> staticTiles = new Array<StaticTiledMapTile>();
                    IntArray intervals = new IntArray();
                    for (Element frameElement : animationElement.getChildrenByName("frame")) {
                        staticTiles.add((StaticTiledMapTile) tileset
                                .getTile(firstgid + frameElement.getIntAttribute("tileid")));
                        intervals.add(frameElement.getIntAttribute("duration"));
                    }

                    AnimatedTiledMapTile animatedTile = new AnimatedTiledMapTile(intervals, staticTiles);
                    animatedTile.setId(tile.getId());
                    animatedTiles.add(animatedTile);
                    tile = animatedTile;
                }

                String terrain = tileElement.getAttribute("terrain", null);
                if (terrain != null) {
                    tile.getProperties().put("terrain", terrain);
                }
                String probability = tileElement.getAttribute("probability", null);
                if (probability != null) {
                    tile.getProperties().put("probability", probability);
                }
                Element properties = tileElement.getChildByName("properties");
                if (properties != null) {
                    loadProperties(tile.getProperties(), properties);
                }
            }
        }

        for (AnimatedTiledMapTile tile : animatedTiles) {
            tileset.putTile(tile.getId(), tile);
        }

        Element properties = element.getChildByName("properties");
        if (properties != null) {
            loadProperties(tileset.getProperties(), properties);
        }
        map.getTileSets().addTileSet(tileset);
    }
}

From source file:com.stercore.code.net.dermetfan.utils.libgdx.maps.MapUtils.java

License:Apache License

/** @see #readableHierarchy(com.badlogic.gdx.maps.Map, int) */
public static String readableHierarchy(TiledMapTileSet set, int indent) {
    String hierarchy = "";
    for (int i = 0; i < indent; i++)
        hierarchy += '\t';
    hierarchy += ClassReflection.getSimpleName(set.getClass()) + ' ' + set.getName() + " (" + set.size()
            + " tiles)\n";
    hierarchy += readableHierarchy(set.getProperties(), indent + 1);
    for (TiledMapTile tile : set)
        hierarchy += readableHierarchy(tile, indent + 1);
    return hierarchy;
}

From source file:com.stercore.code.net.dermetfan.utils.libgdx.maps.TmxMapWriter.java

License:Apache License

/** @param set the {@link TiledMapTileSet} to write in TMX format
 *  @return this {@link TmxMapWriter} */
public TmxMapWriter tmx(TiledMapTileSet set) throws IOException {
    MapProperties props = set.getProperties();
    element("tileset");
    attribute("firstgid", getProperty(props, "firstgid", 1));
    attribute("name", set.getName());
    attribute("tilewidth", getProperty(props, "tilewidth", 0));
    attribute("tileheight", getProperty(props, "tileheight", 0));
    float spacing = getProperty(props, "spacing", Float.NaN), margin = getProperty(props, "margin", Float.NaN);
    if (!Float.isNaN(spacing))
        attribute("spacing", round(spacing));
    if (!Float.isNaN(margin))
        attribute("margin", round(margin));

    Iterator<TiledMapTile> iter = set.iterator();
    if (iter.hasNext()) {
        TiledMapTile tile = iter.next();
        element("tileoffset");
        attribute("x", round(tile.getOffsetX()));
        attribute("y", round(-tile.getOffsetY()));
        pop();//from  w  w  w .ja va2s. c o m
    }

    element("image");
    attribute("source", getProperty(props, "imagesource", ""));
    attribute("imagewidth", getProperty(props, "imagewidth", 0));
    attribute("imageheight", getProperty(props, "imageheight", 0));
    pop();

    iter = set.iterator();
    if (iter.hasNext()) {
        @SuppressWarnings("unchecked")
        Array<String> asAttributes = Pools.obtain(Array.class);
        asAttributes.clear();
        boolean elementEmitted = false;
        for (TiledMapTile tile = iter.next(); iter.hasNext(); tile = iter.next()) {
            MapProperties tileProps = tile.getProperties();
            for (String attribute : asAttributes)
                if (tileProps.containsKey(attribute)) {
                    if (!elementEmitted) {
                        element("tile");
                        elementEmitted = true;
                    }
                    attribute(attribute, tileProps.get(attribute));
                }
            tmx(tileProps, asAttributes);
        }
        asAttributes.clear();
        Pools.free(asAttributes);
        if (elementEmitted)
            pop();
    }

    pop();
    return this;
}

From source file:de.bitowl.advent.game2.MyAtlasTmxMapLoader.java

License:Apache License

protected void loadTileset(TiledMap map, Element element, FileHandle tmxFile, AtlasResolver resolver,
        AtlasTiledMapLoaderParameters parameter) {
    if (element.getName().equals("tileset")) {
        String name = element.get("name", null);
        int firstgid = element.getIntAttribute("firstgid", 1);
        int tilewidth = element.getIntAttribute("tilewidth", 0);
        int tileheight = element.getIntAttribute("tileheight", 0);
        int spacing = element.getIntAttribute("spacing", 0);
        int margin = element.getIntAttribute("margin", 0);
        String source = element.getAttribute("source", null);

        String imageSource = "";
        int imageWidth = 0, imageHeight = 0;

        FileHandle image = null;/*from   w  w w .j  a  va  2 s .  c o m*/
        if (source != null) {
            FileHandle tsx = getRelativeFileHandle(tmxFile, source);
            try {
                element = xml.parse(tsx);
                name = element.get("name", null);
                tilewidth = element.getIntAttribute("tilewidth", 0);
                tileheight = element.getIntAttribute("tileheight", 0);
                spacing = element.getIntAttribute("spacing", 0);
                margin = element.getIntAttribute("margin", 0);
                imageSource = element.getChildByName("image").getAttribute("source");
                imageWidth = element.getChildByName("image").getIntAttribute("width", 0);
                imageHeight = element.getChildByName("image").getIntAttribute("height", 0);
            } catch (IOException e) {
                throw new GdxRuntimeException("Error parsing external tileset.");
            }
        } else {
            imageSource = element.getChildByName("image").getAttribute("source");
            imageWidth = element.getChildByName("image").getIntAttribute("width", 0);
            imageHeight = element.getChildByName("image").getIntAttribute("height", 0);
        }

        if (!map.getProperties().containsKey("atlas")) {
            throw new GdxRuntimeException("The map is missing the 'atlas' property");
        }

        // get the TextureAtlas for this tileset
        FileHandle atlasHandle = getRelativeFileHandle(tmxFile, map.getProperties().get("atlas", String.class));
        atlasHandle = resolve(atlasHandle.path());
        TextureAtlas atlas = resolver.getAtlas(atlasHandle.path());
        //String regionsName = atlasHandle.nameWithoutExtension();
        String regionsName = name;

        if (parameter != null && parameter.forceTextureFilters) {
            for (Texture texture : atlas.getTextures()) {
                trackedTextures.add(texture);
            }
        }

        TiledMapTileSet tileset = new TiledMapTileSet();
        MapProperties props = tileset.getProperties();
        tileset.setName(name);
        props.put("firstgid", firstgid);
        props.put("imagesource", imageSource);
        props.put("imagewidth", imageWidth);
        props.put("imageheight", imageHeight);
        props.put("tilewidth", tilewidth);
        props.put("tileheight", tileheight);
        props.put("margin", margin);
        props.put("spacing", spacing);

        Array<AtlasRegion> regions = atlas.findRegions(regionsName);
        System.out.println(regions.size);
        for (AtlasRegion region : regions) {
            // handle unused tile ids
            if (region != null) {
                StaticTiledMapTile tile = new StaticTiledMapTile(region);

                if (!yUp) {
                    region.flip(false, true);
                }

                int tileid = firstgid + region.index;
                tile.setId(tileid);
                tileset.putTile(tileid, tile);
                System.out.println("put tile " + tileid);
            }
        }

        Array<Element> tileElements = element.getChildrenByName("tile");

        for (Element tileElement : tileElements) {
            int localtid = tileElement.getIntAttribute("id", 0);
            TiledMapTile tile = tileset.getTile(firstgid + localtid);
            if (tile != null) {
                String terrain = tileElement.getAttribute("terrain", null);
                if (terrain != null) {
                    tile.getProperties().put("terrain", terrain);
                }
                String probability = tileElement.getAttribute("probability", null);
                if (probability != null) {
                    tile.getProperties().put("probability", probability);
                }
                Element properties = tileElement.getChildByName("properties");
                if (properties != null) {
                    loadProperties(tile.getProperties(), properties);
                }
            }
        }

        Element properties = element.getChildByName("properties");
        if (properties != null) {
            loadProperties(tileset.getProperties(), properties);
        }
        System.out.println("add tileset to map: " + tileset.getName());
        map.getTileSets().addTileSet(tileset);
    }
}

From source file:de.bitowl.advent.game2.MyTiledMapPacker.java

License:Apache License

/** Returns the tileset name associated with the specified tile id
 * @return a tileset name *//*from  w w  w . j av a  2s  . c  o m*/
private String tilesetNameFromTileId(TiledMap map, int tileid) {
    String name = "";
    if (tileid == 0) {
        return "";
    }

    for (TiledMapTileSet tileset : map.getTileSets()) {
        int firstgid = tileset.getProperties().get("firstgid", -1, Integer.class);
        if (firstgid == -1)
            continue; // skip this tileset
        if (tileid >= firstgid) {
            name = tileset.getName();
        } else {
            return name;
        }
    }

    return name;
}

From source file:de.bitowl.advent.game2.MyTiledMapPacker.java

License:Apache License

/** Traverse the specified tilesets, optionally lookup the used ids and pass every tile image to the {@link TexturePacker2},
 * optionally ignoring unused tile ids */
private void packTilesets(ObjectMap<String, TiledMapTileSet> sets, FileHandle inputDirHandle, File outputDir,
        Settings texturePackerSettings) throws IOException {
    BufferedImage tile;/*  w  w  w.  j ava2 s  . c  om*/
    Vector2 tileLocation;
    TileSetLayout packerTileSet;
    Graphics g;

    packer = new TexturePacker2(texturePackerSettings);

    int tileidx = 0;
    for (TiledMapTileSet set : sets.values()) {
        String tilesetName = set.getName();
        System.out.println("Processing tileset " + tilesetName);
        IntArray usedIds = this.settings.stripUnusedTiles ? getUsedIdsBucket(tilesetName, -1) : null;

        int tileWidth = set.getProperties().get("tilewidth", Integer.class);
        int tileHeight = set.getProperties().get("tileheight", Integer.class);
        int firstgid = set.getProperties().get("firstgid", Integer.class);
        String imageName = set.getProperties().get("imagesource", String.class);

        TileSetLayout layout = new TileSetLayout(firstgid, set, inputDirHandle);
        tileidx = 0;
        for (int gid = layout.firstgid, i = 0; i < layout.numTiles; gid++, i++, tileidx++) {
            if (usedIds != null && !usedIds.contains(gid)) {
                System.out.println("Stripped id #" + gid + " from tileset \"" + tilesetName + "\"");
                continue;
            }

            tileLocation = layout.getLocation(gid);
            tile = new BufferedImage(tileWidth, tileHeight, BufferedImage.TYPE_4BYTE_ABGR);

            g = tile.createGraphics();
            g.drawImage(layout.image, 0, 0, tileWidth, tileHeight, (int) tileLocation.x, (int) tileLocation.y,
                    (int) tileLocation.x + tileWidth, (int) tileLocation.y + tileHeight, null);

            if (isBlended(tile))
                setBlended(gid);
            System.out.println("Adding " + tileWidth + "x" + tileHeight + " (" + (int) tileLocation.x + ", "
                    + (int) tileLocation.y + ")" + tileidx);
            packer.addImage(tile, tilesetName + "_" + tileidx);
        }
    }

    File outputDirTilesets = getRelativeFile(outputDir, this.settings.tilesetOutputDirectory);
    outputDirTilesets.mkdirs();
    packer.pack(outputDirTilesets, this.settings.atlasOutputName + ".atlas");
}

From source file:de.bitowl.advent.game2.TileSetLayout.java

License:Apache License

/** Constructs a Tile Set layout. The tile set image contained in the baseDir should be the original tile set images before
 * being processed by {@link TiledMapPacker} (the ones actually read by Tiled).
 * @param tileset the tile set to process
 * @param baseDir the directory in which the tile set image is stored */
protected TileSetLayout(int firstgid, TiledMapTileSet tileset, FileHandle baseDir) throws IOException {
    int tileWidth = tileset.getProperties().get("tilewidth", Integer.class);
    int tileHeight = tileset.getProperties().get("tileheight", Integer.class);
    int margin = tileset.getProperties().get("margin", Integer.class);
    int spacing = tileset.getProperties().get("spacing", Integer.class);

    this.firstgid = firstgid;

    image = ImageIO.read(baseDir.child(tileset.getProperties().get("imagesource", String.class)).read());

    imageTilePositions = new IntMap<Vector2>();

    // fill the tile regions
    int x, y, tile = 0;
    numRows = 0;//w  ww.j  a  va  2s.c om
    numCols = 0;

    int stopWidth = image.getWidth() - tileWidth;
    int stopHeight = image.getHeight() - tileHeight;

    for (y = margin; y <= stopHeight; y += tileHeight + spacing) {
        for (x = margin; x <= stopWidth; x += tileWidth + spacing) {
            if (y == margin)
                numCols++;
            imageTilePositions.put(tile, new Vector2(x, y));
            tile++;
        }
        numRows++;
    }

    numTiles = numRows * numCols;
}

From source file:de.fhkoeln.game.utils.libgdx.maps.MapUtils.java

License:Apache License

/** @see #readableHierarchy(com.badlogic.gdx.maps.Map, int) */
public static String readableHierarchy(TiledMapTileSet set, int indent) {
    String hierarchy = "";
    for (int i = 0; i < indent; i++)
        hierarchy += '\t';
    hierarchy += set.getClass().getSimpleName() + ' ' + set.getName() + " (" + set.size() + " tiles)\n";
    hierarchy += readableHierarchy(set.getProperties(), indent + 1);
    for (TiledMapTile tile : set)
        hierarchy += readableHierarchy(tile, indent + 1);
    return hierarchy;
}

From source file:me.boxcubed.main.desktop.server.ServerTiledMapLoader.java

License:Apache License

/** Loads the specified tileset data, adding it to the collection of the specified map, given the XML element, the tmxFile and
 * an {@link ImageResolver} used to retrieve the tileset Textures.
 * //w  ww. j ava 2  s. c o  m
 * <p>
 * Default tileset's property keys that are loaded by default are:
 * </p>
 * 
 * <ul>
 * <li><em>firstgid</em>, (int, defaults to 1) the first valid global id used for tile numbering</li>
 * <li><em>imagesource</em>, (String, defaults to empty string) the tileset source image filename</li>
 * <li><em>imagewidth</em>, (int, defaults to 0) the tileset source image width</li>
 * <li><em>imageheight</em>, (int, defaults to 0) the tileset source image height</li>
 * <li><em>tilewidth</em>, (int, defaults to 0) the tile width</li>
 * <li><em>tileheight</em>, (int, defaults to 0) the tile height</li>
 * <li><em>margin</em>, (int, defaults to 0) the tileset margin</li>
 * <li><em>spacing</em>, (int, defaults to 0) the tileset spacing</li>
 * </ul>
 * 
 * <p>
 * The values are extracted from the specified Tmx file, if a value can't be found then the default is used.
 * </p>
 * @param map the Map whose tilesets collection will be populated
 * @param element the XML element identifying the tileset to load
 * @param tmxFile the Filehandle of the tmx file
 * @param imageResolver the {@link ImageResolver} */
@SuppressWarnings("unused")
protected void loadTileSet(TiledMap map, Element element, FileHandle tmxFile, ImageResolver imageResolver) {
    if (element.getName().equals("tileset")) {
        String name = element.get("name", null);
        int firstgid = element.getIntAttribute("firstgid", 1);
        int tilewidth = element.getIntAttribute("tilewidth", 0);
        int tileheight = element.getIntAttribute("tileheight", 0);
        int spacing = element.getIntAttribute("spacing", 0);
        int margin = element.getIntAttribute("margin", 0);
        String source = element.getAttribute("source", null);

        int offsetX = 0;
        int offsetY = 0;

        String imageSource = "";
        int imageWidth = 0, imageHeight = 0;

        FileHandle image = null;
        if (source != null) {
            FileHandle tsx = getRelativeFileHandle(tmxFile, source);
            try {
                element = xml.parse(tsx);
                name = element.get("name", null);
                tilewidth = element.getIntAttribute("tilewidth", 0);
                tileheight = element.getIntAttribute("tileheight", 0);
                spacing = element.getIntAttribute("spacing", 0);
                margin = element.getIntAttribute("margin", 0);
                Element offset = element.getChildByName("tileoffset");
                if (offset != null) {
                    offsetX = offset.getIntAttribute("x", 0);
                    offsetY = offset.getIntAttribute("y", 0);
                }
                Element imageElement = element.getChildByName("image");
                if (imageElement != null) {
                    imageSource = imageElement.getAttribute("source");
                    imageWidth = imageElement.getIntAttribute("width", 0);
                    imageHeight = imageElement.getIntAttribute("height", 0);
                    image = getRelativeFileHandle(tsx, imageSource);
                }
            } catch (IOException e) {
                throw new GdxRuntimeException("Error parsing external tileset.");
            }
        } else {
            Element offset = element.getChildByName("tileoffset");
            if (offset != null) {
                offsetX = offset.getIntAttribute("x", 0);
                offsetY = offset.getIntAttribute("y", 0);
            }
            Element imageElement = element.getChildByName("image");
            if (imageElement != null) {
                imageSource = imageElement.getAttribute("source");
                imageWidth = imageElement.getIntAttribute("width", 0);
                imageHeight = imageElement.getIntAttribute("height", 0);
                image = getRelativeFileHandle(tmxFile, imageSource);
            }
        }

        TiledMapTileSet tileset = new TiledMapTileSet();
        tileset.setName(name);
        tileset.getProperties().put("firstgid", firstgid);
        if (false) {
        }

        else {
            Array<Element> tileElements = element.getChildrenByName("tile");
            for (Element tileElement : tileElements) {
                Element imageElement = tileElement.getChildByName("image");
                if (imageElement != null) {
                    imageSource = imageElement.getAttribute("source");
                    imageWidth = imageElement.getIntAttribute("width", 0);
                    imageHeight = imageElement.getIntAttribute("height", 0);
                    image = getRelativeFileHandle(tmxFile, imageSource);
                }
                TextureRegion texture = imageResolver.getImage(image.path());
                TiledMapTile tile = new StaticTiledMapTile(texture);
                tile.setId(firstgid + tileElement.getIntAttribute("id"));
                tile.setOffsetX(offsetX);
                tile.setOffsetY(flipY ? -offsetY : offsetY);
                tileset.putTile(tile.getId(), tile);
            }
        }
        Array<Element> tileElements = element.getChildrenByName("tile");

        Array<AnimatedTiledMapTile> animatedTiles = new Array<AnimatedTiledMapTile>();

        for (Element tileElement : tileElements) {
            int localtid = tileElement.getIntAttribute("id", 0);
            TiledMapTile tile = tileset.getTile(firstgid + localtid);
            if (tile != null) {
                Element animationElement = tileElement.getChildByName("animation");
                if (animationElement != null) {

                    Array<StaticTiledMapTile> staticTiles = new Array<StaticTiledMapTile>();
                    IntArray intervals = new IntArray();
                    for (Element frameElement : animationElement.getChildrenByName("frame")) {
                        staticTiles.add((StaticTiledMapTile) tileset
                                .getTile(firstgid + frameElement.getIntAttribute("tileid")));
                        intervals.add(frameElement.getIntAttribute("duration"));
                    }

                    AnimatedTiledMapTile animatedTile = new AnimatedTiledMapTile(intervals, staticTiles);
                    animatedTile.setId(tile.getId());
                    animatedTiles.add(animatedTile);
                    tile = animatedTile;
                }

                String terrain = tileElement.getAttribute("terrain", null);
                if (terrain != null) {
                    tile.getProperties().put("terrain", terrain);
                }
                String probability = tileElement.getAttribute("probability", null);
                if (probability != null) {
                    tile.getProperties().put("probability", probability);
                }
                Element properties = tileElement.getChildByName("properties");
                if (properties != null) {
                    loadProperties(tile.getProperties(), properties);
                }
            }
        }

        for (AnimatedTiledMapTile tile : animatedTiles) {
            tileset.putTile(tile.getId(), tile);
        }

        Element properties = element.getChildByName("properties");
        if (properties != null) {
            loadProperties(tileset.getProperties(), properties);
        }
        map.getTileSets().addTileSet(tileset);
    }
}