Java Code Examples for org.andengine.entity.text.Text#getLineAlignmentWidth()

The following examples show how to use org.andengine.entity.text.Text#getLineAlignmentWidth() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HighPerformanceTextVertexBufferObject.java    From tilt-game-android with MIT License 4 votes vote down vote up
@Override
public void onUpdateVertices(final Text pText) {
	final float[] bufferData = this.mBufferData;

	final IFont font = pText.getFont();
	final HorizontalAlign horizontalAlign = pText.getHorizontalAlign();
	final ArrayList<CharSequence> lines = pText.getLines();
	final float lineHeight = font.getLineHeight();
	final IFloatList lineWidths = pText.getLineWidths();
	final float leading = pText.getLeading();
	final float ascent = font.getAscent();

	final float lineAlignmentWidth = pText.getLineAlignmentWidth();

	int charactersToDraw = 0;
	int bufferDataOffset = 0;

	final int lineCount = lines.size();
	for (int row = 0; row < lineCount; row++) {
		final CharSequence line = lines.get(row);

		float xBase;
		switch (horizontalAlign) {
			case RIGHT:
				xBase = lineAlignmentWidth - lineWidths.get(row);
				break;
			case CENTER:
				xBase = (lineAlignmentWidth - lineWidths.get(row)) * 0.5f;
				break;
			case LEFT:
			default:
				xBase = 0;
		}

		final float yBase = (lineCount - row) * lineHeight + ((lineCount - row - 1) * leading) + ascent;

		final int lineLength = line.length();
		Letter previousLetter = null;
		for (int i = 0; i < lineLength; i++) {
			final Letter letter = font.getLetter(line.charAt(i));
			if (previousLetter != null) {
				xBase += previousLetter.getKerning(letter.mCharacter);
			}

			if (!letter.isWhitespace()) {
				final float x = xBase + letter.mOffsetX;
				final float y = yBase - letter.mOffsetY;

				final float x2 = x + letter.mWidth;
				final float y2 = y - letter.mHeight;

				final float u = letter.mU;
				final float v = letter.mV;
				final float u2 = letter.mU2;
				final float v2 = letter.mV2;

				bufferData[bufferDataOffset + 0 * Text.VERTEX_SIZE + Text.VERTEX_INDEX_X] = x;
				bufferData[bufferDataOffset + 0 * Text.VERTEX_SIZE + Text.VERTEX_INDEX_Y] = y2;
				bufferData[bufferDataOffset + 0 * Text.VERTEX_SIZE + Text.TEXTURECOORDINATES_INDEX_U] = u;
				bufferData[bufferDataOffset + 0 * Text.VERTEX_SIZE + Text.TEXTURECOORDINATES_INDEX_V] = v2;

				bufferData[bufferDataOffset + 1 * Text.VERTEX_SIZE + Text.VERTEX_INDEX_X] = x;
				bufferData[bufferDataOffset + 1 * Text.VERTEX_SIZE + Text.VERTEX_INDEX_Y] = y;
				bufferData[bufferDataOffset + 1 * Text.VERTEX_SIZE + Text.TEXTURECOORDINATES_INDEX_U] = u;
				bufferData[bufferDataOffset + 1 * Text.VERTEX_SIZE + Text.TEXTURECOORDINATES_INDEX_V] = v;

				bufferData[bufferDataOffset + 2 * Text.VERTEX_SIZE + Text.VERTEX_INDEX_X] = x2;
				bufferData[bufferDataOffset + 2 * Text.VERTEX_SIZE + Text.VERTEX_INDEX_Y] = y2;
				bufferData[bufferDataOffset + 2 * Text.VERTEX_SIZE + Text.TEXTURECOORDINATES_INDEX_U] = u2;
				bufferData[bufferDataOffset + 2 * Text.VERTEX_SIZE + Text.TEXTURECOORDINATES_INDEX_V] = v2;

				bufferData[bufferDataOffset + 3 * Text.VERTEX_SIZE + Text.VERTEX_INDEX_X] = x2;
				bufferData[bufferDataOffset + 3 * Text.VERTEX_SIZE + Text.VERTEX_INDEX_Y] = y2;
				bufferData[bufferDataOffset + 3 * Text.VERTEX_SIZE + Text.TEXTURECOORDINATES_INDEX_U] = u2;
				bufferData[bufferDataOffset + 3 * Text.VERTEX_SIZE + Text.TEXTURECOORDINATES_INDEX_V] = v2;

				bufferData[bufferDataOffset + 4 * Text.VERTEX_SIZE + Text.VERTEX_INDEX_X] = x;
				bufferData[bufferDataOffset + 4 * Text.VERTEX_SIZE + Text.VERTEX_INDEX_Y] = y;
				bufferData[bufferDataOffset + 4 * Text.VERTEX_SIZE + Text.TEXTURECOORDINATES_INDEX_U] = u;
				bufferData[bufferDataOffset + 4 * Text.VERTEX_SIZE + Text.TEXTURECOORDINATES_INDEX_V] = v;

				bufferData[bufferDataOffset + 5 * Text.VERTEX_SIZE + Text.VERTEX_INDEX_X] = x2;
				bufferData[bufferDataOffset + 5 * Text.VERTEX_SIZE + Text.VERTEX_INDEX_Y] = y;
				bufferData[bufferDataOffset + 5 * Text.VERTEX_SIZE + Text.TEXTURECOORDINATES_INDEX_U] = u2;
				bufferData[bufferDataOffset + 5 * Text.VERTEX_SIZE + Text.TEXTURECOORDINATES_INDEX_V] = v;

				bufferDataOffset += Text.LETTER_SIZE;
				charactersToDraw++;
			}

			xBase += letter.mAdvance;

			previousLetter = letter;
		}
	}
	pText.setCharactersToDraw(charactersToDraw);

	this.setDirtyOnHardware();
}
 
Example 2
Source File: LowMemoryTextVertexBufferObject.java    From tilt-game-android with MIT License 4 votes vote down vote up
@Override
public void onUpdateVertices(final Text pText) {
	final FloatBuffer bufferData = this.mFloatBuffer;

	final IFont font = pText.getFont();
	final HorizontalAlign horizontalAlign = pText.getHorizontalAlign();
	final ArrayList<CharSequence> lines = pText.getLines();
	final float lineHeight = font.getLineHeight();
	final IFloatList lineWidths = pText.getLineWidths();
	final float leading = pText.getLeading();
	final float ascent = font.getAscent();

	final float lineAlignmentWidth = pText.getLineAlignmentWidth();

	int charactersToDraw = 0;
	int bufferDataOffset = 0;

	final int lineCount = lines.size();
	for (int row = 0; row < lineCount; row++) {
		final CharSequence line = lines.get(row);

		float xBase;

		switch (horizontalAlign) {
			case RIGHT:
				xBase = lineAlignmentWidth - lineWidths.get(row);
				break;
			case CENTER:
				xBase = (lineAlignmentWidth - lineWidths.get(row)) * 0.5f;
				break;
			case LEFT:
			default:
				xBase = 0;
		}

		final float yBase = (lineCount - row) * lineHeight + ((lineCount - row - 1) * leading) + ascent;

		final int lineLength = line.length();
		Letter previousLetter = null;
		for (int i = 0; i < lineLength; i++) {
			final Letter letter = font.getLetter(line.charAt(i));
			if (previousLetter != null) {
				xBase += previousLetter.getKerning(letter.mCharacter);
			}

			if (!letter.isWhitespace()) {
				final float x = xBase + letter.mOffsetX;
				final float y = yBase - letter.mOffsetY;

				final float x2 = x + letter.mWidth;
				final float y2 = y - letter.mHeight;

				final float u = letter.mU;
				final float v = letter.mV;
				final float u2 = letter.mU2;
				final float v2 = letter.mV2;

				bufferData.put(bufferDataOffset + 0 * Text.VERTEX_SIZE + Text.VERTEX_INDEX_X, x);
				bufferData.put(bufferDataOffset + 0 * Text.VERTEX_SIZE + Text.VERTEX_INDEX_Y, y2);
				bufferData.put(bufferDataOffset + 0 * Text.VERTEX_SIZE + Text.TEXTURECOORDINATES_INDEX_U, u);
				bufferData.put(bufferDataOffset + 0 * Text.VERTEX_SIZE + Text.TEXTURECOORDINATES_INDEX_V, v2);

				bufferData.put(bufferDataOffset + 1 * Text.VERTEX_SIZE + Text.VERTEX_INDEX_X, x);
				bufferData.put(bufferDataOffset + 1 * Text.VERTEX_SIZE + Text.VERTEX_INDEX_Y, y);
				bufferData.put(bufferDataOffset + 1 * Text.VERTEX_SIZE + Text.TEXTURECOORDINATES_INDEX_U, u);
				bufferData.put(bufferDataOffset + 1 * Text.VERTEX_SIZE + Text.TEXTURECOORDINATES_INDEX_V, v);

				bufferData.put(bufferDataOffset + 2 * Text.VERTEX_SIZE + Text.VERTEX_INDEX_X, x2);
				bufferData.put(bufferDataOffset + 2 * Text.VERTEX_SIZE + Text.VERTEX_INDEX_Y, y2);
				bufferData.put(bufferDataOffset + 2 * Text.VERTEX_SIZE + Text.TEXTURECOORDINATES_INDEX_U, u2);
				bufferData.put(bufferDataOffset + 2 * Text.VERTEX_SIZE + Text.TEXTURECOORDINATES_INDEX_V, v2);

				bufferData.put(bufferDataOffset + 3 * Text.VERTEX_SIZE + Text.VERTEX_INDEX_X, x2);
				bufferData.put(bufferDataOffset + 3 * Text.VERTEX_SIZE + Text.VERTEX_INDEX_Y, y2);
				bufferData.put(bufferDataOffset + 3 * Text.VERTEX_SIZE + Text.TEXTURECOORDINATES_INDEX_U, u2);
				bufferData.put(bufferDataOffset + 3 * Text.VERTEX_SIZE + Text.TEXTURECOORDINATES_INDEX_V, v2);

				bufferData.put(bufferDataOffset + 4 * Text.VERTEX_SIZE + Text.VERTEX_INDEX_X, x);
				bufferData.put(bufferDataOffset + 4 * Text.VERTEX_SIZE + Text.VERTEX_INDEX_Y, y);
				bufferData.put(bufferDataOffset + 4 * Text.VERTEX_SIZE + Text.TEXTURECOORDINATES_INDEX_U, u);
				bufferData.put(bufferDataOffset + 4 * Text.VERTEX_SIZE + Text.TEXTURECOORDINATES_INDEX_V, v);

				bufferData.put(bufferDataOffset + 5 * Text.VERTEX_SIZE + Text.VERTEX_INDEX_X, x2);
				bufferData.put(bufferDataOffset + 5 * Text.VERTEX_SIZE + Text.VERTEX_INDEX_Y, y);
				bufferData.put(bufferDataOffset + 5 * Text.VERTEX_SIZE + Text.TEXTURECOORDINATES_INDEX_U, u2);
				bufferData.put(bufferDataOffset + 5 * Text.VERTEX_SIZE + Text.TEXTURECOORDINATES_INDEX_V, v);

				bufferDataOffset += Text.LETTER_SIZE;
				charactersToDraw++;
			}

			xBase += letter.mAdvance;

			previousLetter = letter;
		}
	}
	pText.setCharactersToDraw(charactersToDraw);

	this.setDirtyOnHardware();
}
 
Example 3
Source File: HighPerformanceTextVertexBufferObject.java    From 30-android-libraries-in-30-days with Apache License 2.0 4 votes vote down vote up
@Override
public void onUpdateVertices(final Text pText) {
	final float[] bufferData = this.mBufferData;

	// TODO Optimize with field access?
	final IFont font = pText.getFont();
	final ArrayList<CharSequence> lines = pText.getLines();
	final float lineHeight = font.getLineHeight();
	final IFloatList lineWidths = pText.getLineWidths();

	final float lineAlignmentWidth = pText.getLineAlignmentWidth();

	int charactersToDraw = 0;
	int bufferDataOffset = 0;

	final int lineCount = lines.size();
	for (int row = 0; row < lineCount; row++) {
		final CharSequence line = lines.get(row);

		float xBase;
		switch(pText.getHorizontalAlign()) {
			case RIGHT:
				xBase = lineAlignmentWidth - lineWidths.get(row);
				break;
			case CENTER:
				xBase = (lineAlignmentWidth - lineWidths.get(row)) * 0.5f;
				break;
			case LEFT:
			default:
				xBase = 0;
		}

		final float yBase = row * (lineHeight + pText.getLeading());

		final int lineLength = line.length();
		Letter previousLetter = null;
		for(int i = 0; i < lineLength; i++) {
			final Letter letter = font.getLetter(line.charAt(i));
			if(previousLetter != null) {
				xBase += previousLetter.getKerning(letter.mCharacter);
			}

			if(!letter.isWhitespace()) {
				final float x = xBase + letter.mOffsetX;
				final float y = yBase + letter.mOffsetY;

				final float y2 = y + letter.mHeight;
				final float x2 = x + letter.mWidth;

				final float u = letter.mU;
				final float v = letter.mV;
				final float u2 = letter.mU2;
				final float v2 = letter.mV2;

				bufferData[bufferDataOffset + 0 * Text.VERTEX_SIZE + Text.VERTEX_INDEX_X] = x;
				bufferData[bufferDataOffset + 0 * Text.VERTEX_SIZE + Text.VERTEX_INDEX_Y] = y;
				bufferData[bufferDataOffset + 0 * Text.VERTEX_SIZE + Text.TEXTURECOORDINATES_INDEX_U] = u;
				bufferData[bufferDataOffset + 0 * Text.VERTEX_SIZE + Text.TEXTURECOORDINATES_INDEX_V] = v;

				bufferData[bufferDataOffset + 1 * Text.VERTEX_SIZE + Text.VERTEX_INDEX_X] = x;
				bufferData[bufferDataOffset + 1 * Text.VERTEX_SIZE + Text.VERTEX_INDEX_Y] = y2;
				bufferData[bufferDataOffset + 1 * Text.VERTEX_SIZE + Text.TEXTURECOORDINATES_INDEX_U] = u;
				bufferData[bufferDataOffset + 1 * Text.VERTEX_SIZE + Text.TEXTURECOORDINATES_INDEX_V] = v2;

				bufferData[bufferDataOffset + 2 * Text.VERTEX_SIZE + Text.VERTEX_INDEX_X] = x2;
				bufferData[bufferDataOffset + 2 * Text.VERTEX_SIZE + Text.VERTEX_INDEX_Y] = y2;
				bufferData[bufferDataOffset + 2 * Text.VERTEX_SIZE + Text.TEXTURECOORDINATES_INDEX_U] = u2;
				bufferData[bufferDataOffset + 2 * Text.VERTEX_SIZE + Text.TEXTURECOORDINATES_INDEX_V] = v2;

				bufferData[bufferDataOffset + 3 * Text.VERTEX_SIZE + Text.VERTEX_INDEX_X] = x2;
				bufferData[bufferDataOffset + 3 * Text.VERTEX_SIZE + Text.VERTEX_INDEX_Y] = y2;
				bufferData[bufferDataOffset + 3 * Text.VERTEX_SIZE + Text.TEXTURECOORDINATES_INDEX_U] = u2;
				bufferData[bufferDataOffset + 3 * Text.VERTEX_SIZE + Text.TEXTURECOORDINATES_INDEX_V] = v2;

				bufferData[bufferDataOffset + 4 * Text.VERTEX_SIZE + Text.VERTEX_INDEX_X] = x2;
				bufferData[bufferDataOffset + 4 * Text.VERTEX_SIZE + Text.VERTEX_INDEX_Y] = y;
				bufferData[bufferDataOffset + 4 * Text.VERTEX_SIZE + Text.TEXTURECOORDINATES_INDEX_U] = u2;
				bufferData[bufferDataOffset + 4 * Text.VERTEX_SIZE + Text.TEXTURECOORDINATES_INDEX_V] = v;

				bufferData[bufferDataOffset + 5 * Text.VERTEX_SIZE + Text.VERTEX_INDEX_X] = x;
				bufferData[bufferDataOffset + 5 * Text.VERTEX_SIZE + Text.VERTEX_INDEX_Y] = y;
				bufferData[bufferDataOffset + 5 * Text.VERTEX_SIZE + Text.TEXTURECOORDINATES_INDEX_U] = u;
				bufferData[bufferDataOffset + 5 * Text.VERTEX_SIZE + Text.TEXTURECOORDINATES_INDEX_V] = v;

				bufferDataOffset += Text.LETTER_SIZE;
				charactersToDraw++;
			}

			xBase += letter.mAdvance;

			previousLetter = letter;
		}
	}
	pText.setCharactersToDraw(charactersToDraw);

	this.setDirtyOnHardware();
}
 
Example 4
Source File: LowMemoryTextVertexBufferObject.java    From 30-android-libraries-in-30-days with Apache License 2.0 4 votes vote down vote up
@Override
public void onUpdateVertices(final Text pText) {
	final FloatBuffer bufferData = this.mFloatBuffer;

	// TODO Optimize with field access?
	final IFont font = pText.getFont();
	final ArrayList<CharSequence> lines = pText.getLines();
	final float lineHeight = font.getLineHeight();
	final IFloatList lineWidths = pText.getLineWidths();

	final float lineAlignmentWidth = pText.getLineAlignmentWidth();

	int charactersToDraw = 0;
	int bufferDataOffset = 0;

	final int lineCount = lines.size();
	for (int i = 0; i < lineCount; i++) {
		final CharSequence line = lines.get(i);

		float xBase;

		switch(pText.getHorizontalAlign()) {
			case RIGHT:
				xBase = lineAlignmentWidth - lineWidths.get(i);
				break;
			case CENTER:
				xBase = (lineAlignmentWidth - lineWidths.get(i)) * 0.5f;
				break;
			case LEFT:
			default:
				xBase = 0;
		}

		final float yBase = i * (lineHeight + pText.getLeading());

		final int lineLength = line.length();
		Letter previousLetter = null;
		for(int j = 0; j < lineLength; j++) {
			final Letter letter = font.getLetter(line.charAt(j));
			if(previousLetter != null) {
				xBase += previousLetter.getKerning(letter.mCharacter);
			}

			if(!letter.isWhitespace()) {
				final float x = xBase + letter.mOffsetX;
				final float y = yBase + letter.mOffsetY;

				final float y2 = y + letter.mHeight;
				final float x2 = x + letter.mWidth;

				final float u = letter.mU;
				final float v = letter.mV;
				final float u2 = letter.mU2;
				final float v2 = letter.mV2;

				bufferData.put(bufferDataOffset + 0 * Text.VERTEX_SIZE + Text.VERTEX_INDEX_X, x);
				bufferData.put(bufferDataOffset + 0 * Text.VERTEX_SIZE + Text.VERTEX_INDEX_Y, y);
				bufferData.put(bufferDataOffset + 0 * Text.VERTEX_SIZE + Text.TEXTURECOORDINATES_INDEX_U, u);
				bufferData.put(bufferDataOffset + 0 * Text.VERTEX_SIZE + Text.TEXTURECOORDINATES_INDEX_V, v);

				bufferData.put(bufferDataOffset + 1 * Text.VERTEX_SIZE + Text.VERTEX_INDEX_X, x);
				bufferData.put(bufferDataOffset + 1 * Text.VERTEX_SIZE + Text.VERTEX_INDEX_Y, y2);
				bufferData.put(bufferDataOffset + 1 * Text.VERTEX_SIZE + Text.TEXTURECOORDINATES_INDEX_U, u);
				bufferData.put(bufferDataOffset + 1 * Text.VERTEX_SIZE + Text.TEXTURECOORDINATES_INDEX_V, v2);

				bufferData.put(bufferDataOffset + 2 * Text.VERTEX_SIZE + Text.VERTEX_INDEX_X, x2);
				bufferData.put(bufferDataOffset + 2 * Text.VERTEX_SIZE + Text.VERTEX_INDEX_Y, y2);
				bufferData.put(bufferDataOffset + 2 * Text.VERTEX_SIZE + Text.TEXTURECOORDINATES_INDEX_U, u2);
				bufferData.put(bufferDataOffset + 2 * Text.VERTEX_SIZE + Text.TEXTURECOORDINATES_INDEX_V, v2);

				bufferData.put(bufferDataOffset + 3 * Text.VERTEX_SIZE + Text.VERTEX_INDEX_X, x2);
				bufferData.put(bufferDataOffset + 3 * Text.VERTEX_SIZE + Text.VERTEX_INDEX_Y, y2);
				bufferData.put(bufferDataOffset + 3 * Text.VERTEX_SIZE + Text.TEXTURECOORDINATES_INDEX_U, u2);
				bufferData.put(bufferDataOffset + 3 * Text.VERTEX_SIZE + Text.TEXTURECOORDINATES_INDEX_V, v2);

				bufferData.put(bufferDataOffset + 4 * Text.VERTEX_SIZE + Text.VERTEX_INDEX_X, x2);
				bufferData.put(bufferDataOffset + 4 * Text.VERTEX_SIZE + Text.VERTEX_INDEX_Y, y);
				bufferData.put(bufferDataOffset + 4 * Text.VERTEX_SIZE + Text.TEXTURECOORDINATES_INDEX_U, u2);
				bufferData.put(bufferDataOffset + 4 * Text.VERTEX_SIZE + Text.TEXTURECOORDINATES_INDEX_V, v);

				bufferData.put(bufferDataOffset + 5 * Text.VERTEX_SIZE + Text.VERTEX_INDEX_X, x);
				bufferData.put(bufferDataOffset + 5 * Text.VERTEX_SIZE + Text.VERTEX_INDEX_Y, y);
				bufferData.put(bufferDataOffset + 5 * Text.VERTEX_SIZE + Text.TEXTURECOORDINATES_INDEX_U, u);
				bufferData.put(bufferDataOffset + 5 * Text.VERTEX_SIZE + Text.TEXTURECOORDINATES_INDEX_V, v);

				bufferDataOffset += Text.LETTER_SIZE;
				charactersToDraw++;
			}

			xBase += letter.mAdvance;

			previousLetter = letter;
		}
	}
	pText.setCharactersToDraw(charactersToDraw);

	this.setDirtyOnHardware();
}