List of usage examples for com.badlogic.gdx.utils FloatArray add
public void add(float value)
From source file:CB_UI_Base.GL_UI.Controls.EditTextField.java
License:Open Source License
/** * Computes the glyph advances for the given character sequence and stores them in the provided {@link FloatArray}. The float * arrays are cleared. An additional element is added at the end. * * @param glyphAdvances the glyph advances output array. * @param glyphPositions the glyph positions output array. */// w w w. ja va 2s . com public void computeGlyphAdvancesAndPositions(CharSequence str, FloatArray glyphAdvances, FloatArray glyphPositions) { glyphAdvances.clear(); glyphPositions.clear(); int index = 0; int end = str.length(); float width = 0; Glyph lastGlyph = null; BitmapFontData data = style.font.getData(); if (data.scaleX == 1) { for (; index < end; index++) { char ch = str.charAt(index); Glyph g = data.getGlyph(ch); if (g != null) { if (lastGlyph != null) width += lastGlyph.getKerning(ch); lastGlyph = g; glyphAdvances.add(g.xadvance); glyphPositions.add(width); width += g.xadvance; } } glyphAdvances.add(0); glyphPositions.add(width); } else { float scaleX = style.font.getData().scaleX; for (; index < end; index++) { char ch = str.charAt(index); Glyph g = data.getGlyph(ch); if (g != null) { if (lastGlyph != null) width += lastGlyph.getKerning(ch) * scaleX; lastGlyph = g; float xadvance = g.xadvance * scaleX; glyphAdvances.add(xadvance); glyphPositions.add(width); width += xadvance; } } glyphAdvances.add(0); glyphPositions.add(width); } }
From source file:com.android.ringfly.common.Assets.java
License:Apache License
/** * /*from w w w .j ava2 s .c o m*/ * * @return */ private static float calculatePixelDensity() { FileHandle textureDir = Gdx.files.internal("data/textures"); FileHandle[] availableDensities = textureDir.list(); FloatArray densities = new FloatArray(); for (int i = 0; i < availableDensities.length; i++) { try { float density = Float.parseFloat(availableDensities[i].name()); densities.add(density); } catch (NumberFormatException ex) { // Ignore anything non-numeric, such as ".svn" folders. } } densities.shrink(); // Remove empty slots to get rid of zeroes. densities.sort(); // Now the lowest density comes first. return CameraHelper.bestDensity(VIRTUAL_WIDTH, VIRTUAL_HEIGHT, densities.items); }
From source file:com.badlydrawngames.veryangryrobots.Assets.java
License:Apache License
private static float calculatePixelDensity() { FileHandle textureDir = Gdx.files.internal("data/textures"); FileHandle[] availableDensities = textureDir.list(); FloatArray densities = new FloatArray(); for (int i = 0; i < availableDensities.length; i++) { try {/* w w w .ja va 2s. co m*/ float density = Float.parseFloat(availableDensities[i].name()); densities.add(density); } catch (NumberFormatException ex) { // Ignore anything non-numeric, such as ".svn" folders. } } densities.shrink(); // Remove empty slots to get rid of zeroes. densities.sort(); // Now the lowest density comes first. return CameraHelper.bestDensity(VIRTUAL_WIDTH, VIRTUAL_HEIGHT, densities.items); }
From source file:com.company.minery.utils.spine.SkeletonBinary.java
License:Open Source License
private Attachment readAttachment(DataInput input, Skin skin, String attachmentName, boolean nonessential) throws IOException { float scale = this.scale; String name = input.readString(); if (name == null) name = attachmentName;//w ww . j a va 2 s .c om switch (AttachmentType.values()[input.readByte()]) { case region: { String path = input.readString(); if (path == null) path = name; RegionAttachment region = attachmentLoader.newRegionAttachment(skin, name, path); if (region == null) return null; region.setPath(path); region.setX(input.readFloat() * scale); region.setY(input.readFloat() * scale); region.setScaleX(input.readFloat()); region.setScaleY(input.readFloat()); region.setRotation(input.readFloat()); region.setWidth(input.readFloat() * scale); region.setHeight(input.readFloat() * scale); Color.rgba8888ToColor(region.getColor(), input.readInt()); region.updateOffset(); return region; } case boundingbox: { BoundingBoxAttachment box = attachmentLoader.newBoundingBoxAttachment(skin, name); if (box == null) return null; box.setVertices(readFloatArray(input, scale)); return box; } case mesh: { String path = input.readString(); if (path == null) path = name; MeshAttachment mesh = attachmentLoader.newMeshAttachment(skin, name, path); if (mesh == null) return null; mesh.setPath(path); float[] uvs = readFloatArray(input, 1); short[] triangles = readShortArray(input); float[] vertices = readFloatArray(input, scale); mesh.setVertices(vertices); mesh.setTriangles(triangles); mesh.setRegionUVs(uvs); mesh.updateUVs(); Color.rgba8888ToColor(mesh.getColor(), input.readInt()); mesh.setHullLength(input.readInt(true) * 2); if (nonessential) { mesh.setEdges(readIntArray(input)); mesh.setWidth(input.readFloat() * scale); mesh.setHeight(input.readFloat() * scale); } return mesh; } case skinnedmesh: { String path = input.readString(); if (path == null) path = name; SkinnedMeshAttachment mesh = attachmentLoader.newSkinnedMeshAttachment(skin, name, path); if (mesh == null) return null; mesh.setPath(path); float[] uvs = readFloatArray(input, 1); short[] triangles = readShortArray(input); int vertexCount = input.readInt(true); FloatArray weights = new FloatArray(uvs.length * 3 * 3); IntArray bones = new IntArray(uvs.length * 3); for (int i = 0; i < vertexCount; i++) { int boneCount = (int) input.readFloat(); bones.add(boneCount); for (int nn = i + boneCount * 4; i < nn; i += 4) { bones.add((int) input.readFloat()); weights.add(input.readFloat() * scale); weights.add(input.readFloat() * scale); weights.add(input.readFloat()); } } mesh.setBones(bones.toArray()); mesh.setWeights(weights.toArray()); mesh.setTriangles(triangles); mesh.setRegionUVs(uvs); mesh.updateUVs(); Color.rgba8888ToColor(mesh.getColor(), input.readInt()); mesh.setHullLength(input.readInt(true) * 2); if (nonessential) { mesh.setEdges(readIntArray(input)); mesh.setWidth(input.readFloat() * scale); mesh.setHeight(input.readFloat() * scale); } return mesh; } } return null; }
From source file:com.company.minery.utils.spine.SkeletonJson.java
License:Open Source License
private Attachment readAttachment(Skin skin, String name, JsonValue map) { float scale = this.scale; name = map.getString("name", name); String path = map.getString("path", name); switch (AttachmentType.valueOf(map.getString("type", AttachmentType.region.name()))) { case region: { RegionAttachment region = attachmentLoader.newRegionAttachment(skin, name, path); if (region == null) return null; region.setPath(path);/*from www. j av a 2s.c o m*/ region.setX(map.getFloat("x", 0) * scale); region.setY(map.getFloat("y", 0) * scale); region.setScaleX(map.getFloat("scaleX", 1)); region.setScaleY(map.getFloat("scaleY", 1)); region.setRotation(map.getFloat("rotation", 0)); region.setWidth(map.getFloat("width") * scale); region.setHeight(map.getFloat("height") * scale); String color = map.getString("color", null); if (color != null) region.getColor().set(Color.valueOf(color)); region.updateOffset(); return region; } case boundingbox: { BoundingBoxAttachment box = attachmentLoader.newBoundingBoxAttachment(skin, name); if (box == null) return null; float[] vertices = map.require("vertices").asFloatArray(); if (scale != 1) { for (int i = 0, n = vertices.length; i < n; i++) vertices[i] *= scale; } box.setVertices(vertices); return box; } case mesh: { MeshAttachment mesh = attachmentLoader.newMeshAttachment(skin, name, path); if (mesh == null) return null; mesh.setPath(path); float[] vertices = map.require("vertices").asFloatArray(); if (scale != 1) { for (int i = 0, n = vertices.length; i < n; i++) vertices[i] *= scale; } mesh.setVertices(vertices); mesh.setTriangles(map.require("triangles").asShortArray()); mesh.setRegionUVs(map.require("uvs").asFloatArray()); mesh.updateUVs(); String color = map.getString("color", null); if (color != null) mesh.getColor().set(Color.valueOf(color)); if (map.has("hull")) mesh.setHullLength(map.require("hull").asInt() * 2); if (map.has("edges")) mesh.setEdges(map.require("edges").asIntArray()); mesh.setWidth(map.getFloat("width", 0) * scale); mesh.setHeight(map.getFloat("height", 0) * scale); return mesh; } case skinnedmesh: { SkinnedMeshAttachment mesh = attachmentLoader.newSkinnedMeshAttachment(skin, name, path); if (mesh == null) return null; mesh.setPath(path); float[] uvs = map.require("uvs").asFloatArray(); float[] vertices = map.require("vertices").asFloatArray(); FloatArray weights = new FloatArray(uvs.length * 3 * 3); IntArray bones = new IntArray(uvs.length * 3); for (int i = 0, n = vertices.length; i < n;) { int boneCount = (int) vertices[i++]; bones.add(boneCount); for (int nn = i + boneCount * 4; i < nn;) { bones.add((int) vertices[i]); weights.add(vertices[i + 1] * scale); weights.add(vertices[i + 2] * scale); weights.add(vertices[i + 3]); i += 4; } } mesh.setBones(bones.toArray()); mesh.setWeights(weights.toArray()); mesh.setTriangles(map.require("triangles").asShortArray()); mesh.setRegionUVs(uvs); mesh.updateUVs(); String color = map.getString("color", null); if (color != null) mesh.getColor().set(Color.valueOf(color)); if (map.has("hull")) mesh.setHullLength(map.require("hull").asInt() * 2); if (map.has("edges")) mesh.setEdges(map.require("edges").asIntArray()); mesh.setWidth(map.getFloat("width", 0) * scale); mesh.setHeight(map.getFloat("height", 0) * scale); return mesh; } } // RegionSequenceAttachment regionSequenceAttachment = (RegionSequenceAttachment)attachment; // // float fps = map.getFloat("fps"); // regionSequenceAttachment.setFrameTime(fps); // // String modeString = map.getString("mode"); // regionSequenceAttachment.setMode(modeString == null ? Mode.forward : Mode.valueOf(modeString)); return null; }
From source file:com.esotericsoftware.spine.utils.SkeletonClipping.java
License:Open Source License
public int clipStart(Slot slot, ClippingAttachment clip) { if (clipAttachment != null) return 0; int n = clip.getWorldVerticesLength(); if (n < 6) return 0; clipAttachment = clip;/* www . j a va 2s. c o m*/ float[] vertices = clippingPolygon.setSize(n); clip.computeWorldVertices(slot, 0, n, vertices, 0, 2); makeClockwise(clippingPolygon); ShortArray triangles = triangulator.triangulate(clippingPolygon); clippingPolygons = triangulator.decompose(clippingPolygon, triangles); for (FloatArray polygon : clippingPolygons) { makeClockwise(polygon); polygon.add(polygon.items[0]); polygon.add(polygon.items[1]); } return clippingPolygons.size; }
From source file:com.esotericsoftware.spine.utils.SkeletonClipping.java
License:Open Source License
/** Clips the input triangle against the convex, clockwise clipping area. If the triangle lies entirely within the clipping * area, false is returned. The clipping area must duplicate the first vertex at the end of the vertices list. */ boolean clip(float x1, float y1, float x2, float y2, float x3, float y3, FloatArray clippingArea, FloatArray output) {//from www. j a va 2s . co m FloatArray originalOutput = output; boolean clipped = false; // Avoid copy at the end. FloatArray input = null; if (clippingArea.size % 4 >= 2) { input = output; output = scratch; } else input = scratch; input.clear(); input.add(x1); input.add(y1); input.add(x2); input.add(y2); input.add(x3); input.add(y3); input.add(x1); input.add(y1); output.clear(); float[] clippingVertices = clippingArea.items; int clippingVerticesLast = clippingArea.size - 4; for (int i = 0;; i += 2) { float edgeX = clippingVertices[i], edgeY = clippingVertices[i + 1]; float edgeX2 = clippingVertices[i + 2], edgeY2 = clippingVertices[i + 3]; float deltaX = edgeX - edgeX2, deltaY = edgeY - edgeY2; float[] inputVertices = input.items; int inputVerticesLength = input.size - 2, outputStart = output.size; for (int ii = 0; ii < inputVerticesLength; ii += 2) { float inputX = inputVertices[ii], inputY = inputVertices[ii + 1]; float inputX2 = inputVertices[ii + 2], inputY2 = inputVertices[ii + 3]; boolean side2 = deltaX * (inputY2 - edgeY2) - deltaY * (inputX2 - edgeX2) > 0; if (deltaX * (inputY - edgeY2) - deltaY * (inputX - edgeX2) > 0) { if (side2) { // v1 inside, v2 inside output.add(inputX2); output.add(inputY2); continue; } // v1 inside, v2 outside float c0 = inputY2 - inputY, c2 = inputX2 - inputX; float s = c0 * (edgeX2 - edgeX) - c2 * (edgeY2 - edgeY); if (Math.abs(s) > 0.000001f) { float ua = (c2 * (edgeY - inputY) - c0 * (edgeX - inputX)) / s; output.add(edgeX + (edgeX2 - edgeX) * ua); output.add(edgeY + (edgeY2 - edgeY) * ua); } else { output.add(edgeX); output.add(edgeY); } } else if (side2) { // v1 outside, v2 inside float c0 = inputY2 - inputY, c2 = inputX2 - inputX; float s = c0 * (edgeX2 - edgeX) - c2 * (edgeY2 - edgeY); if (Math.abs(s) > 0.000001f) { float ua = (c2 * (edgeY - inputY) - c0 * (edgeX - inputX)) / s; output.add(edgeX + (edgeX2 - edgeX) * ua); output.add(edgeY + (edgeY2 - edgeY) * ua); } else { output.add(edgeX); output.add(edgeY); } output.add(inputX2); output.add(inputY2); } clipped = true; } if (outputStart == output.size) { // All edges outside. originalOutput.clear(); return true; } output.add(output.items[0]); output.add(output.items[1]); if (i == clippingVerticesLast) break; FloatArray temp = output; output = input; output.clear(); input = temp; } if (originalOutput != output) { originalOutput.clear(); originalOutput.addAll(output.items, 0, output.size - 2); } else originalOutput.setSize(originalOutput.size - 2); return clipped; }
From source file:com.esotericsoftware.spine.utils.Triangulator.java
License:Open Source License
public Array<FloatArray> decompose(FloatArray verticesArray, ShortArray triangles) { float[] vertices = verticesArray.items; Array<FloatArray> convexPolygons = this.convexPolygons; polygonPool.freeAll(convexPolygons); convexPolygons.clear();/*from w ww. j av a2s. co m*/ Array<ShortArray> convexPolygonsIndices = this.convexPolygonsIndices; polygonIndicesPool.freeAll(convexPolygonsIndices); convexPolygonsIndices.clear(); ShortArray polygonIndices = polygonIndicesPool.obtain(); polygonIndices.clear(); FloatArray polygon = polygonPool.obtain(); polygon.clear(); // Merge subsequent triangles if they form a triangle fan. int fanBaseIndex = -1, lastWinding = 0; short[] trianglesItems = triangles.items; for (int i = 0, n = triangles.size; i < n; i += 3) { int t1 = trianglesItems[i] << 1, t2 = trianglesItems[i + 1] << 1, t3 = trianglesItems[i + 2] << 1; float x1 = vertices[t1], y1 = vertices[t1 + 1]; float x2 = vertices[t2], y2 = vertices[t2 + 1]; float x3 = vertices[t3], y3 = vertices[t3 + 1]; // If the base of the last triangle is the same as this triangle, check if they form a convex polygon (triangle fan). boolean merged = false; if (fanBaseIndex == t1) { int o = polygon.size - 4; float[] p = polygon.items; int winding1 = winding(p[o], p[o + 1], p[o + 2], p[o + 3], x3, y3); int winding2 = winding(x3, y3, p[0], p[1], p[2], p[3]); if (winding1 == lastWinding && winding2 == lastWinding) { polygon.add(x3); polygon.add(y3); polygonIndices.add(t3); merged = true; } } // Otherwise make this triangle the new base. if (!merged) { if (polygon.size > 0) { convexPolygons.add(polygon); convexPolygonsIndices.add(polygonIndices); } else { polygonPool.free(polygon); polygonIndicesPool.free(polygonIndices); } polygon = polygonPool.obtain(); polygon.clear(); polygon.add(x1); polygon.add(y1); polygon.add(x2); polygon.add(y2); polygon.add(x3); polygon.add(y3); polygonIndices = polygonIndicesPool.obtain(); polygonIndices.clear(); polygonIndices.add(t1); polygonIndices.add(t2); polygonIndices.add(t3); lastWinding = winding(x1, y1, x2, y2, x3, y3); fanBaseIndex = t1; } } if (polygon.size > 0) { convexPolygons.add(polygon); convexPolygonsIndices.add(polygonIndices); } // Go through the list of polygons and try to merge the remaining triangles with the found triangle fans. for (int i = 0, n = convexPolygons.size; i < n; i++) { polygonIndices = convexPolygonsIndices.get(i); if (polygonIndices.size == 0) continue; int firstIndex = polygonIndices.get(0); int lastIndex = polygonIndices.get(polygonIndices.size - 1); polygon = convexPolygons.get(i); int o = polygon.size - 4; float[] p = polygon.items; float prevPrevX = p[o], prevPrevY = p[o + 1]; float prevX = p[o + 2], prevY = p[o + 3]; float firstX = p[0], firstY = p[1]; float secondX = p[2], secondY = p[3]; int winding = winding(prevPrevX, prevPrevY, prevX, prevY, firstX, firstY); for (int ii = 0; ii < n; ii++) { if (ii == i) continue; ShortArray otherIndices = convexPolygonsIndices.get(ii); if (otherIndices.size != 3) continue; int otherFirstIndex = otherIndices.get(0); int otherSecondIndex = otherIndices.get(1); int otherLastIndex = otherIndices.get(2); FloatArray otherPoly = convexPolygons.get(ii); float x3 = otherPoly.get(otherPoly.size - 2), y3 = otherPoly.get(otherPoly.size - 1); if (otherFirstIndex != firstIndex || otherSecondIndex != lastIndex) continue; int winding1 = winding(prevPrevX, prevPrevY, prevX, prevY, x3, y3); int winding2 = winding(x3, y3, firstX, firstY, secondX, secondY); if (winding1 == winding && winding2 == winding) { otherPoly.clear(); otherIndices.clear(); polygon.add(x3); polygon.add(y3); polygonIndices.add(otherLastIndex); prevPrevX = prevX; prevPrevY = prevY; prevX = x3; prevY = y3; ii = 0; } } } // Remove empty polygons that resulted from the merge step above. for (int i = convexPolygons.size - 1; i >= 0; i--) { polygon = convexPolygons.get(i); if (polygon.size == 0) { convexPolygons.removeIndex(i); polygonPool.free(polygon); polygonIndices = convexPolygonsIndices.removeIndex(i); polygonIndicesPool.free(polygonIndices); } } return convexPolygons; }
From source file:com.infunity.isometricgame.shared.utils.dermetfan.GeometryUtils.java
License:Apache License
/** @param segments the segments * @param polygon if the segments represent a closed polygon * @param intersections the array to store the intersections in */ public static void intersectSegments(float x1, float y1, float x2, float y2, float[] segments, boolean polygon, FloatArray intersections) { if (polygon && segments.length < 6) throw new IllegalArgumentException("a polygon consists of at least 3 points: " + segments.length); else if (segments.length < 4) throw new IllegalArgumentException( "segments does not contain enough vertices to represent at least one segment: " + segments.length); if (segments.length % 2 != 0) throw new IllegalArgumentException( "malformed segments; the number of vertices is not dividable by 2: " + segments.length); intersections.clear();/* w w w . ja v a 2 s . c om*/ Vector2 tmp = Pools.obtain(Vector2.class); for (int i = 0, n = segments.length - (polygon ? 0 : 2); i < n; i += 2) { float x3 = segments[i], y3 = segments[i + 1], x4 = wrapIndex(i + 2, segments), y4 = wrapIndex(i + 3, segments); if (Intersector.intersectSegments(x1, y1, x2, y2, x3, y3, x4, y4, tmp)) { intersections.add(tmp.x); intersections.add(tmp.y); } } Pools.free(tmp); }
From source file:com.mbrlabs.mundus.commons.assets.TerrainAsset.java
License:Apache License
@Override public void load() { // load height data from terra file final FloatArray floatArray = new FloatArray(); DataInputStream is;//from www.j a va 2 s . c o m try { is = new DataInputStream(file.read()); while (is.available() > 0) { floatArray.add(is.readFloat()); } is.close(); } catch (EOFException e) { // e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); return; } data = floatArray.toArray(); terrain = new Terrain(meta.getTerrain().getSize(), data); terrain.init(); terrain.update(); }