Functionified the docktowindow stuff, maybe cleaned it up a little kinda?
Functionified the docktowindow stuff, maybe cleaned it up a little kinda?

file:a/Tools.cs -> file:b/Tools.cs
--- a/Tools.cs
+++ b/Tools.cs
@@ -723,6 +723,99 @@
 		{
 			return ClampV2ToScreen (vec, 15);
 		}
+
+		// UNDONE: This seems messy.  Can we clean it up?
+		public static Rect DockToWindow(Rect icon, Rect window)
+		{
+			// We can't set the x and y of the center point directly, so build a new vector.
+			Vector2 center = new Vector2 ();
+
+			// If we are near the top or bottom of the screen...
+			if (window.yMax > Screen.height - icon.height ||
+			    window.yMin < icon.height
+			    )
+			{
+				// If we are in a corner...
+				if (window.xMax > Screen.width - icon.width ||
+				    window.xMin < icon.width
+				    )
+				{
+					// If it is a top corner, put the icon below the window.
+					if (window.yMax < Screen.height / 2)
+					{
+						center.y = window.yMax + icon.height / 2;
+					}
+					// If it is a bottom corner, put the icon above the window.
+					else
+					{
+						center.y = window.yMin - icon.height / 2;
+					}
+				}
+				// If we are not in a corner...
+				else
+				{
+					// If we are along the top edge, align the icon's top edge with the top edge of the window
+					if (window.yMax > Screen.height / 2)
+					{
+						center.y = window.yMax - icon.height / 2;
+					}
+					// If we are along the bottom edge, align the icon's bottom edge with the bottom edge of the window
+					else
+					{
+						center.y = window.yMin + icon.height / 2;
+					}
+				}
+
+				// At the top or bottom, if we are towards the right, put the icon to the right of the window
+				if (window.center.x < Screen.width / 2)
+				{
+					center.x = window.xMin - icon.width / 2;
+				}
+				// At the top or bottom, if we are towards the left, put the icon to the left of the window
+				else
+				{
+					center.x = window.xMax + icon.width / 2;
+				}
+
+			}
+			// If we are not along the top or bottom of the screen...
+			else
+			{
+				// By default, center the icon above the window
+				center.y = window.yMin - icon.height / 2;
+				center.x = window.center.x;
+
+				// If we are along a side...
+				if (window.xMax > Screen.width - icon.width ||
+				    window.xMin < icon.width
+				    )
+				{
+					// UNDONE: I'm not sure I like the feel of this part.
+					// If we are along a side towards the bottom, put the icon below the window
+					if (window.center.y > Screen.height / 2)
+					{
+						center.y = window.yMax + icon.height / 2;
+					}
+
+					// Along the left side, align the left edge of the icon with the left edge of the window.
+					if (window.xMax > Screen.width - icon.width)
+					{
+						center.x = window.xMax - icon.width / 2;
+					}
+					// Along the right side, align the right edge of the icon with the right edge of the window.
+					else if (window.xMin < icon.width)
+					{
+						center.x = window.xMin + icon.width / 2;
+					}
+				}
+			}
+
+			// Assign the vector to the center of the rect.
+			icon.center = center;
+
+			// Return the icon's position.
+			return icon;
+		}
 				
 		private static ScreenMessage debugmsg = new ScreenMessage("", 2f, ScreenMessageStyle.UPPER_RIGHT);
 

--- a/VOID_Core.cs
+++ b/VOID_Core.cs
@@ -430,8 +430,25 @@
 		{
 			this.saveTimer += Time.deltaTime;
 
+			Tools.PostDebugMessage (string.Format (
+				"{0}: Checking if time to save; saveTimer: {1}",
+				this.GetType ().Name,
+				this.saveTimer
+			));
+
 			if (this.saveTimer > 2f)
 			{
+				Tools.PostDebugMessage (string.Format (
+					"{0}: Time to save, checking if configDirty: {1}",
+					this.GetType ().Name,
+					this.configDirty
+					));
+
+				if (!this.configDirty)
+				{
+					return;
+				}
+
 				this.SaveConfig ();
 				this.saveTimer = 0;
 			}
@@ -666,11 +683,6 @@
 
 		public void SaveConfig()
 		{
-			if (!this.configDirty)
-			{
-				return;
-			}
-
 			var config = KSP.IO.PluginConfiguration.CreateForType<VOID_Core> ();
 			config.load ();
 

--- a/VOID_EditorCore.cs
+++ b/VOID_EditorCore.cs
@@ -79,71 +79,9 @@
 				this.LoadModulesOfType<IVOID_EditorModule>();
 			}
 
-			Rect _mainPos = new Rect (this.mainWindowPos);
+			Rect _iconPos = Tools.DockToWindow (this.VOIDIconPos, 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;
+			_iconPos = Tools.ClampRectToScreen (_iconPos, (int)_iconPos.width, (int)_iconPos.height);
 
 			if (_iconPos != this.VOIDIconPos)
 			{
@@ -170,8 +108,6 @@
 				{
 					module.StopGUI();
 				}
-
-				this.CheckAndSave ();
 			}
 
 			if (EditorLogic.startPod == null || !HighLogic.LoadedSceneIsEditor)
@@ -189,6 +125,8 @@
 				SimManager.Instance.Gravity = 9.08665;
 				SimManager.Instance.TryStartSimulation();
 			}
+
+			this.CheckAndSave ();
 		}
 
 		public new void FixedUpdate() {}