package com.ezardlabs.lostsector.objects.menus;

import com.ezardlabs.dethsquare.GameObject;
import com.ezardlabs.dethsquare.GuiText;
import com.ezardlabs.dethsquare.Input;
import com.ezardlabs.dethsquare.Input.KeyCode;
import com.ezardlabs.dethsquare.Renderer;
import com.ezardlabs.dethsquare.Script;
import com.ezardlabs.dethsquare.TextureAtlas;
import com.ezardlabs.dethsquare.Vector2;

public class Menu extends Script {
	private final String[] options;
	private final MenuAction[] actions;

	private TextureAtlas font;

	private GameObject gameObject;
	private GameObject[] texts;
	private GuiText[] guiTexts;

	public Menu(String[] options, MenuAction[] actions) {
		this.options = options;
		this.actions = actions;

		if (options.length < 1 || options.length > 4) {
			throw new Error("You must supply between 1 and 4 options to the menu");
		}
		font = new TextureAtlas("fonts/atlas.png", "fonts/atlas.txt");
	}

	@Override
	public void update() {
		if (guiTexts != null && Input.getKeyDown(KeyCode.MOUSE_LEFT)) {
			System.out.println(guiTexts.length);
			for (int i = 0; i < guiTexts.length; i++) {
				if (guiTexts[i].hitTest(Input.mousePosition)) {
					actions[i].onMenuItemSelected(this, i, options[i]);
					return;
				}
			}
		}
	}

	public void open() {
		float height = (2 * options.length + 1) * 50;
		gameObject = new GameObject("Menu",
				new Renderer("images/menus/main/menu" + options.length + ".png", 400, height));
		GameObject.instantiate(gameObject,
				new Vector2(1920 / 2 - 200, 1080 / 2 - ((2 * options.length + 1) * 50) / 2));
		texts = new GameObject[options.length];
		guiTexts = new GuiText[options.length];
		for (int i = 0; i < texts.length; i++) {
			guiTexts[i] = new GuiText(options[i], font, 50);
			texts[i] = GameObject.instantiate(new GameObject("Menu Option", guiTexts[i]),
					new Vector2(1920 / 2f - guiTexts[i].getWidth() / 2f,
							1080 / 2 - height / 2 + (1 + i * 2) * 50));
		}
	}

	public void close() {
		for (int i = 0; i < texts.length; i++) {
			GameObject.destroy(texts[i]);
		}
		GameObject.destroy(gameObject);

		gameObject = null;
		texts = null;
		guiTexts = null;
	}

	public static abstract class MenuAction {
		public abstract void onMenuItemSelected(Menu menu, int index, String text);
	}
}

@@ -487,16 +487,47 @@
 actions;
+%0A%09private final Vector2 offset;
 %0A%0A%09priva
@@ -695,16 +695,132 @@
 ions) %7B%0A
+%09%09this(options, actions, new Vector2());%0A%09%7D%0A%0A%09public Menu(String%5B%5D options, MenuAction%5B%5D actions, Vector2 offset) %7B%0A
 %09%09this.o
@@ -862,16 +862,40 @@
 actions;
+%0A%09%09this.offset = offset;
 %0A%0A%09%09if (
@@ -1643,21 +1643,17 @@
 eObject,
-%0A%09%09%09%09
+ 
 new Vect
@@ -1670,18 +1670,33 @@
  2 - 200
-,
  
++ offset.x,%0A%09%09%09%09
 1080 / 2
@@ -1733,16 +1733,27 @@
  50) / 2
+ + offset.y
 ));%0A%09%09te
@@ -2064,16 +2064,27 @@
 h() / 2f
+ + offset.x
 ,%0A%09%09%09%09%09%09
@@ -2124,16 +2124,27 @@
  2) * 50
+ + offset.y
 ));%0A%09%09%7D%0A

Add option to offset menu from the centre of the screen