Major Surgery to change to the new copy-on-update access method for KER.
[VOID.git] / VOID_EditorHUD.cs
blob:a/VOID_EditorHUD.cs -> blob:b/VOID_EditorHUD.cs
--- a/VOID_EditorHUD.cs
+++ b/VOID_EditorHUD.cs
@@ -26,61 +26,43 @@
 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
-using Engineer.VesselSimulator;
+using KerbalEngineer.VesselSimulator;
 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_Module, IVOID_EditorModule
+	[VOID_Scenes(GameScenes.EDITOR)]
+	public class VOID_EditorHUD : VOID_HUDModule
 	{
 		/*
 		 * Fields
 		 * */
-		[AVOID_SaveValue("colorIndex")]
-		protected VOID_SaveValue<int> _colorIndex = 0;
-
-		protected List<Color> textColors = new List<Color>();
-
-		protected GUIStyle labelStyle;
-
+		protected HUDWindow ehudWindow;
 		protected EditorVesselOverlays _vesselOverlays;
+
+		[AVOID_SaveValue("snapToLeft")]
+		protected VOID_SaveValue<bool> snapToLeft;
 
 		/*
 		 * Properties
 		 * */
-		public int ColorIndex
-		{
-			get
-			{
-				return this._colorIndex;
-			}
-			set
-			{
-				if (this._colorIndex >= this.textColors.Count - 1)
-				{
-					this._colorIndex = 0;
-					return;
-				}
-
-				this._colorIndex = value;
-			}
-		}
-
 		protected EditorVesselOverlays vesselOverlays
 		{
 			get
 			{
 				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;
@@ -118,61 +100,32 @@
 		 * */
 		public VOID_EditorHUD() : base()
 		{
-			this._Name = "Heads-Up Display";
-
-			this.toggleActive = true;
-
-			this.textColors.Add(Color.green);
-			this.textColors.Add(Color.black);
-			this.textColors.Add(Color.white);
-			this.textColors.Add(Color.red);
-			this.textColors.Add(Color.blue);
-			this.textColors.Add(Color.yellow);
-			this.textColors.Add(Color.gray);
-			this.textColors.Add(Color.cyan);
-			this.textColors.Add(Color.magenta);
-
-			this.labelStyle = new GUIStyle ();
-			// this.labelStyle.alignment = TextAnchor.UpperRight;
-			this.labelStyle.normal.textColor = this.textColors [this.ColorIndex];
+			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);
 
 			Tools.PostDebugMessage (this.GetType().Name + ": Constructed.");
 		}
 
-		public override void DrawGUI()
-		{
-			SimManager.RequestSimulation();
+		public void ehudWindowFunc(int id)
+		{
+			StringBuilder hudString = Tools.GetStringBuilder();
 
 			if (this.core.LastStage == null)
 			{
 				return;
 			}
 
-			float hudLeft;
-			StringBuilder hudString;
-
-			if (EditorLogic.fetch.editorScreen == EditorLogic.EditorScreen.Parts)
-			{
-				hudLeft = EditorPanels.Instance.partsPanelWidth + 10;
-			}
-			else if (EditorLogic.fetch.editorScreen == EditorLogic.EditorScreen.Actions)
-			{
-				hudLeft = EditorPanels.Instance.actionsPanelWidth + 10;
-			}
-			else
-			{
-				return;
-			}
-
-			GUI.skin = this.core.Skin;
-
-			Rect hudPos = new Rect (hudLeft, 48, 300, 32);
-
-			hudString = new StringBuilder();
-
-			// GUI.skin = AssetBase.GetGUISkin("KSP window 2");
-
-			labelStyle.normal.textColor = textColors [ColorIndex];
+			VOID_Styles.labelHud.alignment = TextAnchor.UpperLeft;
 
 			hudString.Append("Total Mass: ");
 			hudString.Append(this.core.LastStage.totalMass.ToString("F3"));
@@ -200,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: ");
@@ -211,19 +172,79 @@
 						this.CoMmarker.posMarkerObject.transform.position - this.CoTmarker.posMarkerObject.transform.position
 					).ToString("F3"));
 			}
-
-			GUI.Label (
-				hudPos,
+			#if DEBUG
+			else
+			{
+				Tools.PostDebugMessage(this, "CoM and CoT markers are not active, thrust offset skipped.");
+			}
+			#endif
+
+			GUILayout.Label(
 				hudString.ToString(),
-				labelStyle);
-		}
-
-		public override void DrawConfigurables()
-		{
-			if (GUILayout.Button ("Change HUD color", GUILayout.ExpandWidth (false)))
-			{
-				++this.ColorIndex;
-			}
+				VOID_Styles.labelHud,
+				GUILayout.ExpandWidth(true),
+				GUILayout.ExpandHeight(true)
+			);
+
+			if (!this.positionsLocked)
+			{
+				GUI.DragWindow();
+			}
+
+			GUI.BringWindowToBack(id);
+
+			Tools.PutStringBuilder(hudString);
+		}
+
+		public override void DrawGUI()
+		{
+			float hudLeft;
+
+			if (EditorLogic.fetch.editorScreen == EditorScreen.Parts)
+			{
+				hudLeft = EditorPanels.Instance.partsPanelWidth + 10f;
+				hudLeft += EditorPartList.Instance.transformTopLeft.position.x -
+					EditorPartList.Instance.transformTopLeft.parent.parent.position.x -
+					72f;
+			}
+			else if (EditorLogic.fetch.editorScreen == EditorScreen.Actions)
+			{
+				hudLeft = EditorPanels.Instance.actionsPanelWidth + 10f;
+			}
+			else
+			{
+				return;
+			}
+
+			Tools.PostDebugMessage(this,
+				"EditorPartList topLeft.parent.parent.position: {0}\n" +
+				"EditorPartList topLeft.parent.position: {1}\n" +
+				"EditorPartList topLeft.position: {2}\n" +
+				"snapToEdge: {3} (pos.Xmin: {4}; hudLeft: {5})",
+				EditorPartList.Instance.transformTopLeft.parent.parent.position,
+				EditorPartList.Instance.transformTopLeft.parent.position,
+				EditorPartList.Instance.transformTopLeft.position,
+				this.snapToLeft, this.ehudWindow.WindowPos.xMin, hudLeft
+			);
+
+			base.DrawGUI();
+
+			Rect hudPos = this.ehudWindow.WindowPos;
+
+			if (this.snapToLeft && this.positionsLocked)
+			{
+				hudPos.xMin = hudLeft;
+			}
+			else
+			{
+				hudPos.xMin = Mathf.Max(hudLeft, hudPos.xMin);
+			}
+
+			hudPos.width = this.ehudWindow.defaultWindowPos.width;
+
+			this.ehudWindow.WindowPos = hudPos;
+
+			this.snapToLeft.value = Mathf.Abs(this.ehudWindow.WindowPos.xMin - hudLeft) < 15f;
 		}
 	}
 }