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

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

Introduction

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

Prototype

public String getName() 

Source Link

Usage

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 . j a v a2  s .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

/** You can either run the {@link MyTiledMapPacker#main(String[])} method or reference this class in your own project and call
 * this method.//from  w  w w  .  j a va  2  s.  c o m
 * 
 * Keep in mind that this preprocessor will need to load the maps by using the {@link TmxMapLoader} loader and this in turn
 * will need a valid OpenGL context to work: this is probably subject to change in the future, where loading both maps metadata
 * and graphics resources should be made conditional.
 * 
 * Process a directory containing TMX map files representing Tiled maps and produce a single TextureAtlas as well as new
 * processed TMX map files, correctly referencing the generated {@link TextureAtlas} by using the "atlas" custom map property.
 * 
 * Typically, your maps will lie in a directory, such as "maps/" and your tilesets in a subdirectory such as "maps/city": this
 * layout will ensure that MapEditor will reference your tileset with a very simple relative path and no parent directory
 * names, such as "..", will ever happen in your TMX file definition avoiding much of the confusion caused by the preprocessor
 * working with relative paths.
 * 
 * <strong>WARNING!</strong> Use caution if you have a "../" in the path of your tile sets! The output for these tile sets will
 * be relative to the output directory. For example, if your output directory is "C:\mydir\maps" and you have a tileset with
 * the path "../tileset.png", the tileset will be output to "C:\mydir\" and the maps will be in "C:\mydir\maps".
 * 
 * @param inputDir the input directory containing the tmx files (and tile sets, relative to the path listed in the tmx file)
 * @param outputDir The output directory for the TMX files, <strong>should be empty before running</strong>.
 * @param settings the settings used in the TexturePacker */
public void processMaps(File inputDir, File outputDir, Settings settings) throws IOException {
    FileHandle inputDirHandle = new FileHandle(inputDir.getAbsolutePath());
    File[] files = inputDir.listFiles(new TmxFilter());
    ObjectMap<String, TiledMapTileSet> tilesetsToPack = new ObjectMap<String, TiledMapTileSet>();

    for (File file : files) {
        map = mapLoader.load(file.getAbsolutePath());

        // if enabled, build a list of used tileids for the tileset used by this map
        if (this.settings.stripUnusedTiles) {
            int mapWidth = map.getProperties().get("width", Integer.class);
            int mapHeight = map.getProperties().get("height", Integer.class);
            int numlayers = map.getLayers().getCount();
            int bucketSize = mapWidth * mapHeight * numlayers;

            Iterator<MapLayer> it = map.getLayers().iterator();
            while (it.hasNext()) {
                MapLayer layer = it.next();

                // some layers can be plain MapLayer instances (ie. object groups), just ignore them
                if (layer instanceof TiledMapTileLayer) {
                    TiledMapTileLayer tlayer = (TiledMapTileLayer) layer;

                    for (int y = 0; y < mapHeight; ++y) {
                        for (int x = 0; x < mapWidth; ++x) {
                            if (tlayer.getCell(x, y) != null) {
                                int tileid = tlayer.getCell(x, y).getTile().getId() & ~0xE0000000;
                                String tilesetName = tilesetNameFromTileId(map, tileid);
                                IntArray usedIds = getUsedIdsBucket(tilesetName, bucketSize);
                                usedIds.add(tileid);

                                // track this tileset to be packed if not already tracked
                                if (!tilesetsToPack.containsKey(tilesetName)) {
                                    tilesetsToPack.put(tilesetName, map.getTileSets().getTileSet(tilesetName));
                                }
                            }
                        }
                    }
                }
            }
        } else {
            for (TiledMapTileSet tileset : map.getTileSets()) {
                String tilesetName = tileset.getName();
                if (!tilesetsToPack.containsKey(tilesetName)) {
                    tilesetsToPack.put(tilesetName, tileset);
                }
            }
        }

        FileHandle tmxFile = new FileHandle(file.getAbsolutePath());
        writeUpdatedTMX(map, outputDir, tmxFile);
    }

    packTilesets(tilesetsToPack, inputDirHandle, outputDir, settings);
}

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 *///  www  .j av a2s.  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  . jav  a 2  s  .  c o m
    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.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:net.dermetfan.gdx.maps.MapUtils.java

License:Apache License

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