Got the VOID windows and icon behaving acceptably in the editor.
[VOID.git] / VOID_Core.cs
blob:a/VOID_Core.cs -> blob:b/VOID_Core.cs
--- a/VOID_Core.cs
+++ b/VOID_Core.cs
@@ -68,7 +68,7 @@
 		 * Fields
 		 * */
 		protected string VoidName = "VOID";
-		protected string VoidVersion = "0.9.9";
+		protected string VoidVersion = "0.9.13";
 
 		protected bool _factoryReset = false;
 
@@ -120,6 +120,11 @@
 		[AVOID_SaveValue("resourceRate")]
 		protected VOID_SaveValue<float> resourceRate = 0.2f;
 
+		[AVOID_SaveValue("updatePeriod")]
+		protected VOID_SaveValue<double> _updatePeriod = 1001f/15000f;
+		protected float _updateTimer = 0f;
+		protected string stringFrequency;
+
 		// Celestial Body Housekeeping
 		protected List<CelestialBody> _allBodies = new List<CelestialBody>();
 		protected bool bodiesLoaded = false;
@@ -130,8 +135,9 @@
 
 		public float saveTimer = 0;
 
-		protected string defaultSkin = "KSP window 2";
-		protected VOID_SaveValue<int> _skinIdx = int.MinValue;
+		[AVOID_SaveValue("defaultSkin")]
+		protected VOID_SaveValue<string> defaultSkin = "KSP window 2";
+		protected int _skinIdx = int.MinValue;
 		protected List<GUISkin> skin_list;
 		protected string[] forbiddenSkins =
 		{
@@ -214,6 +220,22 @@
 			}
 		}
 
+		public float updateTimer
+		{
+			get
+			{
+				return this._updateTimer;
+			}
+		}
+
+		public double updatePeriod
+		{
+			get
+			{
+				return this._updatePeriod;
+			}
+		}
+
 		/*
 		 * Methods
 		 * */
@@ -246,6 +268,13 @@
 				));
 			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,
@@ -322,8 +351,8 @@
 				}
 				if (module.guiRunning && !module.toggleActive ||
 				    !this.togglePower ||
-				    !HighLogic.LoadedSceneIsFlight
-				    || this.factoryReset
+				    !HighLogic.LoadedSceneIsFlight ||
+				    this.factoryReset
 				    )
 				{
 					module.StopGUI();
@@ -336,6 +365,7 @@
 			}
 
 			this.CheckAndSave ();
+			this._updateTimer += Time.deltaTime;
 		}
 
 		public void FixedUpdate()
@@ -406,9 +436,9 @@
 			this.LabelStyles["center_bold"].alignment = TextAnchor.UpperCenter;
 			this.LabelStyles["center_bold"].fontStyle = FontStyle.Bold;
 
-			this.LabelStyles["txt_right"] = new GUIStyle(GUI.skin.label);
-			this.LabelStyles["txt_right"].normal.textColor = Color.white;
-			this.LabelStyles["txt_right"].alignment = TextAnchor.UpperRight;
+			this.LabelStyles["right"] = new GUIStyle(GUI.skin.label);
+			this.LabelStyles["right"].normal.textColor = Color.white;
+			this.LabelStyles["right"].alignment = TextAnchor.UpperRight;
 
 			this.GUIStylesLoaded = true;
 		}
@@ -432,6 +462,17 @@
 
 			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;
 			}
@@ -503,7 +544,12 @@
 			{
 				this._skinIdx--;
 				if (this._skinIdx < 0) this._skinIdx = skin_list.Count - 1;
-				Tools.PostDebugMessage("[VOID] new this._skin = " + this._skinIdx + " :: skin_list.Count = " + skin_list.Count);
+				Tools.PostDebugMessage (string.Format (
+					"{0}: new this._skinIdx = {1} :: skin_list.Count = {2}",
+					this.GetType().Name,
+					this._skinIdx,
+					this.skin_list.Count
+				));
 			}
 
 			string skin_name = skin_list[this._skinIdx].name;
@@ -517,11 +563,37 @@
 			{
 				this._skinIdx++;
 				if (this._skinIdx >= skin_list.Count) this._skinIdx = 0;
-				Tools.PostDebugMessage("[VOID] new this._skin = " + this._skinIdx + " :: skin_list.Count = " + skin_list.Count);
+				Tools.PostDebugMessage (string.Format (
+					"{0}: new this._skinIdx = {1} :: skin_list.Count = {2}",
+					this.GetType().Name,
+					this._skinIdx,
+					this.skin_list.Count
+					));
+			}
+
+			if (this.Skin.name != this.defaultSkin)
+			{
+				this.defaultSkin = this.Skin.name;
 			}
 
 			GUILayout.EndHorizontal();
 
+			GUILayout.BeginHorizontal();
+			GUILayout.Label("Update Rate (Hz):");
+			if (this.stringFrequency == null)
+			{
+				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;
+				double.TryParse(stringFrequency, out updateFreq);
+				this._updatePeriod = 1 / updateFreq;
+			}
+			GUILayout.EndHorizontal();
+
 			foreach (IVOID_Module mod in this.Modules)
 			{
 				mod.DrawConfigurables ();
@@ -537,6 +609,7 @@
 				return;
 			}
 
+			/*
 			Tools.PostDebugMessage(string.Format(
 				"Event.current.type: {0}" +
 				"\nthis.VOIDIconLocked: {1}" +
@@ -550,6 +623,7 @@
 				this.VOIDIconPos.value.xMax,
 				this.VOIDIconPos.value.yMax
 				));
+				*/
 
 			if (!this.VOIDIconLocked &&
 			    VOIDIconPos.value.Contains(Event.current.mousePosition)
@@ -632,7 +706,7 @@
 				}
 			}
 
-			if (!this.configWindowMinimized)
+			if (!this.configWindowMinimized && !this.mainGuiMinimized)
 			{
 				Rect _configWindowPos = this.configWindowPos;
 
@@ -654,6 +728,19 @@
 			}
 		}
 
+		public void ResetGUI()
+		{
+			this.StopGUI ();
+
+			foreach (IVOID_Module module in this.Modules)
+			{
+				module.StopGUI ();
+				module.StartGUI ();
+			}
+
+			this.StartGUI ();
+		}
+
 		public override void LoadConfig()
 		{
 			base.LoadConfig ();
@@ -666,11 +753,6 @@
 
 		public void SaveConfig()
 		{
-			if (!this.configDirty)
-			{
-				return;
-			}
-
 			var config = KSP.IO.PluginConfiguration.CreateForType<VOID_Core> ();
 			config.load ();