WKLib 0.2.3
A modding library for White Knuckle
Loading...
Searching...
No Matches
RootPanel.cs
Go to the documentation of this file.
1using Imui.Controls;
2using Imui.Core;
3using Imui.IO.UGUI;
4using ImuiBepInEx.API;
5using UnityEngine;
6using UnityEngine.SceneManagement;
7using WKLib.API;
12using static WKLib.Core.Config.ConfigManager;
13
14namespace WKLib.Core.UI;
15
16// Right after InputSystem executes
17[DefaultExecutionOrder(-999)]
18internal class RootPanel : MonoSingleton<RootPanel>
19{
20 public ImGui gui = null;
21 public ImuiPanel ImuiPanel = null;
22
23 public ThemeController ThemeController = null;
24 public OverlayState OverlayState = null;
25
26 public bool IsOpen
27 {
28 get => OverlayState.IsOpen;
29 set => OverlayState.IsOpen = value;
30 }
31
32 private bool isDemoOpen = false;
33
34 public override void OnEnable()
35 {
36 void OnSceneChange(Scene scene, LoadSceneMode loadSceneMode)
37 {
38 if (AutoCloseOverlay.Value)
39 IsOpen = false;
40 }
41
42 base.OnEnable();
43
44 SceneManager.sceneLoaded -= OnSceneChange;
45 SceneManager.sceneLoaded += OnSceneChange;
46
47 ImuiPanel.Canvas.hideFlags = HideFlags.HideAndDontSave;
48 DontDestroyOnLoad(ImuiPanel.Canvas);
49
50 var backend = transform.GetComponent<ImuiUnityGUIBackend>();
51 if (gui == null)
52 gui = new ImGui(backend, backend);
53
54 OverlayState = gameObject.GetComponent<OverlayState>();
55 if (OverlayState == null)
56 OverlayState = gameObject.AddComponent<OverlayState>();
57
58 ThemeController = gameObject.GetComponent<ThemeController>();
59 if (ThemeController == null)
60 ThemeController = gameObject.AddComponent<ThemeController>();
61
62 ThemeController.SetTheme(gui);
63 ModListWindow.Initialize();
64 }
65
66 private void Update()
67 {
68 ThemeController.DetectChanges(gui);
69
70 gui.BeginFrame();
71 if (InputUtility.GetKeyDown(OverlayKey.Value))
72 {
73 IsOpen = !IsOpen;
74 }
75
76 HandleAPIInput();
77
78 // Draw overlay warnings (like not being able to open)
79 OverlayState.Draw(gui);
80
81 if (IsOpen)
82 {
83 DrawRootMenuBar();
84 }
85
86 ChangeLogWindow.Draw(gui, IsOpen);
87 if (EnableDemoWindow.Value)
88 {
89 DemoWindow.Draw(gui, ref isDemoOpen);
90 }
91 ModListWindow.Draw(gui, IsOpen);
92
93 DrawAPIWindows();
94
95 gui.EndFrame();
96 gui.Render();
97 }
98
99 private void DrawRootMenuBar()
100 {
101 gui.BeginMenuBar();
102
103 if (gui.BeginMenu("General"))
104 {
105 gui.Menu("Open mod list", ref ModListWindow.isOpen);
106
107 if (EnableDemoWindow.Value)
108 gui.Menu("Open demo menu", ref isDemoOpen);
109
110 gui.Separator();
111
112 gui.Menu("Open changelog", ref ChangeLogWindow.isOpen);
113
114 gui.Separator();
115
116 if (gui.Menu("Close menu"))
117 {
118 IsOpen = false;
119 }
120
121 gui.EndMenu();
122 }
123
124 if (gui.BeginMenu("Windows"))
125 {
126 if (gui.Menu("Close all windows"))
127 {
128 ModListWindow.isOpen = false;
129 ModListWindow.CloseConfigWindows();
130
131 CloseAPIWindows();
132 }
133
134 gui.EndMenu();
135 }
136
137 gui.EndMenuBar();
138 }
139
140 private void DrawAPIWindows()
141 {
142 foreach (var API in WKLibAPI.internalAPIs)
143 {
144 if (API == null)
145 continue;
146
147 foreach (var window in API.Windows)
148 {
149 if (window == null)
150 continue;
151
152 window.Draw(gui, IsOpen);
153 }
154 }
155 }
156
157 private void HandleAPIInput()
158 {
159 foreach (var API in WKLibAPI.internalAPIs)
160 {
161 if (API == null)
162 continue;
163
164 foreach (var window in API.Windows)
165 {
166 if (window == null)
167 continue;
168
169 window.HandleInput(gui);
170 }
171 }
172 }
173
174 private void CloseAPIWindows()
175 {
176 foreach (var API in WKLibAPI.internalAPIs)
177 {
178 if (API == null)
179 continue;
180
181 foreach (var window in API.Windows)
182 {
183 if (window == null)
184 continue;
185
186 window.isOpen = false;
187 }
188 }
189 }
190}
static bool GetKeyDown(KeyCode key)