Megacommit towards making relays that work for CelestialBodies. I think I'm overcomplicating this. BodyRelayDev
[AntennaRange.git] /
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
// AntennaRange © 2014 toadicus
//
// This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License. To view a
// copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/3.0/

using KSP;
using System;
using ToadicusTools;
using UnityEngine;

namespace AntennaRange
{
	/// <summary>
	/// A <see cref="UnityEngine.MonoBehaviour"/> responsible for managing configuration items for AntennaRange.
	/// </summary>
	[KSPAddon(KSPAddon.Startup.SpaceCentre, false)]
	public class ARConfiguration : MonoBehaviour
	{
		private const string WINDOW_POS_KEY = "configWindowPos";
		private const string REQUIRE_LOS_KEY = "requireLineOfSight";
		private const string GRACE_RATIO_KEY = "graceRatio";
		private const string REQUIRE_PROBE_CONNECTION_KEY = "requireConnectionForControl";
		private const string FIXED_POWER_KEY = "fixedPowerCost";
		private const string PRETTY_LINES_KEY = "drawPrettyLines";
		private const string UPDATE_DELAY_KEY = "updateDelay";

		private bool runOnce;

		/// <summary>
		/// Indicates whether connections require line of sight.
		/// </summary>
		public static bool RequireLineOfSight
		{
			get;
			private set;
		}

		/// <summary>
		/// A "fudge factor" ratio that pretends planets and moons are slightly smaller than reality to make
		/// building communication constellations easier.
		/// </summary>
		public static double RadiusRatio
		{
			get;
			private set;
		}

		/// <summary>
		/// Indicates whether unmanned vessels require a connection for control.
		/// </summary>
		public static bool RequireConnectionForControl
		{
			get;
			private set;
		}

		/// <summary>
		/// If true, relays will fix their power cost when above nominal range, decreasing data rate instead.
		/// </summary>
		public static bool FixedPowerCost
		{
			get;
			private set;
		}

		/// <summary>
		/// Indicates whether this AntennaRange will draw pretty lines in map view.
		/// </summary>
		public static bool PrettyLines
		{
			get;
			set;
		}

		public static long UpdateDelay
		{
			get;
			private set;
		}

#pragma warning disable 1591

		private bool showConfigWindow;
		private Rect configWindowPos;

		private string updateDelayStr;
		private long updateDelay;

		private IButton toolbarButton;
		private ApplicationLauncherButton appLauncherButton;

		private System.Version runningVersion;

		private KSP.IO.PluginConfiguration _config;
		private KSP.IO.PluginConfiguration config
		{
			get
			{
				if (this._config == null)
				{
					this._config = KSP.IO.PluginConfiguration.CreateForType<AntennaRelay>();
				}

				return this._config;
			}
		}

		public void Awake()
		{
			Tools.PostDebugMessage(this, "Waking up.");

			this.runningVersion = this.GetType().Assembly.GetName().Version;

			this.showConfigWindow = false;
			this.configWindowPos = new Rect(Screen.width / 4, Screen.height / 2, 180, 15);


			this.configWindowPos = this.LoadConfigValue(WINDOW_POS_KEY, this.configWindowPos);

			ARConfiguration.RequireLineOfSight = this.LoadConfigValue(REQUIRE_LOS_KEY, false);

			ARConfiguration.RadiusRatio = (1 - this.LoadConfigValue(GRACE_RATIO_KEY, .05d));
			ARConfiguration.RadiusRatio *= ARConfiguration.RadiusRatio;

			ARConfiguration.RequireConnectionForControl =
				this.LoadConfigValue(REQUIRE_PROBE_CONNECTION_KEY, false);

			ARConfiguration.FixedPowerCost = this.LoadConfigValue(FIXED_POWER_KEY, false);

			ARConfiguration.PrettyLines = this.LoadConfigValue(PRETTY_LINES_KEY, true);

			ARConfiguration.UpdateDelay = this.LoadConfigValue(UPDATE_DELAY_KEY, 16L);

			this.updateDelayStr = ARConfiguration.UpdateDelay.ToString();

			GameEvents.onGameSceneLoadRequested.Add(this.onSceneChangeRequested);

			this.runOnce = true;

			Debug.Log(string.Format("{0} v{1} - ARConfiguration loaded!", this.GetType().Name, this.runningVersion));

			Tools.PostDebugMessage(this, "Awake.");
		}

		public void Update()
		{
			if (runOnce)
			{
				KerbinRelay.TrackingStationMaxLevel =
					ScenarioUpgradeableFacilities.protoUpgradeables[SpaceCenterFacility.TrackingStation.ToString()]
						.facilityRefs[0].MaxLevel;
				
				runOnce = false;
			}
		}

		public void OnGUI()
		{
			// Only runs once, if the Toolbar is available.
			if (ToolbarManager.ToolbarAvailable)
			{
				if (this.toolbarButton == null)
				{
					Tools.PostDebugMessage(this, "Toolbar available; initializing toolbar button.");

					this.toolbarButton = ToolbarManager.Instance.add("AntennaRange", "ARConfiguration");
					this.toolbarButton.Visibility = new GameScenesVisibility(GameScenes.SPACECENTER);
					this.toolbarButton.Text = "AR";
					this.toolbarButton.TexturePath = "AntennaRange/Textures/toolbarIcon";
					this.toolbarButton.TextColor = (Color)XKCDColors.Amethyst;
					this.toolbarButton.OnClick += delegate(ClickEvent e)
					{
						this.toggleConfigWindow();
					};
				}
			}
			else if (this.appLauncherButton == null && ApplicationLauncher.Ready)
			{
				Tools.PostDebugMessage(this, "Toolbar available; initializing AppLauncher button.");

				this.appLauncherButton = ApplicationLauncher.Instance.AddModApplication(
					this.toggleConfigWindow,
					this.toggleConfigWindow,
					ApplicationLauncher.AppScenes.SPACECENTER,
					GameDatabase.Instance.GetTexture(
						"AntennaRange/Textures/appLauncherIcon",
						false
					)
				);
			}

			if (this.showConfigWindow)
			{
				Rect configPos = GUILayout.Window(354163056,
					this.configWindowPos,
					this.ConfigWindow,
					string.Format("AntennaRange {0}.{1}", this.runningVersion.Major, this.runningVersion.Minor),
					GUILayout.ExpandHeight(true),
					GUILayout.ExpandWidth(true)
				);

				configPos = Tools.ClampRectToScreen(configPos, 20);

				if (configPos != this.configWindowPos)
				{
					this.configWindowPos = configPos;
					this.SaveConfigValue(WINDOW_POS_KEY, this.configWindowPos);
				}
			}
		}

		public void ConfigWindow(int _)
		{
			GUILayout.BeginVertical(GUILayout.ExpandHeight(true));

			GUILayout.BeginHorizontal(GUILayout.ExpandWidth(true));

			bool requireLineOfSight = GUITools.Toggle(ARConfiguration.RequireLineOfSight, "Require Line of Sight");
			if (requireLineOfSight != ARConfiguration.RequireLineOfSight)
			{
				ARConfiguration.RequireLineOfSight = requireLineOfSight;
				this.SaveConfigValue(REQUIRE_LOS_KEY, requireLineOfSight);
			}

			GUILayout.EndHorizontal();

			GUILayout.BeginHorizontal(GUILayout.ExpandWidth(true));

			bool requireConnectionForControl =
				GUITools.Toggle(
					ARConfiguration.RequireConnectionForControl,
					"Require Connection for Probe Control"
				);
			if (requireConnectionForControl != ARConfiguration.RequireConnectionForControl)
			{
				ARConfiguration.RequireConnectionForControl = requireConnectionForControl;
				this.SaveConfigValue(REQUIRE_PROBE_CONNECTION_KEY, requireConnectionForControl);
			}

			GUILayout.EndHorizontal();

			GUILayout.BeginHorizontal();

			bool fixedPowerCost = GUITools.Toggle(ARConfiguration.FixedPowerCost, "Use Fixed Power Cost");
			if (fixedPowerCost != ARConfiguration.FixedPowerCost)
			{
				ARConfiguration.FixedPowerCost = fixedPowerCost;
				this.SaveConfigValue(FIXED_POWER_KEY, fixedPowerCost);
			}

			GUILayout.EndHorizontal();

			GUILayout.BeginHorizontal();

			bool prettyLines = GUITools.Toggle(ARConfiguration.PrettyLines, "Draw Pretty Lines");
			if (prettyLines != ARConfiguration.PrettyLines)
			{
				ARConfiguration.PrettyLines = prettyLines;
				this.SaveConfigValue(PRETTY_LINES_KEY, prettyLines);
			}

			GUILayout.EndHorizontal();

			GUILayout.BeginHorizontal();

			GUILayout.Label("Update Delay", GUILayout.ExpandWidth(false));

			this.updateDelayStr = GUILayout.TextField(this.updateDelayStr, 4, GUILayout.Width(40f));

			GUILayout.Label("ms", GUILayout.ExpandWidth(false));

			GUILayout.EndHorizontal();

			if (this.updateDelayStr.Length > 1 && long.TryParse(this.updateDelayStr, out this.updateDelay))
			{
				ARConfiguration.UpdateDelay = Math.Min(Math.Max(this.updateDelay, 16), 2500);
				this.updateDelayStr = ARConfiguration.UpdateDelay.ToString();
			}

			if (requireLineOfSight)
			{
				GUILayout.BeginHorizontal();

				double graceRatio = 1d - Math.Sqrt(ARConfiguration.RadiusRatio);
				double newRatio;

				GUILayout.Label(string.Format("Line of Sight 'Fudge Factor': {0:P0}", graceRatio));

				GUILayout.EndHorizontal();

				GUILayout.BeginHorizontal();

				newRatio = GUILayout.HorizontalSlider((float)graceRatio, 0f, 1f, GUILayout.ExpandWidth(true));
				newRatio = Math.Round(newRatio, 2);

				if (newRatio != graceRatio)
				{
					ARConfiguration.RadiusRatio = (1d - newRatio) * (1d - newRatio);
					this.SaveConfigValue(GRACE_RATIO_KEY, newRatio);
				}

				GUILayout.EndHorizontal();
			}

			GUILayout.EndVertical();

			GUI.DragWindow();
		}

		public void OnDestroy()
		{
			GameEvents.onGameSceneLoadRequested.Remove(this.onSceneChangeRequested);

			if (this.toolbarButton != null)
			{
				this.toolbarButton.Destroy();
			}

			if (this.appLauncherButton != null)
			{
				ApplicationLauncher.Instance.RemoveModApplication(this.appLauncherButton);
			}
		}

		protected void onSceneChangeRequested(GameScenes scene)
		{
			if (scene != GameScenes.SPACECENTER)
			{
				print("ARConfiguration: Requesting Destruction.");
				MonoBehaviour.Destroy(this);
			}
		}

		private void toggleConfigWindow()
		{
			this.showConfigWindow = !this.showConfigWindow;
			this.updateDelayStr = ARConfiguration.UpdateDelay.ToString();
		}

		private T LoadConfigValue<T>(string key, T defaultValue)
		{
			this.config.load();

			return config.GetValue(key, defaultValue);
		}

		private void SaveConfigValue<T>(string key, T value)
		{
			this.config.load();

			this.config.SetValue(key, value);

			this.config.save();
		}
	}
}