forked from theSpeare/RoverScience-Revisit
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathConsoleGUI.cs
More file actions
363 lines (288 loc) · 17.1 KB
/
ConsoleGUI.cs
File metadata and controls
363 lines (288 loc) · 17.1 KB
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
using KSP.Localization;
using System;
using System.Collections.Generic;
using UnityEngine;
namespace RoverScience
{
public partial class RoverScienceGUI
{
static GUIStyle consoleAreaStyle = new GUIStyle(HighLogic.Skin.textArea);
static GUIStyle boldFont;
static GUIStyle noWrapFont;
private bool analyzeButtonPressedOnce = false;
private string inputMaxDistance = "100";
private Dictionary<string, string> RichColors = new Dictionary<string, string>
{
{ "green", "<color=#00ff00ff>" },
{ "red", "<color=#ff0000ff>" },
{ "blue", "<color=#add8e6ff>" },
{ "yellow", "<color=#ffff00ff>" },
{ "orange", "<color=#ffa500ff>" },
{ "Very High!", "<color=#00ff00ff>" },
{ "High", "<color=#00ff00ff>" },
{ "Normal", "<color=#add8e6ff>" },
{ "Low", "<color=#ffff00ff>" }
};
private string SetRichColor(string s, string color) => $"{RichColors[color]}{s}</color>";
private string PotentialFontColor(string name)
=> RichColors.ContainsKey(name) ? SetRichColor(name, name) : SetRichColor(name, "red");
private string PredictionFontColor(double percentage)
{
if (percentage > 70) return SetRichColor(Localizer.Format("#LOC_RoverScience_GUI_Percentage", percentage.ToString()), "green");
else if (percentage >= 50) return SetRichColor(Localizer.Format("#LOC_RoverScience_GUI_Percentage", percentage.ToString()), "yellow");
else return SetRichColor(Localizer.Format("#LOC_RoverScience_GUI_Percentage", percentage.ToString()), "red");
}
private string DecayFontColor (double percentage)
{
if (percentage > 70) return SetRichColor(Localizer.Format("#LOC_RoverScience_GUI_Percentage", percentage.ToString()), "red");
else if (percentage >= 50) return SetRichColor(Localizer.Format("#LOC_RoverScience_GUI_Percentage", percentage.ToString()), "yellow");
else return SetRichColor(Localizer.Format("#LOC_RoverScience_GUI_Percentage", percentage.ToString()), "green");
}
private void DrawRoverConsoleGUI(int windowID)
{
if (roverScience.part.vessel != FlightGlobals.ActiveVessel)
{
Utilities.LogVerbose("ConsoleGUI not drawn - not active vessel");
return;
}
if (Rover.scienceSpot.established && Rover.ScienceSpotReached)
{
consoleGUI.rect.height = 559;
} else if (Rover.scienceSpot.established)
{
consoleGUI.rect.height = 495;
} else if (!Rover.scienceSpot.established)
{
consoleGUI.rect.height = 466;
}
boldFont = new GUIStyle(GUI.skin.label);
noWrapFont = new GUIStyle(GUI.skin.label);
boldFont.fontStyle = FontStyle.Bold;
boldFont.wordWrap = false;
noWrapFont.wordWrap = false;
GUILayout.BeginVertical(consoleAreaStyle);
scrollPosition = GUILayout.BeginScrollView(scrollPosition, new GUILayoutOption[] { GUILayout.Width(240), GUILayout.Height(340) });
GUILayout.BeginHorizontal(); GUILayout.FlexibleSpace();
GUILayout.BeginHorizontal();
GUILayout.Label(Localizer.GetStringByTag("#LOC_RoverScience_GUI_SpotsAnalyzed") , boldFont); // "Science Spots Analyzed: "
GUILayout.Label(roverScience.amountOfTimesAnalyzed.ToString(), boldFont);
GUILayout.EndHorizontal();
GUILayout.FlexibleSpace(); GUILayout.EndHorizontal();
GUILayout.BeginHorizontal(); GUILayout.FlexibleSpace();
GUILayout.Label(Localizer.GetStringByTag("#LOC_RoverScience_GUI_ReuseLoss") + DecayFontColor(roverScience.ScienceDecayPercentage), boldFont); // "Science Loss due to re-use: "
GUILayout.FlexibleSpace(); GUILayout.EndHorizontal();
GUICenter("_____________________");
GUIBreakline();
if (!Rover.landingSpot.established)
{
GUILayout.Label(Localizer.GetStringByTag("#LOC_RoverScience_GUI_NoLanding")); // > No landing spot established!
GUILayout.Label(Localizer.GetStringByTag("#LOC_RoverScience_GUI_NoLandingWheels")); // > You must first establish a landing spot by landing somewhere. Make sure you have wheels!
GUIBreakline();
GUILayout.Label(Localizer.Format("#LOC_RoverScience_GUI_WheelsDetected", Rover.NumberWheels)); // > Rover wheels detected: <<1>>
GUILayout.Label(Localizer.Format("#LOC_RoverScience_GUI_WheelsLanded", Rover.NumberWheelsLanded)); // > Rover wheels landed: <<1>>
} else {
if (!Rover.scienceSpot.established)
{
// PRINT OUT CONSOLE CONTENTS
if (roverScience.ScienceDecayPercentage >= 100)
{
GUILayout.Label(SetRichColor(Localizer.GetStringByTag("#LOC_RoverScience_GUI_ScienceLimit"), "red")); // > You have analyzed too many times.\n> Science loss is now at 100%.\n> Send another rover.
} else {
GUILayout.Label(Localizer.GetStringByTag("#LOC_RoverScience_GUI_DriveAround")); // > Drive around to search for science spots . . .
GUILayout.Label(Localizer.Format("#LOC_RoverScience_GUI_ScanningRange", Rover.maxRadius)); // > Currently scanning at range: " + rover.maxRadius + "m");
//GUILayout.Label("> Total dist. traveled searching: " + Math.Round(rover.distanceTraveledTotal, 2));
GUIBreakline();
foreach (string line in consolePrintOut)
{
GUILayout.Label(line);
}
if (Vessel.mainBody.bodyName == "Kerbin")
{
GUILayout.Label(SetRichColor(Localizer.GetStringByTag("#LOC_RoverScience_GUI_HomeWorld"), "red")); // > WARNING - there is very little rover science for Kerbin!
}
}
} else {
if (!Rover.ScienceSpotReached)
{
double relativeBearing = Rover.Heading - Rover.BearingToScienceSpot;
GUILayout.BeginVertical();
GUILayout.BeginHorizontal(); GUILayout.FlexibleSpace();
if (!Rover.AnomalyPresent)
{
GUILayout.Label(SetRichColor(Localizer.GetStringByTag("#LOC_RoverScience_GUI_Potential"), "yellow")); // "[POTENTIAL SCIENCE SPOT]"
} else
{
GUILayout.Label(SetRichColor(Localizer.GetStringByTag("#LOC_RoverScience_GUI_Anomaly"), "yellow")); // "[ANOMALY DETECTED]"
}
GUILayout.FlexibleSpace(); GUILayout.EndHorizontal();
GUILayout.Label(Localizer.Format("#LOC_RoverScience_GUI_DistToSpot", Math.Round(Rover.DistanceFromScienceSpot, 1))); // "> Distance to spot (m): <<1>>" + );
//GUILayout.Label("Bearing of Site (degrees): " + Math.Round(rover.bearingToScienceSpot, 1));
//GUILayout.Label("Rover Bearing (degrees): " + Math.Round(rover.heading, 1));
//GUILayout.Label("Rel. Bearing (degrees): " + Math.Round(relativeBearing, 1));
if (!Rover.AnomalyPresent)
{
//GUILayout.Label("> Science Potential: " + rover.scienceSpot.predictedSpot + " (" + roverScience.currentPredictionAccuracy + "% confident)");
GUILayout.Label(Localizer.Format("#LOC_RoverScience_GUI_Prediction", PotentialFontColor(Rover.scienceSpot.predictedSpot))); // > Science Prediction: <<1>>"
GUILayout.Label(Localizer.Format("#LOC_RoverScience_GUI_Confidence", PredictionFontColor(roverScience.CurrentPredictionAccuracy))); // > Prediction is <<1>> confident");
}
else
{
GUILayout.Label(Localizer.GetStringByTag("#LOC_RoverScience_GUI_Anomaly2")); //"> ANOMALY DETECTED. Something interesting is nearby . . . the science potential should be very significant")
}
//GUIBreakline();
//GUIBreakline();
//This block handles writing getDriveDirection
//GUILayout.BeginHorizontal ();
//GUILayout.FlexibleSpace ();
//GUILayout.Label(getDriveDirection(rover.bearingToScienceSpot, rover.heading));
//GUILayout.FlexibleSpace ();
//GUILayout.EndHorizontal ();
GUILayout.EndVertical();
}
else
{
GUILayout.BeginHorizontal(); GUILayout.FlexibleSpace();
GUILayout.Label(SetRichColor(Localizer.GetStringByTag("#LOC_RoverScience_GUI_SpotReached"), "green")); // "[SCIENCE SPOT REACHED]"
GUILayout.FlexibleSpace(); GUILayout.EndHorizontal();
//GUILayout.Label("Total dist. traveled for this spot: " + Math.Round(rover.distanceTraveledTotal, 1));
//GUILayout.Label("Distance from landing site: " +
//Math.Round(rover.getDistanceBetweenTwoPoints(rover.scienceSpot.location, rover.landingSpot.location), 1));
GUILayout.BeginHorizontal();
GUILayout.Label(Localizer.Format("#LOC_RoverScience_GUI_ActualPotential", PotentialFontColor(Rover.scienceSpot.potentialGenerated))); // "> Science Potential: <<1>> (actual)");
GUILayout.EndHorizontal();
GUIBreakline();
GUILayout.Label(Localizer.GetStringByTag("#LOC_RoverScience_GUI_Note")); // "> NOTE: The more you analyze, the less you will get each time!");
}
}
}
GUILayout.EndScrollView();
GUILayout.EndVertical();
// ACTIVATE ROVER BUTTON
if (Rover.ScienceSpotReached)
{
if (!analyzeButtonPressedOnce)
{
if (GUILayout.Button(Localizer.GetStringByTag("#LOC_RoverScience_GUI_BtnAnalyze"), GUILayout.Height(60))) // "Analyze Science"
{
if (roverScience.container.GetStoredDataCount() == 0)
{
if (Rover.ScienceSpotReached)
{
analyzeButtonPressedOnce = true;
consolePrintOut.Clear();
}
else if (!Rover.ScienceSpotReached)
{
ScreenMessages.PostScreenMessage(Localizer.GetStringByTag("#LOC_RoverScience_GUI_GetToSpot"), 3, ScreenMessageStyle.UPPER_CENTER); // "Cannot analyze - Get to the science spot first!"
}
}
else
{
ScreenMessages.PostScreenMessage(Localizer.GetStringByTag("#LOC_RoverScience_GUI_Full"), 3, ScreenMessageStyle.UPPER_CENTER); // "Cannot analyze - Rover Brain already contains data!"
}
}
}
else
{
GUILayout.BeginHorizontal();
if (GUILayout.Button(Localizer.GetStringByTag("#LOC_RoverScience_GUI_BtnConfirm"))) // "Confirm"
{
analyzeButtonPressedOnce = false;
roverScience.AnalyzeScienceSample();
}
if (GUILayout.Button(Localizer.GetStringByTag("#LOC_RoverScience_GUI_BtnCancel"))) //"Cancel"
{
analyzeButtonPressedOnce = false;
}
GUILayout.EndHorizontal();
}
}
if (Rover.scienceSpot.established)
{
if (GUILayout.Button(Localizer.GetStringByTag("#LOC_RoverScience_GUI_BtnResetSpot"))) // "Reset Science Spot"
{
Rover.scienceSpot.established = false;
Rover.ResetDistanceTraveled();
DrawWaypoint.Instance.DestroyInterestingObject();
DrawWaypoint.Instance.HideMarker();
consolePrintOut.Clear();
}
}
//if (GUILayout.Button ("Reorient from Part")) {
//roverScience.command.MakeReference ();
//}
GUIBreakline();
GUIBreakline();
if (roverScience.ScienceDecayPercentage < 100)
{
GUILayout.BeginHorizontal();
inputMaxDistance = GUILayout.TextField(inputMaxDistance, 5, new GUILayoutOption[] { GUILayout.Width(40) });
if (GUILayout.Button(Localizer.Format("#LOC_RoverScience_GUI_BtnScanRange", roverScience.CurrentMaxDistance))) // "Set Scan Range [Max: <<1>>]"
{
int inputMaxDistanceInt;
bool isNumber = int.TryParse(inputMaxDistance, out inputMaxDistanceInt);
if ((isNumber) && (inputMaxDistanceInt <= roverScience.CurrentMaxDistance) && (inputMaxDistanceInt >= 40))
{
Rover.maxRadius = inputMaxDistanceInt;
Utilities.Log("Set maxRadius to input: " + Rover.maxRadius);
ScreenMessages.PostScreenMessage(Localizer.Format("#LOC_RoverScience_GUI_Scanning", Rover.maxRadius), 3, ScreenMessageStyle.UPPER_CENTER); // "Now scanning for science spots at range: <<1>>
}
if (inputMaxDistanceInt > roverScience.CurrentMaxDistance)
{
ScreenMessages.PostScreenMessage(Localizer.Format("#LOC_RoverScience_GUI_OverMax", roverScience.CurrentMaxDistance), 3, ScreenMessageStyle.UPPER_CENTER); // "Cannot set scan range over max distance of: <<1>>
} else if (inputMaxDistanceInt < 40)
{
ScreenMessages.PostScreenMessage(Localizer.GetStringByTag("#LOC_RoverScience_GUI_MinRange"), 3, ScreenMessageStyle.UPPER_CENTER); // "Minimum of 40m scan range is required"
}
}
}
GUILayout.EndHorizontal();
if (GUILayout.Button (Localizer.GetStringByTag("#LOC_RoverScience_GUI_BtnUpgradeMenu"))) { // "Upgrade Menu"
upgradeGUI.Toggle ();
}
GUILayout.Space (5);
if (GUILayout.Button (Localizer.GetStringByTag("#LOC_RoverScience_GUI_BtnShutdown"))) { // "Close and Shutdown"
Rover.scienceSpot.established = false;
Rover.ResetDistanceTraveled ();
consolePrintOut.Clear ();
DrawWaypoint.Instance.HideMarker();
consoleGUI.Hide ();
upgradeGUI.Hide ();
}
GUI.DragWindow ();
}
private string GetDriveDirection(double destination, double currentHeading)
{
// This will calculate the closest angle to the destination, given a current heading.
// Everything here will be in degrees, not radians
// Shift destination angle to 000 bearing. Apply this shift to the currentHeading in the same direction.
double delDestAngle = 0;
double shiftedCurrentHeading = 0;
if (destination > 180) {
// Delta will be clockwise
delDestAngle = 360 - destination;
shiftedCurrentHeading = currentHeading + delDestAngle;
if (shiftedCurrentHeading > 360) shiftedCurrentHeading -= 360;
} else {
// Delta will be anti-clockwise
delDestAngle = destination;
shiftedCurrentHeading = currentHeading - delDestAngle;
if (shiftedCurrentHeading < 0) shiftedCurrentHeading += 360;
}
double absShiftedCurrentHeading = Math.Abs(shiftedCurrentHeading);
if (absShiftedCurrentHeading < 6) {
return Localizer.GetStringByTag("#LOC_RoverScience_GUI_DriveFwd"); // "DRIVE FORWARD";
}
if ((absShiftedCurrentHeading > 174) && (absShiftedCurrentHeading < 186)) {
return Localizer.GetStringByTag("#LOC_RoverScience_GUI_TurnAround"); // "TURN AROUND";
}
if (absShiftedCurrentHeading < 180) {
return Localizer.GetStringByTag("#LOC_RoverScience_GUI_TurnLeft"); // "TURN LEFT";
}
if (absShiftedCurrentHeading > 180) {
return Localizer.GetStringByTag("#LOC_RoverScience_GUI_TurnRight"); // "TURN RIGHT";
}
return Localizer.GetStringByTag("#LOC_RoverScience_GUI_DirectionError"); // "ERROR: FAILED TO RESOLVE DRIVE DIRECTION";
}
}
}