New CareerStatus module!
[VOID.git] / VOID_CareerStatus.cs
blob:a/VOID_CareerStatus.cs -> blob:b/VOID_CareerStatus.cs
// VOID // VOID
// //
// VOID_CareerStatus.cs // VOID_CareerStatus.cs
// //
// Copyright © 2014, toadicus // Copyright © 2014, toadicus
// All rights reserved. // All rights reserved.
// //
// Redistribution and use in source and binary forms, with or without modification, // Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met: // are permitted provided that the following conditions are met:
// //
// 1. Redistributions of source code must retain the above copyright notice, // 1. Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer. // this list of conditions and the following disclaimer.
// //
// 2. Redistributions in binary form must reproduce the above copyright notice, // 2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation and/or other // this list of conditions and the following disclaimer in the documentation and/or other
// materials provided with the distribution. // materials provided with the distribution.
// //
// 3. Neither the name of the copyright holder nor the names of its contributors may be used // 3. Neither the name of the copyright holder nor the names of its contributors may be used
// to endorse or promote products derived from this software without specific prior written permission. // to endorse or promote products derived from this software without specific prior written permission.
// //
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE // 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. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   
using KSP; using KSP;
using System; using System;
using System.Text; using System.Text;
using ToadicusTools; using ToadicusTools;
using UnityEngine; using UnityEngine;
   
namespace VOID namespace VOID
{ {
  [VOID_Scenes(GameScenes.FLIGHT, GameScenes.EDITOR, GameScenes.SPACECENTER)]
public class VOID_CareerStatus : VOID_WindowModule public class VOID_CareerStatus : VOID_WindowModule
{ {
public static VOID_CareerStatus Instance public static VOID_CareerStatus Instance
{ {
get; get;
private set; private set;
} }
   
public static string formatDelta(double delta) public static string formatDelta(double delta)
{ {
if (delta > 0) if (delta > 0)
{ {
return string.Format(string.Intern("<color='green'>{0:#,#.##}↑</color>"), delta); return string.Format("<color='green'>{0:#,#.##}↑</color>", delta);
} }
else if (delta < 0) else if (delta < 0)
{ {
return string.Format(string.Intern("<color='red'>{0:#,#.##}↓</color>"), delta); return string.Format("<color='red'>{0:#,#.##}↓</color>", delta);
} }
else else
{ {
return string.Intern("0"); return string.Intern("0");
} }
} }
   
public static string formatDelta(float delta) public static string formatDelta(float delta)
{ {
return formatDelta((double)delta); return formatDelta((double)delta);
} }
   
private int fontSize; private GUIContent fundsContent;
private Texture2D origFundsGreen; private GUIContent repContent;
private Texture2D origFundsRed; private GUIContent scienceContent;
private Texture2D origRepGreen;  
private Texture2D origRepRed; #pragma warning disable 0414
private Texture2D origScience; private Texture2D fundsIconGreen;
  private Texture2D fundsIconRed;
public Texture2D fundsIconGreen private Texture2D reputationIconGreen;
{ private Texture2D reputationIconRed;
get; private Texture2D scienceIcon;
private set; #pragma warning restore 0414
}  
  public override bool toggleActive
public Texture2D fundsIconRed {
{ get
get; {
private set; return base.toggleActive && this.inValidGame;
} }
  set
public Texture2D reputationIconGreen {
{ base.toggleActive = this.inValidGame && value;
get; }
private set;  
}  
   
public Texture2D reputationIconRed  
{  
get;  
private set;  
}  
   
public Texture2D scienceIcon  
{  
get;  
private set;  
} }
   
public double lastFundsChange public double lastFundsChange
{ {
get; get;
private set; private set;
} }
   
public float lastRepChange public float lastRepChange
{ {
get; get;
private set; private set;
} }
   
public float lastScienceChange public float lastScienceChange
{ {
get; get;
private set; private set;
} }
   
public double currentFunds public double currentFunds
{ {
  get;
  private set;
  }
   
  public float currentReputation
  {
  get;
  private set;
  }
   
  public float currentScience
  {
  get;
  private set;
  }
   
  private bool inValidGame
  {
get get
{ {
if (Funding.Instance != null) switch (HighLogic.CurrentGame.Mode)
{ {
return Funding.Instance.Funds; case Game.Modes.CAREER:
  case Game.Modes.SCIENCE_SANDBOX:
  return true;
  default:
  return false;
} }
else }
{ }
return double.NaN;  
} private bool currenciesInitialized
}  
}  
   
public float currentReputation  
{ {
get get
{ {
if (Reputation.Instance != null) Tools.PostDebugMessage(
{ this,
return Reputation.Instance.reputation; "Checking init state:" +
} "\n\tcurrentFunds={0}" +
else "\n\tcurrentScience={1}" +
{ "\n\tcurrentReputation={2}",
return float.NaN; this.currentFunds,
} this.currentScience,
} this.currentReputation
} );
   
public float currentScience return !(
{ double.IsNaN(this.currentFunds) ||
get float.IsNaN(this.currentScience) ||
{ float.IsNaN(this.currentReputation)
if (ResearchAndDevelopment.Instance != null) );
{ }
return ResearchAndDevelopment.Instance.Science; }
}  
else public override void DrawGUI()
{ {
return float.NaN; if (Event.current.type != EventType.Layout && !this.currenciesInitialized)
} {
} this.initCurrencies();
  }
   
  base.DrawGUI();
} }
   
public override void ModuleWindow(int _) public override void ModuleWindow(int _)
{ {
   
/*if (this.fontSize != this.core.Skin.label.fontSize)  
{  
this.fontSize = this.core.Skin.label.fontSize;  
   
this.fundsIconGreen = this.origFundsGreen.Clone().ResizeByHeight(this.fontSize);  
this.fundsIconRed = this.origFundsRed.Clone().ResizeByHeight(this.fontSize);  
this.reputationIconGreen = this.origRepGreen.Clone().ResizeByHeight(this.fontSize);  
this.reputationIconRed = this.origRepRed.Clone().ResizeByHeight(this.fontSize);  
this.scienceIcon = this.origScience.Clone().ResizeByHeight(this.fontSize);  
   
GC.Collect();  
}*/  
   
GUILayout.BeginVertical(); GUILayout.BeginVertical();
   
// VOID_Data.fundingStatus.DoGUIHorizontal();  
   
GUILayout.BeginHorizontal(GUILayout.ExpandWidth(true)); GUILayout.BeginHorizontal(GUILayout.ExpandWidth(true));
   
GUILayout.Label(VOID_Data.fundingStatus.Label); GUILayout.Label(VOID_Data.fundingStatus.Label);
GUILayout.FlexibleSpace(); GUILayout.FlexibleSpace();
// GUILayout.Label(this.fundsIconGreen); this.fundsContent.text = VOID_Data.fundingStatus.Value;
GUILayout.Label(VOID_Data.fundingStatus.Value, GUILayout.ExpandWidth(false)); GUILayout.Label(this.fundsContent, GUILayout.ExpandWidth(true));
   
GUILayout.EndHorizontal(); GUILayout.EndHorizontal();
   
VOID_Data.reputationStatus.DoGUIHorizontal(); GUILayout.BeginHorizontal(GUILayout.ExpandWidth(true));
  GUILayout.Label(VOID_Data.reputationStatus.Label);
VOID_Data.scienceStatus.DoGUIHorizontal(); GUILayout.FlexibleSpace();
  this.repContent.text = VOID_Data.reputationStatus.Value;
  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(true));
  GUILayout.EndHorizontal();
   
GUILayout.EndVertical(); GUILayout.EndVertical();
   
GUI.DragWindow(); GUI.DragWindow();
} }
   
private void onFundsChange(double delta) // TODO: Update event handlers to do something useful with the new "reasons" parameter.
{ private void onFundsChange(double newValue, TransactionReasons reasons)
this.lastFundsChange = delta; {
} this.lastFundsChange = newValue - this.currentFunds;
  this.currentFunds = newValue;
private void onRepChange(float delta) }
{  
this.lastRepChange = delta; private void onRepChange(float newValue, TransactionReasons reasons)
} {
  this.lastRepChange = newValue - this.currentReputation;
private void onScienceChange(float delta) this.currentReputation = newValue;
{ }
this.lastScienceChange = delta;  
  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;
} }
   
/* /*
* MissionRecoveryDialog::fundsIconGreen.name: UiElements_05 * MissionRecoveryDialog::fundsIconGreen.name: UiElements_05
* MissionRecoveryDialog::fundsIconRed.name: UiElements_06 * MissionRecoveryDialog::fundsIconRed.name: UiElements_06
* MissionRecoveryDialog::reputationIconGreen.name: UiElements_07 * MissionRecoveryDialog::reputationIconGreen.name: UiElements_07
* MissionRecoveryDialog::reputationIconRed.name: UiElements_08 * MissionRecoveryDialog::reputationIconRed.name: UiElements_08
* MissionRecoveryDialog::scienceIcon.name: UiElements_12 * MissionRecoveryDialog::scienceIcon.name: UiElements_12
* */ * */
public VOID_CareerStatus() : base() public VOID_CareerStatus() : base()
{ {
VOID_CareerStatus.Instance = this; VOID_CareerStatus.Instance = this;
   
this._Name = "Career Status"; this.Name = "Career Status";
this.fontSize = int.MinValue;  
   
GameEvents.OnFundsChanged.Add(this.onFundsChange); GameEvents.OnFundsChanged.Add(this.onFundsChange);
GameEvents.OnReputationChanged.Add(this.onRepChange); GameEvents.OnReputationChanged.Add(this.onRepChange);
GameEvents.OnScienceChanged.Add(this.onScienceChange); GameEvents.OnScienceChanged.Add(this.onScienceChange);
   
foreach (Texture2D tex in Resources.FindObjectsOfTypeAll<Texture2D>()) bool texturesLoaded;
{  
if ( texturesLoaded = IOTools.LoadTexture(out this.fundsIconGreen, "VOID/Textures/fundsgreen.png", 10, 18);
this.origFundsGreen != null && texturesLoaded &= IOTools.LoadTexture(out this.fundsIconRed, "VOID/Textures/fundsred.png", 10, 18);
this.origFundsRed != null && texturesLoaded &= IOTools.LoadTexture(out this.reputationIconGreen, "VOID/Textures/repgreen.png", 16, 18);
this.origRepGreen != null && texturesLoaded &= IOTools.LoadTexture(out this.reputationIconRed, "VOID/Textures/repred.png", 16, 18);
this.origRepRed != null && texturesLoaded &= IOTools.LoadTexture(out this.scienceIcon, "VOID/Textures/science.png", 16, 18);
this.origScience != null  
) this.fundsContent = new GUIContent();
{ this.repContent = new GUIContent();
break; this.scienceContent = new GUIContent();
}  
  if (texturesLoaded)
switch (tex.name) {
{ this.fundsContent.image = this.fundsIconGreen;
case "UiElements_05": this.repContent.image = this.reputationIconGreen;
this.fundsIconGreen = this.origFundsGreen = tex; this.scienceContent.image = this.scienceIcon;
break; }
case "UiElements_06":  
this.fundsIconRed = this.origFundsRed = tex; this.currentFunds = double.NaN;
break; this.currentScience = float.NaN;
case "UiElements_07": this.currentReputation = float.NaN;
this.reputationIconGreen = this.origRepGreen = tex;  
break;  
case "UiElements_08":  
this.reputationIconRed = this.origRepRed = tex;  
break;  
case "UiElements_12":  
this.scienceIcon = this.origScience = tex;  
break;  
default:  
continue;  
}  
}  
} }
   
~VOID_CareerStatus() ~VOID_CareerStatus()
{ {
GameEvents.OnFundsChanged.Remove(this.onFundsChange); GameEvents.OnFundsChanged.Remove(this.onFundsChange);
GameEvents.OnReputationChanged.Remove(this.onRepChange); GameEvents.OnReputationChanged.Remove(this.onRepChange);
GameEvents.OnScienceChanged.Remove(this.onScienceChange); GameEvents.OnScienceChanged.Remove(this.onScienceChange);
   
VOID_CareerStatus.Instance = null; 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,  
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,  
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,  
VOID_CareerStatus.formatDelta(VOID_CareerStatus.Instance.lastScienceChange)  
);  
}  
);  
}  
} }