package com.badlogic.gdx.scenes.scene2d.actors;\r
\r
import com.badlogic.gdx.graphics.g2d.BitmapFont;\r
+import com.badlogic.gdx.graphics.g2d.BitmapFont.HAlignment;\r
import com.badlogic.gdx.graphics.g2d.BitmapFont.TextBounds;\r
import com.badlogic.gdx.graphics.g2d.BitmapFontCache;\r
import com.badlogic.gdx.graphics.g2d.SpriteBatch;\r
import com.badlogic.gdx.scenes.scene2d.Actor;\r
+import com.badlogic.gdx.scenes.scene2d.Layout;\r
\r
-public class Label extends Actor {\r
+public class Label extends Actor implements Layout {\r
public BitmapFontCache cache;\r
+ public VAlignment valignment = VAlignment.BOTTOM;\r
+ public String text;\r
+ public TextBounds bounds;\r
\r
- public Label (String name, BitmapFont font, String text) {\r
+ private WrapType wrapType;\r
+ private HAlignment halignment;\r
+ private float lastWidth = -1;\r
+\r
+ public Label (String name, BitmapFont font) {\r
super(name);\r
cache = new BitmapFontCache(font);\r
+ }\r
+\r
+ public Label (String name, BitmapFont font, String text) {\r
+ this(name, font);\r
setText(text);\r
}\r
\r
public void setText (String text) {\r
- cache.setText(text, 0, cache.getFont().isFlipped() ? 0 : cache.getFont().getCapHeight());\r
- TextBounds bounds = cache.getBounds();\r
+ this.text = text;\r
+ wrapType = WrapType.singleLine;\r
+ bounds = cache.setText(text, 0, cache.getFont().isFlipped() ? 0 : cache.getFont().getCapHeight());\r
+ width = bounds.width;\r
+ height = bounds.height;\r
+ }\r
+\r
+ public void setMultiLineText (String text) {\r
+ this.text = text;\r
+ wrapType = WrapType.multiLine;\r
+ bounds = cache.getFont().getMultiLineBounds(text);\r
+ cache.setMultiLineText(text, 0, cache.getFont().isFlipped() ? 0 : bounds.height);\r
width = bounds.width;\r
height = bounds.height;\r
}\r
\r
+ public void setWrappedText (String text, HAlignment alignment) {\r
+ this.text = text;\r
+ this.halignment = alignment;\r
+ wrapType = WrapType.wrapped;\r
+ bounds = cache.getFont().getWrappedBounds(text, width);\r
+ cache.setWrappedText(text, 0, cache.getFont().isFlipped() ? 0 : bounds.height, width, alignment);\r
+ }\r
+\r
+ public void setFont (BitmapFont font) {\r
+ cache = new BitmapFontCache(font);\r
+ switch (wrapType) {\r
+ case singleLine:\r
+ setText(text);\r
+ break;\r
+ case multiLine:\r
+ setMultiLineText(text);\r
+ break;\r
+ case wrapped:\r
+ setWrappedText(text, halignment);\r
+ break;\r
+ }\r
+ }\r
+\r
@Override protected void draw (SpriteBatch batch, float parentAlpha) {\r
cache.setColor(color.r, color.g, color.b, color.a * parentAlpha);\r
- cache.setPosition(x, y);\r
+ switch (valignment) {\r
+ case TOP:\r
+ cache.setPosition(x, y + height - bounds.height);\r
+ break;\r
+ case CENTER:\r
+ cache.setPosition(x, y + (height - bounds.height) / 2);\r
+ break;\r
+ case BOTTOM:\r
+ cache.setPosition(x, y);\r
+ break;\r
+ }\r
cache.draw(batch);\r
}\r
\r
@Override public Actor hit (float x, float y) {\r
return x > 0 && y > 0 && x < width && y < height ? this : null;\r
}\r
+\r
+ public void layout () {\r
+ if (wrapType == WrapType.wrapped && lastWidth != width) setWrappedText(text, halignment);\r
+ lastWidth = width;\r
+ }\r
+\r
+ public float getPrefWidth () {\r
+ switch (wrapType) {\r
+ case singleLine:\r
+ return cache.getFont().getBounds(text).width;\r
+ case multiLine:\r
+ return cache.getFont().getMultiLineBounds(text).width;\r
+ case wrapped:\r
+ }\r
+ return 0;\r
+ }\r
+\r
+ public float getPrefHeight () {\r
+ switch (wrapType) {\r
+ case singleLine:\r
+ return cache.getFont().getBounds(text).height;\r
+ case multiLine:\r
+ return cache.getFont().getMultiLineBounds(text).width;\r
+ case wrapped:\r
+ }\r
+ return 0;\r
+ }\r
+\r
+ static public enum VAlignment {\r
+ TOP, CENTER, BOTTOM\r
+ }\r
+\r
+ static private enum WrapType {\r
+ singleLine, multiLine, wrapped\r
+ }\r
}\r