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 // Check if enter is down
96 if (InputUtility.GetKeyDown(KeyCode.Return))
97 {
98 gui.ResetActiveControl();
99 }
100
101 gui.EndFrame();
102 gui.Render();
103 }
104
105 private void DrawRootMenuBar()
106 {
107 gui.BeginMenuBar();
108
109 if (gui.BeginMenu("General"))
110 {
111 gui.Menu("Open mod list", ref ModListWindow.isOpen);
112
113 if (EnableDemoWindow.Value)
114 gui.Menu("Open demo menu", ref isDemoOpen);
115
116 gui.Separator();
117
118 gui.Menu("Open changelog", ref ChangeLogWindow.isOpen);
119
120 gui.Separator();
121
122 if (gui.Menu("Close menu"))
123 {
124 IsOpen = false;
125 }
126
127 gui.EndMenu();
128 }
129
130 if (gui.BeginMenu("Windows"))
131 {
132 if (gui.Menu("Close all windows"))
133 {
134 ModListWindow.isOpen = false;
135 ModListWindow.CloseConfigWindows();
136
137 CloseAPIWindows();
138 }
139
140 gui.EndMenu();
141 }
142
143 gui.EndMenuBar();
144 }
145
146 private void DrawAPIWindows()
147 {
148 foreach (var API in WKLibAPI.internalAPIs)
149 {
150 if (API == null)
151 continue;
152
153 foreach (var window in API.Windows)
154 {
155 if (window == null)
156 continue;
157
158 window.Draw(gui, IsOpen);
159 }
160 }
161 }
162
163 private void HandleAPIInput()
164 {
165 foreach (var API in WKLibAPI.internalAPIs)
166 {
167 if (API == null)
168 continue;
169
170 foreach (var window in API.Windows)
171 {
172 if (window == null)
173 continue;
174
175 window.HandleInput(gui);
176 }
177 }
178 }
179
180 private void CloseAPIWindows()
181 {
182 foreach (var API in WKLibAPI.internalAPIs)
183 {
184 if (API == null)
185 continue;
186
187 foreach (var window in API.Windows)
188 {
189 if (window == null)
190 continue;
191
192 window.isOpen = false;
193 }
194 }
195 }
196}
static bool GetKeyDown(KeyCode key)
static void Draw(ImGui gui, ref bool open)
Definition DemoWindow.cs:91