Major changes to the way VOID_Core was implemented to allow a more-extensible core framework for use in multiple scenes.
[VOID.git] / VOID_CareerStatus.cs
blob:a/VOID_CareerStatus.cs -> blob:b/VOID_CareerStatus.cs
--- a/VOID_CareerStatus.cs
+++ b/VOID_CareerStatus.cs
@@ -26,37 +26,40 @@
 // 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.
 
+// TODO: Remove ToadicusTools. prefixes after refactor is done.
+
 using KSP;
 using System;
 using System.Text;
-using ToadicusTools;
+
+using ToadicusTools.Text;
 using UnityEngine;
 
 namespace VOID
 {
 	[VOID_Scenes(GameScenes.FLIGHT, GameScenes.EDITOR, GameScenes.SPACECENTER)]
-	public class VOID_CareerStatus : VOID_WindowModule
+	[VOID_GameModes(Game.Modes.CAREER, Game.Modes.SCIENCE_SANDBOX)]
+	public class VOID_CareerStatus : VOID_SingletonWindow<VOID_CareerStatus>
 	{
-		public static VOID_CareerStatus Instance
-		{
-			get;
-			private set;
+		public static string formatDelta(double delta, string numberFormat)
+		{
+			if (delta > 0)
+			{
+				return string.Format("<color='lime'>{0}↑</color>", delta.ToString(numberFormat, SIFormatProvider.SIFormatter));
+			}
+			else if (delta < 0)
+			{
+				return string.Format("<color='red'>{0}↓</color>", delta.ToString(numberFormat, SIFormatProvider.SIFormatter));
+			}
+			else
+			{
+				return "0";
+			}
 		}
 
 		public static string formatDelta(double delta)
 		{
-			if (delta > 0)
-			{
-				return string.Format("<color='green'>{0:#,#.##}↑</color>", delta);
-			}
-			else if (delta < 0)
-			{
-				return string.Format("<color='red'>{0:#,#.##}↓</color>", delta);
-			}
-			else
-			{
-				return string.Intern("0");
-			}
+			return formatDelta(delta, "#,##0.##");
 		}
 
 		public static string formatDelta(float delta)
@@ -76,114 +79,103 @@
 		private Texture2D scienceIcon;
 		#pragma warning restore 0414
 
-		public override bool toggleActive
+		public double lastFundsChange
+		{
+			get;
+			private set;
+		}
+
+		public float lastRepChange
+		{
+			get;
+			private set;
+		}
+
+		public float lastScienceChange
+		{
+			get;
+			private set;
+		}
+
+		public double currentFunds
+		{
+			get;
+			private set;
+		}
+
+		public float currentReputation
+		{
+			get;
+			private set;
+		}
+
+		public float currentScience
+		{
+			get;
+			private set;
+		}
+
+		private bool currenciesInitialized
 		{
 			get
 			{
-				return base.toggleActive && this.inValidGame;
-			}
-			set
-			{
-				base.toggleActive = this.inValidGame && value;
-			}
-		}
-
-		public double lastFundsChange
-		{
-			get;
-			private set;
-		}
-
-		public float lastRepChange
-		{
-			get;
-			private set;
-		}
-
-		public float lastScienceChange
-		{
-			get;
-			private set;
-		}
-
-		public double currentFunds
-		{
-			get;
-			private set;
-		}
-
-		public float currentReputation
-		{
-			get;
-			private set;
-		}
-
-		public float currentScience
-		{
-			get;
-			private set;
-		}
-
-		private bool inValidGame
-		{
-			get
-			{
-				switch (HighLogic.CurrentGame.Mode)
-				{
-					case Game.Modes.CAREER:
-					case Game.Modes.SCIENCE_SANDBOX:
-						return true;
-					default:
-						return false;
-				}
-			}
-		}
-
-		private bool currenciesInitialized
-		{
-			get
-			{
-				return (
-					this.currentFunds == double.NaN ||
-					this.currentScience == float.NaN ||
-					this.currentReputation == float.NaN
+				ToadicusTools.Logging.PostDebugMessage(
+					this,
+					"Checking init state:" +
+					"\n\tcurrentFunds={0}" +
+					"\n\tcurrentScience={1}" +
+					"\n\tcurrentReputation={2}",
+					this.currentFunds,
+					this.currentScience,
+					this.currentReputation
 				);
-			}
-		}
-
-		public override void ModuleWindow(int _)
-		{
-			if (!this.currenciesInitialized)
+
+				return !(
+					double.IsNaN(this.currentFunds) ||
+					float.IsNaN(this.currentScience) ||
+					float.IsNaN(this.currentReputation)
+				);
+			}
+		}
+
+		public override void DrawGUI()
+		{
+			if (Event.current.type != EventType.Layout && !this.currenciesInitialized)
 			{
 				this.initCurrencies();
 			}
 
+			base.DrawGUI();
+		}
+
+		public override void ModuleWindow(int id)
+		{
 			GUILayout.BeginVertical();
 
 			GUILayout.BeginHorizontal(GUILayout.ExpandWidth(true));
 			GUILayout.Label(VOID_Data.fundingStatus.Label);
 			GUILayout.FlexibleSpace();
 			this.fundsContent.text = VOID_Data.fundingStatus.Value;
-			GUILayout.Label(this.fundsContent, GUILayout.ExpandWidth(false));
+			GUILayout.Label(this.fundsContent, GUILayout.ExpandWidth(true));
 			GUILayout.EndHorizontal();
 
 			GUILayout.BeginHorizontal(GUILayout.ExpandWidth(true));
 			GUILayout.Label(VOID_Data.reputationStatus.Label);
 			GUILayout.FlexibleSpace();
 			this.repContent.text = VOID_Data.reputationStatus.Value;
-			GUILayout.Label(this.repContent, GUILayout.ExpandWidth(false));
+			GUILayout.Label(this.repContent, GUILayout.ExpandWidth(true));
 			GUILayout.EndHorizontal();
 
 			GUILayout.BeginHorizontal(GUILayout.ExpandWidth(true));
 			GUILayout.Label(VOID_Data.scienceStatus.Label);
 			GUILayout.FlexibleSpace();
 			this.scienceContent.text = VOID_Data.scienceStatus.Value;
-			GUILayout.Label(this.scienceContent, GUILayout.ExpandWidth(false));
+			GUILayout.Label(this.scienceContent, GUILayout.ExpandWidth(true));
 			GUILayout.EndHorizontal();
 
 			GUILayout.EndVertical();
 
-			GUI.DragWindow();
+			base.ModuleWindow(id);
 		}
 
 		// TODO: Update event handlers to do something useful with the new "reasons" parameter.
@@ -205,8 +197,24 @@
 			this.currentScience = newValue;
 		}
 
+		private void onGameStateLoad(ConfigNode node)
+		{
+			this.initCurrencies();
+		}
+
 		private void initCurrencies()
 		{
+			ToadicusTools.Logging.PostDebugMessage(
+				this,
+				"Initializing currencies." +
+				"\n\tFunding.Instance={0}" +
+				"ResearchAndDevelopment.Instance={1}" +
+				"Reputation.Instance={2}",
+				Funding.Instance == null ? "NULL" : Funding.Instance.ToString(),
+				ResearchAndDevelopment.Instance == null ? "NULL" : ResearchAndDevelopment.Instance.ToString(),
+				Reputation.Instance == null ? "NULL" : Reputation.Instance.ToString()
+			);
+
 			this.currentFunds = Funding.Instance != null ? Funding.Instance.Funds : double.NaN;
 			this.currentReputation = Reputation.Instance != null ? Reputation.Instance.reputation : float.NaN;
 			this.currentScience = ResearchAndDevelopment.Instance != null ?
@@ -222,21 +230,20 @@
 		 * */
 		public VOID_CareerStatus() : base()
 		{
-			VOID_CareerStatus.Instance = this;
-
-			this._Name = "Career Status";
+			this.Name = "Career Status";
 
 			GameEvents.OnFundsChanged.Add(this.onFundsChange);
 			GameEvents.OnReputationChanged.Add(this.onRepChange);
 			GameEvents.OnScienceChanged.Add(this.onScienceChange);
+			GameEvents.onGameStateLoad.Add(this.onGameStateLoad);
 
 			bool texturesLoaded;
 
-			texturesLoaded = IOTools.LoadTexture(out this.fundsIconGreen, "VOID/Textures/fundsgreen.png", 10, 18);
-			texturesLoaded &= IOTools.LoadTexture(out this.fundsIconRed, "VOID/Textures/fundsred.png", 10, 18);
-			texturesLoaded &= IOTools.LoadTexture(out this.reputationIconGreen, "VOID/Textures/repgreen.png", 16, 18);
-			texturesLoaded &= IOTools.LoadTexture(out this.reputationIconRed, "VOID/Textures/repred.png", 16, 18);
-			texturesLoaded &= IOTools.LoadTexture(out this.scienceIcon, "VOID/Textures/science.png", 16, 18);
+			texturesLoaded = ToadicusTools.IOTools.LoadTexture(out this.fundsIconGreen, "VOID/Textures/fundsgreen.png", 10, 18);
+			texturesLoaded &= ToadicusTools.IOTools.LoadTexture(out this.fundsIconRed, "VOID/Textures/fundsred.png", 10, 18);
+			texturesLoaded &= ToadicusTools.IOTools.LoadTexture(out this.reputationIconGreen, "VOID/Textures/repgreen.png", 16, 18);
+			texturesLoaded &= ToadicusTools.IOTools.LoadTexture(out this.reputationIconRed, "VOID/Textures/repred.png", 16, 18);
+			texturesLoaded &= ToadicusTools.IOTools.LoadTexture(out this.scienceIcon, "VOID/Textures/science.png", 16, 18);
 
 			this.fundsContent = new GUIContent();
 			this.repContent = new GUIContent();
@@ -249,16 +256,24 @@
 				this.scienceContent.image = this.scienceIcon;
 			}
 
-			this.initCurrencies();
-		}
-
-		~VOID_CareerStatus()
+			this.currentFunds = double.NaN;
+			this.currentScience = float.NaN;
+			this.currentReputation = float.NaN;
+		}
+
+		public override void Dispose()
 		{
 			GameEvents.OnFundsChanged.Remove(this.onFundsChange);
 			GameEvents.OnReputationChanged.Remove(this.onRepChange);
 			GameEvents.OnScienceChanged.Remove(this.onScienceChange);
-
-			VOID_CareerStatus.Instance = null;
+			GameEvents.onGameStateLoad.Remove(this.onGameStateLoad);
+
+			base.Dispose();
+		}
+
+		~VOID_CareerStatus()
+		{
+			this.Dispose();
 		}
 	}
 }