package com.ezardlabs.dethsquare;

import com.ezardlabs.dethsquare.TextureAtlas.Sprite;

public class GuiText extends Component {
	private String text;
	private TextureAtlas font;
	private float fontSize;
	private float spaceWidth;
	private GameObject[] characters = new GameObject[0];

	public GuiText(String text, TextureAtlas font, float fontSize) {
		this.text = text;
		this.font = font;
		this.fontSize = fontSize;
	}

	@Override
	public void start() {
		calculateSpaceWidth();
		generateRenderers();
	}

	public String getText() {
		return text;
	}

	public void setText(String text) {
		this.text = text;
		generateRenderers();
	}

	public TextureAtlas getFont() {
		return font;
	}

	public void setFont(TextureAtlas font) {
		this.font = font;
		calculateSpaceWidth();
		generateRenderers();
	}

	public float getFontSize() {
		return fontSize;
	}

	public void setFontSize(float fontSize) {
		this.fontSize = fontSize;
		calculateSpaceWidth();
		generateRenderers();
	}

	private void generateRenderers() {
		if (text == null) return;

		for (GameObject go : characters) {
			GameObject.destroy(go);
		}

		characters = new GameObject[text.length()];

		Sprite s;
		float xOffset = 0;
		for (int i = 0; i < text.length(); i++) {
			switch(text.charAt(i)) {
				case ' ':
					xOffset += spaceWidth;
					continue;
				case '!':
					s = font.getSprite("exclamation-mark");
					break;
				case '?':
					s = font.getSprite("question-mark");
					break;
				case '.':
					s = font.getSprite("period");
					break;
				default:
					s = font.getSprite(String.valueOf(text.charAt(i)));
					break;
			}
			if (s == null) continue;

			float width = (s.w / s.h) * fontSize;

			characters[i] = new GameObject(null, new GuiRenderer(font, s, width, fontSize));
			GameObject.instantiate(characters[i], new Vector2(transform.position.x + xOffset, transform.position.y));

			xOffset += width + Screen.scale * 6.25f;
		}
	}

	private void calculateSpaceWidth() {
		Sprite[] chars = font.getSprites();
		float total = 0;
		for (Sprite s : chars) {
			total += (s.w / s.h) * fontSize;
		}
		spaceWidth = total / chars.length;
	}
}

@@ -111,16 +111,23 @@
 extends 
+Bounded
 Componen
@@ -1249,16 +1249,17 @@
 %09%09switch
+ 
 (text.ch
@@ -1808,17 +1808,22 @@
 ters%5Bi%5D,
- 
+%0A%09%09%09%09%09
 new Vect
@@ -1930,16 +1930,144 @@
 25f;%0A%09%09%7D
+%0A%0A%09%09bounds.set(transform.position.x, transform.position.y, transform.position.x + xOffset,%0A%09%09%09%09transform.position.y + fontSize);
 %0A%09%7D%0A%0A%09pr
@@ -2264,11 +2264,298 @@
 ngth;%0A%09%7D
+%0A%0A%09public boolean hitTest(float x, float y) %7B%0A%09%09return x %3E bounds.left * Screen.scale && x %3C bounds.right * Screen.scale &&%0A%09%09%09%09y %3E bounds.top * Screen.scale && y %3C bounds.bottom * Screen.scale;%0A%09%7D%0A%0A%09public boolean hitTest(Vector2 position) %7B%0A%09%09return hitTest(position.x, position.y);%0A%09%7D
 %0A%7D%0A

Add hitTest method to GuiText