Revert ".csproj changes for laptop development."
Revert ".csproj changes for laptop development."

This reverts commit 637d9fafe902376c462cc8df4344ed99771137e6.

file:a/Tools.cs -> file:b/Tools.cs
--- a/Tools.cs
+++ b/Tools.cs
@@ -686,6 +686,43 @@
 			GUILayout.Label((dv2 * 1000).ToString("F2") + "m/s", GUILayout.ExpandWidth(false));
 			GUILayout.EndHorizontal();
 		}
+
+		// This implementation is adapted from FARGUIUtils.ClampToScreen
+		public static Rect ClampRectToScreen(Rect window, int xMargin, int yMargin)
+		{
+			window.x = Mathf.Clamp (window.x, xMargin - window.width, Screen.width - xMargin);
+			window.y = Mathf.Clamp (window.y, yMargin - window.height, Screen.height - yMargin);
+
+			return window;
+		}
+
+		public static Rect ClampRectToScreen(Rect window, int Margin)
+		{
+			return ClampRectToScreen(window, Margin, Margin);
+		}
+
+		public static Rect ClampRectToScreen(Rect window)
+		{
+			return ClampRectToScreen (window, 30);
+		}
+
+		public static Vector2 ClampV2ToScreen(Vector2 vec, uint xMargin, uint yMargin)
+		{
+			vec.x = Mathf.Clamp (vec.x, xMargin, Screen.width - xMargin);
+			vec.y = Mathf.Clamp (vec.y, yMargin, Screen.height - yMargin);
+
+			return vec;
+		}
+
+		public static Vector2 ClampV2ToScreen(Vector2 vec, uint Margin)
+		{
+			return ClampV2ToScreen(vec, Margin, Margin);
+		}
+
+		public static Vector2 ClampV2ToScreen(Vector2 vec)
+		{
+			return ClampV2ToScreen (vec, 15);
+		}
 				
 		private static ScreenMessage debugmsg = new ScreenMessage("", 2f, ScreenMessageStyle.UPPER_RIGHT);
 

--- a/VOID.csproj
+++ b/VOID.csproj
@@ -33,6 +33,11 @@
     <WarningLevel>4</WarningLevel>
     <ConsolePause>false</ConsolePause>
     <DefineConstants>TRACE</DefineConstants>
+    <CustomCommands>
+      <CustomCommands>
+        <Command type="AfterBuild" command="/usr/bin/cp -fv ${ProjectDir}/bin/Release/VOID.dll /mnt/games/ssd/KSP_linux/GameData/VOID/Plugins/" />
+      </CustomCommands>
+    </CustomCommands>
   </PropertyGroup>
   <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
   <ItemGroup>

--- a/VOIDFlightMaster.cs
+++ b/VOIDFlightMaster.cs
@@ -46,14 +46,22 @@
 
 		public void Awake()
 		{
-			Tools.PostDebugMessage ("VOIDLightMaster: Waking up.");
+			Tools.PostDebugMessage ("VOIDFlightMaster: Waking up.");
 			this.Core = (VOID_Core)VOID_Core.Instance;
+			this.Core.StopGUI ();
 			this.Core.StartGUI ();
 			Tools.PostDebugMessage ("VOIDFlightMaster: Awake.");
 		}
 
 		public void Update()
 		{
+			if (!HighLogic.LoadedSceneIsEditor)
+			{
+				this.Core = null;
+				VOID_Core.Reset();
+				return;
+			}
+
 			if (this.Core == null)
 			{
 				this.Awake();
@@ -77,12 +85,22 @@
 
 		public void FixedUpdate()
 		{
+			if (this.Core == null || !HighLogic.LoadedSceneIsFlight)
+			{
+				return;
+			}
+
+			this.Core.FixedUpdate ();
+		}
+
+		public void OnGUI()
+		{
 			if (this.Core == null)
 			{
 				return;
 			}
 
-			this.Core.FixedUpdate ();
+			this.Core.OnGUI();
 		}
     }
 
@@ -95,24 +113,26 @@
 		{
 			Tools.PostDebugMessage ("VOIDEditorMaster: Waking up.");
 			this.Core = VOID_EditorCore.Instance;
+			this.Core.StopGUI ();
 			this.Core.StartGUI ();
 			Tools.PostDebugMessage ("VOIDEditorMaster: Awake.");
 		}
 
 		public void Update()
 		{
+			if (!HighLogic.LoadedSceneIsEditor)
+			{
+				this.Core = null;
+				VOID_EditorCore.Reset();
+				return;
+			}
+
 			if (this.Core == null)
 			{
 				this.Awake();
 			}
 
 			this.Core.Update ();
-
-			if (this.Core.vessel != null)
-			{
-				SimManager.Instance.Gravity = this.Core.vessel.mainBody.gravParameter / Math.Pow(this.Core.vessel.mainBody.Radius, 2);
-				SimManager.Instance.TryStartSimulation();
-			}
 
 			if (this.Core.factoryReset)
 			{
@@ -124,13 +144,23 @@
 
 		public void FixedUpdate()
 		{
-			if (this.Core == null)
+			if (this.Core == null || !HighLogic.LoadedSceneIsEditor)
 			{
 				return;
 			}
 
 			this.Core.FixedUpdate ();
 		}
+
+		public void OnGUI()
+		{
+			if (this.Core == null)
+			{
+				return;
+			}
+
+			this.Core.OnGUI();
+		}
 	}
 }
 

--- a/VOID_Core.cs
+++ b/VOID_Core.cs
@@ -97,6 +97,7 @@
 		protected Texture2D VOIDIconTexture;
 		protected string VOIDIconOnPath = "VOID/Textures/void_icon_on";
 		protected string VOIDIconOffPath = "VOID/Textures/void_icon_off";
+		protected bool VOIDIconLocked = true;
 
 		protected int windowBaseID = -96518722;
 		protected int _windowID = 0;
@@ -286,19 +287,22 @@
 			));
 		}
 
+		protected void Preload_BeforeUpdate()
+		{
+			if (!this.bodiesLoaded)
+			{
+				this.LoadAllBodies();
+			}
+
+			if (!this.vesselTypesLoaded)
+			{
+				this.LoadVesselTypes();
+			}
+		}
+
 		public void Update()
 		{
-			this.saveTimer += Time.deltaTime;
-
-			if (!this.bodiesLoaded)
-			{
-				this.LoadAllBodies();
-			}
-
-			if (!this.vesselTypesLoaded)
-			{
-				this.LoadVesselTypes();
-			}
+			this.Preload_BeforeUpdate ();
 
 			if (!this.guiRunning)
 			{
@@ -331,11 +335,7 @@
 				}
 			}
 
-			if (this.saveTimer > 2f)
-			{
-				this.SaveConfig ();
-				this.saveTimer = 0;
-			}
+			this.CheckAndSave ();
 		}
 
 		public void FixedUpdate()
@@ -426,16 +426,31 @@
 			this.vesselTypesLoaded = true;
 		}
 
+		protected void CheckAndSave()
+		{
+			this.saveTimer += Time.deltaTime;
+
+			if (this.saveTimer > 2f)
+			{
+				this.SaveConfig ();
+				this.saveTimer = 0;
+			}
+		}
+
 		public void VOIDMainWindow(int _)
 		{
 			GUILayout.BeginVertical();
 			
-			if (this.powerAvailable)
-			{
-				string str = "ON";
-				if (togglePower) str = "OFF";
-				if (GUILayout.Button("Power " + str)) togglePower = !togglePower;
-			    if (togglePower)
+			if (this.powerAvailable || HighLogic.LoadedSceneIsEditor)
+			{
+				if (!HighLogic.LoadedSceneIsEditor)
+				{
+					string str = "ON";
+					if (togglePower) str = "OFF";
+					if (GUILayout.Button("Power " + str)) togglePower = !togglePower;
+				}
+
+				if (togglePower || HighLogic.LoadedSceneIsEditor)
 			    {
 					foreach (IVOID_Module module in this.Modules)
 					{
@@ -469,7 +484,12 @@
 
 		public override void DrawConfigurables()
 		{
-			this.consumeResource = GUILayout.Toggle (this.consumeResource, "Consume Resources");
+			if (HighLogic.LoadedSceneIsFlight)
+			{
+				this.consumeResource = GUILayout.Toggle (this.consumeResource, "Consume Resources");
+
+				this.VOIDIconLocked = GUILayout.Toggle (this.VOIDIconLocked, "Lock Icon Position");
+			}
 
 			GUILayout.BeginHorizontal(GUILayout.ExpandWidth(true));
 
@@ -510,6 +530,59 @@
 			this._factoryReset = GUILayout.Toggle (this._factoryReset, "Factory Reset");
 		}
 
+		public void OnGUI()
+		{
+			if (Event.current.type == EventType.Repaint)
+			{
+				return;
+			}
+
+			Tools.PostDebugMessage(string.Format(
+				"Event.current.type: {0}" +
+				"\nthis.VOIDIconLocked: {1}" +
+				"\nEvent.current.mousePosition: {2}" +
+				"\nVOIDIconPos: ({3}, {4}),({5}, {6})",
+				Event.current.type,
+				this.VOIDIconLocked,
+				Event.current.mousePosition,
+				this.VOIDIconPos.value.xMin,
+				this.VOIDIconPos.value.yMin,
+				this.VOIDIconPos.value.xMax,
+				this.VOIDIconPos.value.yMax
+				));
+
+			if (!this.VOIDIconLocked &&
+			    VOIDIconPos.value.Contains(Event.current.mousePosition)
+			    && Event.current.type == EventType.mouseDrag
+			    )
+			{
+				Tools.PostDebugMessage(string.Format(
+					"Event.current.type: {0}" +
+					"\ndelta.x: {1}; delta.y: {2}",
+					Event.current.type,
+					Event.current.delta.x,
+					Event.current.delta.y
+				));
+
+				Rect tmp = new Rect(VOIDIconPos);
+
+				tmp.x = Event.current.mousePosition.x - tmp.width / 2;
+				tmp.y = Event.current.mousePosition.y - tmp.height / 2;
+
+				if (tmp.x > Screen.width - tmp.width)
+				{
+					tmp.x = Screen.width - tmp.width;
+				}
+
+				if (tmp.y > Screen.height - tmp.height)
+				{
+					tmp.y = Screen.height - tmp.height;
+				}
+
+				VOIDIconPos = tmp;
+			}
+		}
+
 		public override void DrawGUI()
 		{
 			if (!this._modulesLoaded)
@@ -533,7 +606,7 @@
 
 			this.VOIDIconTexture = this.VOIDIconOff;  //icon off default
 			if (this.togglePower) this.VOIDIconTexture = this.VOIDIconOn;     //or on if power_toggle==true
-			if (GUI.Button(new Rect(VOIDIconPos), VOIDIconTexture, new GUIStyle()))
+			if (GUI.Button(VOIDIconPos, VOIDIconTexture, new GUIStyle()) && this.VOIDIconLocked)
 			{
 				this.mainGuiMinimized = !this.mainGuiMinimized;
 			}
@@ -551,6 +624,8 @@
 					GUILayout.Height (50)
 				);
 
+				_mainWindowPos = Tools.ClampRectToScreen (_mainWindowPos);
+
 				if (_mainWindowPos != this.mainWindowPos)
 				{
 					this.mainWindowPos = _mainWindowPos;
@@ -570,6 +645,8 @@
 					GUILayout.Height (50)
 				);
 
+				_configWindowPos = Tools.ClampRectToScreen (_configWindowPos);
+
 				if (_configWindowPos != this.configWindowPos)
 				{
 					this.configWindowPos = _configWindowPos;

--- a/VOID_EditorCore.cs
+++ b/VOID_EditorCore.cs
@@ -18,6 +18,7 @@
 //
 //  You should have received a copy of the GNU General Public License
 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+using Engineer.VesselSimulator;
 using KSP;
 using System;
 using System.Collections.Generic;
@@ -56,9 +57,12 @@
 
 		public new static void Reset()
 		{
-			_instance.StopGUI();
-			_instance = null;
-			_initialized = false;
+			if (_initialized)
+			{
+				_instance.StopGUI();
+				_instance = null;
+				_initialized = false;
+			}
 		}
 
 		public VOID_EditorCore() : base()
@@ -66,26 +70,124 @@
 			this._Name = "VOID Editor Core";
 		}
 
+		public new void OnGUI() {}
+
 		public override void DrawGUI()
 		{
 			if (!this._modulesLoaded)
 			{
 				this.LoadModulesOfType<IVOID_EditorModule>();
 			}
+
+			Rect _mainPos = new Rect (this.mainWindowPos);
+
+			Rect _iconPos = new Rect(this.VOIDIconPos);
+			Vector2 _iconCtr = new Vector2 ();
+
+			// HACK: This is really messy.  Clean it up.
+			if (_mainPos.yMax > Screen.height - 30 ||
+			    _mainPos.yMin < 30
+			    )
+			{
+				if (_mainPos.xMax > Screen.width - 30 ||
+				    _mainPos.xMin < 30
+				    )
+				{
+					if (_mainPos.yMax < Screen.height / 2)
+					{
+						_iconCtr.y = _mainPos.yMax + 15;
+					}
+					else
+					{
+						_iconCtr.y = _mainPos.yMin - 15;
+					}
+				}
+				else
+				{
+					if (_mainPos.yMax > Screen.height / 2)
+					{
+						_iconCtr.y = _mainPos.yMax - 15;
+					}
+					else
+					{
+						_iconCtr.y = _mainPos.yMin + 15;
+					}
+				}
+
+				if (_mainPos.center.x < Screen.width / 2)
+				{
+					_iconCtr.x = _mainPos.xMin - 15;
+				}
+				else
+				{
+					_iconCtr.x = _mainPos.xMax + 15;
+				}
+
+			}
+			else
+			{
+				if (_mainPos.xMax > Screen.width - 30)
+				{
+					_iconCtr.x = _mainPos.xMax - 15;
+				}
+				else if (_mainPos.xMin < 30)
+				{
+					_iconCtr.x = _mainPos.xMin + 15;
+				}
+				else
+				{
+					_iconCtr.x = _mainPos.center.x;
+				}
+
+				_iconCtr.y = _mainPos.yMin - 15;
+			}
+
+			_iconCtr = Tools.ClampV2ToScreen (_iconCtr, 15);
+			_iconPos.center = _iconCtr;
+
+			if (_iconPos != this.VOIDIconPos)
+			{
+				this.VOIDIconPos = _iconPos;
+			}
+
+			base.DrawGUI();
 		}
 
 		public new void Update()
 		{
 			foreach (IVOID_EditorModule module in this.Modules)
 			{
-				if (HighLogic.LoadedSceneIsEditor && module.toggleActive)
+				if (EditorLogic.startPod == null)
+				{
+					module.StopGUI();
+					continue;
+				}
+				if (HighLogic.LoadedSceneIsEditor && module.toggleActive && EditorLogic.SortedShipList.Any())
 				{
 					module.StartGUI();
 				}
-				if (!HighLogic.LoadedSceneIsEditor || !module.toggleActive)
+				if (!HighLogic.LoadedSceneIsEditor || !module.toggleActive || !EditorLogic.SortedShipList.Any())
 				{
 					module.StopGUI();
 				}
+
+				this.CheckAndSave ();
+			}
+
+			if (EditorLogic.startPod == null || !HighLogic.LoadedSceneIsEditor)
+			{
+				this.StopGUI();
+				return;
+			}
+			else if (!this.guiRunning && HighLogic.LoadedSceneIsEditor)
+			{
+				this.StartGUI();
+			}
+
+			if (EditorLogic.SortedShipList.Count > 0)
+			{
+				SimManager.Instance.Gravity = 9.08665;
+				SimManager.Instance.TryStartSimulation();
 			}
 		}
 

--- a/VOID_EditorHUD.cs
+++ b/VOID_EditorHUD.cs
@@ -20,10 +20,11 @@
 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 //
 
+using Engineer.VesselSimulator;
 using KSP;
-using UnityEngine;
 using System;
 using System.Collections.Generic;
+using UnityEngine;
 
 namespace VOID
 {
@@ -80,6 +81,7 @@
 			this.textColors.Add(Color.magenta);
 
 			this.labelStyle = new GUIStyle ();
+			this.labelStyle.alignment = TextAnchor.UpperRight;
 			this.labelStyle.normal.textColor = this.textColors [this.ColorIndex];
 
 			Tools.PostDebugMessage (this.GetType().Name + ": Constructed.");
@@ -87,13 +89,24 @@
 
 		public override void DrawGUI()
 		{
-			GUI.skin = AssetBase.GetGUISkin("KSP window 2");
+			SimManager.Instance.RequestSimulation();
+
+			if (SimManager.Instance.LastStage == null)
+			{
+				return;
+			}
+
+			// GUI.skin = AssetBase.GetGUISkin("KSP window 2");
 
 			labelStyle.normal.textColor = textColors [ColorIndex];
 
 			GUI.Label (
-				new Rect (Screen.width - 310, 80, 300f, 90f),
-				"Sample HUD Line 1\nSample HUD Line 2\nSample HUD Line 3\nSample HUD Line 4\n",
+				new Rect (Screen.width - 310, 80, 300f, 32f),
+				"Total Mass: " + SimManager.Instance.LastStage.totalMass.ToString("F3") + "t" +
+				" Part Count: " + EditorLogic.SortedShipList.Count +
+				"\nTotal Delta-V: " + Tools.MuMech_ToSI(SimManager.Instance.LastStage.totalDeltaV) + "m/s" +
+				"\nBottom Stage Delta-V: " + Tools.MuMech_ToSI(SimManager.Instance.LastStage.deltaV) + "m/s" +
+				"\nBottom Stage T/W Ratio: " + SimManager.Instance.LastStage.thrustToWeight.ToString("F3"),
 				labelStyle);
 		}
 

--- a/VOID_Module.cs
+++ b/VOID_Module.cs
@@ -81,7 +81,7 @@
 		 * */
 		public void StartGUI()
 		{
-			if (!this.toggleActive)
+			if (!this.toggleActive || this.guiRunning)
 			{
 				return;
 			}
@@ -93,6 +93,10 @@
 
 		public void StopGUI()
 		{
+			if (!this.guiRunning)
+			{
+				return;
+			}
 			Tools.PostDebugMessage (string.Format("Removing {0} from the draw queue.", this.GetType().Name));
 			RenderingManager.RemoveFromPostDrawQueue (3, this.DrawGUI);
 			this._Running = false;
@@ -204,6 +208,8 @@
 				GUILayout.Height(this.defHeight)
 			);
 
+			_Pos = Tools.ClampRectToScreen (_Pos);
+
 			if (_Pos != this.WindowPos)
 			{
 				this.WindowPos = _Pos;