VOID_CareerStatus: Changes to texture loading and now disabling the module entirely for non-career saves.
[VOID.git] / VOID_CareerStatus.cs
blob:a/VOID_CareerStatus.cs -> blob:b/VOID_CareerStatus.cs
--- a/VOID_CareerStatus.cs
+++ b/VOID_CareerStatus.cs
@@ -34,6 +34,7 @@
 
 namespace VOID
 {
+	[VOID_Scenes(GameScenes.FLIGHT, GameScenes.EDITOR, GameScenes.SPACECENTER)]
 	public class VOID_CareerStatus : VOID_WindowModule
 	{
 		public static VOID_CareerStatus Instance
@@ -67,13 +68,63 @@
 		private GUIContent repContent;
 		private GUIContent scienceContent;
 
+		#pragma warning disable 0414
 		private Texture2D fundsIconGreen;
 		private Texture2D fundsIconRed;
 		private Texture2D reputationIconGreen;
 		private Texture2D reputationIconRed;
 		private Texture2D scienceIcon;
+		#pragma warning restore 0414
 
 		public override bool toggleActive
+		{
+			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
 			{
@@ -81,59 +132,44 @@
 				{
 					case Game.Modes.CAREER:
 					case Game.Modes.SCIENCE_SANDBOX:
-						return base.toggleActive;
+						return true;
 					default:
 						return false;
 				}
 			}
-			set
-			{
-				switch (HighLogic.CurrentGame.Mode)
-				{
-					case Game.Modes.CAREER:
-					case Game.Modes.SCIENCE_SANDBOX:
-						base.toggleActive = value;
-						break;
-					default:
-						return;
-				}
-			}
-		}
-
-		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
+			{
+				Tools.PostDebugMessage(
+					this,
+					"Checking init state:" +
+					"\n\tcurrentFunds={0}" +
+					"\n\tcurrentScience={1}" +
+					"\n\tcurrentReputation={2}",
+					this.currentFunds,
+					this.currentScience,
+					this.currentReputation
+				);
+
+				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 _)
@@ -144,21 +180,21 @@
 			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();
@@ -166,22 +202,42 @@
 			GUI.DragWindow();
 		}
 
-		private void onFundsChange(double newValue)
+		// TODO: Update event handlers to do something useful with the new "reasons" parameter.
+		private void onFundsChange(double newValue, TransactionReasons reasons)
 		{
 			this.lastFundsChange = newValue - this.currentFunds;
 			this.currentFunds = newValue;
 		}
 
-		private void onRepChange(float newValue)
+		private void onRepChange(float newValue, TransactionReasons reasons)
 		{
 			this.lastRepChange = newValue - this.currentReputation;
 			this.currentReputation = newValue;
 		}
 
-		private void onScienceChange(float newValue)
+		private void onScienceChange(float newValue, TransactionReasons reasons)
 		{
 			this.lastScienceChange = newValue - this.currentScience;
 			this.currentScience = newValue;
+		}
+
+		private void initCurrencies()
+		{
+			Tools.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 ?
+				ResearchAndDevelopment.Instance.Science : float.NaN;
 		}
 
 		/*
@@ -195,7 +251,7 @@
 		{
 			VOID_CareerStatus.Instance = this;
 
-			this._Name = "Career Status";
+			this.Name = "Career Status";
 
 			GameEvents.OnFundsChanged.Add(this.onFundsChange);
 			GameEvents.OnReputationChanged.Add(this.onRepChange);
@@ -220,10 +276,9 @@
 				this.scienceContent.image = this.scienceIcon;
 			}
 
-			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 ?
-				ResearchAndDevelopment.Instance.Science : float.NaN;
+			this.currentFunds = double.NaN;
+			this.currentScience = float.NaN;
+			this.currentReputation = float.NaN;
 		}
 
 		~VOID_CareerStatus()
@@ -235,57 +290,6 @@
 			VOID_CareerStatus.Instance = null;
 		}
 	}
-
-	public static partial class VOID_Data
-	{
-		public static readonly VOID_StrValue fundingStatus = new VOID_StrValue(
-			string.Intern("Funds"),
-			delegate()
-		{
-			if (VOID_CareerStatus.Instance == null)
-			{
-				return string.Empty;
-			}
-
-			return string.Format("{0} ({1})",
-				VOID_CareerStatus.Instance.currentFunds.ToString("#,#.##"),
-				VOID_CareerStatus.formatDelta(VOID_CareerStatus.Instance.lastFundsChange)
-			);
-		}
-		);
-
-		public static readonly VOID_StrValue reputationStatus = new VOID_StrValue(
-			string.Intern("Reputation"),
-			delegate()
-		{
-			if (VOID_CareerStatus.Instance == null)
-			{
-				return string.Empty;
-			}
-
-			return string.Format("{0} ({1})",
-				VOID_CareerStatus.Instance.currentReputation.ToString("#,#.##"),
-				VOID_CareerStatus.formatDelta(VOID_CareerStatus.Instance.lastRepChange)
-			);
-		}
-		);
-
-		public static readonly VOID_StrValue scienceStatus = new VOID_StrValue(
-			string.Intern("Science"),
-			delegate()
-		{
-			if (VOID_CareerStatus.Instance == null)
-			{
-				return string.Empty;
-			}
-
-			return string.Format("{0} ({1})",
-				VOID_CareerStatus.Instance.currentScience.ToString("#,#.##"),
-				VOID_CareerStatus.formatDelta(VOID_CareerStatus.Instance.lastScienceChange)
-			);
-		}
-		);
-	}
 }