From 025855819d8052410bc8bbc2c62016211a3c99d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Zurcher?= Date: Sun, 9 Nov 2014 00:38:42 +0100 Subject: Label: extends Widget, has a padding --- core/src/ch/asynk/tankontank/game/hud/Label.java | 61 ++++++++---------------- 1 file changed, 19 insertions(+), 42 deletions(-) diff --git a/core/src/ch/asynk/tankontank/game/hud/Label.java b/core/src/ch/asynk/tankontank/game/hud/Label.java index e3f753f..0b6001e 100644 --- a/core/src/ch/asynk/tankontank/game/hud/Label.java +++ b/core/src/ch/asynk/tankontank/game/hud/Label.java @@ -1,63 +1,48 @@ package ch.asynk.tankontank.game.hud; -import com.badlogic.gdx.utils.Disposable; import com.badlogic.gdx.graphics.g2d.Batch; import com.badlogic.gdx.graphics.g2d.BitmapFont; import com.badlogic.gdx.graphics.g2d.BitmapFont.TextBounds; -import com.badlogic.gdx.graphics.glutils.ShapeRenderer; import ch.asynk.tankontank.engine.gfx.Drawable; -public class Label implements Drawable, Disposable +public class Label extends Widget { - public boolean visible; - public float x; - public float y; - private float ry; - private String text; private BitmapFont font; + private float padding; + private float rx; + private float ry; + protected String text; - public Label(BitmapFont font, String text) - { - this.font = font; - this.text = text; - this.visible = true; - } - - @Override - public void dispose() + public Label(BitmapFont font) { - font.dispose(); + this(font, 0f); } - public float getWidth() + public Label(BitmapFont font, float padding) { - TextBounds b = getBounds(); - return b.width; + this.font = font; + this.padding = padding; } - public float getHeight() + @Override + public void dispose() { - TextBounds b = getBounds(); - return b.height; } + @Override public void setPosition(float x, float y) { - TextBounds b = getBounds(); - this.x = x; - this.y = y; - this.ry = (y + b.height); - } - - public TextBounds getBounds() - { - return font.getMultiLineBounds(text); + TextBounds b = font.getMultiLineBounds((text == null) ? "" : text); + set(x, y, (b.width + (2 * padding)), (b.height + (2 * padding))); + this.rx = x + (padding); + this.ry = (y + padding + b.height); } public void write(String text) { this.text = text; + setPosition(rect.x, rect.y); } public void write(String text, float x, float y) @@ -70,14 +55,6 @@ public class Label implements Drawable, Disposable public void draw(Batch batch) { if (!visible) return; - font.drawMultiLine(batch, text, x, ry); - } - - @Override - public void drawDebug(ShapeRenderer shapes) - { - if (!visible) return; - TextBounds b = getBounds(); - shapes.rect(x, y, b.width, b.height); + font.drawMultiLine(batch, text, rx, ry); } } -- cgit v1.1-2-g2b99