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;
|
2019-04-18 00:05:37 +00:00
|
|
|
|
|
2020-05-31 17:07:22 +00:00
|
|
|
|
m_isOpened = value;
|
2019-04-18 00:05:37 +00:00
|
|
|
|
|
2020-04-18 22:41:09 +00:00
|
|
|
|
if (this.IsRegisteredInMainMenu)
|
|
|
|
|
MainMenu.SetEntryColor(m_mainMenuEntry, m_isOpened ? MainMenu.Instance.openedWindowTextColor : MainMenu.Instance.ClosedWindowTextColor);
|
|
|
|
|
|
2020-05-31 17:07:22 +00:00
|
|
|
|
if (m_isOpened)
|
2019-04-18 00:05:37 +00:00
|
|
|
|
{
|
2020-05-31 17:07:22 +00:00
|
|
|
|
this.OnWindowOpened ();
|
2019-04-18 00:05:37 +00:00
|
|
|
|
}
|
2020-05-31 17:07:22 +00:00
|
|
|
|
else
|
2019-04-18 00:05:37 +00:00
|
|
|
|
{
|
|
|
|
|
if (this.DestroyOnClose)
|
|
|
|
|
Destroy(this);
|
2020-05-31 17:07:22 +00:00
|
|
|
|
this.OnWindowClosed ();
|
2019-04-18 00:05:37 +00:00
|
|
|
|
}
|
2020-05-31 17:07:22 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-30 15:14:44 +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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-18 00:05:37 +00:00
|
|
|
|
[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;
|
2019-07-28 19:34:11 +00:00
|
|
|
|
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; } }
|
|
|
|
|
|
|
|
|
|
[SerializeField] private bool m_registerInMainMenuOnStart = false;
|
|
|
|
|
public bool IsRegisteredInMainMenu { get; private set; }
|
2020-04-18 22:41:09 +00:00
|
|
|
|
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
|
|
|
|
|
2019-04-18 00:05:37 +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
|
|
|
|
|
2019-04-18 00:05:37 +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()
|
|
|
|
|
{
|
2019-08-30 16:17:08 +00:00
|
|
|
|
Behaviours.UIManager.onGUI += this.OnGUICustom;
|
2019-08-30 15:50:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected virtual void OnDisable()
|
|
|
|
|
{
|
2019-08-30 16:17:08 +00:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2019-08-30 16:17:08 +00:00
|
|
|
|
void OnGUICustom() {
|
2020-05-31 17:07:22 +00:00
|
|
|
|
|
2019-07-28 19:34:11 +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 ();
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-30 15:14:44 +00:00
|
|
|
|
|
|
|
|
|
if (!this.ShouldBeDrawn)
|
|
|
|
|
{
|
2020-05-31 17:07:22 +00:00
|
|
|
|
return;
|
2019-08-30 15:14:44 +00:00
|
|
|
|
}
|
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)
|
2019-07-28 19:34:11 +00:00
|
|
|
|
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() {
|
|
|
|
|
|
|
|
|
|
PauseMenu.onDrawItems += this.OnPauseMenuGUI;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void UnRegisterButtonInPauseMenu() {
|
|
|
|
|
|
|
|
|
|
PauseMenu.onDrawItems -= this.OnPauseMenuGUI;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnPauseMenuGUI() {
|
|
|
|
|
|
|
|
|
|
// display button for opening/closing window
|
|
|
|
|
|
2019-07-09 19:41:58 +00:00
|
|
|
|
var originalColor = GUI.contentColor;
|
|
|
|
|
if (this.IsOpened)
|
|
|
|
|
GUI.contentColor = PauseMenu.Instance.openedWindowTextColor;
|
|
|
|
|
|
2020-05-31 17:07:22 +00:00
|
|
|
|
// string text = this.IsOpened ? "Hide " + this.windowName : "Show " + this.windowName;
|
|
|
|
|
string text = this.windowName;
|
|
|
|
|
|
|
|
|
|
if (GUILayout.Button (text)) {
|
|
|
|
|
this.IsOpened = ! this.IsOpened;
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-09 19:41:58 +00:00
|
|
|
|
GUI.contentColor = originalColor;
|
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
|
|
|
|
|
2020-04-18 22:41:09 +00:00
|
|
|
|
m_mainMenuEntry = new MenuEntry(){ name = this.windowName, sortPriority = m_sortPriorityForMainMenu,
|
2020-04-19 01:21:07 +00:00
|
|
|
|
clickAction = this.OnButtonClickedInMainMenu };
|
2020-04-18 22:41:09 +00:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|