SanAndreasUnity/Assets/Scripts/UI/PauseMenuWindow.cs

303 lines
7.2 KiB
C#
Raw Normal View History

2020-05-31 17:07:22 +00:00
using System.Collections.Generic;
using UnityEngine;
namespace SanAndreasUnity.UI {
public class PauseMenuWindow : MonoBehaviour {
public string windowName = "";
[SerializeField] private bool m_isOpenedByDefaultInMainMenu = false;
[SerializeField] private bool m_isOpenedByDefaultInPauseMenu = false;
private bool m_isOpened = false;
public bool IsOpened {
get { return this.m_isOpened; }
set {
if (m_isOpened == value)
return;
2020-05-31 17:07:22 +00:00
m_isOpened = value;
if (this.IsRegisteredInMainMenu)
MainMenu.SetEntryColor(m_mainMenuEntry, m_isOpened ? MainMenu.Instance.openedWindowTextColor : MainMenu.Instance.ClosedWindowTextColor);
if (m_pauseMenuEntry != null)
PauseMenu.Instance.menuBar.SetEntryColor(m_pauseMenuEntry, m_isOpened ? PauseMenu.Instance.openedWindowTextColor : PauseMenu.Instance.ClosedWindowTextColor);
2020-05-31 17:07:22 +00:00
if (m_isOpened)
{
2020-05-31 17:07:22 +00:00
this.OnWindowOpened ();
}
2020-05-31 17:07:22 +00:00
else
{
if (this.DestroyOnClose)
Destroy(this);
2020-05-31 17:07:22 +00:00
this.OnWindowClosed ();
}
2020-05-31 17:07:22 +00:00
}
}
private bool ShouldBeDrawn {
get {
if (Behaviours.Loader.IsLoading)
return false;
if (!this.IsOpened)
return false;
if (!PauseMenu.IsOpened && !Behaviours.GameManager.IsInStartupScene)
return false;
return true;
}
}
[SerializeField] private bool m_destroyOnClose = false;
public bool DestroyOnClose { get { return m_destroyOnClose; } set { m_destroyOnClose = value; } }
2020-05-31 17:07:22 +00:00
private static int lastWindowId = 1352345;
private int windowId = lastWindowId++;
public int WindowId { get { return this.windowId; } }
public Rect windowRect = Utilities.GUIUtils.GetCenteredRectPerc(new Vector2(0.5f, 0.5f));
public Vector2 WindowSize { get { return this.windowRect.size; } }
public bool useScrollView = false;
protected Vector2 scrollPos = Vector2.zero;
protected GUIStyle m_scrollViewStyle = null;
2020-05-31 17:07:22 +00:00
protected bool isDraggable = true;
public bool IsDraggable { get { return this.isDraggable; } }
protected bool isModal = false;
public bool IsModal { get { return this.isModal; } }
protected bool m_hasExitButton = true;
protected bool m_hasMinimizeButton = true;
private bool m_isMinimized = false;
public bool IsMinimized { get { return this.m_isMinimized; } set { m_isMinimized = value; } }
public const float kMinimizedWindowHeight = 45;
private bool m_hasStarted = false;
[SerializeField] private float m_spaceBeforeContent = 0f;
public float SpaceBeforeContent { get { return m_spaceBeforeContent; } set { m_spaceBeforeContent = value; } }
[SerializeField] private float m_spaceAfterContent = 0f;
public float SpaceAfterContent { get { return m_spaceAfterContent; } set { m_spaceAfterContent = value; } }
private Utilities.MenuBarEntry m_pauseMenuEntry;
2020-05-31 17:07:22 +00:00
[SerializeField] private bool m_registerInMainMenuOnStart = false;
public bool IsRegisteredInMainMenu { get; private set; }
private MenuEntry m_mainMenuEntry;
2019-07-09 15:22:07 +00:00
[SerializeField] int m_sortPriorityForMainMenu = 0;
2020-05-31 17:07:22 +00:00
private static GameObject s_windowsContainer;
public static T Create<T>() where T : PauseMenuWindow
{
if (null == s_windowsContainer)
{
s_windowsContainer = new GameObject("Windows");
DontDestroyOnLoad( s_windowsContainer );
}
2020-05-31 17:07:22 +00:00
T window = s_windowsContainer.AddComponent<T>();
return window;
}
2020-05-31 17:07:22 +00:00
2019-04-24 22:43:49 +00:00
public void DestroyWindow()
{
Destroy(this);
}
2019-08-30 15:50:59 +00:00
protected virtual void Awake()
{
}
protected virtual void OnEnable()
{
Behaviours.UIManager.onGUI += this.OnGUICustom;
2019-08-30 15:50:59 +00:00
}
protected virtual void OnDisable()
{
Behaviours.UIManager.onGUI -= this.OnGUICustom;
2019-08-30 15:50:59 +00:00
}
2020-05-31 17:07:22 +00:00
void WindowStart() {
if (m_registerInMainMenuOnStart)
this.RegisterInMainMenu ();
if (m_isOpenedByDefaultInMainMenu)
this.IsOpened = true;
this.OnWindowStart ();
}
/// <summary>
/// Called on first OnGUI().
/// </summary>
protected virtual void OnWindowStart() {
}
protected virtual void OnWindowOpened() {
}
protected virtual void OnWindowClosed() {
}
protected virtual void OnLoaderFinished ()
{
if (m_isOpenedByDefaultInPauseMenu)
{
this.IsOpened = true;
}
}
void OnGUICustom() {
2020-05-31 17:07:22 +00:00
if (null == m_scrollViewStyle)
m_scrollViewStyle = GUI.skin.scrollView;
2020-05-31 17:07:22 +00:00
if (!m_hasStarted) {
m_hasStarted = true;
this.WindowStart ();
}
if (!this.ShouldBeDrawn)
{
2020-05-31 17:07:22 +00:00
return;
}
2020-05-31 17:07:22 +00:00
Rect newRect;
Rect inputRect = this.windowRect;
if(this.IsMinimized)
inputRect.height = kMinimizedWindowHeight;
if (this.isModal)
newRect = GUI.ModalWindow (this.windowId, inputRect, WindowFunction, this.windowName);
else
newRect = GUI.Window( this.windowId, inputRect, WindowFunction, this.windowName );
if (this.IsMinimized)
this.windowRect.position = newRect.position; // only copy position
else
this.windowRect = newRect;
}
void WindowFunction( int id ) {
float buttonWidth = 16;
float buttonHeight = 16;
float buttonYOffset = 2;
// exit button
if (m_hasExitButton) {
Color exitButtonColor = Color.Lerp (Color.red, Color.white, 0.0f);
// exitButtonColor.a = 0.7f;
if (Utilities.GUIUtils.ButtonWithColor (new Rect (this.windowRect.width - buttonWidth - 2, buttonYOffset, buttonWidth, buttonHeight),
"x", exitButtonColor)) {
this.IsOpened = false;
}
}
// minimize button
if (m_hasMinimizeButton) {
if (GUI.Button (new Rect (this.windowRect.width - buttonWidth - 2 - buttonWidth - 2, buttonYOffset, buttonWidth, buttonHeight), "-")) {
this.IsMinimized = !this.IsMinimized;
}
}
if (!this.IsMinimized) {
// draw contents inside window
if (this.SpaceBeforeContent > 0)
GUILayout.Space (this.SpaceBeforeContent);
this.OnWindowGUIBeforeContent ();
if (this.useScrollView)
this.scrollPos = GUILayout.BeginScrollView (this.scrollPos, m_scrollViewStyle);
2020-05-31 17:07:22 +00:00
this.OnWindowGUI ();
if (this.useScrollView)
GUILayout.EndScrollView ();
if (this.SpaceAfterContent > 0)
GUILayout.Space (this.SpaceAfterContent);
this.OnWindowGUIAfterContent ();
}
if (this.isDraggable)
GUI.DragWindow ();
}
protected virtual void OnWindowGUIBeforeContent() {
}
protected virtual void OnWindowGUI() {
}
protected virtual void OnWindowGUIAfterContent() {
}
public void RegisterButtonInPauseMenu() {
m_pauseMenuEntry = PauseMenu.Instance.menuBar.RegisterMenuEntry(this.windowName, 0,
this.OnButtonClickedInPauseMenu);
2020-05-31 17:07:22 +00:00
}
private void OnButtonClickedInPauseMenu()
{
this.IsOpened = !this.IsOpened;
2020-05-31 17:07:22 +00:00
}
public void RegisterInMainMenu ()
{
if (this.IsRegisteredInMainMenu)
return;
this.IsRegisteredInMainMenu = true;
2019-07-09 15:22:07 +00:00
m_mainMenuEntry = new MenuEntry(){ name = this.windowName, sortPriority = m_sortPriorityForMainMenu,
2020-04-19 01:21:07 +00:00
clickAction = this.OnButtonClickedInMainMenu };
MainMenu.RegisterMenuEntry (m_mainMenuEntry);
2020-05-31 17:07:22 +00:00
}
2020-04-18 21:13:19 +00:00
private void OnButtonClickedInMainMenu()
{
this.IsOpened = !this.IsOpened;
}
2020-05-31 17:07:22 +00:00
}
}