SanAndreasUnity/Assets/Scripts/UI/MainMenu.cs

114 lines
2.6 KiB
C#
Raw Normal View History

2020-05-31 17:07:22 +00:00
using System.Collections.Generic;
using UnityEngine;
using SanAndreasUnity.Behaviours;
using UnityEngine.SceneManagement;
using SanAndreasUnity.Utilities;
namespace SanAndreasUnity.UI
{
public class MainMenu : MonoBehaviour {
public static MainMenu Instance { get; private set; }
2020-05-31 17:07:22 +00:00
public float minButtonHeight = 25f;
public float minButtonWidth = 70f;
public float spaceAtBottom = 15f;
public float spaceBetweenButtons = 5f;
public Color openedWindowTextColor = Color.green;
2020-05-31 17:07:22 +00:00
public bool drawBackground = false;
public Color backgroundColor = Color.black;
public bool drawLogo = false;
private static GUILayoutOption[] s_buttonOptions = new GUILayoutOption[0];
public static GUILayoutOption[] ButtonLayoutOptions { get { return s_buttonOptions; } }
2019-07-09 15:22:07 +00:00
static MenuEntry s_rootMenuEntry = new MenuEntry();
2020-05-31 17:07:22 +00:00
void Awake()
2020-05-31 17:07:22 +00:00
{
if (null == Instance)
Instance = this;
2020-05-31 17:07:22 +00:00
}
void OnGUI ()
{
if (!GameManager.IsInStartupScene)
return;
// draw main menu gui
// background
if (this.drawBackground)
{
GUIUtils.DrawRect (GUIUtils.ScreenRect, this.backgroundColor);
}
// logo
if (this.drawLogo)
{
if (GameManager.Instance.logoTexture != null)
{
GUI.DrawTexture (GUIUtils.GetCenteredRect (GameManager.Instance.logoTexture.GetSize ()), GameManager.Instance.logoTexture);
}
}
// draw menu entries at bottom of screen
2020-05-31 17:07:22 +00:00
s_buttonOptions = new GUILayoutOption[]{ GUILayout.MinWidth(minButtonWidth), GUILayout.MinHeight(minButtonHeight) };
GUILayout.BeginArea (new Rect (0f, Screen.height - (minButtonHeight + spaceAtBottom), Screen.width, minButtonHeight + spaceAtBottom));
// GUILayout.Space (5);
// GUILayout.FlexibleSpace ();
GUILayout.BeginHorizontal ();
GUILayout.Space (5);
GUILayout.FlexibleSpace ();
// draw registered menu items
2019-07-09 15:22:07 +00:00
foreach (var item in s_rootMenuEntry.children)
2020-05-31 17:07:22 +00:00
{
2019-07-09 15:22:07 +00:00
if (item.drawAction != null)
item.drawAction();
2020-05-31 17:07:22 +00:00
GUILayout.Space (this.spaceBetweenButtons);
}
if (MainMenu.DrawMenuEntry ("Exit"))
2020-05-31 17:07:22 +00:00
{
GameManager.ExitApplication ();
}
GUILayout.FlexibleSpace ();
GUILayout.Space (5);
GUILayout.EndHorizontal ();
// add some space below buttons
// GUILayout.Space (spaceAtBottom);
GUILayout.EndArea ();
}
public static bool DrawMenuEntry(string text)
{
return GUIUtils.ButtonWithCalculatedSize(text);
}
2019-07-09 15:22:46 +00:00
public static void RegisterMenuEntry (MenuEntry menuEntry)
2020-05-31 17:07:22 +00:00
{
2019-07-09 15:22:07 +00:00
s_rootMenuEntry.AddChild (menuEntry);
2020-05-31 17:07:22 +00:00
}
}
}