Version 0.9.10.
[VOID.git] / VOID_Core.cs
blob:a/VOID_Core.cs -> blob:b/VOID_Core.cs
--- a/VOID_Core.cs
+++ b/VOID_Core.cs
@@ -21,9 +21,9 @@
 using System;
 using System.Collections.Generic;
 using System.Linq;
-using System.Reflection;
 using KSP;
 using UnityEngine;
+using Engineer.VesselSimulator;
 
 namespace VOID
 {
@@ -68,7 +68,7 @@
 		 * Fields
 		 * */
 		protected string VoidName = "VOID";
-		protected string VoidVersion = "0.9.10";
+		protected string VoidVersion = "0.9.14";
 
 		protected bool _factoryReset = false;
 
@@ -119,6 +119,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>();
@@ -215,6 +220,22 @@
 			}
 		}
 
+		public float updateTimer
+		{
+			get
+			{
+				return this._updateTimer;
+			}
+		}
+
+		public double updatePeriod
+		{
+			get
+			{
+				return this._updatePeriod;
+			}
+		}
+
 		/*
 		 * Methods
 		 * */
@@ -312,6 +333,13 @@
 		{
 			this.Preload_BeforeUpdate ();
 
+			if (this.vessel != null)
+			{
+				SimManager.Instance.Gravity = VOID_Core.Instance.vessel.mainBody.gravParameter /
+					Math.Pow(VOID_Core.Instance.vessel.mainBody.Radius, 2);
+				SimManager.Instance.TryStartSimulation();
+			}
+
 			if (!this.guiRunning)
 			{
 				this.StartGUI ();
@@ -330,8 +358,8 @@
 				}
 				if (module.guiRunning && !module.toggleActive ||
 				    !this.togglePower ||
-				    !HighLogic.LoadedSceneIsFlight
-				    || this.factoryReset
+				    !HighLogic.LoadedSceneIsFlight ||
+				    this.factoryReset
 				    )
 				{
 					module.StopGUI();
@@ -344,6 +372,7 @@
 			}
 
 			this.CheckAndSave ();
+			this._updateTimer += Time.deltaTime;
 		}
 
 		public void FixedUpdate()
@@ -405,6 +434,9 @@
 
 		protected void LoadGUIStyles()
 		{
+			this.LabelStyles["link"] = new GUIStyle(GUI.skin.label);
+			this.LabelStyles["link"].fontStyle = FontStyle.Bold;
+
 			this.LabelStyles["center"] = new GUIStyle(GUI.skin.label);
 			this.LabelStyles["center"].normal.textColor = Color.white;
 			this.LabelStyles["center"].alignment = TextAnchor.UpperCenter;
@@ -414,9 +446,13 @@
 			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.LabelStyles ["red"] = new GUIStyle(GUI.skin.label);
+			this.LabelStyles ["red"].normal.textColor = Color.red;
+			this.LabelStyles ["red"].alignment = TextAnchor.MiddleCenter;
 
 			this.GUIStylesLoaded = true;
 		}
@@ -437,12 +473,6 @@
 		protected void CheckAndSave()
 		{
 			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)
 			{
@@ -485,10 +515,7 @@
 			}
 			else
 			{
-			    GUIStyle label_txt_red = new GUIStyle(GUI.skin.label);
-			    label_txt_red.normal.textColor = Color.red;
-			    label_txt_red.alignment = TextAnchor.MiddleCenter;
-			    GUILayout.Label("-- POWER LOST --", label_txt_red);
+			    GUILayout.Label("-- POWER LOST --", this.LabelStyles["red"]);
 			}
 
 			this.configWindowMinimized = !GUILayout.Toggle (!this.configWindowMinimized, "Configuration");
@@ -562,6 +589,22 @@
 
 			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 ();
@@ -577,6 +620,7 @@
 				return;
 			}
 
+			/*
 			Tools.PostDebugMessage(string.Format(
 				"Event.current.type: {0}" +
 				"\nthis.VOIDIconLocked: {1}" +
@@ -590,6 +634,7 @@
 				this.VOIDIconPos.value.xMax,
 				this.VOIDIconPos.value.yMax
 				));
+				*/
 
 			if (!this.VOIDIconLocked &&
 			    VOIDIconPos.value.Contains(Event.current.mousePosition)
@@ -646,7 +691,7 @@
 
 			this.VOIDIconTexture = this.VOIDIconOff;  //icon off default
 			if (this.togglePower) this.VOIDIconTexture = this.VOIDIconOn;     //or on if power_toggle==true
-			if (GUI.Button(VOIDIconPos, VOIDIconTexture, new GUIStyle()) && this.VOIDIconLocked)
+			if (GUI.Button(VOIDIconPos, VOIDIconTexture) && this.VOIDIconLocked)
 			{
 				this.mainGuiMinimized = !this.mainGuiMinimized;
 			}
@@ -672,7 +717,7 @@
 				}
 			}
 
-			if (!this.configWindowMinimized)
+			if (!this.configWindowMinimized && !this.mainGuiMinimized)
 			{
 				Rect _configWindowPos = this.configWindowPos;
 
@@ -694,6 +739,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 ();