-
Notifications
You must be signed in to change notification settings - Fork 0
/
KerbonautManagerPlugin.cs
250 lines (197 loc) · 7.07 KB
/
KerbonautManagerPlugin.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
using System.Collections;
using BepInEx;
using HarmonyLib;
using KSP.Game;
using KSP.Sim.impl;
using KSP.UI;
using KSP.UI.Binding;
using SpaceWarp;
using SpaceWarp.API.Assets;
using SpaceWarp.API.Mods;
using SpaceWarp.API.UI.Appbar;
using UnityEngine;
namespace KerbonautManager
{
[BepInPlugin(ModGuid, ModName, ModVer)]
[BepInDependency(SpaceWarpPlugin.ModGuid, SpaceWarpPlugin.ModVer)]
public class KerbonautManagerPlugin : BaseSpaceWarpPlugin
{
private enum Panel
{
A,
B
}
#region Metadata
public const string ModGuid = "com.munix.KerbonautManager";
public const string ModName = "Kerbonaut Manager";
public const string ModVer = "0.2.1";
#endregion
#region Internal constants
private const string LocationAPath =
"GameManager/Default Game Instance(Clone)/UI Manager(Clone)/Popup Canvas/KerbalManager(Clone)/KSP2UIWindow/Root/UIPanel/GRP-Body/KerbalManager_LocationA/Scroll View/Viewport/LocationAPanels/";
private const string LocationBPath =
"GameManager/Default Game Instance(Clone)/UI Manager(Clone)/Popup Canvas/KerbalManager(Clone)/KSP2UIWindow/Root/UIPanel/GRP-Body/KerbalManager_LocationB/Scroll View/Viewport/LocationBPanels/";
internal const string ToolbarButtonID = "BTN-KerbonautManagerOAB";
#endregion
#region State
private static KerbonautManagerPlugin _instance;
private bool _loaded;
private Dictionary<Panel, GameObject> _kerbalPanels = new() { { Panel.A, null }, { Panel.B, null } };
private readonly Dictionary<Panel, List<IGGuid>> _kerbals = new();
#endregion
#region External objects
private Camera _mainCamera;
#endregion
#region Lifecycle
public override void OnInitialized()
{
base.OnInitialized();
if (_loaded)
{
Destroy(this);
}
_loaded = true;
_instance = this;
_mainCamera = Camera.main;
var harmony = new Harmony(ModGuid);
harmony.PatchAll(typeof(KerbonautManagerPlugin));
harmony.PatchAll(typeof(KerbalsVanishingPatch));
var buttonTexture = AssetManager.GetAsset<Texture2D>($"{SpaceWarpMetadata.ModID}/images/icon.png");
Appbar.RegisterOABAppButton("Kerbonaut Manager", ToolbarButtonID, buttonTexture, isOpen =>
{
if (!isOpen)
{
KerbonautManagerWindow.SetSelectedKerbal(null);
}
GameObject.Find(ToolbarButtonID)?.GetComponent<UIValue_WriteBool_Toggle>()?.SetValue(isOpen);
KerbonautManagerWindow.IsOpen = isOpen;
});
Logger.LogInfo("KerbonautManager is initialized.");
}
private void LateUpdate()
{
if (!Input.GetMouseButtonDown(1) || !IsInVab() || (!_kerbalPanels[Panel.A] && !_kerbalPanels[Panel.B]))
{
return;
}
ProcessPanel(Panel.A);
ProcessPanel(Panel.B);
}
private void ProcessPanel(Panel panel)
{
if (!_kerbalPanels[panel])
{
return;
}
var kerbalButtons = _kerbalPanels[panel].transform.GetChild(4);
for (var kerbalIndex = 0; kerbalIndex < kerbalButtons.childCount; kerbalIndex++)
{
var rectTransform = kerbalButtons.GetChild(kerbalIndex) as RectTransform;
RectTransformUtility.ScreenPointToLocalPointInRectangle(
rectTransform,
Input.mousePosition,
_mainCamera,
out var localPoint
);
if (!rectTransform!.rect.Contains(localPoint))
{
continue;
}
KerbonautManagerWindow.IsOpen = true;
KerbonautManagerWindow.SetSelectedKerbal(_kerbals[panel][kerbalIndex]);
return;
}
}
private void OnGUI()
{
KerbonautManagerWindow.UpdateGUI();
}
private static bool IsInVab()
{
return Game && Game.GlobalGameState?.GetState() == GameState.VehicleAssemblyBuilder;
}
#endregion
#region Harmony patches
[HarmonyPatch(typeof(KerbalManager), "OnShowWindow")]
[HarmonyPostfix]
public static void KerbalManager_OnShowWindow()
{
if (!IsInVab())
{
return;
}
RefreshPanel(Panel.A);
RefreshPanel(Panel.B);
KerbonautManagerWindow.IsOpen = true;
}
[HarmonyPatch(typeof(KerbalManager), "OnHideWindow")]
[HarmonyPrefix]
public static void KerbalManager_OnHideWindow()
{
if (!IsInVab())
{
return;
}
RefreshPanel(Panel.A);
RefreshPanel(Panel.B);
KerbonautManagerWindow.IsOpen = false;
}
[HarmonyPatch(typeof(KerbalManager), "ReloadLocations")]
[HarmonyPostfix]
public static void KerbalManager_ReloadLocations()
{
if (!IsInVab())
{
return;
}
RefreshPanel(Panel.A);
RefreshPanel(Panel.B);
}
[HarmonyPatch(typeof(KerbalManager), "OnDropdownASelection")]
[HarmonyPostfix]
public static void KerbalManager_OnDropdownASelection()
{
if (!IsInVab())
{
return;
}
RefreshPanel(Panel.A);
}
[HarmonyPatch(typeof(KerbalManager), "OnDropdownBSelection")]
[HarmonyPostfix]
public static void KerbalManager_OnDropdownBSelection()
{
if (!IsInVab())
{
return;
}
RefreshPanel(Panel.B);
}
#endregion
#region State update methods
private static void RefreshPanel(Panel panel)
{
_instance._kerbalPanels[panel] = null;
_instance._kerbals[panel] = null;
AddPanelKerbals(panel, GameObject.Find(panel == Panel.A ? LocationAPath : LocationBPath));
}
private static void AddPanelKerbals(Panel panel, GameObject location)
{
if (location.transform.childCount <= 1)
{
return;
}
var panelObject = location.transform.GetChild(1).gameObject;
_instance._kerbalPanels[panel] = panelObject;
var kerbalList = panelObject.GetComponent<ContextBindRoot>().BoundParentContext
.Lists["kerbalSlotList"].Data;
var guids = (
from kerbal in kerbalList
where !(bool)kerbal.Properties["isEmpty"].GetObject()
select (IGGuid)kerbal.Properties["kerbalId"].GetObject()
).ToList();
_instance._kerbals[panel] = guids;
}
#endregion
}
}