/** * 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; }
public Element parse(FileHandle file) throws IOException { try { return parse(file.reader("UTF-8")); } catch (Exception ex) { throw new SerializationException("Error parsing file: " + file, ex); } }
/** * Loads a PolygonRegion from a PSH (Polygon SHape) file. The PSH file format defines the polygon * vertices before triangulation: * * <p>s 200.0, 100.0, ... * * <p>Lines not prefixed with "s" are ignored. PSH files can be created with external tools, eg: * <br> * https://code.google.com/p/libgdx-polygoneditor/ <br> * http://www.codeandweb.com/physicseditor/ * * @param file file handle to the shape definition file */ public PolygonRegion load(TextureRegion textureRegion, FileHandle file) { BufferedReader reader = file.reader(256); try { while (true) { String line = reader.readLine(); if (line == null) break; if (line.startsWith("s")) { // Read shape. String[] polygonStrings = line.substring(1).trim().split(","); float[] vertices = new float[polygonStrings.length]; for (int i = 0, n = vertices.length; i < n; i++) vertices[i] = Float.parseFloat(polygonStrings[i]); // It would probably be better if PSH stored the vertices and triangles, then we don't // have to triangulate here. return new PolygonRegion( textureRegion, vertices, triangulator.computeTriangles(vertices).toArray()); } } } catch (IOException ex) { throw new GdxRuntimeException("Error reading polygon shape file: " + file, ex); } finally { StreamUtils.closeQuietly(reader); } throw new GdxRuntimeException("Polygon shape not found: " + file); }