Major reworking of the HUD underpinnings to be more modular and require less code duplication. Also made the Editor HUD movable, but for now it's stuck along the left edge.
[VOID.git] / VOID_EditorHUD.cs
blob:a/VOID_EditorHUD.cs -> blob:b/VOID_EditorHUD.cs
--- a/VOID_EditorHUD.cs
+++ b/VOID_EditorHUD.cs
@@ -30,23 +30,23 @@
 using KSP;
 using System;
 using System.Collections.Generic;
-using System.Linq;
 using System.Text;
 using ToadicusTools;
 using UnityEngine;
 
 namespace VOID
 {
-	public class VOID_EditorHUD : VOID_HUDModule, IVOID_EditorModule
+	[VOID_Scenes(GameScenes.EDITOR)]
+	public class VOID_EditorHUD : VOID_HUDModule
 	{
 		/*
 		 * Fields
 		 * */
-		[AVOID_SaveValue("ehudWindowPos")]
-		protected VOID_SaveValue<Rect> ehudWindowPos;
-
 		protected HUDWindow ehudWindow;
 		protected EditorVesselOverlays _vesselOverlays;
+
+		[AVOID_SaveValue("snapToLeft")]
+		protected VOID_SaveValue<bool> snapToLeft;
 
 		/*
 		 * Properties
@@ -57,9 +57,12 @@
 			{
 				if (this._vesselOverlays == null)
 				{
-					this._vesselOverlays = (EditorVesselOverlays)Resources
-						.FindObjectsOfTypeAll(typeof(EditorVesselOverlays))
-						.FirstOrDefault();
+					UnityEngine.Object[] overlayObjs = Resources.FindObjectsOfTypeAll(typeof(EditorVesselOverlays));
+
+					if (overlayObjs.Length > 0)
+					{
+						this._vesselOverlays = (EditorVesselOverlays)overlayObjs[0];
+					}
 				}
 
 				return this._vesselOverlays;
@@ -97,16 +100,18 @@
 		 * */
 		public VOID_EditorHUD() : base()
 		{
-			this._Name = "Heads-Up Display";
-
-			this.toggleActive = true;
+			this.Name = "Heads-Up Display";
+
+			this.Active = true;
+
+			this.snapToLeft.value = true;
 
 			this.ehudWindow = new HUDWindow(
+				"editorHUD",
 				this.ehudWindowFunc,
 				new Rect(EditorPanels.Instance.partsPanelWidth + 10f, 125f, 300f, 64f)
 			);
 			this.Windows.Add(this.ehudWindow);
-			this.ehudWindowPos = this.ehudWindow.WindowPos;
 
 			Tools.PostDebugMessage (this.GetType().Name + ": Constructed.");
 		}
@@ -120,7 +125,7 @@
 				return;
 			}
 
-			// GUI.skin = AssetBase.GetGUISkin("KSP window 2");
+			VOID_Styles.labelHud.alignment = TextAnchor.UpperLeft;
 
 			hudString.Append("Total Mass: ");
 			hudString.Append(this.core.LastStage.totalMass.ToString("F3"));
@@ -148,8 +153,16 @@
 			hudString.Append("Bottom Stage T/W Ratio: ");
 			hudString.Append(this.core.LastStage.thrustToWeight.ToString("F3"));
 
+			Tools.PostDebugMessage(this,
+				"CoMmarker.gameObject.activeInHierarchy: {0};" +
+				"CoTmarker.gameObject.activeInHierarchy: {1}",
+				this.CoMmarker.gameObject.activeInHierarchy,
+				this.CoTmarker.gameObject.activeInHierarchy
+			);
+
 			if (this.CoMmarker.gameObject.activeInHierarchy && this.CoTmarker.gameObject.activeInHierarchy)
 			{
+				Tools.PostDebugMessage(this, "CoM and CoT markers are active, doing thrust offset.");
 				hudString.Append('\n');
 
 				hudString.Append("Thrust Offset: ");
@@ -159,8 +172,19 @@
 						this.CoMmarker.posMarkerObject.transform.position - this.CoTmarker.posMarkerObject.transform.position
 					).ToString("F3"));
 			}
-
-			GUILayout.Label(hudString.ToString(), VOID_Styles.labelHud, GUILayout.ExpandWidth(true));
+			#if DEBUG
+			else
+			{
+				Tools.PostDebugMessage(this, "CoM and CoT markers are not active, thrust offset skipped.");
+			}
+			#endif
+
+			GUILayout.Label(
+				hudString.ToString(),
+				VOID_Styles.labelHud,
+				GUILayout.ExpandWidth(true),
+				GUILayout.ExpandHeight(true)
+			);
 
 			if (!this.positionsLocked)
 			{
@@ -189,8 +213,6 @@
 			{
 				return;
 			}
-
-			bool snapToEdge = Mathf.Abs(this.ehudWindowPos.value.xMin - hudLeft) < 15f;
 
 			Tools.PostDebugMessage(this,
 				"EditorPartList topLeft.parent.parent.position: {0}\n" +
@@ -200,20 +222,27 @@
 				EditorPartList.Instance.transformTopLeft.parent.parent.position,
 				EditorPartList.Instance.transformTopLeft.parent.position,
 				EditorPartList.Instance.transformTopLeft.position,
-				snapToEdge, this.ehudWindowPos.value.xMin, hudLeft
+				this.snapToLeft, this.ehudWindow.WindowPos.xMin, hudLeft
 			);
 
 			base.DrawGUI();
 
 			Rect hudPos = this.ehudWindow.WindowPos;
 
-			hudPos.xMin = hudLeft;
+			if (this.snapToLeft && this.positionsLocked)
+			{
+				hudPos.xMin = hudLeft;
+			}
+			else
+			{
+				hudPos.xMin = Mathf.Max(hudLeft, hudPos.xMin);
+			}
+
 			hudPos.width = this.ehudWindow.defaultWindowPos.width;
 
-			this.ehudWindowPos.value = hudPos;
-
-			this.ehudWindow.WindowPos = this.ehudWindowPos;
-
+			this.ehudWindow.WindowPos = hudPos;
+
+			this.snapToLeft.value = Mathf.Abs(this.ehudWindow.WindowPos.xMin - hudLeft) < 15f;
 		}
 	}
 }