package org.egordorichev.lasttry.ui;

import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.GlyphLayout;
import com.badlogic.gdx.math.Rectangle;
import org.egordorichev.lasttry.LastTry;
import org.egordorichev.lasttry.graphics.Fonts;

public class UiTextButton extends UiComponent {
	/** Text, to be drawn on the button */
	protected String label;

	/** Label width in pixels */
	protected int labelWidth;

	/** Current font */
	private BitmapFont font;

	public UiTextButton(Rectangle rectangle, Origin origin, String label) {
		super(rectangle, origin);

		this.label = label;

		this.setFont(Fonts.f22);
	}

	public UiTextButton(Rectangle rectangle, String label) {
		this(rectangle, Origin.TOP_LEFT, label);
	}

	/** Renders the element */
	@Override
	public void render() {
		if (this.hidden) {
			return;
		}

		super.render();

		this.font.draw(LastTry.batch, this.label, this.getX(),
			this.getY() + (this.getHeight() - this.font.getLineHeight()) / 2);
	}

	/** Handles state change */
	@Override
	protected void onStateChange() {
		if (this.state == State.MOUSE_IN) {
			this.setFont(Fonts.f24);
		} else if (this.state == State.NORMAL) {
			this.setFont(Fonts.f22);
		}
	}

	/** Sets font and calculates new size */
	private void setFont(BitmapFont font) {
		this.font = font;

		GlyphLayout glyphLayout = new GlyphLayout();
		glyphLayout.setText(this.font, this.label);

		this.labelWidth = (int) glyphLayout.width;
		this.rect.width = this.labelWidth;
		this.rect.height = this.font.getLineHeight() * 1.f;
	}
}
@@ -300,191 +300,19 @@
 s Ui
-Component %7B%0A%09/** Text, to be drawn on the button */%0A%09protected String label;%0A%0A%09/** Label width in pixels */%0A%09protected int labelWidth;%0A%0A%09/** Current font */%0A%09private BitmapFont font;%0A
+TextLabel %7B
 %0A%09pu
@@ -410,59 +410,15 @@
 igin
-);%0A%0A%09%09this.label = label;%0A%0A%09%09this.setFont(Fonts.f22
+, label
 );%0A%09
@@ -528,812 +528,226 @@
 %0A%09%7D%0A
-%0A%09/** Renders the element */%0A%09@Override%0A%09public void render() %7B%0A%09%09if (this.hidden) %7B%0A%09%09%09return;%0A%09%09%7D%0A%0A%09%09super.render();%0A%0A%09%09this.font.draw(LastTry.batch, this.label, this.getX(),%0A%09%09%09this.getY() + (this.getHeight() - this.font.getLineHeight()) / 2);%0A%09%7D%0A%0A%09/** Handles state change */%0A%09@Override%0A%09protected void onStateChange() %7B%0A%09%09if (this.state == State.MOUSE_IN) %7B%0A%09%09%09this.setFont(Fonts.f24);%0A%09%09%7D else if (this.state == State.NORMAL) %7B%0A%09%09%09this.setFont(Fonts.f22);%0A%09%09%7D%0A%09%7D%0A%0A%09/** Sets font and calculates new size */%0A%09private void setFont(BitmapFont font) %7B%0A%09%09this.font = font;%0A%0A%09%09GlyphLayout glyphLayout = new GlyphLayout();%0A%09%09glyphLayout.setText(this.font, this.label);%0A%0A%09%09this.labelWidth = (int) glyphLayout.width;%0A%09%09this.rect.width = this.labelWidth;%0A%09%09this.rect.height = this.font.getLineHeight() * 1.f;%0A%09
+%09%0A%09/** Handles state change */%0A%09@Override%0A%09protected void onStateChange() %7B%0A%09%09if (this.state == State.MOUSE_IN) %7B%0A%09%09%09this.setFont(Fonts.f24);%0A%09%09%7D else if (this.state == State.NORMAL) %7B%0A%09%09%09this.setFont(Fonts.f22);%0A%09%09%7D%0A%09%7D%0A
 %7D%0A
-%7D

Improve UiTextButton