
var guiSkin : GUISkin;
var avatar : Texture2D;
var avatarRing : Texture2D;
var nativeVerticalResolution = 1200.0;
var player : Transform;
var spawnableObject : GameObject;

private var showingPopup = false;

function Start()
{
	if (!guiSkin)
	{
		print("HUD: No GUISkin selected, disabling script");
		enabled = false;
	}
}

function OnGUI() 
{
	GUI.skin = guiSkin;
	
	// Make the GUI independent of screen size
	GUI.matrix = Matrix4x4.TRS(
		Vector3.zero, 
		Quaternion.identity, 
		Vector3(Screen.height/nativeVerticalResolution, Screen.height/nativeVerticalResolution, 1));
		
	// Avatar
	DrawImageTopAligned(Vector2(10, 10), avatar);
	DrawImageTopAligned(Vector2(10, 10), avatarRing);
	
	// Player position
	if (player)
	{
		var string = "pos: (" + player.position.x + ", " + player.position.y + ", " + player.position.z + ")";
		var labelX = avatarRing.width + 40;
		GUI.Label(Rect(labelX, 10, 400, 40), string);
	}
	
	var yStart = 300;
	var buttonSpacing = 120;
	var y = yStart;
	
	// If showing the popup menu, disable the other buttons
	if (showingPopup)
		GUI.enabled = false;
		
	// Buttons
	if (GUI.Button(Rect(10, y, 100, 100), "Spawn\nObject"))
	{
		if (spawnableObject)
		{
			GameObject.Instantiate(spawnableObject, player.position, Quaternion());
		}
	}
	
	y += buttonSpacing;
	if (GUI.Button(Rect(10, y, 100, 100), "Spawn\nMenu"))
	{
		showingPopup = true;
	}
	
	y += buttonSpacing;
	if (GUI.Button(Rect(10, y, 100, 100), "Button 3"))
	{
	}
	
	y += buttonSpacing;
	if (GUI.Button(Rect(10, y, 100, 100), "Button 4"))
	{
	}
	
	y += buttonSpacing;
	if (GUI.Button(Rect(10, y, 100, 100), "Button 5"))
	{
	}
	
	// Show popup menu
	if (showingPopup)
	{
		GUI.enabled = true;
		
		var winRect = Rect(120, yStart + (buttonSpacing*1), 200, 100);
		GUILayout.Window(0, winRect, DrawPopupMenu, "Popup Menu");
	}
}

function DrawPopupMenu(winId : int)
{
	if (GUILayout.Button("Cancel"))
	{
		showingPopup = false;
	}
	
	if (GUILayout.Button("Quit"))
	{
		print("Quit pressed");
		Application.Quit();
	}
}

function DrawImageTopAligned (pos : Vector2, image : Texture2D)
{
	var rect = Rect(
		pos.x, 
		pos.y, 
		image.width, 
		image.height);
		
	GUI.Label(rect, image);
}

// Make the script also execute in edit mode
@script ExecuteInEditMode()