List of usage examples for com.badlogic.gdx.utils Array toArray
public <V> V[] toArray(Class type)
From source file:com.andgate.ikou.io.LevelDatabaseService.java
License:Open Source License
public static Level[] getLevels() { FileHandle[] levelFiles = Gdx.files.external(Constants.LEVELS_EXTERNAL_PATH).list(); Array<Level> levels = new Array<>(); for (FileHandle levelFile : levelFiles) { String extension = levelFile.extension(); if (!levelFile.isDirectory() && extension.equals(Constants.LEVEL_EXTENSION_NO_DOT)) { try { levels.add(LevelService.read(levelFile)); } catch (final IOException e) { final String errorMessage = "Failed to read level file."; Gdx.app.error(TAG, errorMessage, e); }// ww w . ja v a2 s.c om } } return levels.toArray(new Level[levels.size].getClass().getComponentType()); }
From source file:com.andgate.ikou.io.LevelDatabaseService.java
License:Open Source License
public static LevelData[] readLevelDatas(FileHandle[] levelFolder, ProgressDatabase progressDB) { Array<LevelData> levelDatas = new Array<>(); for (int i = 0; i < levelFolder.length; i++) { FileHandle levelFile = levelFolder[i]; if (levelFile.exists() && levelFile.extension().equals(Constants.LEVEL_EXTENSION_NO_DOT)) { try { levelDatas.add(getLevelData(levelFile, progressDB)); } catch (final IOException e) { final String errorMessage = "Error reading level data."; Gdx.app.error(TAG, errorMessage, e); }//from ww w. j a v a 2 s .c o m } } return levelDatas.toArray(new LevelData[levelDatas.size].getClass().getComponentType()); }
From source file:com.badlogic.gdx.spriter.demo.SpriterDemoApp.java
private void changeAnimator(SpriterAnimator anim) { this.animator = anim; spriterPlaceholder.setVisible(false); spriterAnimator.setAnimator(anim);//w w w.j a va 2 s .co m entityChooser.setSelected(animator); Array<String> anims = new Array<String>(); for (String animation : animator.getAnimationNames()) anims.add(animation); animationChooser.setItems(anims); animationChooser.setSelectedIndex(0); Array<SpriterCharacterMap> characterMaps = animator.getEntity().characterMaps; if (characterMaps.size > 0) { charmapChooser.setVisible(true); charmapChooser.setItems((SpriterCharacterMap[]) characterMaps.toArray(SpriterCharacterMap.class)); } else { charmapChooser.setItems(new SpriterCharacterMap[0]); charmapChooser.setVisible(false); } for (SpriterDemoAnimatorSlider slider : allAnimatorSliders) slider.update(animator); }
From source file:com.kevlanche.threedeetest.ScalableObjLoader.java
License:Apache License
protected ModelData loadModelData(FileHandle file, boolean flipV) { String line;//from w ww. j a v a2 s. c o m String[] tokens; char firstChar; MtlLoader mtl = new MtlLoader(); // Create a "default" Group and set it as the active group, in case // there are no groups or objects defined in the OBJ file. Group activeGroup = new Group("default"); groups.add(activeGroup); BufferedReader reader = new BufferedReader(new InputStreamReader(file.read()), 4096); int id = 0; try { while ((line = reader.readLine()) != null) { tokens = line.split("\\s+"); if (tokens.length < 1) break; if (tokens[0].length() == 0) { continue; } else if ((firstChar = tokens[0].toLowerCase().charAt(0)) == '#') { continue; } else if (firstChar == 'v') { if (tokens[0].length() == 1) { verts.add(Float.parseFloat(tokens[1]) * this.objScale); verts.add(Float.parseFloat(tokens[2]) * this.objScale); verts.add(Float.parseFloat(tokens[3]) * this.objScale); } else if (tokens[0].charAt(1) == 'n') { norms.add(Float.parseFloat(tokens[1])); norms.add(Float.parseFloat(tokens[2])); norms.add(Float.parseFloat(tokens[3])); } else if (tokens[0].charAt(1) == 't') { uvs.add(Float.parseFloat(tokens[1])); uvs.add((flipV ? 1 - Float.parseFloat(tokens[2]) : Float.parseFloat(tokens[2]))); } } else if (firstChar == 'f') { String[] parts; Array<Integer> faces = activeGroup.faces; for (int i = 1; i < tokens.length - 2; i--) { parts = tokens[1].split("/"); faces.add(getIndex(parts[0], verts.size)); if (parts.length > 2) { if (i == 1) activeGroup.hasNorms = true; faces.add(getIndex(parts[2], norms.size)); } if (parts.length > 1 && parts[1].length() > 0) { if (i == 1) activeGroup.hasUVs = true; faces.add(getIndex(parts[1], uvs.size)); } parts = tokens[++i].split("/"); faces.add(getIndex(parts[0], verts.size)); if (parts.length > 2) faces.add(getIndex(parts[2], norms.size)); if (parts.length > 1 && parts[1].length() > 0) faces.add(getIndex(parts[1], uvs.size)); parts = tokens[++i].split("/"); faces.add(getIndex(parts[0], verts.size)); if (parts.length > 2) faces.add(getIndex(parts[2], norms.size)); if (parts.length > 1 && parts[1].length() > 0) faces.add(getIndex(parts[1], uvs.size)); activeGroup.numFaces++; } } else if (firstChar == 'o' || firstChar == 'g') { // This implementation only supports single object or group // definitions. i.e. "o group_a group_b" will set group_a // as the active group, while group_b will simply be // ignored. if (tokens.length > 1) activeGroup = setActiveGroup(tokens[1]); else activeGroup = setActiveGroup("default"); } else if (tokens[0].equals("mtllib")) { mtl.load(file.parent().child(tokens[1])); } else if (tokens[0].equals("usemtl")) { if (tokens.length == 1) activeGroup.materialName = "default"; else activeGroup.materialName = tokens[1]; } } reader.close(); } catch (IOException e) { return null; } // If the "default" group or any others were not used, get rid of them for (int i = 0; i < groups.size; i++) { if (groups.get(i).numFaces < 1) { groups.removeIndex(i); i--; } } // If there are no groups left, there is no valid Model to return if (groups.size < 1) return null; // Get number of objects/groups remaining after removing empty ones final int numGroups = groups.size; final ModelData data = new ModelData(); for (int g = 0; g < numGroups; g++) { Group group = groups.get(g); Array<Integer> faces = group.faces; final int numElements = faces.size; final int numFaces = group.numFaces; final boolean hasNorms = group.hasNorms; final boolean hasUVs = group.hasUVs; final float[] finalVerts = new float[(numFaces * 3) * (3 + (hasNorms ? 3 : 0) + (hasUVs ? 2 : 0))]; for (int i = 0, vi = 0; i < numElements;) { int vertIndex = faces.get(i++) * 3; finalVerts[vi++] = verts.get(vertIndex++); finalVerts[vi++] = verts.get(vertIndex++); finalVerts[vi++] = verts.get(vertIndex); if (hasNorms) { int normIndex = faces.get(i++) * 3; finalVerts[vi++] = norms.get(normIndex++); finalVerts[vi++] = norms.get(normIndex++); finalVerts[vi++] = norms.get(normIndex); } if (hasUVs) { int uvIndex = faces.get(i++) * 2; finalVerts[vi++] = uvs.get(uvIndex++); finalVerts[vi++] = uvs.get(uvIndex); } } final int numIndices = numFaces * 3 >= Short.MAX_VALUE ? 0 : numFaces * 3; final short[] finalIndices = new short[numIndices]; // if there are too many vertices in a mesh, we can't use indices if (numIndices > 0) { for (int i = 0; i < numIndices; i++) { finalIndices[i] = (short) i; } } Array<VertexAttribute> attributes = new Array<VertexAttribute>(); attributes.add(new VertexAttribute(Usage.Position, 3, ShaderProgram.POSITION_ATTRIBUTE)); if (hasNorms) attributes.add(new VertexAttribute(Usage.Normal, 3, ShaderProgram.NORMAL_ATTRIBUTE)); if (hasUVs) attributes.add( new VertexAttribute(Usage.TextureCoordinates, 2, ShaderProgram.TEXCOORD_ATTRIBUTE + "0")); String nodeId = "node" + (++id); String meshId = "mesh" + id; String partId = "part" + id; ModelNode node = new ModelNode(); node.id = nodeId; node.meshId = meshId; node.scale = new Vector3(1, 1, 1); node.translation = new Vector3(); node.rotation = new Quaternion(); ModelNodePart pm = new ModelNodePart(); pm.meshPartId = partId; pm.materialId = group.materialName; node.parts = new ModelNodePart[] { pm }; ModelMeshPart part = new ModelMeshPart(); part.id = partId; part.indices = finalIndices; part.primitiveType = GL10.GL_TRIANGLES; ModelMesh mesh = new ModelMesh(); mesh.id = meshId; mesh.attributes = attributes.toArray(VertexAttribute.class); mesh.vertices = finalVerts; mesh.parts = new ModelMeshPart[] { part }; data.nodes.add(node); data.meshes.add(mesh); ModelMaterial mm = mtl.getMaterial(group.materialName); data.materials.add(mm); } //for (ModelMaterial m : mtl.materials) //data.materials.add(m); // An instance of ObjLoader can be used to load more than one OBJ. // Clearing the Array cache instead of instantiating new // Arrays should result in slightly faster load times for // subsequent calls to loadObj if (verts.size > 0) verts.clear(); if (norms.size > 0) norms.clear(); if (uvs.size > 0) uvs.clear(); if (groups.size > 0) groups.clear(); return data; }
From source file:com.kotcrab.vis.editor.module.editor.PluginLoaderModule.java
License:Apache License
private void loadPluginsJars() throws IOException { Array<URL> urls = new Array<>(); for (PluginDescriptor descriptor : pluginsToLoad) { for (FileHandle lib : descriptor.libs) urls.add(new URL("jar:file:" + lib.path() + "!/")); urls.add(new URL("jar:file:" + descriptor.file.path() + "!/")); }/*from w w w . j a v a2s. co m*/ URLClassLoader classLoader = ChildFirstURLClassLoader.newInstance(urls.toArray(URL.class), Thread.currentThread().getContextClassLoader()); Thread.currentThread().setContextClassLoader(classLoader); for (PluginDescriptor descriptor : pluginsToLoad) { try { if (descriptor.compatibility != App.PLUGIN_COMPATIBILITY_CODE) Log.warn(TAG, "Loading: " + descriptor.folderName + " (compatibility code mismatch! Will try to load anyway!)"); else Log.debug(TAG, "Loading: " + descriptor.folderName); currentlyLoadingPlugin = descriptor.folderName; JarFile jarFile = new JarFile(descriptor.file.path()); loadJarClasses(classLoader, descriptor, jarFile.entries()); IOUtils.closeQuietly(jarFile); } catch (IOException | ReflectiveOperationException | LinkageError e) { loadingCurrentPluginFailed(e); } } }
From source file:com.mbrlabs.mundus.commons.g3d.MG3dModelLoader.java
License:Apache License
private void parseMeshes(ModelData model, JsonValue json) { JsonValue meshes = json.get("meshes"); if (meshes != null) { model.meshes.ensureCapacity(meshes.size); for (JsonValue mesh = meshes.child; mesh != null; mesh = mesh.next) { ModelMesh jsonMesh = new ModelMesh(); String id = mesh.getString("id", ""); jsonMesh.id = id;// ww w .j a va2 s . com JsonValue attributes = mesh.require("attributes"); jsonMesh.attributes = parseAttributes(attributes); jsonMesh.vertices = mesh.require("vertices").asFloatArray(); JsonValue meshParts = mesh.require("parts"); Array<ModelMeshPart> parts = new Array<ModelMeshPart>(); for (JsonValue meshPart = meshParts.child; meshPart != null; meshPart = meshPart.next) { ModelMeshPart jsonPart = new ModelMeshPart(); String partId = meshPart.getString("id", null); if (partId == null) { throw new GdxRuntimeException("Not id given for mesh part"); } for (ModelMeshPart other : parts) { if (other.id.equals(partId)) { throw new GdxRuntimeException("Mesh part with id '" + partId + "' already in defined"); } } jsonPart.id = partId; String type = meshPart.getString("type", null); if (type == null) { throw new GdxRuntimeException("No primitive type given for mesh part '" + partId + "'"); } jsonPart.primitiveType = parseType(type); jsonPart.indices = meshPart.require("indices").asShortArray(); parts.add(jsonPart); } jsonMesh.parts = parts.toArray(ModelMeshPart.class); model.meshes.add(jsonMesh); } } }
From source file:com.mbrlabs.mundus.commons.g3d.MG3dModelLoader.java
License:Apache License
private VertexAttribute[] parseAttributes(JsonValue attributes) { Array<VertexAttribute> vertexAttributes = new Array<VertexAttribute>(); int unit = 0; int blendWeightCount = 0; for (JsonValue value = attributes.child; value != null; value = value.next) { String attribute = value.asString(); String attr = (String) attribute; if (attr.equals("POSITION")) { vertexAttributes.add(VertexAttribute.Position()); } else if (attr.equals("NORMAL")) { vertexAttributes.add(VertexAttribute.Normal()); } else if (attr.equals("COLOR")) { vertexAttributes.add(VertexAttribute.ColorUnpacked()); } else if (attr.equals("COLORPACKED")) { vertexAttributes.add(VertexAttribute.ColorPacked()); } else if (attr.equals("TANGENT")) { vertexAttributes.add(VertexAttribute.Tangent()); } else if (attr.equals("BINORMAL")) { vertexAttributes.add(VertexAttribute.Binormal()); } else if (attr.startsWith("TEXCOORD")) { vertexAttributes.add(VertexAttribute.TexCoords(unit++)); } else if (attr.startsWith("BLENDWEIGHT")) { vertexAttributes.add(VertexAttribute.BoneWeight(blendWeightCount++)); } else {//from w w w . ja va2 s .c om throw new GdxRuntimeException("Unknown vertex attribute '" + attr + "', should be one of position, normal, uv, tangent or binormal"); } } return vertexAttributes.toArray(VertexAttribute.class); }
From source file:com.stercore.code.net.dermetfan.utils.libgdx.box2d.Box2DUtils.java
License:Apache License
/** splits the given Shape using the segment described by the two given Vector2s * @param shape the Shape to split//from ww w. ja v a 2 s .c o m * @param a the first point of the segment * @param b the second point of the segment * @param store the {@link Pair} to store the split Shapes in * @return if the given shape was split */ @SuppressWarnings("unchecked") public static <T extends Shape> boolean split(T shape, Vector2 a, Vector2 b, Pair<T, T> store) { Type type = shape.getType(); if (type == Type.Circle) throw new IllegalArgumentException("shapes of the type " + Type.Circle + " cannot be split since Box2D does not support curved shapes other than circles: " + shape); if (type == Type.Edge) { Vector2 vertex1 = Pools.obtain(Vector2.class), vertex2 = Pools.obtain(Vector2.class), intersection = Pools.obtain(Vector2.class); EdgeShape es = (EdgeShape) shape; es.getVertex1(vertex1); es.getVertex2(vertex2); if (!Intersector.intersectSegments(a, b, vertex1, vertex2, intersection)) { Pools.free(vertex1); Pools.free(vertex2); Pools.free(intersection); return false; } EdgeShape sa = new EdgeShape(), sb = new EdgeShape(); sa.set(vertex1, intersection); sb.set(intersection, vertex2); store.set((T) sa, (T) sb); Pools.free(vertex1); Pools.free(vertex2); Pools.free(intersection); return true; } store.clear(); Vector2 vertices[] = vertices(shape), aa = Pools.obtain(Vector2.class).set(a), bb = Pools.obtain(Vector2.class).set(b); Array<Vector2> aVertices = Pools.obtain(Array.class), bVertices = Pools.obtain(Array.class); aVertices.clear(); bVertices.clear(); if (type == Type.Polygon) { aVertices.add(aa); aVertices.add(bb); GeometryUtils.arrangeClockwise(aVertices); if (GeometryUtils.intersectSegments(a, b, GeometryUtils.toFloatArray(vertices), aVertices.first(), aVertices.peek()) < 2) { Pools.free(aa); Pools.free(bb); Pools.free(aVertices); Pools.free(bVertices); return false; } bVertices.add(aa); bVertices.add(bb); for (Vector2 vertice : vertices) { float det = MathUtils.det(aa.x, aa.y, vertice.x, vertice.y, bb.x, bb.y); if (det < 0) aVertices.add(vertice); else if (det > 0) bVertices.add(vertice); else { aVertices.add(vertice); bVertices.add(vertice); } } GeometryUtils.arrangeClockwise(aVertices); GeometryUtils.arrangeClockwise(bVertices); Vector2[] aVerticesArray = aVertices.toArray(Vector2.class), bVerticesArray = bVertices.toArray(Vector2.class); if (checkPreconditions) { if (aVertices.size >= 3 && aVertices.size <= maxPolygonVertices && bVertices.size >= 3 && bVertices.size <= maxPolygonVertices) { float[] aVerticesFloatArray = GeometryUtils.toFloatArray(aVerticesArray), bVerticesFloatArray = GeometryUtils.toFloatArray(bVerticesArray); if (GeometryUtils.area(aVerticesFloatArray) > minExclusivePolygonArea && GeometryUtils.area(bVerticesFloatArray) > minExclusivePolygonArea) { PolygonShape sa = new PolygonShape(), sb = new PolygonShape(); sa.set(aVerticesFloatArray); sb.set(bVerticesFloatArray); store.set((T) sa, (T) sb); } } } else { PolygonShape sa = new PolygonShape(), sb = new PolygonShape(); sa.set(aVerticesArray); sb.set(bVerticesArray); store.set((T) sa, (T) sb); } } else if (type == Type.Chain) { Vector2 tmp = Pools.obtain(Vector2.class); boolean intersected = false; for (int i = 0; i < vertices.length; i++) { if (!intersected) aVertices.add(vertices[i]); else bVertices.add(vertices[i]); if (!intersected && i + 1 < vertices.length && Intersector.intersectSegments(vertices[i], vertices[i + 1], aa, bb, tmp)) { intersected = true; aVertices.add(tmp); bVertices.add(tmp); } } if (intersected) if (!checkPreconditions || aVertices.size >= 3 && bVertices.size >= 3) { ChainShape sa = new ChainShape(), sb = new ChainShape(); sa.createChain((Vector2[]) aVertices.toArray(Vector2.class)); sb.createChain((Vector2[]) bVertices.toArray(Vector2.class)); store.set((T) sa, (T) sb); } Pools.free(tmp); } Pools.free(aa); Pools.free(bb); Pools.free(aVertices); Pools.free(bVertices); return store.isFull(); }
From source file:es.eucm.ead.editor.control.pastelisteners.BehaviorCopyListener.java
License:Open Source License
@Override public void paste(Array<Behavior> object) { controller.action(AddBehavior.class, object.toArray(Behavior.class)); }
From source file:es.eucm.ead.editor.utils.GeometryUtils.java
License:Open Source License
/** * Collapses a collection of non-overlapping JTS geometries into a single * geometry./*from w w w. j av a2 s.co m*/ * * @param gs * input (non-overlapping polygons) * @return a JTS multipolygon, good for triangulation */ public static Geometry collapse(Array<Geometry> gs) { com.vividsolutions.jts.geom.Polygon ga[] = gs.toArray(com.vividsolutions.jts.geom.Polygon.class); Geometry g = gf.createMultiPolygon(ga); g.union(); return g; }