WKLib 0.2.3
A modding library for White Knuckle
Loading...
Searching...
No Matches
OverlayState.cs
Go to the documentation of this file.
1using System.Collections.Generic;
2using Imui.Core;
3using UnityEngine;
4using UnityEngine.UI;
5using WKLib.API.UI;
7
8namespace WKLib.Core.UI;
9
10internal class OverlayState : MonoBehaviour
11{
12 public static List<PopupSettings> Popups = new List<PopupSettings>();
13
14 private Dictionary<GraphicRaycaster, bool> _originalState = new Dictionary<GraphicRaycaster, bool>();
15 private CL_GameManager gameManager = null;
16 private GraphicRaycaster mainGraphicRaycaster = null;
17
18 private bool isOpen = false;
19
20 public bool IsOpen
21 {
22 get => isOpen;
23 set
24 {
25 if (gameManager == null)
26 gameManager = GameObject.Find("GameManager")?.GetComponent<CL_GameManager>();
27
28 var gameIsLoading = gameManager != null && gameManager.loading;
29 var playerIsReviving = gameManager != null && gameManager.reviving;
30
31 if (!gameIsLoading
32 && !playerIsReviving)
33 {
34 if (isOpen == value)
35 return;
36 }
37 else
38 {
39 value = false;
40 }
41
42 isOpen = value;
43
44 var gameIsPaused = gameManager != null && gameManager.isPaused;
45
46 var terminalInUse = OS_Manager.inUse;
47
48 var pauseMenuExists = gameManager != null && gameManager.pauseMenu;
49 var canPause = gameManager != null && gameManager.canPause;
50
51 if (isOpen)
52 {
53 _originalState.Clear();
54
55 if (pauseMenuExists)
56 {
57 if (gameIsPaused)
58 return;
59
60 if (!terminalInUse && canPause
61 && !gameIsLoading && !playerIsReviving)
62 {
63 gameManager?.Pause();
64 }
65 else
66 {
67 // Close and show message
68 isOpen = false;
69
70 PopupSettings newPopup = new PopupSettings();
71
72 if (terminalInUse)
73 {
74 newPopup.PopupText = "Cannot open overlay in terminal.";
75 }
76 else if (!canPause)
77 {
78 newPopup.PopupText = "Cannot open because game cannot be paused.";
79 }
80 else if (gameIsLoading)
81 {
82 newPopup.PopupText = "Cannot open overlay because the game is loading.";
83 }
84 else if (playerIsReviving)
85 {
86 newPopup.PopupText = "Cannot open overlay because the player is reviving.";
87 }
88
89 newPopup.TimeTillClose = Time.realtimeSinceStartup + newPopup.PopupTime;
90
91 Popups.Add(newPopup);
92 }
93 }
94 else if (mainGraphicRaycaster != null)// We are in the main menu or some ui scene
95 {
96 // So disable the ui and override !
97 var graphicRaycasters = FindObjectsOfType<GraphicRaycaster>();
98 foreach (var graphicRaycaster in graphicRaycasters)
99 {
100 if (graphicRaycaster == null)
101 continue;
102
103 _originalState[graphicRaycaster] = graphicRaycaster.IsActive();
104
105 graphicRaycaster.enabled = false;
106 }
107
108 mainGraphicRaycaster.enabled = true;
109 }
110 }
111 else
112 {
113 //gui?.ResetActiveControl();
114
115 if (pauseMenuExists)
116 {
117 if (!gameIsPaused)
118 return;
119
120 if (!terminalInUse && canPause
121 && !gameIsLoading && !playerIsReviving)
122 {
123 gameManager?.UnPause();
124 }
125 }
126 else
127 {
128 foreach(var originalState in _originalState)
129 {
130 if (originalState.Key == null)
131 continue;
132
133 originalState.Key.enabled = true;
134 }
135 }
136 }
137 }
138 }
139
140 private void OnEnable()
141 {
142 mainGraphicRaycaster = gameObject.GetComponent<GraphicRaycaster>();
143 }
144
145 //TODO: Change popup window position
146 public void Draw(ImGui gui)
147 {
148 foreach (var popup in Popups)
149 {
150 if (Time.realtimeSinceStartup < popup.TimeTillClose)
151 {
152 QuickPopupWindow.Draw(gui, popup.PopupText);
153 }
154 }
155 }
156}