package com.ezardlabs.dethsquare;

import com.ezardlabs.dethsquare.TextureAtlas.Sprite;

public class GuiText extends BoundedComponent {
	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;
		}

		bounds.set(transform.position.x, transform.position.y, transform.position.x + xOffset,
				transform.position.y + fontSize);
	}

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

	public boolean hitTest(float x, float y) {
		return x > bounds.left * Screen.scale && x < bounds.right * Screen.scale &&
				y > bounds.top * Screen.scale && y < bounds.bottom * Screen.scale;
	}

	public boolean hitTest(Vector2 position) {
		return hitTest(position.x, position.y);
	}
}

@@ -234,22 +234,21 @@
 idth;%0A%09p
-rivate
+ublic
  GameObj
@@ -1037,22 +1037,25 @@
 = null) 
-return
+text = %22%22
 ;%0A%0A%09%09for
@@ -1086,16 +1086,37 @@
 ters) %7B%0A
+%09%09%09if(go != null) %7B%0A%09
 %09%09%09GameO
@@ -1134,16 +1134,21 @@
 oy(go);%0A
+%09%09%09%7D%0A
 %09%09%7D%0A%0A%09%09c
@@ -1550,24 +1550,84 @@
 %09%09%09%09%09break;%0A
+%09%09%09%09case ':':%0A%09%09%09%09%09s = font.getSprite(%22colon%22);%0A%09%09%09%09%09break;%0A
 %09%09%09%09default:

Add support for colons