@Override public void render(float delta) { Gdx.gl.glClearColor(0, 0.2f, 0, 1); Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); camera.update(); game.batch.setProjectionMatrix(camera.combined); game.batch.begin(); TextBounds text1Bounds = game.font.getBounds("You did it!"); float text1X = SIZE / 2 - text1Bounds.width / 2; float text1Y = SIZE * 3 / 4 - text1Bounds.height / 2; game.font.draw(game.batch, "You did it!", text1X, text1Y); TextBounds text2Bounds = game.font.getBounds("Tap anywhere to play again."); float text2X = SIZE / 2 - text2Bounds.width / 2; float text2Y = SIZE / 2 - text1Bounds.height / 2; game.font.draw(game.batch, "Tap anywhere to play again.", text2X, text2Y); game.batch.end(); if (Gdx.input.justTouched()) { game.setScreen(new GameScreen(game)); dispose(); } }
@Override public void draw (float delta) { Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); viewMatrix.setToOrtho2D(0, 0, 480, 320); spriteBatch.setProjectionMatrix(viewMatrix); spriteBatch.setTransformMatrix(transformMatrix); spriteBatch.begin(); spriteBatch.disableBlending(); spriteBatch.setColor(Color.WHITE); spriteBatch.draw(background, 0, 0, 480, 320, 0, 0, 512, 512, false, false); spriteBatch.enableBlending(); spriteBatch.draw(logo, 0, 320 - 128, 480, 128, 0, 256, 512, 256, false, false); String text = "It is the end my friend.\nTouch to continue!"; TextBounds bounds = font.getMultiLineBounds(text); spriteBatch.setBlendFunction(GL20.GL_ONE, GL20.GL_ONE_MINUS_SRC_ALPHA); font.drawMultiLine(spriteBatch, text, 0, 160 + bounds.height / 2, 480, HAlignment.CENTER); spriteBatch.end(); }
public void layout () { final BitmapFont font = style.font; final Drawable selectedDrawable = style.selection; itemHeight = font.getCapHeight() - font.getDescent() * 2; itemHeight += selectedDrawable.getTopHeight() + selectedDrawable.getBottomHeight(); textOffsetX = selectedDrawable.getLeftWidth(); textOffsetY = selectedDrawable.getTopHeight() - font.getDescent(); prefWidth = 0; for (int i = 0; i < items.size; i++) { TextBounds bounds = font.getBounds(items.get(i).toString()); prefWidth = Math.max(bounds.width, prefWidth); } prefWidth += selectedDrawable.getLeftWidth() + selectedDrawable.getRightWidth(); prefHeight = items.size * itemHeight; Drawable background = style.background; if (background != null) { prefWidth += background.getLeftWidth() + background.getRightWidth(); prefHeight += background.getTopHeight() + background.getBottomHeight(); } }
private void renderSingleLineCached () { String text = "Single Line Cached"; float x = logoSprite.getX(); float y = logoSprite.getY(); float width = logoSprite.getWidth(); float height = logoSprite.getHeight(); // Obviously you wouldn't set the cache text every frame in real code. TextBounds bounds = cache.setMultiLineText(text, 0, 0); cache.setColors(Color.BLUE, 1, 4); x += width / 2 - bounds.width / 2; y += height / 2 + bounds.height / 2; cache.setPosition(x, y); cache.draw(spriteBatch); }
private void renderWrapped () { String text = "Wrapped Wrapped Wrapped Wrapped"; float x = logoSprite.getX(); float y = logoSprite.getY(); float width = logoSprite.getWidth(); float height = logoSprite.getHeight(); TextBounds bounds = font.getWrappedBounds(text, width); x += width / 2 - bounds.width / 2; y += height / 2 + bounds.height / 2; font.drawWrapped(spriteBatch, text, x, y, width); // Note that wrapped text can be aligned: // font.drawWrapped(spriteBatch, text, x, y, width, HAlignment.CENTER); }
private void renderWrappedCached () { String text = "Wrapped Cached Wrapped Cached"; float x = logoSprite.getX(); float y = logoSprite.getY(); float width = logoSprite.getWidth(); float height = logoSprite.getHeight(); // Obviously you wouldn't set the cache text every frame in real code. TextBounds bounds = cache.setWrappedText(text, 0, 0, width); // Note that wrapped text can be aligned: // cache.setWrappedText(text, 0, 0, width, HAlignment.CENTER); x += width / 2 - bounds.width / 2; y += height / 2 + bounds.height / 2; cache.setPosition(x, y); cache.draw(spriteBatch); }
private void renderMultiLine () { String text = "Multi\nLine"; float x = logoSprite.getX(); float y = logoSprite.getY(); float width = logoSprite.getWidth(); float height = logoSprite.getHeight(); TextBounds bounds = font.getMultiLineBounds(text); x += width / 2 - bounds.width / 2; y += height / 2 + bounds.height / 2; font.drawMultiLine(spriteBatch, text, x, y); // Note that multi line text can be aligned: // font.drawMultiLine(spriteBatch, text, x, y, width, HAlignment.CENTER); }
private void renderMultiLineCached () { String text = "Multi Line\nCached"; int lines = 2; float x = logoSprite.getX(); float y = logoSprite.getY(); float width = logoSprite.getWidth(); float height = logoSprite.getHeight(); // Obviously you wouldn't set the cache text every frame in real code. TextBounds bounds = cache.setMultiLineText(text, 0, 0); // Note that multi line text can be aligned: // cache.setMultiLineText(text, 0, 0, width, HAlignment.CENTER); x += width / 2 - bounds.width / 2; y += height / 2 + bounds.height / 2; cache.setPosition(x, y); cache.draw(spriteBatch); }
/** * Don't use this to make GUI elements. See GUIUtil.java */ public static Entity createGUIText(String name, String s, float x, float y, float xAnchor, float yAnchor, CGUIFunction function) { Entity text = world.createEntity(); text.addComponent(new CGUIPosition(x, y, xAnchor, yAnchor)); text.addComponent(new CText(s)); TextBounds bounds = RenderingSystem.getTextBounds(s, true); text.addComponent(new CSize(bounds.width, bounds.height)); text.addComponent(function, ComponentType.getTypeFor(CGUIFunction.class)); text.addComponent(new CName(name)); setEntityLayer(text, Name.LAYER_GUI); tagEntity(text, Name.GUI); world.addEntity(text); text.getComponent(CGUIFunction.class).onCreate(text); return text; }
@Override public void draw(Batch batch, float parentAlpha) { super.draw(batch, parentAlpha); string = String.format("L%d - Destroy %d of %d", levelInfo.level + 1, levelInfo.numNeededToPass, levelInfo.numTotal); drawShadowString(batch, font, string, bounds.x + 4, bounds.y + bounds.height - 6, Color.GREEN); string = getDestroyedBar(); TextBounds txtSize = barFont.getBounds(string); drawShadowString(batch, barFont, string, bounds.x + bounds.width - 4 - txtSize.width, bounds.y + bounds.height - 6, Color.GREEN); string = String.format("Destroyed %d", numDestroyed); txtSize = font.getBounds(string); drawShadowString(batch, font, string, bounds.width / 2 - txtSize.width / 2, bounds.y + bounds.height - 6, (numDestroyed < levelInfo.numNeededToPass) ? Color.YELLOW : Color.GREEN); if (showFps) { drawShadowString(batch, font, Gdx.graphics.getFramesPerSecond() + " FPS", bounds.x + 4, bounds.y + 22, Color.YELLOW); } }
public CheckBox(String name, String text, int hotkey, int x, int y) { super(name, x, y, -1, -1); this.text = text; this.hotkey = hotkey; tChecked = GUIResources.getInstance().getSprite(TEXTURE_CHECKED); tUnchecked = GUIResources.getInstance().getSprite(TEXTURE_UNCHECKED); font = GUIResources.getInstance().getFont(GUIResources.FONT_STANDARD_WHITE); setChecked(false); TextBounds b = font.getBounds(text); txo = tChecked.getWidth() + SMALL_SPACER; tyo = (tChecked.getHeight() - b.height) / 2; setBounds(tChecked.getWidth() + (int) Math.ceil(b.width), tChecked.getHeight()); }
/** * Draw this button with temporary text. * @param batch sprite batch to be used. * @param text current button text. (This doesn't update the internal button text.) * @param x coordinate. * @param y coordinate. */ public void draw(SpriteBatch batch, String text, float x, float y) { TextBounds bnd = font.draw(batch, text, x, y); t1.set(x, y, 0f); float y2 = y - font.getLineHeight(); t2.set(x + bnd.width, y2, 0f); camera.project(t1); camera.project(t2); t1.y = (float)(Gdx.graphics.getHeight()) - t1.y; t2.y = (float)(Gdx.graphics.getHeight()) - t2.y; if(justTouched((int)t1.x, (int)t1.y, (int)t2.x, (int)t2.y)) { action.buttonAction(id); } }
public void setText(String paramString) { if (paramString == null) throw new IllegalArgumentException("text cannot be null."); BitmapFont localBitmapFont = this.style.font; StringBuffer localStringBuffer = new StringBuffer(); for (int i = 0; i < paramString.length(); i++) { char c = paramString.charAt(i); if (localBitmapFont.containsCharacter(c)) localStringBuffer.append(c); } this.text = localStringBuffer.toString(); updateDisplayText(); this.cursor = 0; clearSelection(); this.textBounds.set(localBitmapFont.getBounds(this.displayText)); BitmapFont.TextBounds localTextBounds = this.textBounds; localTextBounds.height -= 2.0F * localBitmapFont.getDescent(); localBitmapFont.computeGlyphAdvancesAndPositions(this.displayText, this.glyphAdvances, this.glyphPositions); }
@Override protected void drawBackground (Batch batch, float parentAlpha, float x, float y) { super.drawBackground(batch, parentAlpha, x, y); TextBounds bounds = squadText.getBounds(); float xPos = x + ((getWidth() * 0.5f) - (bounds.width * 0.5f)); float yPos = y + ((getHeight()) - (bounds.height * 0.5f) ) + 4; //The constant should not be needed squadText.setPosition(xPos, yPos); squadText.draw(batch, parentAlpha); }
public boolean isPressed(Vector2 touchVector, BitmapFont font) { TextBounds bounds = font.getBounds(text); if(touchVector.x > x && touchVector.x < x + bounds.width && touchVector.y < y && touchVector.y > y - bounds.height) { return true; } return false; }
protected void debugDrawUI() { if (Env.debug) { if (Env.drawFPS) { String fpsText = String.format("%d FPS", Gdx.graphics.getFramesPerSecond()); TextBounds bounds = debugFont.getBounds(fpsText); batch.setProjectionMatrix(uiCamera.combined); batch.begin(); debugFont.setColor(1.0f, 1.0f, 1.0f, 1.0f); debugFont.draw(batch, fpsText, uiViewport.getWorldWidth() - bounds.width - 20.0f, 20.0f); batch.end(); } Table.drawDebug(Env.game.getStage()); } }
protected void drawBackground (Batch batch, float parentAlpha, float x, float y) { float width = getWidth(), height = getHeight(); float padTop = getPadTop(); super.drawBackground(batch, parentAlpha, x, y); // Draw button table. buttonTable.getColor().a = getColor().a; buttonTable.pack(); buttonTable.setPosition(width - buttonTable.getWidth(), Math.min(height - padTop, height - buttonTable.getHeight())); buttonTable.draw(batch, parentAlpha); // Draw the title without the batch transformed or clipping applied. y += height; TextBounds bounds = titleCache.getBounds(); if ((titleAlignment & Align.left) != 0) x += getPadLeft(); else if ((titleAlignment & Align.right) != 0) x += width - bounds.width - getPadRight(); else x += (width - bounds.width) / 2; if ((titleAlignment & Align.top) == 0) { if ((titleAlignment & Align.bottom) != 0) y -= padTop - bounds.height; else y -= (padTop - bounds.height) / 2; } titleCache.tint(Color.tmp.set(getColor()).mul(style.titleFontColor)); titleCache.setPosition((int)x, (int)y); titleCache.draw(batch, parentAlpha); }
/** Adds glyphs for the the specified text. * @param x The x position for the left most character. * @param y The y position for the top of most capital letters in the font (the {@link BitmapFont#getCapHeight() cap height}). * @param start The first character of the string to draw. * @param end The last character of the string to draw (exclusive). * @return The bounds of the cached string (the height is the distance from y to the baseline). */ public TextBounds addText (CharSequence str, float x, float y, int start, int end) { requireSequence(str, start, end); y += font.data.ascent; textBounds.width = addToCache(str, x, y, start, end); textBounds.height = font.data.capHeight; return textBounds; }
@Override public void draw3dText (Vector3 location, String textString) { if (spriteBatch == null) { spriteBatch = new SpriteBatch(); } if (font == null) { font = new BitmapFont(); } // this check is necessary to avoid "mirrored" instances of the text if (camera.frustum.pointInFrustum(location)) { if (viewport != null) { camera.project(location, viewport.getScreenX(), viewport.getScreenY(), viewport.getScreenWidth(), viewport.getScreenHeight()); } else { camera.project(location); } shapeRenderer.end(); spriteBatch.begin(); // the text will be centered on the position TextBounds bounds = font.getBounds(textString); font.draw(spriteBatch, textString, location.x - (bounds.width / 2), location.y + (bounds.height / 2)); spriteBatch.end(); shapeRenderer.begin(ShapeType.Line); } }
private void renderSingleLine () { String text = "Single Line"; float x = logoSprite.getX(); float y = logoSprite.getY(); float width = logoSprite.getWidth(); float height = logoSprite.getHeight(); TextBounds bounds = font.getBounds(text); x += width / 2 - bounds.width / 2; y += height / 2 + bounds.height / 2; font.draw(spriteBatch, text, x, y); }
public static TextBounds getTextBounds(String s, boolean scaledDown) { TextBounds bounds = font.getBounds(s); if (scaledDown) { bounds.width = bounds.width / scale; bounds.height = bounds.height / scale; } return bounds; }
void draw(Batch batch, float parentAlpha) { if (target != null && text != null) { final float PADDING = 10f; TextBounds bounds = font.getBounds(text); float x = (Gdx.input.getX() / Sizes.worldScreenFactorX()) + OFFSET; float y = ((Gdx.graphics.getHeight() - Gdx.input.getY()) / Sizes.worldScreenFactorY()) - OFFSET; float width = bounds.width + PADDING * 2; float height = font.getLineHeight() * 2; background.setColor(new Color(1f, 1f, 1f, alpha)); background.draw(batch, x, y, width, height); font.setColor(1f, 1f, 1f, alpha); font.draw(batch, text, x + PADDING, y + height - PADDING - 3f); } }
public static boolean isTouched(String str, float x, float y, Color color, int size){ if (!Gdx.input.justTouched()){ return false; } TextBounds bounds = get(color, size).getBounds(str); Rectangle rect = new Rectangle(x, y, bounds.width, -bounds.height); return rect.contains(Main.getX(), Main.getY()); }
void drawHUD() { HUDText.draw(batch, "Score: " + world.getScore(), 50.0F, height - 20.0F); TextBounds tb = HUDText.getBounds("Level: 000 "); if (world.getMode() == World.Mode.ENDUR) { HUDText.draw(batch, "Level: " + world.getCurrentLevel(), width - tb.width, height - 20.0F); } else if (world.getMode() == World.Mode.MINUTE) { HUDText.draw(batch, "Time: " + world.getTimeRemaining(), width - 150.0F, height - 20.0F); } }
private void renderText() { render.RenderText("yahei", "Power:"+model.getPower()+ " Score:"+model.getScore() ,0, Gdx.graphics.getHeight()); String rightTxt = "Life:"+model.getLife(); TextBounds bounds = render.getTextBounds("yahei", rightTxt); render.RenderText("yahei", rightTxt,(int) (Gdx.graphics.getWidth()-bounds.width), Gdx.graphics.getHeight()); String stageName = model.getCurrentStage().getTitle(); bounds = render.getTextBounds("yahei", stageName); render.RenderText("yahei", stageName,0,30); }
public void render() { fader.render(Sleep.batch, font); // render output int outputStartPosY = startPosY + (int) font.getLineHeight(); TextBounds wrappedBounds; for (int i = outputLog.size() - 1 - outputLogIndex; i >= 0; i--) { wrappedBounds = font.getWrappedBounds(outputLog.get(i), wrapWidth); outputStartPosY += wrappedBounds.height + font.getWrappedBounds(" ", wrapWidth).height / 4; if (outputStartPosY >= Gdx.graphics.getHeight() + wrappedBounds.height) { break; } font.drawWrapped(Sleep.batch, outputLog.get(i), startPosX, outputStartPosY, wrapWidth); } // render current input font.draw(Sleep.batch, ">" + inputProcessor.currentInput.toString(), startPosX, startPosY + font.getLineHeight()); // render cursor Color c = new Color(Sleep.batch.getColor()); Sleep.batch.setColor(cursorColor.r * c.r, cursorColor.g * c.g, cursorColor.b * c.b, cursorColor.a * c.a); // int blendSrc = Sleep.batch.getBlendSrcFunc(); // int blendDst = Sleep.batch.getBlendDstFunc(); // Sleep.batch.setBlendFunction(GL20.GL_ONE_MINUS_DST_COLOR, GL20.GL_ONE_MINUS_SRC_COLOR); Sleep.batch.draw(Sleep.assets.get("images/cursor2.png", Texture.class), 3 + startPosX + font.getSpaceWidth() + cursor * font.getSpaceWidth(), startPosY - font.getLineHeight() / 4); // Sleep.batch.setBlendFunction(blendSrc, blendDst); Sleep.batch.setColor(c); fader.renderDone(Sleep.batch, font); }
public void drawStringCentred(String string, SpriteBatch batch, int x, int y) { TextBounds bounds = font.getBounds(string); x -= bounds.width / 2; y -= bounds.height / 2; drawString(string, batch, x, y); }
@Override public void drawText() { drawCentered(GUIResources.getInstance().getFont(player.getColor()), strMetal); BitmapFont font = GUIResources.getInstance().getFont(GUIResources.FONT_SMALL_WHITE); TextBounds b = font.getBounds(strDeltaA); draw(font, strDeltaA, getX() + getWidth() - b.width - 4, getY() + getHeight() - b.height - 4); b = font.getBounds(strDeltaR); draw(font, strDeltaR, getX() + getWidth() - b.width - 4, getY() + 4); //draw(, strMetal, getX() + getWidth() / 2 - 20, getY() + halfHeight + halfHeight / 2 + 7); //draw(GUIResources.getInstance().getFont(GUIResources.FONT_STANDARD_WHITE), strDelta, getX() + getWidth() / 2 - 10, getY() + halfHeight / 2 + 7); }
public void test(int size) { FreeTypeFontGenerator generator = new FreeTypeFontGenerator(Gdx.files.internal("font/arial.ttf")); FreeTypeFontParameter parameter = new FreeTypeFontParameter(); parameter.size = size; BitmapFont font = generator.generateFont(parameter); generator.dispose(); TextBounds b = font.getBounds("azertyuiopqsdfghjklmwxcvbn"); System.out.println(size+ " --> " + b.width + " " + b.height); font.dispose(); }
public TextBounds getTextBounds () { if (sizeInvalid) scaleAndComputeSize(); return bounds; }
/** Clears any cached glyphs and adds glyphs for the specified text. * @see #addText(CharSequence, float, float, int, int) */ public TextBounds setText (CharSequence str, float x, float y) { clear(); return addText(str, x, y, 0, str.length()); }
/** Clears any cached glyphs and adds glyphs for the specified text. * @see #addText(CharSequence, float, float, int, int) */ public TextBounds setText (CharSequence str, float x, float y, int start, int end) { clear(); return addText(str, x, y, start, end); }
/** Adds glyphs for the specified text. * @see #addText(CharSequence, float, float, int, int) */ public TextBounds addText (CharSequence str, float x, float y) { return addText(str, x, y, 0, str.length()); }
/** Clears any cached glyphs and adds glyphs for the specified text, which may contain newlines (\n). * @see #addMultiLineText(CharSequence, float, float, float, HAlignment) */ public TextBounds setMultiLineText (CharSequence str, float x, float y) { clear(); return addMultiLineText(str, x, y, 0, HAlignment.LEFT); }
/** Clears any cached glyphs and adds glyphs for the specified text, which may contain newlines (\n). * @see #addMultiLineText(CharSequence, float, float, float, HAlignment) */ public TextBounds setMultiLineText (CharSequence str, float x, float y, float alignmentWidth, HAlignment alignment) { clear(); return addMultiLineText(str, x, y, alignmentWidth, alignment); }
/** Adds glyphs for the specified text, which may contain newlines (\n). * @see #addMultiLineText(CharSequence, float, float, float, HAlignment) */ public TextBounds addMultiLineText (CharSequence str, float x, float y) { return addMultiLineText(str, x, y, 0, HAlignment.LEFT); }
/** Clears any cached glyphs and adds glyphs for the specified text, which may contain newlines (\n) and is automatically * wrapped within the specified width. * @see #addWrappedText(CharSequence, float, float, float, HAlignment) */ public TextBounds setWrappedText (CharSequence str, float x, float y, float wrapWidth) { clear(); return addWrappedText(str, x, y, wrapWidth, HAlignment.LEFT); }
/** Clears any cached glyphs and adds glyphs for the specified text, which may contain newlines (\n) and is automatically * wrapped within the specified width. * @see #addWrappedText(CharSequence, float, float, float, HAlignment) */ public TextBounds setWrappedText (CharSequence str, float x, float y, float wrapWidth, HAlignment alignment) { clear(); return addWrappedText(str, x, y, wrapWidth, alignment); }
/** Adds glyphs for the specified text, which may contain newlines (\n) and is automatically wrapped within the specified width. * @see #addWrappedText(CharSequence, float, float, float, HAlignment) */ public TextBounds addWrappedText (CharSequence str, float x, float y, float wrapWidth) { return addWrappedText(str, x, y, wrapWidth, HAlignment.LEFT); }