Search in sources :

Example 6 with FloatArray

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

the class ConvexHull method computePolygon.

/** Returns a list of points on the convex hull in counter-clockwise order. Note: the last point in the returned list is the
	 * same as the first one. */
/** Returns the convex hull polygon for the given point cloud.
	 * @param points x,y pairs describing points. Duplicate points will result in undefined behavior.
	 * @param sorted If false, the points will be sorted by the x coordinate then the y coordinate, which is required by the convex
	 *           hull algorithm. If sorting is done the input array is not modified and count additional working memory is needed.
	 * @return pairs of coordinates that describe the convex hull polygon in counterclockwise order. Note the returned array is
	 *         reused for later calls to the same method. */
public FloatArray computePolygon(float[] points, int offset, int count, boolean sorted) {
    int end = offset + count;
    if (!sorted) {
        if (sortedPoints == null || sortedPoints.length < count)
            sortedPoints = new float[count];
        System.arraycopy(points, offset, sortedPoints, 0, count);
        points = sortedPoints;
        offset = 0;
        sort(points, count);
    }
    FloatArray hull = this.hull;
    hull.clear();
    // Lower hull.
    for (int i = offset; i < end; i += 2) {
        float x = points[i];
        float y = points[i + 1];
        while (hull.size >= 4 && ccw(x, y) <= 0) hull.size -= 2;
        hull.add(x);
        hull.add(y);
    }
    // Upper hull.
    for (int i = end - 4, t = hull.size + 2; i >= offset; i -= 2) {
        float x = points[i];
        float y = points[i + 1];
        while (hull.size >= t && ccw(x, y) <= 0) hull.size -= 2;
        hull.add(x);
        hull.add(y);
    }
    return hull;
}
Also used : FloatArray(com.badlogic.gdx.utils.FloatArray)

Example 7 with FloatArray

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

the class TextAreaTest2 method create.

@Override
public void create() {
    stage = new Stage();
    Gdx.input.setInputProcessor(stage);
    skin = new Skin(Gdx.files.internal("data/uiskin.json"));
    //Create a string that perfectly fills the float array used in the textarea float array
    FloatArray dummyArray = new FloatArray();
    String limit = "";
    // Minus one, because TextField adds a magic char
    for (int i = 0; i < dummyArray.items.length - 1; i++) {
        limit += "a";
    }
    TextArea textArea = new TextArea(limit, skin);
    textArea.setX(10);
    textArea.setY(10);
    textArea.setWidth(200);
    textArea.setHeight(200);
    stage.addActor(textArea);
}
Also used : FloatArray(com.badlogic.gdx.utils.FloatArray) TextArea(com.badlogic.gdx.scenes.scene2d.ui.TextArea) Stage(com.badlogic.gdx.scenes.scene2d.Stage) Skin(com.badlogic.gdx.scenes.scene2d.ui.Skin)

Example 8 with FloatArray

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

the class GlyphLayout method wrap.

private GlyphRun wrap(BitmapFontData fontData, GlyphRun first, Pool<GlyphRun> glyphRunPool, int wrapIndex, int widthIndex) {
    GlyphRun second = glyphRunPool.obtain();
    second.color.set(first.color);
    int glyphCount = first.glyphs.size;
    // Increase first run width up to the end index.
    while (widthIndex < wrapIndex) first.width += first.xAdvances.get(widthIndex++);
    // Reduce first run width by the wrapped glyphs that have contributed to the width.
    while (widthIndex > wrapIndex + 1) first.width -= first.xAdvances.get(--widthIndex);
    // The second run will contain the remaining glyph data, so swap instances rather than copying to reduce large allocations.
    if (wrapIndex < glyphCount) {
        // Starts empty.
        Array<Glyph> glyphs1 = second.glyphs;
        // Starts with all the glyphs.
        Array<Glyph> glyphs2 = first.glyphs;
        glyphs1.addAll(glyphs2, 0, wrapIndex);
        glyphs2.removeRange(0, wrapIndex - 1);
        first.glyphs = glyphs1;
        second.glyphs = glyphs2;
        // Equivalent to:
        // second.glyphs.addAll(first.glyphs, wrapIndex, glyphCount - wrapIndex);
        // first.glyphs.truncate(wrapIndex);
        // Starts empty.
        FloatArray xAdvances1 = second.xAdvances;
        // Starts with all the xAdvances.
        FloatArray xAdvances2 = first.xAdvances;
        xAdvances1.addAll(xAdvances2, 0, wrapIndex + 1);
        // Leave first entry to be overwritten by next line.
        xAdvances2.removeRange(1, wrapIndex);
        xAdvances2.set(0, -glyphs2.first().xoffset * fontData.scaleX - fontData.padLeft);
        first.xAdvances = xAdvances1;
        second.xAdvances = xAdvances2;
    // Equivalent to:
    // second.xAdvances.add(-second.glyphs.first().xoffset * fontData.scaleX - fontData.padLeft);
    // second.xAdvances.addAll(first.xAdvances, wrapIndex + 1, first.xAdvances.size - (wrapIndex + 1));
    // first.xAdvances.truncate(wrapIndex + 1);
    }
    if (wrapIndex == 0) {
        // If the first run is now empty, remove it.
        glyphRunPool.free(first);
        runs.pop();
    } else
        adjustLastGlyph(fontData, first);
    return second;
}
Also used : FloatArray(com.badlogic.gdx.utils.FloatArray) Glyph(com.badlogic.gdx.graphics.g2d.BitmapFont.Glyph)

Example 9 with FloatArray

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

the class TextField method updateDisplayText.

void updateDisplayText() {
    BitmapFont font = style.font;
    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;
}
Also used : FloatArray(com.badlogic.gdx.utils.FloatArray) BitmapFontData(com.badlogic.gdx.graphics.g2d.BitmapFont.BitmapFontData) GlyphRun(com.badlogic.gdx.graphics.g2d.GlyphLayout.GlyphRun) BitmapFont(com.badlogic.gdx.graphics.g2d.BitmapFont)

Example 10 with FloatArray

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

the class VerticalGroup method computeSize.

private void computeSize() {
    sizeInvalid = false;
    SnapshotArray<Actor> children = getChildren();
    int n = children.size;
    prefWidth = 0;
    if (wrap) {
        prefHeight = 0;
        if (columnSizes == null)
            columnSizes = new FloatArray();
        else
            columnSizes.clear();
        FloatArray columnSizes = this.columnSizes;
        float space = this.space, wrapSpace = this.wrapSpace;
        float pad = padTop + padBottom, groupHeight = getHeight() - pad, x = 0, y = 0, columnWidth = 0;
        int i = 0, incr = 1;
        if (reverse) {
            i = n - 1;
            n = -1;
            incr = -1;
        }
        for (; i != n; i += incr) {
            Actor child = children.get(i);
            float width, height;
            if (child instanceof Layout) {
                Layout layout = (Layout) child;
                width = layout.getPrefWidth();
                height = layout.getPrefHeight();
            } else {
                width = child.getWidth();
                height = child.getHeight();
            }
            float incrY = height + (y > 0 ? space : 0);
            if (y + incrY > groupHeight && y > 0) {
                columnSizes.add(y);
                columnSizes.add(columnWidth);
                prefHeight = Math.max(prefHeight, y + pad);
                if (x > 0)
                    x += wrapSpace;
                x += columnWidth;
                columnWidth = 0;
                y = 0;
                incrY = height;
            }
            y += incrY;
            columnWidth = Math.max(columnWidth, width);
        }
        columnSizes.add(y);
        columnSizes.add(columnWidth);
        prefHeight = Math.max(prefHeight, y + pad);
        if (x > 0)
            x += wrapSpace;
        prefWidth = Math.max(prefWidth, x + columnWidth);
    } else {
        prefHeight = padTop + padBottom + space * (n - 1);
        for (int i = 0; i < n; i++) {
            Actor child = children.get(i);
            if (child instanceof Layout) {
                Layout layout = (Layout) child;
                prefWidth = Math.max(prefWidth, layout.getPrefWidth());
                prefHeight += layout.getPrefHeight();
            } else {
                prefWidth = Math.max(prefWidth, child.getWidth());
                prefHeight += child.getHeight();
            }
        }
    }
    prefWidth += padLeft + padRight;
    if (round) {
        prefWidth = Math.round(prefWidth);
        prefHeight = Math.round(prefHeight);
    }
}
Also used : FloatArray(com.badlogic.gdx.utils.FloatArray) Layout(com.badlogic.gdx.scenes.scene2d.utils.Layout) Actor(com.badlogic.gdx.scenes.scene2d.Actor)

Aggregations

FloatArray (com.badlogic.gdx.utils.FloatArray)11 Actor (com.badlogic.gdx.scenes.scene2d.Actor)4 Layout (com.badlogic.gdx.scenes.scene2d.utils.Layout)4 Glyph (com.badlogic.gdx.graphics.g2d.BitmapFont.Glyph)2 GlyphRun (com.badlogic.gdx.graphics.g2d.GlyphLayout.GlyphRun)2 IntArray (com.badlogic.gdx.utils.IntArray)2 BitmapFont (com.badlogic.gdx.graphics.g2d.BitmapFont)1 BitmapFontData (com.badlogic.gdx.graphics.g2d.BitmapFont.BitmapFontData)1 Stage (com.badlogic.gdx.scenes.scene2d.Stage)1 Skin (com.badlogic.gdx.scenes.scene2d.ui.Skin)1 TextArea (com.badlogic.gdx.scenes.scene2d.ui.TextArea)1