List of usage examples for com.badlogic.gdx.utils FloatArray get
public float get(int index)
From source file:com.calanti.androidnativekeyboardinputtest.libgdxModified_1_9_3.CalTextField.java
License:Apache License
void updateDisplayText() { BitmapFont font = style.font;/*from www.ja v a 2s . c o m*/ BitmapFont.BitmapFontData data = font.getData(); String text = this.text; int textLength = text.length(); StringBuilder buffer = new StringBuilder(); for (int i = 0; i < textLength; i++) { char c = text.charAt(i); /** calanti addition - some fonts apparently have these glyphs, * causes building the glyphPositions to break for TextArea */ if (c == ENTER_ANDROID || c == ENTER_DESKTOP) { buffer.append(' '); continue; } buffer.append(data.hasGlyph(c) ? c : ' '); } String newDisplayText = buffer.toString(); if (passwordMode && data.hasGlyph(passwordCharacter)) { if (passwordBuffer == null) passwordBuffer = new StringBuilder(newDisplayText.length()); if (passwordBuffer.length() > textLength) passwordBuffer.setLength(textLength); else { for (int i = passwordBuffer.length(); i < textLength; i++) passwordBuffer.append(passwordCharacter); } displayText = passwordBuffer; } else displayText = newDisplayText; layout.setText(font, displayText); glyphPositions.clear(); float x = 0; if (layout.runs.size > 0) { GlyphLayout.GlyphRun run = layout.runs.first(); FloatArray xAdvances = run.xAdvances; fontOffset = xAdvances.first(); for (int i = 1, n = xAdvances.size; i < n; i++) { glyphPositions.add(x); x += xAdvances.get(i); } } else fontOffset = 0; glyphPositions.add(x); if (selectionStart > newDisplayText.length()) selectionStart = textLength; }
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 w w .j a v a2 s . c o 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 a the first point of the segment * @param b the second point of the segment * @param polygon the polygon, assumed to be convex * @param intersection1 the first intersection point * @param intersection2 the second intersection point * @return the number of intersection points * @see #intersectSegments(com.badlogic.gdx.math.Vector2, com.badlogic.gdx.math.Vector2, float[], boolean, com.badlogic.gdx.utils.Array)*/ public static int intersectSegments(Vector2 a, Vector2 b, float[] polygon, Vector2 intersection1, Vector2 intersection2) {// ww w .j a va 2 s . co m FloatArray intersections = Pools.obtain(FloatArray.class); intersectSegments(a.x, a.y, b.x, b.y, polygon, true, intersections); int size = intersections.size; if (size >= 2) { intersection1.set(intersections.get(0), intersections.get(1)); if (size == 4) intersection2.set(intersections.get(2), intersections.get(3)); else if (size > 4) assert false : "more intersection points with a convex polygon found than possible: " + size; } Pools.free(intersections); return size / 2; }
From source file:com.infunity.isometricgame.shared.utils.dermetfan.GeometryUtils.java
License:Apache License
/** @see #intersectSegments(float, float, float, float, float[], boolean, com.badlogic.gdx.utils.FloatArray) */ public static void intersectSegments(Vector2 a, Vector2 b, float[] segments, boolean polygon, Array<Vector2> intersections) { FloatArray fa = Pools.obtain(FloatArray.class); intersectSegments(a.x, a.y, b.x, b.y, segments, polygon, fa); if (fa.size < 1) { intersections.clear();/*from w w w. j a v a2 s .co m*/ Pools.free(fa); return; } intersections.ensureCapacity(fa.size / 2 - intersections.size); for (int i = 1; i < fa.size; i += 2) if (intersections.size > i / 2) intersections.get(i / 2).set(fa.get(i - 1), fa.get(i)); else intersections.add(new Vector2(fa.get(i - 1), fa.get(i))); Pools.free(fa); }
From source file:com.kotcrab.vis.ui.widget.PatchedVisTextField.java
License:Apache License
@Override void updateDisplayText() { BitmapFont font = style.font;//from w ww . j a v a 2s . com BitmapFontData data = font.getData(); String text = this.text; int textLength = text.length(); StringBuilder buffer = new StringBuilder(); for (int i = 0; i < textLength; i++) { char c = text.charAt(i); buffer.append(data.hasGlyph(c) ? c : ' '); } String newDisplayText = buffer.toString(); if (passwordMode && data.hasGlyph(passwordCharacter)) { if (passwordBuffer == null) passwordBuffer = new StringBuilder(newDisplayText.length()); if (passwordBuffer.length() > textLength) passwordBuffer.setLength(textLength); else { for (int i = passwordBuffer.length(); i < textLength; i++) passwordBuffer.append(passwordCharacter); } displayText = passwordBuffer; } else displayText = newDisplayText; //PATCH: Disabled color markup for BitmapFont when updating GlyphLayout https://github.com/libgdx/libgdx/issues/4576 BitmapFontData fontData = font.getData(); boolean markupEnabled = fontData.markupEnabled; fontData.markupEnabled = false; layout.setText(font, displayText); fontData.markupEnabled = markupEnabled; glyphPositions.clear(); float x = 0; if (layout.runs.size > 0) { GlyphRun run = layout.runs.first(); fontOffset = run.xAdvances.first(); for (GlyphRun glyphRun : layout.runs) { FloatArray xAdvances = glyphRun.xAdvances; for (int i = 1, n = xAdvances.size; i < n; i++) { glyphPositions.add(x); x += xAdvances.get(i); } glyphPositions.add(x); } } else { fontOffset = 0; } glyphPositions.add(x); if (selectionStart > newDisplayText.length()) selectionStart = textLength; }
From source file:com.minikara.ttfinput.TextField.java
License:Apache License
void updateDisplayText() { BitmapFont font = style.font;/* www .j a va2s. c o m*/ BitmapFontData data = font.getData(); String text = this.text; int textLength = text.length(); StringBuilder buffer = new StringBuilder(); for (int i = 0; i < textLength; i++) { char c = text.charAt(i); buffer.append(data.hasGlyph(c) ? c : ' '); } String newDisplayText = buffer.toString(); if (passwordMode && data.hasGlyph(passwordCharacter)) { if (passwordBuffer == null) passwordBuffer = new StringBuilder(newDisplayText.length()); if (passwordBuffer.length() > textLength) passwordBuffer.setLength(textLength); else { for (int i = passwordBuffer.length(); i < textLength; i++) passwordBuffer.append(passwordCharacter); } displayText = passwordBuffer; } else displayText = newDisplayText; layout.setText(font, displayText); glyphPositions.clear(); float x = 0; if (layout.runs.size > 0) { GlyphRun run = layout.runs.first(); FloatArray xAdvances = run.xAdvances; fontOffset = xAdvances.first(); for (int i = 1, n = xAdvances.size; i < n; i++) { glyphPositions.add(x); x += xAdvances.get(i); } } else fontOffset = 0; glyphPositions.add(x); if (selectionStart > newDisplayText.length()) selectionStart = textLength; }
From source file:com.stercore.code.net.dermetfan.utils.libgdx.box2d.Box2DMapObjectParser.java
License:Apache License
/** creates a {@link Fixture} from a {@link MapObject} * @param mapObject the {@link MapObject} to parse * @param body the {@link Body} to create the {@link Fixture Fixtures} on * @return the parsed {@link Fixture} */ public Fixture createFixture(MapObject mapObject, Body body) { if ((mapObject = listener.createFixture(mapObject)) == null) return null; String orientation = findProperty(aliases.orientation, aliases.orthogonal, heritage, mapProperties, layerProperties, mapObject.getProperties()); transform(mat4, orientation);/*from w w w . j a va 2s . co m*/ Shape shape = null; if (mapObject instanceof RectangleMapObject) { Rectangle rectangle = ((RectangleMapObject) mapObject).getRectangle(); vec3.set(rectangle.x, rectangle.y, 0); vec3.mul(mat4); float x = vec3.x, y = vec3.y, width, height; if (!orientation.equals(aliases.staggered)) { vec3.set(rectangle.width, rectangle.height, 0).mul(mat4); width = vec3.x; height = vec3.y; } else { width = rectangle.width * unitScale; height = rectangle.height * unitScale; } ((PolygonShape) (shape = new PolygonShape())).setAsBox(width / 2, height / 2, vec2.set(x - body.getPosition().x + width / 2, y - body.getPosition().y + height / 2), body.getAngle()); } else if (mapObject instanceof PolygonMapObject || mapObject instanceof PolylineMapObject) { FloatArray vertices = Pools.obtain(FloatArray.class); vertices.clear(); vertices.addAll(mapObject instanceof PolygonMapObject ? ((PolygonMapObject) mapObject).getPolygon().getTransformedVertices() : ((PolylineMapObject) mapObject).getPolyline().getTransformedVertices()); for (int ix = 0, iy = 1; iy < vertices.size; ix += 2, iy += 2) { vec3.set(vertices.get(ix), vertices.get(iy), 0); vec3.mul(mat4); vertices.set(ix, vec3.x - body.getPosition().x); vertices.set(iy, vec3.y - body.getPosition().y); } if (mapObject instanceof PolygonMapObject) ((PolygonShape) (shape = new PolygonShape())).set(vertices.items, 0, vertices.size); else if (vertices.size == 4) ((EdgeShape) (shape = new EdgeShape())).set(vertices.get(0), vertices.get(1), vertices.get(2), vertices.get(3)); else { vertices.shrink(); ((ChainShape) (shape = new ChainShape())).createChain(vertices.items); } Pools.free(vertices); } else if (mapObject instanceof CircleMapObject || mapObject instanceof EllipseMapObject) { if (mapObject instanceof CircleMapObject) { Circle circle = ((CircleMapObject) mapObject).getCircle(); vec3.set(circle.x, circle.y, circle.radius); } else { Ellipse ellipse = ((EllipseMapObject) mapObject).getEllipse(); if (ellipse.width != ellipse.height) throw new IllegalArgumentException("Cannot parse " + mapObject.getName() + " because " + ClassReflection.getSimpleName(mapObject.getClass()) + "s that are not circles are not supported"); vec3.set(ellipse.x + ellipse.width / 2, ellipse.y + ellipse.height / 2, ellipse.width / 2); } vec3.mul(mat4); vec3.sub(body.getPosition().x, body.getPosition().y, 0); CircleShape circleShape = (CircleShape) (shape = new CircleShape()); circleShape.setPosition(vec2.set(vec3.x, vec3.y)); circleShape.setRadius(vec3.z); } else if (mapObject instanceof TextureMapObject) throw new IllegalArgumentException("Cannot parse " + mapObject.getName() + " because " + ClassReflection.getSimpleName(mapObject.getClass()) + "s are not supported"); else assert false : mapObject + " is a not known subclass of " + MapObject.class.getName(); MapProperties properties = mapObject.getProperties(); FixtureDef fixtureDef = new FixtureDef(); fixtureDef.shape = shape; assignProperties(fixtureDef, heritage); assignProperties(fixtureDef, mapProperties); assignProperties(fixtureDef, layerProperties); assignProperties(fixtureDef, properties); Fixture fixture = body.createFixture(fixtureDef); fixture.setUserData(findProperty(aliases.userData, fixture.getUserData(), heritage, mapProperties, layerProperties, properties)); shape.dispose(); fixtures.put(findAvailableName(mapObject.getName(), fixtures), fixture); listener.created(fixture, mapObject); return fixture; }
From source file:de.fhkoeln.game.utils.Box2DMapObjectParser.java
License:Apache License
/** creates a {@link com.badlogic.gdx.physics.box2d.Fixture} from a {@link com.badlogic.gdx.maps.MapObject} * @param mapObject the {@link com.badlogic.gdx.maps.MapObject} to parse * @param body the {@link com.badlogic.gdx.physics.box2d.Body} to create the {@link com.badlogic.gdx.physics.box2d.Fixture Fixtures} on * @return the parsed {@link com.badlogic.gdx.physics.box2d.Fixture} */ public Fixture createFixture(MapObject mapObject, Body body) { if ((mapObject = listener.createFixture(mapObject)) == null) return null; String orientation = findProperty(aliases.orientation, aliases.orthogonal, heritage, mapProperties, layerProperties, mapObject.getProperties()); transform(mat4, orientation);/*w w w. j av a 2 s .c o m*/ Shape shape = null; if (mapObject instanceof RectangleMapObject) { Rectangle rectangle = ((RectangleMapObject) mapObject).getRectangle(); vec3.set(rectangle.x, rectangle.y, 0); vec3.mul(mat4); float x = vec3.x, y = vec3.y, width, height; if (!orientation.equals(aliases.staggered)) { vec3.set(rectangle.width, rectangle.height, 0).mul(mat4); width = vec3.x; height = vec3.y; } else { width = rectangle.width * unitScale; height = rectangle.height * unitScale; } ((PolygonShape) (shape = new PolygonShape())).setAsBox(width / 2, height / 2, vec2.set(x - body.getPosition().x + width / 2, y - body.getPosition().y + height / 2), body.getAngle()); } else if (mapObject instanceof PolygonMapObject || mapObject instanceof PolylineMapObject) { FloatArray vertices = Pools.obtain(FloatArray.class); vertices.clear(); vertices.addAll(mapObject instanceof PolygonMapObject ? ((PolygonMapObject) mapObject).getPolygon().getTransformedVertices() : ((PolylineMapObject) mapObject).getPolyline().getTransformedVertices()); for (int ix = 0, iy = 1; iy < vertices.size; ix += 2, iy += 2) { vec3.set(vertices.get(ix), vertices.get(iy), 0); vec3.mul(mat4); vertices.set(ix, vec3.x - body.getPosition().x); vertices.set(iy, vec3.y - body.getPosition().y); } if (mapObject instanceof PolygonMapObject) ((PolygonShape) (shape = new PolygonShape())).set(vertices.items, 0, vertices.size); else if (vertices.size == 4) ((EdgeShape) (shape = new EdgeShape())).set(vertices.get(0), vertices.get(1), vertices.get(2), vertices.get(3)); else { vertices.shrink(); ((ChainShape) (shape = new ChainShape())).createChain(vertices.items); } Pools.free(vertices); } else if (mapObject instanceof CircleMapObject || mapObject instanceof EllipseMapObject) { if (mapObject instanceof CircleMapObject) { Circle circle = ((CircleMapObject) mapObject).getCircle(); vec3.set(circle.x, circle.y, circle.radius); } else { Ellipse ellipse = ((EllipseMapObject) mapObject).getEllipse(); if (ellipse.width != ellipse.height) throw new IllegalArgumentException("Cannot parse " + mapObject.getName() + " because " + mapObject.getClass().getSimpleName() + "s that are not circles are not supported"); vec3.set(ellipse.x + ellipse.width / 2, ellipse.y + ellipse.height / 2, ellipse.width / 2); } vec3.mul(mat4); vec3.sub(body.getPosition().x, body.getPosition().y, 0); CircleShape circleShape = (CircleShape) (shape = new CircleShape()); circleShape.setPosition(vec2.set(vec3.x, vec3.y)); circleShape.setRadius(vec3.z); } else if (mapObject instanceof TextureMapObject) throw new IllegalArgumentException("Cannot parse " + mapObject.getName() + " because " + mapObject.getClass().getSimpleName() + "s are not supported"); else assert false : mapObject + " is a not known subclass of " + MapObject.class.getName(); MapProperties properties = mapObject.getProperties(); FixtureDef fixtureDef = new FixtureDef(); fixtureDef.shape = shape; assignProperties(fixtureDef, heritage); assignProperties(fixtureDef, mapProperties); assignProperties(fixtureDef, layerProperties); assignProperties(fixtureDef, properties); Fixture fixture = body.createFixture(fixtureDef); fixture.setUserData(findProperty(aliases.userData, fixture.getUserData(), heritage, mapProperties, layerProperties, properties)); shape.dispose(); fixtures.put(findAvailableName(mapObject.getName(), fixtures), fixture); listener.created(fixture, mapObject); return fixture; }