forked from GarwelGarwel/KerbalHealth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
KerbalHealthEditorReport.cs
194 lines (181 loc) · 10.3 KB
/
KerbalHealthEditorReport.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
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using KSP.UI.Screens;
namespace KerbalHealth
{
[KSPAddon(KSPAddon.Startup.EditorAny, false)]
class KerbalHealthEditorReport : MonoBehaviour
{
ApplicationLauncherButton appLauncherButton;
IButton toolbarButton;
bool dirty = false;
Rect reportPosition = new Rect(0.5f, 0.5f, 300, 50);
PopupDialog reportWindow; // Health Report window
System.Collections.Generic.List<DialogGUIBase> gridContents; // Health Report grid's labels
DialogGUILabel spaceLbl, recupLbl, shieldingLbl, exposureLbl;
int colNum = 3; // # of columns in Health Report
static bool healthModulesEnabled = true;
public void Start()
{
if (!Core.ModEnabled) return;
Core.Log("KerbalHealthEditorReport.Start", Core.LogLevel.Important);
GameEvents.onEditorShipModified.Add(delegate(ShipConstruct sc) { Invalidate(); });
GameEvents.onEditorPodDeleted.Add(Invalidate);
GameEvents.onEditorScreenChange.Add(delegate(EditorScreen s) { Invalidate(); });
if (ToolbarManager.ToolbarAvailable && Core.UseBlizzysToolbar)
{
Core.Log("Registering Blizzy's Toolbar button...", Core.LogLevel.Important);
toolbarButton = ToolbarManager.Instance.add("KerbalHealth", "HealthReport");
toolbarButton.Text = "Kerbal Health Report";
toolbarButton.TexturePath = "KerbalHealth/toolbar";
toolbarButton.ToolTip = "Kerbal Health";
toolbarButton.OnClick += (e) => { if (reportWindow == null) DisplayData(); else UndisplayData(); };
}
else
{
Core.Log("Registering AppLauncher button...", Core.LogLevel.Important);
Texture2D icon = new Texture2D(38, 38);
icon.LoadImage(System.IO.File.ReadAllBytes(Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), "icon.png")));
appLauncherButton = ApplicationLauncher.Instance.AddModApplication(DisplayData, UndisplayData, null, null, null, null, ApplicationLauncher.AppScenes.ALWAYS, icon);
}
Core.Log("KerbalHealthEditorReport.Start finished.", Core.LogLevel.Important);
}
public void DisplayData()
{
Core.Log("KerbalHealthEditorReport.DisplayData");
if ((ShipConstruction.ShipManifest == null) || (!ShipConstruction.ShipManifest.HasAnyCrew()))
{
Core.Log("Ship is empty. Let's get outta here!", Core.LogLevel.Important);
return;
}
gridContents = new List<DialogGUIBase>((Core.KerbalHealthList.Count + 1) * colNum)
{
// Creating column titles
new DialogGUILabel("<b><color=\"white\">Name</color></b>", true),
new DialogGUILabel("<b><color=\"white\">Trend</color></b>", true),
new DialogGUILabel("<b><color=\"white\">Time Left</color></b>", true)
};
// Initializing Health Report's grid with empty labels, to be filled in Update()
for (int i = 0; i < ShipConstruction.ShipManifest.CrewCount * colNum; i++)
gridContents.Add(new DialogGUILabel("", true));
// Preparing factors checklist
List<DialogGUIToggle> checklist = new List<DialogGUIToggle>();
foreach (HealthFactor f in Core.Factors)
checklist.Add(new DialogGUIToggle(f.IsEnabledInEditor, f.Title, (state) => { f.SetEnabledInEditor(state); Invalidate(); }));
checklist.Add(new DialogGUIToggle(true, "Health modules", (state) => { healthModulesEnabled = state; Invalidate(); }));
reportWindow = PopupDialog.SpawnPopupDialog(
new Vector2(0.5f, 0.5f),
new Vector2(0.5f, 0.5f),
new MultiOptionDialog(
"Health Report",
"",
"Health Report",
HighLogic.UISkin,
reportPosition,
new DialogGUIGridLayout(new RectOffset(0, 0, 0, 0), new Vector2(80, 30), new Vector2(20, 0), UnityEngine.UI.GridLayoutGroup.Corner.UpperLeft, UnityEngine.UI.GridLayoutGroup.Axis.Horizontal, TextAnchor.MiddleCenter, UnityEngine.UI.GridLayoutGroup.Constraint.FixedColumnCount, colNum, gridContents.ToArray()),
new DialogGUIHorizontalLayout(
new DialogGUILabel("<color=\"white\">Space: </color>", false),
spaceLbl = new DialogGUILabel("N/A", true),
new DialogGUILabel("<color=\"white\">Recuperation: </color>", false),
recupLbl = new DialogGUILabel("N/A", true)),
new DialogGUIHorizontalLayout(
new DialogGUILabel("<color=\"white\">Shielding: </color>", false),
shieldingLbl = new DialogGUILabel("N/A", true),
new DialogGUILabel("<color=\"white\">Exposure: </color>", false),
exposureLbl = new DialogGUILabel("N/A", true)),
new DialogGUIHorizontalLayout(
new DialogGUILabel("", true),
new DialogGUILabel("Factors", true),
new DialogGUIButton("Reset", OnResetButtonSelected, false)),
new DialogGUIGridLayout(new RectOffset(0, 0, 0, 0), new Vector2(140, 30), new Vector2(20, 0), UnityEngine.UI.GridLayoutGroup.Corner.UpperLeft, UnityEngine.UI.GridLayoutGroup.Axis.Horizontal, TextAnchor.MiddleCenter, UnityEngine.UI.GridLayoutGroup.Constraint.FixedColumnCount, 2, checklist.ToArray())),
false,
HighLogic.UISkin,
false);
Invalidate();
}
public static bool HealthModulesEnabled => healthModulesEnabled;
public void OnResetButtonSelected()
{
foreach (HealthFactor f in Core.Factors)
f.ResetEnabledInEditor();
healthModulesEnabled = true;
Invalidate();
}
string GetShielding() => (ShipConstruction.ShipManifest.CrewCount != 0) ? Core.KerbalHealthList.Find(ShipConstruction.ShipManifest.GetAllCrew(false)[0]).VesselModifiers.Shielding.ToString("F1") : "N/A";
string GetExposure() => (ShipConstruction.ShipManifest.CrewCount != 0) ? Core.KerbalHealthList.Find(ShipConstruction.ShipManifest.GetAllCrew(false)[0]).LastExposure.ToString("P1") : "N/A";
public void UndisplayData()
{
if (reportWindow != null)
{
Vector3 v = reportWindow.RTrf.position;
reportPosition = new Rect(v.x / Screen.width + 0.5f, v.y / Screen.height + 0.5f, 300, 50);
reportWindow.Dismiss();
}
}
public void Update()
{
if (!Core.ModEnabled)
{
if (reportWindow != null) reportWindow.Dismiss();
return;
}
if ((reportWindow != null) && dirty)
{
if (gridContents == null)
{
Core.Log("gridContents is null.", Core.LogLevel.Error);
return;
}
if (gridContents.Count != (ShipConstruction.ShipManifest.CrewCount + 1) * colNum) // # of tracked kerbals has changed => close & reopen the window
{
Core.Log("Kerbals' number has changed. Recreating the Health Report window.", Core.LogLevel.Important);
UndisplayData();
DisplayData();
}
// Fill the Health Report's grid with kerbals' health data
int i = 0;
KerbalHealthStatus khs = null;
HealthModifierSet.VesselCache.Clear();
foreach (ProtoCrewMember pcm in ShipConstruction.ShipManifest.GetAllCrew(false))
{
if (pcm == null) continue;
gridContents[(i + 1) * colNum].SetOptionText(pcm.name);
khs = Core.KerbalHealthList?.Find(pcm)?.Clone();
if (khs == null)
{
Core.Log("Could not create a clone of KerbalHealthStatus for " + pcm.name + ". It is " + ((Core.KerbalHealthList?.Find(pcm) == null) ? "not " : "") + "found in KerbalHealthList, which contains " + Core.KerbalHealthList.Count + " records.", Core.LogLevel.Error);
i++;
continue;
}
khs.HP = khs.MaxHP;
double ch = khs.HealthChangePerDay();
double b = khs.GetBalanceHP();
string s = "";
if (b > 0) s = "-> " + b.ToString("F0") + " HP (" + (b / khs.MaxHP * 100).ToString("F0") + "%)";
else s = ch.ToString("F1") + " HP/day";
gridContents[(i + 1) * colNum + 1].SetOptionText(s);
if (b > khs.NextConditionHP()) s = "—";
else s = ((khs.LastRecuperation > khs.LastDecay) ? "> " : "") + Core.ParseUT(khs.TimeToNextCondition());
gridContents[(i + 1) * colNum + 2].SetOptionText(s);
i++;
}
spaceLbl.SetOptionText("<color=\"white\">" + khs.VesselModifiers.Space.ToString("F1") + "</color>");
recupLbl.SetOptionText("<color=\"white\">" + khs.VesselModifiers.Recuperation.ToString("F1") + "%</color>");
shieldingLbl.SetOptionText("<color=\"white\">" + khs.VesselModifiers.Shielding.ToString("F1") + "</color>");
exposureLbl.SetOptionText("<color=\"white\">" + khs.LastExposure.ToString("P1") + "</color>");
dirty = false;
}
}
public void Invalidate() => dirty = true;
public void OnDisable()
{
Core.Log("KerbalHealthEditorReport.OnDisable", Core.LogLevel.Important);
UndisplayData();
if (toolbarButton != null) toolbarButton.Destroy();
if ((appLauncherButton != null) && (ApplicationLauncher.Instance != null))
ApplicationLauncher.Instance.RemoveModApplication(appLauncherButton);
Core.Log("KerbalHealthEditorReport.OnDisable finished.", Core.LogLevel.Important);
}
}
}