Search in sources :

Example 6 with Array

use of com.badlogic.gdx.utils.Array in project libgdx by libgdx.

the class GridPacker method packPage.

private Page packPage(Array<Rect> inputRects, int cellWidth, int cellHeight) {
    Page page = new Page();
    page.outputRects = new Array();
    int maxWidth = settings.maxWidth, maxHeight = settings.maxHeight;
    if (settings.edgePadding) {
        maxWidth -= settings.paddingX;
        maxHeight -= settings.paddingY;
    }
    int x = 0, y = 0;
    for (int i = inputRects.size - 1; i >= 0; i--) {
        if (x + cellWidth > maxWidth) {
            y += cellHeight;
            if (y > maxHeight - cellHeight)
                break;
            x = 0;
        }
        Rect rect = inputRects.removeIndex(i);
        rect.x = x;
        rect.y = y;
        rect.width += settings.paddingX;
        rect.height += settings.paddingY;
        page.outputRects.add(rect);
        x += cellWidth;
        page.width = Math.max(page.width, x);
        page.height = Math.max(page.height, y + cellHeight);
    }
    // Flip so rows start at top.
    for (int i = page.outputRects.size - 1; i >= 0; i--) {
        Rect rect = page.outputRects.get(i);
        rect.y = page.height - rect.y - rect.height;
    }
    return page;
}
Also used : Array(com.badlogic.gdx.utils.Array) Rect(com.badlogic.gdx.tools.texturepacker.TexturePacker.Rect) Page(com.badlogic.gdx.tools.texturepacker.TexturePacker.Page)

Example 7 with Array

use of com.badlogic.gdx.utils.Array in project libgdx by libgdx.

the class PolygonRegionLoader method getDependencies.

/** If the PSH file contains a line starting with {@link PolygonRegionParameters#texturePrefix params.texturePrefix}, an
	 * {@link AssetDescriptor} for the file referenced on that line will be added to the returned Array. Otherwise a sibling of the
	 * given file with the same name and the first found extension in {@link PolygonRegionParameters#textureExtensions
	 * params.textureExtensions} will be used. If no suitable file is found, the returned Array will be empty. */
@Override
public Array<AssetDescriptor> getDependencies(String fileName, FileHandle file, PolygonRegionParameters params) {
    if (params == null)
        params = defaultParameters;
    String image = null;
    try {
        BufferedReader reader = file.reader(params.readerBuffer);
        for (String line = reader.readLine(); line != null; line = reader.readLine()) if (line.startsWith(params.texturePrefix)) {
            image = line.substring(params.texturePrefix.length());
            break;
        }
        reader.close();
    } catch (IOException e) {
        throw new GdxRuntimeException("Error reading " + fileName, e);
    }
    if (image == null && params.textureExtensions != null)
        for (String extension : params.textureExtensions) {
            FileHandle sibling = file.sibling(file.nameWithoutExtension().concat("." + extension));
            if (sibling.exists())
                image = sibling.name();
        }
    if (image != null) {
        Array<AssetDescriptor> deps = new Array<AssetDescriptor>(1);
        deps.add(new AssetDescriptor<Texture>(file.sibling(image), Texture.class));
        return deps;
    }
    return null;
}
Also used : GdxRuntimeException(com.badlogic.gdx.utils.GdxRuntimeException) Array(com.badlogic.gdx.utils.Array) FileHandle(com.badlogic.gdx.files.FileHandle) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) Texture(com.badlogic.gdx.graphics.Texture) AssetDescriptor(com.badlogic.gdx.assets.AssetDescriptor)

Example 8 with Array

use of com.badlogic.gdx.utils.Array in project libgdx by libgdx.

the class BitmapFontLoader method loadSync.

@Override
public BitmapFont loadSync(AssetManager manager, String fileName, FileHandle file, BitmapFontParameter parameter) {
    if (parameter != null && parameter.atlasName != null) {
        TextureAtlas atlas = manager.get(parameter.atlasName, TextureAtlas.class);
        String name = file.sibling(data.imagePaths[0]).nameWithoutExtension().toString();
        AtlasRegion region = atlas.findRegion(name);
        if (region == null)
            throw new GdxRuntimeException("Could not find font region " + name + " in atlas " + parameter.atlasName);
        return new BitmapFont(file, region);
    } else {
        int n = data.getImagePaths().length;
        Array<TextureRegion> regs = new Array(n);
        for (int i = 0; i < n; i++) {
            regs.add(new TextureRegion(manager.get(data.getImagePath(i), Texture.class)));
        }
        return new BitmapFont(data, regs, true);
    }
}
Also used : GdxRuntimeException(com.badlogic.gdx.utils.GdxRuntimeException) Array(com.badlogic.gdx.utils.Array) TextureRegion(com.badlogic.gdx.graphics.g2d.TextureRegion) TextureAtlas(com.badlogic.gdx.graphics.g2d.TextureAtlas) AtlasRegion(com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasRegion) BitmapFont(com.badlogic.gdx.graphics.g2d.BitmapFont)

Example 9 with Array

use of com.badlogic.gdx.utils.Array in project libgdx by libgdx.

the class TideMapLoader method loadLayer.

private void loadLayer(TiledMap map, Element element) {
    if (element.getName().equals("Layer")) {
        String id = element.getAttribute("Id");
        String visible = element.getAttribute("Visible");
        Element dimensions = element.getChildByName("Dimensions");
        String layerSize = dimensions.getAttribute("LayerSize");
        String tileSize = dimensions.getAttribute("TileSize");
        String[] layerSizeParts = layerSize.split(" x ");
        int layerSizeX = Integer.parseInt(layerSizeParts[0]);
        int layerSizeY = Integer.parseInt(layerSizeParts[1]);
        String[] tileSizeParts = tileSize.split(" x ");
        int tileSizeX = Integer.parseInt(tileSizeParts[0]);
        int tileSizeY = Integer.parseInt(tileSizeParts[1]);
        TiledMapTileLayer layer = new TiledMapTileLayer(layerSizeX, layerSizeY, tileSizeX, tileSizeY);
        layer.setName(id);
        layer.setVisible(visible.equalsIgnoreCase("True"));
        Element tileArray = element.getChildByName("TileArray");
        Array<Element> rows = tileArray.getChildrenByName("Row");
        TiledMapTileSets tilesets = map.getTileSets();
        TiledMapTileSet currentTileSet = null;
        int firstgid = 0;
        int x, y;
        for (int row = 0, rowCount = rows.size; row < rowCount; row++) {
            Element currentRow = rows.get(row);
            y = rowCount - 1 - row;
            x = 0;
            for (int child = 0, childCount = currentRow.getChildCount(); child < childCount; child++) {
                Element currentChild = currentRow.getChild(child);
                String name = currentChild.getName();
                if (name.equals("TileSheet")) {
                    currentTileSet = tilesets.getTileSet(currentChild.getAttribute("Ref"));
                    firstgid = currentTileSet.getProperties().get("firstgid", Integer.class);
                } else if (name.equals("Null")) {
                    x += currentChild.getIntAttribute("Count");
                } else if (name.equals("Static")) {
                    Cell cell = new Cell();
                    cell.setTile(currentTileSet.getTile(firstgid + currentChild.getIntAttribute("Index")));
                    layer.setCell(x++, y, cell);
                } else if (name.equals("Animated")) {
                    // Create an AnimatedTile
                    int interval = currentChild.getInt("Interval");
                    Element frames = currentChild.getChildByName("Frames");
                    Array<StaticTiledMapTile> frameTiles = new Array<StaticTiledMapTile>();
                    for (int frameChild = 0, frameChildCount = frames.getChildCount(); frameChild < frameChildCount; frameChild++) {
                        Element frame = frames.getChild(frameChild);
                        String frameName = frame.getName();
                        if (frameName.equals("TileSheet")) {
                            currentTileSet = tilesets.getTileSet(frame.getAttribute("Ref"));
                            firstgid = currentTileSet.getProperties().get("firstgid", Integer.class);
                        } else if (frameName.equals("Static")) {
                            frameTiles.add((StaticTiledMapTile) currentTileSet.getTile(firstgid + frame.getIntAttribute("Index")));
                        }
                    }
                    Cell cell = new Cell();
                    cell.setTile(new AnimatedTiledMapTile(interval / 1000f, frameTiles));
                    // TODO: Reuse existing animated tiles
                    layer.setCell(x++, y, cell);
                }
            }
        }
        Element properties = element.getChildByName("Properties");
        if (properties != null) {
            loadProperties(layer.getProperties(), properties);
        }
        map.getLayers().add(layer);
    }
}
Also used : AnimatedTiledMapTile(com.badlogic.gdx.maps.tiled.tiles.AnimatedTiledMapTile) Element(com.badlogic.gdx.utils.XmlReader.Element) Array(com.badlogic.gdx.utils.Array) StaticTiledMapTile(com.badlogic.gdx.maps.tiled.tiles.StaticTiledMapTile) Cell(com.badlogic.gdx.maps.tiled.TiledMapTileLayer.Cell)

Example 10 with Array

use of com.badlogic.gdx.utils.Array in project libgdx by libgdx.

the class TideMapLoader method loadTileSheets.

/** Loads the tilesets
	 * @param root the root XML element
	 * @return a list of filenames for images containing tiles
	 * @throws IOException */
private Array<FileHandle> loadTileSheets(Element root, FileHandle tideFile) throws IOException {
    Array<FileHandle> images = new Array<FileHandle>();
    Element tilesheets = root.getChildByName("TileSheets");
    for (Element tileset : tilesheets.getChildrenByName("TileSheet")) {
        Element imageSource = tileset.getChildByName("ImageSource");
        FileHandle image = getRelativeFileHandle(tideFile, imageSource.getText());
        images.add(image);
    }
    return images;
}
Also used : Array(com.badlogic.gdx.utils.Array) FileHandle(com.badlogic.gdx.files.FileHandle) Element(com.badlogic.gdx.utils.XmlReader.Element)

Aggregations

Array (com.badlogic.gdx.utils.Array)67 FileHandle (com.badlogic.gdx.files.FileHandle)18 GdxRuntimeException (com.badlogic.gdx.utils.GdxRuntimeException)16 AssetDescriptor (com.badlogic.gdx.assets.AssetDescriptor)9 Texture (com.badlogic.gdx.graphics.Texture)8 TextureAtlas (com.badlogic.gdx.graphics.g2d.TextureAtlas)7 IntArray (com.badlogic.gdx.utils.IntArray)7 Element (com.badlogic.gdx.utils.XmlReader.Element)7 IOException (java.io.IOException)7 BitmapFont (com.badlogic.gdx.graphics.g2d.BitmapFont)5 TextureRegion (com.badlogic.gdx.graphics.g2d.TextureRegion)5 JsonValue (com.badlogic.gdx.utils.JsonValue)5 ObjectMap (com.badlogic.gdx.utils.ObjectMap)5 Color (com.badlogic.gdx.graphics.Color)4 ModelMaterial (com.badlogic.gdx.graphics.g3d.model.data.ModelMaterial)4 Page (com.badlogic.gdx.tools.texturepacker.TexturePacker.Page)4 Rect (com.badlogic.gdx.tools.texturepacker.TexturePacker.Rect)4 Json (com.badlogic.gdx.utils.Json)4 TextureParameter (com.badlogic.gdx.assets.loaders.TextureLoader.TextureParameter)3 GwtFileHandle (com.badlogic.gdx.backends.gwt.GwtFileHandle)3