New StageInfo module for more detailed engineering information.
New StageInfo module for more detailed engineering information.

--- a/Properties/AssemblyInfo.cs
+++ b/Properties/AssemblyInfo.cs
@@ -39,7 +39,7 @@
 // The assembly version has the format "{Major}.{Minor}.{Build}.{Revision}".
 // The form "{Major}.{Minor}.*" will automatically update the build and revision,
 // and "{Major}.{Minor}.{Build}.*" will update just the revision.
-[assembly: AssemblyVersion("0.14.1.*")]
+[assembly: AssemblyVersion("0.14.3.*")]
 // The following attributes are used to specify the signing key for the assembly,
 // if desired. See the Mono documentation for more information about signing.
 //[assembly: AssemblyDelaySign(false)]

--- a/VOID.csproj
+++ b/VOID.csproj
@@ -97,6 +97,7 @@
     <Compile Include="VOID_HUDAdvanced.cs" />
     <Compile Include="VOID_TWR.cs" />
     <Compile Include="VOID_CareerStatus.cs" />
+    <Compile Include="VOID_StageInfo.cs" />
   </ItemGroup>
   <ProjectExtensions>
     <MonoDevelop>

--- a/VOID_CareerStatus.cs
+++ b/VOID_CareerStatus.cs
@@ -166,19 +166,20 @@
 			GUI.DragWindow();
 		}
 
-		private void onFundsChange(double newValue)
+		// TODO: Update event handlers to do something useful with the new "reasons" parameter.
+		private void onFundsChange(double newValue, TransactionReasons reasons)
 		{
 			this.lastFundsChange = newValue - this.currentFunds;
 			this.currentFunds = newValue;
 		}
 
-		private void onRepChange(float newValue)
+		private void onRepChange(float newValue, TransactionReasons reasons)
 		{
 			this.lastRepChange = newValue - this.currentReputation;
 			this.currentReputation = newValue;
 		}
 
-		private void onScienceChange(float newValue)
+		private void onScienceChange(float newValue, TransactionReasons reasons)
 		{
 			this.lastScienceChange = newValue - this.currentScience;
 			this.currentScience = newValue;

--- a/VOID_Core.cs
+++ b/VOID_Core.cs
@@ -31,6 +31,7 @@
 using System;
 using System.Collections.Generic;
 using System.Linq;
+using System.Text;
 using ToadicusTools;
 using UnityEngine;
 
@@ -391,17 +392,33 @@
 			if (!this.GUIStylesLoaded)
 			{
 				this.LoadGUIStyles();
+
+				Tools.PostDebugMessage(
+					this,
+					"ToolbarAvailable: {0}, UseToobarManager: {1}",
+					ToolbarManager.ToolbarAvailable,
+					this.UseToolbarManager);
 			}
 
 			if (!this.UseToolbarManager)
 			{
 				if (this.AppLauncherButton == null)
 				{
+					Tools.PostDebugMessage(this,
+						"UseToolbarManager = false (ToolbarAvailable = {0}) and " +
+						"AppLauncherButton is null, making AppLauncher button.",
+						ToolbarManager.ToolbarAvailable
+					);
 					this.InitializeAppLauncherButton();
 				}
 			}
 			else if (this.ToolbarButton == null)
 			{
+				Tools.PostDebugMessage(this,
+					"UseToolbarManager = true (ToolbarAvailable = {0}) and " +
+					"ToolbarButton is null, making Toolbar button.",
+					ToolbarManager.ToolbarAvailable
+				);
 				this.InitializeToolbarButton();
 			}
 
@@ -717,7 +734,7 @@
 				this.stringFrequency = (1f / this.updatePeriod).ToString();
 			}
 			this.stringFrequency = GUILayout.TextField(this.stringFrequency.ToString(), 5, GUILayout.ExpandWidth(true));
-			// GUILayout.FlexibleSpace();
+
 			if (GUILayout.Button("Apply"))
 			{
 				double updateFreq = 1f / this.updatePeriod;
@@ -771,43 +788,52 @@
 
 		protected void LoadModulesOfType<T>()
 		{
-			var types = AssemblyLoader.loadedAssemblies
-				.Select(a => a.assembly.GetExportedTypes())
-				.SelectMany(t => t)
-				.Where(v => typeof(T).IsAssignableFrom(v)
-					&& !(v.IsInterface || v.IsAbstract) &&
-					!typeof(VOID_Core).IsAssignableFrom(v)
-				);
-
-			Tools.PostDebugMessage(string.Format(
-				"{0}: Found {1} modules to check.",
-				this.GetType().Name,
-				types.Count()
-			));
-			foreach (var voidType in types)
-			{
-				if (!HighLogic.LoadedSceneIsEditor &&
-					typeof(IVOID_EditorModule).IsAssignableFrom(voidType))
-				{
-					continue;
-				}
-
-				Tools.PostDebugMessage(string.Format(
-					"{0}: found Type {1}",
-					this.GetType().Name,
-					voidType.Name
-				));
-
-				this.LoadModule(voidType);
+			StringBuilder sb = new StringBuilder("Loading modules...");
+			sb.AppendLine();
+
+			foreach (AssemblyLoader.LoadedAssembly assy in AssemblyLoader.loadedAssemblies)
+			{
+				foreach (Type loadedType in assy.assembly.GetExportedTypes())
+				{
+					if (
+						loadedType.IsInterface ||
+						loadedType.IsAbstract ||
+						!typeof(T).IsAssignableFrom(loadedType) ||
+						this.GetType().IsAssignableFrom(loadedType)
+					)
+					{
+						continue;
+					}
+
+					// HACK: This stops editor modules from loading in flight.  It is a dirty hack and should be fixed.
+					if (!HighLogic.LoadedSceneIsEditor && typeof(IVOID_EditorModule).IsAssignableFrom(loadedType))
+					{
+						continue;
+					}
+
+					sb.AppendFormat("Loading IVOID_Module type {0}...", loadedType.Name);
+
+					try
+					{
+						this.LoadModule(loadedType);
+						sb.AppendLine("Success.");
+					}
+					catch (Exception ex)
+					{
+						sb.AppendFormat("Failed, caught {0}", ex.GetType().Name);
+						sb.AppendLine();
+
+						#if DEBUG
+						Debug.LogException(ex);
+						#endif
+					}
+				}
 			}
 
 			this._modulesLoaded = true;
 
-			Tools.PostDebugMessage(string.Format(
-				"{0}: Loaded {1} modules.",
-				this.GetType().Name,
-				this.Modules.Count
-			));
+			sb.AppendFormat("Loaded {0} modules.", this.Modules.Count);
+			sb.AppendLine();
 		}
 
 		protected void LoadModule(Type T)
@@ -915,10 +941,7 @@
 
 			this.iconStyle = new GUIStyle(GUI.skin.button);
 			this.iconStyle.padding = new RectOffset(0, 0, 0, 0);
-			// this.iconStyle.margin = new RectOffset(0, 0, 0, 0);
-			// this.iconStyle.contentOffset = new Vector2(0, 0);
 			this.iconStyle.overflow = new RectOffset(0, 0, 0, 0);
-			// this.iconStyle.border = new RectOffset(0, 0, 0, 0);
 
 			this.GUIStylesLoaded = true;
 		}
@@ -942,6 +965,7 @@
 			// Do nothing if (the Toolbar is not available.
 			if (!ToolbarManager.ToolbarAvailable)
 			{
+				Tools.PostDebugMessage(this, "Refusing to make a ToolbarButton: ToolbarAvailable = false");
 				return;
 			}
 

--- a/VOID_DataLogger.cs
+++ b/VOID_DataLogger.cs
@@ -229,6 +229,7 @@
 					"Temperature (°C);" +
 					"Gravity (m/s²);" +
 					"Atmosphere Density (g/m³);" +
+					"Downrange Distance  (m);" +
 					"\n"
 				);
 			}
@@ -289,6 +290,10 @@
 			line.Append((vessel.atmDensity * 1000).ToString("F3"));
 			line.Append(';');
 
+			// Downrange Distance
+			line.Append((VOID_Data.downrangeDistance.Value.ToString("G3")));
+			line.Append(';');
+
 			line.Append('\n');
 
 			csvList.Add(line.ToString());

--- a/VOID_EditorCore.cs
+++ b/VOID_EditorCore.cs
@@ -69,6 +69,7 @@
 			if (_initialized)
 			{
 				_instance.StopGUI();
+				_instance.Dispose();
 				_instance = null;
 				_initialized = false;
 			}

--- a/VOID_HUD.cs
+++ b/VOID_HUD.cs
@@ -189,6 +189,19 @@
 					VOID_Data.vesselHeading.ValueUnitString(),
 					VOID_Data.vesselPitch.ToSIString(2)
 				);
+
+				if (
+					this.core.vessel.mainBody == this.core.Kerbin &&
+					(
+						this.core.vessel.situation == Vessel.Situations.FLYING ||
+						this.core.vessel.situation == Vessel.Situations.SUB_ORBITAL ||
+						this.core.vessel.situation == Vessel.Situations.LANDED ||
+						this.core.vessel.situation == Vessel.Situations.SPLASHED
+					)
+				)
+				{
+					rightHUD.AppendFormat("\nRange to KSC: {0}", VOID_Data.downrangeDistance.ValueUnitString(2));
+				}
 			}
 			else
 			{
@@ -226,7 +239,7 @@
 			this.leftHUDPos.value = GUI.Window(
 				this.core.windowID,
 				this.leftHUDPos,
-				this.leftHUDWindow,
+				VOID_Tools.GetWindowHandler(this.leftHUDWindow),
 				GUIContent.none,
 				GUIStyle.none
 			);
@@ -234,7 +247,7 @@
 			this.rightHUDPos.value = GUI.Window(
 				this.core.windowID,
 				this.rightHUDPos,
-				this.rightHUDWindow,
+				VOID_Tools.GetWindowHandler(this.rightHUDWindow),
 				GUIContent.none,
 				GUIStyle.none
 			);
@@ -271,6 +284,44 @@
 			() => core.vessel.getSurfacePitch(),
 			"°"
 		);
+
+		public static readonly VOID_DoubleValue downrangeDistance = new VOID_DoubleValue(
+			"Downrange Distance",
+			delegate() {
+
+			if (core.vessel == null ||
+				Planetarium.fetch == null ||
+				core.vessel.mainBody != Planetarium.fetch.Home
+			)
+			{
+				return double.NaN;
+			}
+
+			double vesselLongitude = core.vessel.longitude * Math.PI / 180d;
+			double vesselLatitude = core.vessel.latitude * Math.PI / 180d;
+
+			const double kscLongitude = 285.442323427289 * Math.PI / 180d;
+			const double kscLatitude = -0.0972112860655246 * Math.PI / 180d;
+
+			double diffLon = vesselLongitude - kscLongitude;
+			double diffLat = vesselLatitude - kscLatitude;
+
+			double sinHalfDiffLat = Math.Sin(diffLat / 2d);
+			double sinHalfDiffLon = Math.Sin(diffLon / 2d);
+
+			double cosVesselLon = Math.Cos(vesselLongitude);
+			double cosKSCLon = Math.Cos(kscLongitude);
+
+			double haversine =
+				sinHalfDiffLat * sinHalfDiffLat +
+				cosVesselLon * cosKSCLon * sinHalfDiffLon * sinHalfDiffLon;
+
+			double arc = 2d * Math.Atan2(Math.Sqrt(haversine), Math.Sqrt(1d - haversine));
+
+			return core.vessel.mainBody.Radius * arc;
+		},
+			"m"
+		);
 	}
 }
 

file:b/VOID_StageInfo.cs (new)
--- /dev/null
+++ b/VOID_StageInfo.cs
@@ -1,1 +1,95 @@
+// VOID © 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 Engineer.VesselSimulator;
+using KSP;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using ToadicusTools;
+using UnityEngine;
+
+namespace VOID
+{
+	public class VOID_StageInfo : VOID_WindowModule
+	{
+		private Table stageTable;
+
+		private Table.Column<int> stageNumberCol;
+		private Table.Column<double> stageDeltaVCol;
+		private Table.Column<double> stageMassCol;
+
+		private bool stylesApplied;
+
+		public VOID_StageInfo() : base()
+		{
+			this._Name = "Stage Information";
+
+			this.stylesApplied = false;
+
+			this.stageTable = new Table();
+
+			this.stageNumberCol = new Table.Column<int>(40f);
+			this.stageTable.Add(this.stageNumberCol);
+
+			this.stageDeltaVCol = new Table.Column<double>(80f);
+			this.stageDeltaVCol.Format = "S2";
+			this.stageTable.Add(this.stageDeltaVCol);
+
+			this.stageMassCol = new Table.Column<double>(80f);
+			this.stageMassCol.Format = "G2";
+			this.stageTable.Add(this.stageMassCol);
+		}
+
+		public override void ModuleWindow(int _)
+		{
+			if (
+				HighLogic.LoadedSceneIsEditor ||
+				(TimeWarp.WarpMode == TimeWarp.Modes.LOW) ||
+				(TimeWarp.CurrentRate <= TimeWarp.MaxPhysicsRate)
+			)
+			{
+				Engineer.VesselSimulator.SimManager.RequestSimulation();
+			}
+
+			if (!this.stylesApplied)
+			{
+				this.stageNumberCol.Style = core.LabelStyles["center"];
+				this.stageDeltaVCol.Style = core.LabelStyles["center"];
+				this.stageMassCol.Style = core.LabelStyles["center"];
+			}
+
+			this.stageTable.ClearColumns();
+
+			if (core.Stages == null || core.Stages.Length == 0)
+			{
+				GUILayout.BeginVertical();
+
+				GUILayout.Label("No stage data!");
+
+				GUILayout.EndVertical();
+
+				return;
+			}
+
+			foreach (Stage stage in core.Stages)
+			{
+				this.stageNumberCol.Add(stage.number);
+
+				this.stageDeltaVCol.Add(stage.deltaV);
+
+				this.stageMassCol.Add(stage.mass);
+			}
+
+			this.stageTable.Render();
+
+			GUI.DragWindow();
+		}
+	}
+
+	public class VOID_StageInfoEditor : VOID_StageInfo, IVOID_EditorModule {}
+}
+
+

--- a/VOID_SurfAtmo.cs
+++ b/VOID_SurfAtmo.cs
@@ -65,6 +65,9 @@
 			this.precisionValues [idx]= (ushort)VOID_Data.terrainElevation.DoGUIHorizontal (this.precisionValues [idx]);
 			idx++;
 
+			this.precisionValues[idx] = (ushort)VOID_Data.downrangeDistance.DoGUIHorizontal(this.precisionValues[idx]);
+			idx++;
+
 			this.precisionValues [idx]= (ushort)VOID_Data.surfVelocity.DoGUIHorizontal (this.precisionValues [idx]);
 			idx++;
 
@@ -76,7 +79,8 @@
 
 			VOID_Data.temperature.DoGUIHorizontal ("F2");
 
-			VOID_Data.atmDensity.DoGUIHorizontal (3);
+			this.precisionValues [idx]= (ushort)VOID_Data.atmDensity.DoGUIHorizontal (this.precisionValues [idx]);
+			idx++;
 
 			VOID_Data.atmPressure.DoGUIHorizontal ("F2");
 

--- a/VOID_Tools.cs
+++ b/VOID_Tools.cs
@@ -341,11 +341,23 @@
 					#endif
 					{
 						Debug.LogWarning(
-							string.Format("[{0}]: ArgumentException caught during window call.", func.Target.GetType().Name)
-						);
+							string.Format("[{0}]: ArgumentException caught during window call.  This is not a bug.",
+								func.Target.GetType().Name
+							));
+
 						/*#if DEBUG
 						Debug.LogException(ex);
 						#endif*/
+					}
+					catch (Exception ex)
+					{
+						Debug.LogError(
+							string.Format("[{0}]: {1} caught during window call.\nMessage:\n{2}\nStackTrace:\n{3}",
+								func.Target.GetType().Name,
+								ex.GetType().Name,
+								ex.Message,
+								ex.StackTrace
+							));
 					}
 				};
 			}